@@ -23,9 +23,8 @@ import Data.Array (catMaybes, concatMap, filter)
2323import Data.Array as Array
2424import Data.ArrayBuffer.Builder (execPut)
2525import Data.ArrayBuffer.DataView as DV
26−import Data.ArrayBuffer.Types (DataView)
2726import Data.Either (Either(..))
28−import Data.Maybe (Maybe(..), fromMaybe, isNothing, maybe)
27+import Data.Maybe (Maybe(..), fromMaybe, maybe)
2928import Data.String as String
3029import Data.String.Pattern as String.Pattern
3130import Data.String.Regex as String.Regex
@@ -34,7 +33,7 @@ import Data.Traversable (sequence, traverse)
3433import Data.TraversableWithIndex (traverseWithIndex)
3534import Effect (Effect)
3635import Google.Protobuf.Compiler.Plugin (CodeGeneratorRequest(..), CodeGeneratorResponse(..), CodeGeneratorResponse_File(..), parseCodeGeneratorRequest, putCodeGeneratorResponse)
37−import Google.Protobuf.Descriptor (DescriptorProto(..), EnumDescriptorProto(..), EnumValueDescriptorProto(..), FieldDescriptorProto(..), FieldDescriptorProto_Label(..), FieldDescriptorProto_Type(..), FileDescriptorProto(..), OneofDescriptorProto(..))
36+import Google.Protobuf.Descriptor (DescriptorProto(..), EnumDescriptorProto(..), EnumValueDescriptorProto(..), FieldDescriptorProto(..), FieldDescriptorProto_Label(..), FieldDescriptorProto_Type(..), FieldOptions(..), FileDescriptorProto(..), OneofDescriptorProto(..))
3837import Node.Buffer (toArrayBuffer, fromArrayBuffer)
3938import Node.Encoding (Encoding(..))
4039import Node.Path (basenameWithoutExt)
@@ -382,90 +381,119 @@ genFile proto_file (FileDescriptorProto
382381 , type: Just ftype
383382 , type_name
384383 , oneof_index: Nothing -- must not be a member of a oneof, that case handled seperately
385− }) = go flabel ftype type_name
384+ , options
385+ }) = go flabel ftype type_name options
386386 where
387387 fname = decapitalize name'
388388 -- For repeated fields of primitive numeric types, always put the packed
389389 -- encoding.
390390 -- https://developers.google.com/protocol-buffers/docs/encoding?hl=en#packed
391− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_DOUBLE _ =
391+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_DOUBLE _ (Just (FieldOptions {packed:Just false})) =
392+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.double"
393+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_DOUBLE _ _ =
392394 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.double'"
393− go _ FieldDescriptorProto_Type_TYPE_DOUBLE _ =
395+ go _ FieldDescriptorProto_Type_TYPE_DOUBLE _ _ =
394396 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.double"
395− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FLOAT _ =
397+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FLOAT _ (Just (FieldOptions {packed:Just false})) =
398+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.float"
399+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FLOAT _ _ =
396400 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.float'"
397− go _ FieldDescriptorProto_Type_TYPE_FLOAT _ =
401+ go _ FieldDescriptorProto_Type_TYPE_FLOAT _ _ =
398402 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.float"
399− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT64 _ =
403+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT64 _ (Just (FieldOptions {packed:Just false})) =
404+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.int64"
405+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT64 _ _ =
400406 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.int64'"
401− go _ FieldDescriptorProto_Type_TYPE_INT64 _ =
407+ go _ FieldDescriptorProto_Type_TYPE_INT64 _ _ =
402408 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.int64"
403− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT64 _ =
409+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT64 _ (Just (FieldOptions {packed:Just false})) =
410+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.uint64"
411+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT64 _ _ =
404412 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.uint64'"
405− go _ FieldDescriptorProto_Type_TYPE_UINT64 _ =
413+ go _ FieldDescriptorProto_Type_TYPE_UINT64 _ _ =
406414 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.uint64"
407− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT32 _ =
415+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT32 _ (Just (FieldOptions {packed:Just false})) =
416+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.int32"
417+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT32 _ _ =
408418 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.int32'"
409− go _ FieldDescriptorProto_Type_TYPE_INT32 _ =
419+ go _ FieldDescriptorProto_Type_TYPE_INT32 _ _ =
410420 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.int32"
411− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED64 _ =
421+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED64 _ (Just (FieldOptions {packed:Just false})) =
422+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.fixed64"
423+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED64 _ _ =
412424 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.fixed64'"
413− go _ FieldDescriptorProto_Type_TYPE_FIXED64 _ =
425+ go _ FieldDescriptorProto_Type_TYPE_FIXED64 _ _ =
414426 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.fixed64"
415− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED32 _ =
427+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED32 _ (Just (FieldOptions {packed:Just false})) =
428+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.fixed32"
429+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED32 _ _ =
416430 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.fixed32'"
417− go _ FieldDescriptorProto_Type_TYPE_FIXED32 _ =
431+ go _ FieldDescriptorProto_Type_TYPE_FIXED32 _ _ =
418432 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.fixed32"
419− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BOOL _ =
433+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BOOL _ (Just (FieldOptions {packed:Just false})) =
434+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.bool"
435+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BOOL _ _ =
420436 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.bool'"
421− go _ FieldDescriptorProto_Type_TYPE_BOOL _ =
437+ go _ FieldDescriptorProto_Type_TYPE_BOOL _ _ =
422438 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.bool"
423− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_STRING _ =
439+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_STRING _ _ =
424440 Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.string"
425− go _ FieldDescriptorProto_Type_TYPE_STRING _ =
441+ go _ FieldDescriptorProto_Type_TYPE_STRING _ _ =
426442 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.string"
427− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) =
443+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) _ =
428444 Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " $ Runtime.putLenDel " <> mkFieldType "put" tname
429− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_MESSAGE _ =
445+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_MESSAGE _ _ =
430446 Left "Failed genFieldPut missing FieldDescriptorProto type_name"
431447 -- Do we want to do message default checking? A message would be “default,” and would not be encoded on the wire,
432448 -- iff “all singular fields are unset and all repeated fields are empty.”
433449 -- https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#message
434450 -- For now we just say `(const false)`, the message is never default.
435− go _ FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) =
451+ go _ FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) _ =
436452 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) $ Runtime.putLenDel " <> mkFieldType "put" tname
437453 -- Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " (_==(Newtype.wrap " <> mkFieldType "default" tname <> ")) $ Runtime.putLenDel " <> mkFieldType "put" tname
438− go _ FieldDescriptorProto_Type_TYPE_MESSAGE _ =
454+ go _ FieldDescriptorProto_Type_TYPE_MESSAGE _ _ =
439455 Left "Failed genFieldPut missing FieldDescriptorProto type_name"
440− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BYTES _ =
456+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BYTES _ _ =
441457 Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " $ Encode.bytes"
442− go _ FieldDescriptorProto_Type_TYPE_BYTES _ =
458+ go _ FieldDescriptorProto_Type_TYPE_BYTES _ _ =
443459 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.bytes"
444− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT32 _ =
460+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT32 _ (Just (FieldOptions {packed:Just false})) =
461+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.uint32"
462+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT32 _ _ =
445463 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.uint32'"
446− go _ FieldDescriptorProto_Type_TYPE_UINT32 _ =
464+ go _ FieldDescriptorProto_Type_TYPE_UINT32 _ _ =
447465 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.uint32"
448− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_ENUM _ =
466+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_ENUM _ (Just (FieldOptions {packed:Just false})) =
467+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Runtime.putEnum"
468+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_ENUM _ _ =
449469 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Runtime.putEnum'"
450− go _ FieldDescriptorProto_Type_TYPE_ENUM _ =
470+ go _ FieldDescriptorProto_Type_TYPE_ENUM _ _ =
451471 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Runtime.putEnum"
452− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED32 _ =
472+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED32 _ (Just (FieldOptions {packed:Just false})) =
473+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.sfixed32"
474+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED32 _ _ =
453475 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.sfixed32'"
454− go _ FieldDescriptorProto_Type_TYPE_SFIXED32 _ =
476+ go _ FieldDescriptorProto_Type_TYPE_SFIXED32 _ _ =
455477 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.sfixed32"
456− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED64 _ =
478+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED64 _ (Just (FieldOptions {packed:Just false})) =
479+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.sfixed64"
480+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED64 _ _ =
457481 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.sfixed64'"
458− go _ FieldDescriptorProto_Type_TYPE_SFIXED64 _ =
482+ go _ FieldDescriptorProto_Type_TYPE_SFIXED64 _ _ =
459483 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.sfixed64"
460− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT32 _ =
484+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT32 _ (Just (FieldOptions {packed:Just false})) =
485+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.sint32"
486+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT32 _ _ =
461487 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.sint32'"
462− go _ FieldDescriptorProto_Type_TYPE_SINT32 _ =
488+ go _ FieldDescriptorProto_Type_TYPE_SINT32 _ _ =
463489 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.sint32"
464− go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT64 _ =
490+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT64 _ (Just (FieldOptions {packed:Just false})) =
491+ Right $ Just $ " Runtime.putRepeated " <> show fnumber <> " r." <> fname <> " Encode.sint64"
492+ go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT64 _ _ =
465493 Right $ Just $ " Runtime.putPacked " <> show fnumber <> " r." <> fname <> " Encode.sint64'"
466− go _ FieldDescriptorProto_Type_TYPE_SINT64 _ =
494+ go _ FieldDescriptorProto_Type_TYPE_SINT64 _ _ =
467495 Right $ Just $ " Runtime.putOptional " <> show fnumber <> " r." <> fname <> " Common.isDefault Encode.sint64"
468− go _ FieldDescriptorProto_Type_TYPE_GROUP _ = Left "Failed genFieldPut GROUP not supported"
496+ go _ FieldDescriptorProto_Type_TYPE_GROUP _ _ = Left "Failed genFieldPut GROUP not supported"
469497 genFieldPut _ (FieldDescriptorProto { oneof_index: Just _ }) = Right Nothing -- It's a Oneof, this case is handled separately
470498 genFieldPut _ arg = Left $ "Failed genFieldPut missing FieldDescriptorProto name or number or label or type\n" <> show arg
471499