From bf38b50da910f8dbf7f0af8ed7afc7ac380e204a Mon Sep 17 00:00:00 2001 From: Marco Colletti Date: Mon, 15 Dec 2025 11:43:18 +0100 Subject: [PATCH] types: support proto json_name override --- types/proto.go | 11 +++++++++++ types/proto_test.go | 1 + types/testdata/dsls.go | 8 ++++++++ types/testdata/protojson.proto_ | 10 ++++++++++ 4 files changed, 30 insertions(+) create mode 100644 types/testdata/protojson.proto_ diff --git a/types/proto.go b/types/proto.go index fcdaadff2..98641c3f7 100644 --- a/types/proto.go +++ b/types/proto.go @@ -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: diff --git a/types/proto_test.go b/types/proto_test.go index 1fca0477a..faaded393 100644 --- a/types/proto_test.go +++ b/types/proto_test.go @@ -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) { diff --git a/types/testdata/dsls.go b/types/testdata/dsls.go index 7f0f8aa70..5aa35582c 100644 --- a/types/testdata/dsls.go +++ b/types/testdata/dsls.go @@ -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") + }) + }) +} diff --git a/types/testdata/protojson.proto_ b/types/testdata/protojson.proto_ new file mode 100644 index 000000000..7a936362c --- /dev/null +++ b/types/testdata/protojson.proto_ @@ -0,0 +1,10 @@ +syntax = "proto3"; + +package types; + +option go_package = "example.com/test/types"; + +message ProtoJSON { + string value = 1 [json_name = "customValue"]; +} +