Skip to content

Commit 8475824

Browse files
authored
[chore] Change AnyValue proto funcs to be auto-generated by pdatagen (#13693)
Updates #13631 Signed-off-by: Bogdan Drutu <[email protected]>
1 parent a3ff02b commit 8475824

File tree

13 files changed

+916
-586
lines changed

13 files changed

+916
-586
lines changed

internal/cmd/pdatagen/internal/pdata/pcommon_package.go

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var pcommon = &Package{
4141
},
4242
},
4343
structs: []baseStruct{
44+
anyValueStruct,
4445
arrayValueStruct,
4546
keyValueListStruct,
4647
anyValueSlice,
@@ -94,6 +95,69 @@ var mapStruct = &messageSlice{
9495
element: keyValue,
9596
}
9697

98+
var anyValue = &messageStruct{
99+
structName: "Value",
100+
packageName: "pcommon",
101+
originFullName: "otlpcommon.AnyValue",
102+
}
103+
104+
// anyValueStruct needs to be different from anyValue because otherwise we cause initialization circular deps with mapStruct.
105+
var anyValueStruct = &messageStruct{
106+
structName: "Value",
107+
originFullName: "otlpcommon.AnyValue",
108+
fields: []Field{
109+
&OneOfField{
110+
typeName: "MetricType",
111+
originFieldName: "Value",
112+
testValueIdx: 1, //
113+
omitOriginFieldNameInNames: true,
114+
values: []oneOfValue{
115+
&OneOfPrimitiveValue{
116+
fieldName: "StringValue",
117+
protoID: 1,
118+
originFieldName: "StringValue",
119+
protoType: proto.TypeString,
120+
},
121+
&OneOfPrimitiveValue{
122+
fieldName: "BoolValue",
123+
protoID: 2,
124+
originFieldName: "BoolValue",
125+
protoType: proto.TypeBool,
126+
},
127+
&OneOfPrimitiveValue{
128+
fieldName: "IntValue",
129+
protoID: 3,
130+
originFieldName: "IntValue",
131+
protoType: proto.TypeInt64,
132+
},
133+
&OneOfPrimitiveValue{
134+
fieldName: "DoubleValue",
135+
protoID: 4,
136+
originFieldName: "DoubleValue",
137+
protoType: proto.TypeDouble,
138+
},
139+
&OneOfMessageValue{
140+
fieldName: "ArrayValue",
141+
protoID: 5,
142+
returnMessage: arrayValueStruct,
143+
},
144+
&OneOfMessageValue{
145+
fieldName: "KvlistValue",
146+
protoID: 6,
147+
returnMessage: keyValueListStruct,
148+
},
149+
&OneOfPrimitiveValue{
150+
fieldName: "BytesValue",
151+
protoID: 7,
152+
originFieldName: "BytesValue",
153+
protoType: proto.TypeBytes,
154+
},
155+
},
156+
},
157+
},
158+
hasOnlyOrig: true,
159+
}
160+
97161
var keyValueListStruct = &messageStruct{
98162
structName: "KeyValueList",
99163
description: "KeyValueList is a list of KeyValue messages. We need KeyValueList as a message since oneof in AnyValue does not allow repeated fields.",
@@ -145,12 +209,6 @@ var timestampType = &TypedType{
145209
testVal: "1234567890",
146210
}
147211

148-
var anyValue = &messageStruct{
149-
structName: "Value",
150-
packageName: "pcommon",
151-
originFullName: "otlpcommon.AnyValue",
152-
}
153-
154212
var traceIDType = &TypedType{
155213
structName: "TraceID",
156214
packageName: "pcommon",

internal/cmd/pdatagen/internal/proto/field.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (pf *Field) DefaultValue() string {
5353
case TypeBool:
5454
return `false`
5555
case TypeBytes:
56-
return `[]byte{}`
56+
return `nil`
5757
case TypeString:
5858
return `""`
5959
case TypeMessage:

internal/cmd/pdatagen/internal/proto/json_marshal.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ const marshalJSONBytes = `{{ if .repeated -}}
8080
}
8181
dest.WriteArrayEnd()
8282
}
83-
{{- else }}
83+
{{- else }}{{ if not .nullable }}
8484
if len(orig.{{ .fieldName }}) > 0 {
85+
{{- end }}
8586
dest.WriteObjectField("{{ .jsonTag }}")
8687
dest.WriteBytes(orig.{{ .fieldName }})
88+
{{- if not .nullable -}}
8789
}
88-
{{- end }}`
90+
{{- end }}{{- end }}`
8991

9092
func (pf *Field) GenMarshalJSON() string {
9193
tf := pf.getTemplateFields()

internal/cmd/pdatagen/internal/proto/json_unmarshal.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ const unmarshalJSONBytes = ` case {{ .allJSONTags }}:
6868
for iter.ReadArray() {
6969
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, iter.ReadBytes())
7070
}
71+
{{ else if ne .oneOfGroup "" -}}
72+
{
73+
var ov *{{ .oneOfMessageFullName }}
74+
if !UseProtoPooling.IsEnabled() {
75+
ov = &{{ .oneOfMessageFullName }}{}
76+
} else {
77+
ov = ProtoPool{{ .oneOfMessageName }}.Get().(*{{ .oneOfMessageFullName }})
78+
}
79+
ov.{{ .fieldName }} = iter.ReadBytes()
80+
orig.{{ .oneOfGroup }} = ov
81+
}
7182
{{ else -}}
7283
orig.{{ .fieldName }} = iter.ReadBytes()
7384
{{- end }}`

internal/cmd/pdatagen/internal/proto/proto_unmarshal.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,23 @@ const unmarshalProtoBytes = `
239239
} else {
240240
ov = ProtoPool{{ .oneOfMessageName }}.Get().(*{{ .oneOfMessageFullName }})
241241
}
242-
ov.{{ .fieldName }} = make([]byte, length)
243-
copy(ofv.{{ .fieldName }}, buf[startPos:pos])
242+
if length != 0 {
243+
ov.{{ .fieldName }} = make([]byte, length)
244+
copy(ov.{{ .fieldName }}, buf[startPos:pos])
245+
}
244246
orig.{{ .oneOfGroup }} = ov
245247
{{- else if .repeated -}}
246-
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, make([]byte, length))
247-
copy(orig.{{ .fieldName }}[len(orig.{{ .fieldName }}) - 1], buf[startPos:pos])
248+
if length != 0 {
249+
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, make([]byte, length))
250+
copy(orig.{{ .fieldName }}[len(orig.{{ .fieldName }}) - 1], buf[startPos:pos])
251+
} else {
252+
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, nil)
253+
}
248254
{{- else -}}
249-
orig.{{ .fieldName }} = make([]byte, length)
250-
copy(orig.{{ .fieldName }}, buf[startPos:pos])
255+
if length != 0 {
256+
orig.{{ .fieldName }} = make([]byte, length)
257+
copy(orig.{{ .fieldName }}, buf[startPos:pos])
258+
}
251259
{{- end }}`
252260

253261
const unmarshalProtoMessage = `

0 commit comments

Comments
 (0)