diff --git a/README.md b/README.md index b229511..144eeda 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ Decode the protobuf wire format using gleam `Decoder`s! There is currently no support for encoding the protobuf wire format. +Groups (`SGROUP` and `EGROUP`) are not supported. If encountered, they are +ignored and treated as regular fields. Any fields nested within them will be +treated as if they were not nested. + [![Package Version](https://img.shields.io/hexpm/v/protobin)](https://hex.pm/packages/protobin) [![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/protobin/) diff --git a/src/protobin.gleam b/src/protobin.gleam index a2abf1c..879947b 100644 --- a/src/protobin.gleam +++ b/src/protobin.gleam @@ -131,6 +131,7 @@ fn wire_type_read_fn(ty: WireType) -> ValueParser { wire_type.I64 -> read_fixed(64) wire_type.Len -> read_len wire_type.I32 -> read_fixed(32) + wire_type.SGroup | wire_type.EGroup -> read_group } } @@ -218,6 +219,11 @@ fn read_len(bits: BitArray, len_pos: BytePos) -> ValueResult { Ok(Parsed(value:, rest:, pos: pos + len)) } +/// Currently just does nothing. +fn read_group(bits: BitArray, pos: BytePos) -> ValueResult { + Ok(Parsed(value: <<>>, rest: bits, pos: pos)) +} + /// Decode a repeated field that may be either packed or expanded. /// /// If it is impossible for a field to be packed (ie because it is encoded as diff --git a/src/protobin/internal/wire_type.gleam b/src/protobin/internal/wire_type.gleam index 0f8ad6d..dbce2c9 100644 --- a/src/protobin/internal/wire_type.gleam +++ b/src/protobin/internal/wire_type.gleam @@ -4,6 +4,8 @@ pub type WireType { VarInt I64 Len + SGroup + EGroup I32 } @@ -12,6 +14,8 @@ pub fn parse(i: Int) -> Option(WireType) { 0 -> Some(VarInt) 1 -> Some(I64) 2 -> Some(Len) + 3 -> Some(SGroup) + 4 -> Some(EGroup) 5 -> Some(I32) _ -> None } diff --git a/test/ignores-groups.pb b/test/ignores-groups.pb new file mode 100644 index 0000000000000000000000000000000000000000..20ad4c178a4abc670935c32d53a5e5aefca42a74 GIT binary patch literal 16 Xcmd Decoder(IgnoresGroups) { + use a <- decode.field(1, protobin.decode_uint()) + use b <- decode.field(3, protobin.decode_bool()) + use c <- decode.field(4, protobin.decode_string()) + use d <- decode.field(5, protobin.decode_uint()) + decode.success(IgnoresGroups(a:, b:, c:, d:)) +} + +pub fn ignore_groups_test() { + let path = "./test/ignores-groups.pb" + let assert Ok(bits) = file.read_bits(from: path) + let assert Ok(ignores_groups) = + parse(from: bits, using: ignores_groups_decoder()) + + assert ignores_groups + == Parsed( + value: IgnoresGroups(a: 42, b: False, c: "hi", d: 22), + rest: <<>>, + pos: 16, + ) +}