Skip to content

Commit

Permalink
Add support for tracking unknown fields. (#129)
Browse files Browse the repository at this point in the history
- Every message has a new field containing a list of unknown fields:
  ```unknownFields :: Message msg => Lens' msg [TaggedValue]```
- Unknown fields are preserved by `decodeMessage`, `encodeMessage`,
  and `showMessage`
- Unknown fields still cause an error for `readMessage`.

A few TODOs:
- For now, unknown groups are printed sub-optimally by `showMessage`: the
  start/end group tags (and everything in between) all get displayed as
  individual fields, rather than being organized into a sub-struct.
- The `discardUnknownFields` function isn't recursive, unlike in other
  languages.
- The Ord instance doesn't try to do anything special, just treating
  the unknown fields as a list of values.  If it really matters then
  `discardUnknownFields` can help resolve the ambiguity.
  • Loading branch information
judah committed Sep 1, 2017
1 parent 992997b commit e5aa6cf
Show file tree
Hide file tree
Showing 16 changed files with 447 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ data CodeGeneratorRequest = CodeGeneratorRequest{_CodeGeneratorRequest'fileToGen
_CodeGeneratorRequest'parameter ::
!(Prelude.Maybe Data.Text.Text),
_CodeGeneratorRequest'protoFile ::
![Proto.Google.Protobuf.Descriptor.FileDescriptorProto]}
![Proto.Google.Protobuf.Descriptor.FileDescriptorProto],
_CodeGeneratorRequest'_unknownFields ::
!Data.ProtoLens.FieldSet}
deriving (Prelude.Show, Prelude.Eq, Prelude.Ord)

instance (Lens.Labels.HasLens' f CodeGeneratorRequest x a,
Expand Down Expand Up @@ -71,7 +73,8 @@ instance Data.Default.Class.Default CodeGeneratorRequest where
def
= CodeGeneratorRequest{_CodeGeneratorRequest'fileToGenerate = [],
_CodeGeneratorRequest'parameter = Prelude.Nothing,
_CodeGeneratorRequest'protoFile = []}
_CodeGeneratorRequest'protoFile = [],
_CodeGeneratorRequest'_unknownFields = ([])}

instance Data.ProtoLens.Message CodeGeneratorRequest where
descriptor
Expand Down Expand Up @@ -111,11 +114,15 @@ instance Data.ProtoLens.Message CodeGeneratorRequest where
[("file_to_generate", fileToGenerate__field_descriptor),
("parameter", parameter__field_descriptor),
("proto_file", protoFile__field_descriptor)])
(Lens.Family2.Unchecked.lens _CodeGeneratorRequest'_unknownFields
(\ x__ y__ -> x__{_CodeGeneratorRequest'_unknownFields = y__}))

data CodeGeneratorResponse = CodeGeneratorResponse{_CodeGeneratorResponse'error
:: !(Prelude.Maybe Data.Text.Text),
_CodeGeneratorResponse'file ::
![CodeGeneratorResponse'File]}
![CodeGeneratorResponse'File],
_CodeGeneratorResponse'_unknownFields ::
!Data.ProtoLens.FieldSet}
deriving (Prelude.Show, Prelude.Eq, Prelude.Ord)

instance (Lens.Labels.HasLens' f CodeGeneratorResponse x a,
Expand Down Expand Up @@ -153,7 +160,8 @@ instance Data.Default.Class.Default CodeGeneratorResponse where
def
= CodeGeneratorResponse{_CodeGeneratorResponse'error =
Prelude.Nothing,
_CodeGeneratorResponse'file = []}
_CodeGeneratorResponse'file = [],
_CodeGeneratorResponse'_unknownFields = ([])}

instance Data.ProtoLens.Message CodeGeneratorResponse where
descriptor
Expand Down Expand Up @@ -182,13 +190,17 @@ instance Data.ProtoLens.Message CodeGeneratorResponse where
(Data.Map.fromList
[("error", error__field_descriptor),
("file", file__field_descriptor)])
(Lens.Family2.Unchecked.lens _CodeGeneratorResponse'_unknownFields
(\ x__ y__ -> x__{_CodeGeneratorResponse'_unknownFields = y__}))

data CodeGeneratorResponse'File = CodeGeneratorResponse'File{_CodeGeneratorResponse'File'name
:: !(Prelude.Maybe Data.Text.Text),
_CodeGeneratorResponse'File'insertionPoint
:: !(Prelude.Maybe Data.Text.Text),
_CodeGeneratorResponse'File'content ::
!(Prelude.Maybe Data.Text.Text)}
!(Prelude.Maybe Data.Text.Text),
_CodeGeneratorResponse'File'_unknownFields
:: !Data.ProtoLens.FieldSet}
deriving (Prelude.Show, Prelude.Eq, Prelude.Ord)

instance (Lens.Labels.HasLens' f CodeGeneratorResponse'File x a,
Expand Down Expand Up @@ -258,7 +270,8 @@ instance Data.Default.Class.Default CodeGeneratorResponse'File
= CodeGeneratorResponse'File{_CodeGeneratorResponse'File'name =
Prelude.Nothing,
_CodeGeneratorResponse'File'insertionPoint = Prelude.Nothing,
_CodeGeneratorResponse'File'content = Prelude.Nothing}
_CodeGeneratorResponse'File'content = Prelude.Nothing,
_CodeGeneratorResponse'File'_unknownFields = ([])}

instance Data.ProtoLens.Message CodeGeneratorResponse'File where
descriptor
Expand Down Expand Up @@ -298,4 +311,8 @@ instance Data.ProtoLens.Message CodeGeneratorResponse'File where
(Data.Map.fromList
[("name", name__field_descriptor),
("insertion_point", insertionPoint__field_descriptor),
("content", content__field_descriptor)])
("content", content__field_descriptor)])
(Lens.Family2.Unchecked.lens
_CodeGeneratorResponse'File'_unknownFields
(\ x__ y__ ->
x__{_CodeGeneratorResponse'File'_unknownFields = y__}))
Loading

0 comments on commit e5aa6cf

Please sign in to comment.