Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions types/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,22 @@ func generateProtoField(buf *strings.Builder, nat *expr.NamedAttributeExpr, fiel
buf.WriteString(codegen.Goify(nat.Name, false))
buf.WriteString(" = ")
buf.WriteString(fmt.Sprintf("%d", *fieldNum))
buf.WriteString(protoJSONOption(nat.Attribute))
buf.WriteString(";\n")

*fieldNum++
}

func protoJSONOption(att *expr.AttributeExpr) string {
if att == nil || att.Meta == nil {
return ""
}
if names := att.Meta["proto:tag:json"]; len(names) > 0 && names[0] != "" {
return fmt.Sprintf(" [json_name = %q]", names[0])
}
return ""
}

func protoType(dt expr.DataType) string {
switch dt.Kind() {
case expr.BooleanKind:
Expand Down
1 change: 1 addition & 0 deletions types/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestProto(t *testing.T) {
{"alias", testdata.Alias},
{"array", testdata.Array},
{"recArray", testdata.ArrayArray},
{"protojson", testdata.ProtoJSON},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions types/testdata/dsls.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ var ArrayArray = func() {
Required("array")
})
}

var ProtoJSON = func() {
var _ = Type("ProtoJSON", func() {
Attribute("value", String, func() {
Meta("proto:tag:json", "customValue")
})
})
}
10 changes: 10 additions & 0 deletions types/testdata/protojson.proto_
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package types;

option go_package = "example.com/test/types";

message ProtoJSON {
string value = 1 [json_name = "customValue"];
}

Loading