Skip to content

Commit 418a8b3

Browse files
ilCollezMarco Colletti
andauthored
types: support proto json_name override (#231)
Co-authored-by: Marco Colletti <m.colletti@ips-intelligence.com>
1 parent 38b03c9 commit 418a8b3

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

types/proto.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,22 @@ func generateProtoField(buf *strings.Builder, nat *expr.NamedAttributeExpr, fiel
169169
buf.WriteString(codegen.Goify(nat.Name, false))
170170
buf.WriteString(" = ")
171171
buf.WriteString(fmt.Sprintf("%d", *fieldNum))
172+
buf.WriteString(protoJSONOption(nat.Attribute))
172173
buf.WriteString(";\n")
173174

174175
*fieldNum++
175176
}
176177

178+
func protoJSONOption(att *expr.AttributeExpr) string {
179+
if att == nil || att.Meta == nil {
180+
return ""
181+
}
182+
if names := att.Meta["proto:tag:json"]; len(names) > 0 && names[0] != "" {
183+
return fmt.Sprintf(" [json_name = %q]", names[0])
184+
}
185+
return ""
186+
}
187+
177188
func protoType(dt expr.DataType) string {
178189
switch dt.Kind() {
179190
case expr.BooleanKind:

types/proto_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func TestProto(t *testing.T) {
2929
{"alias", testdata.Alias},
3030
{"array", testdata.Array},
3131
{"recArray", testdata.ArrayArray},
32+
{"protojson", testdata.ProtoJSON},
3233
}
3334
for _, c := range cases {
3435
t.Run(c.Name, func(t *testing.T) {

types/testdata/dsls.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,11 @@ var ArrayArray = func() {
9090
Required("array")
9191
})
9292
}
93+
94+
var ProtoJSON = func() {
95+
var _ = Type("ProtoJSON", func() {
96+
Attribute("value", String, func() {
97+
Meta("proto:tag:json", "customValue")
98+
})
99+
})
100+
}

types/testdata/protojson.proto_

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto3";
2+
3+
package types;
4+
5+
option go_package = "example.com/test/types";
6+
7+
message ProtoJSON {
8+
string value = 1 [json_name = "customValue"];
9+
}
10+

0 commit comments

Comments
 (0)