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
70 changes: 64 additions & 6 deletions internal/cmd/pdatagen/internal/pdata/pcommon_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var pcommon = &Package{
},
},
structs: []baseStruct{
anyValueStruct,
arrayValueStruct,
keyValueListStruct,
anyValueSlice,
Expand Down Expand Up @@ -94,6 +95,69 @@ var mapStruct = &messageSlice{
element: keyValue,
}

var anyValue = &messageStruct{
structName: "Value",
packageName: "pcommon",
originFullName: "otlpcommon.AnyValue",
}

// anyValueStruct needs to be different from anyValue because otherwise we cause initialization circular deps with mapStruct.
var anyValueStruct = &messageStruct{
structName: "Value",
originFullName: "otlpcommon.AnyValue",
fields: []Field{
&OneOfField{
typeName: "MetricType",
originFieldName: "Value",
testValueIdx: 1, //
omitOriginFieldNameInNames: true,
values: []oneOfValue{
&OneOfPrimitiveValue{
fieldName: "StringValue",
protoID: 1,
originFieldName: "StringValue",
protoType: proto.TypeString,
},
&OneOfPrimitiveValue{
fieldName: "BoolValue",
protoID: 2,
originFieldName: "BoolValue",
protoType: proto.TypeBool,
},
&OneOfPrimitiveValue{
fieldName: "IntValue",
protoID: 3,
originFieldName: "IntValue",
protoType: proto.TypeInt64,
},
&OneOfPrimitiveValue{
fieldName: "DoubleValue",
protoID: 4,
originFieldName: "DoubleValue",
protoType: proto.TypeDouble,
},
&OneOfMessageValue{
fieldName: "ArrayValue",
protoID: 5,
returnMessage: arrayValueStruct,
},
&OneOfMessageValue{
fieldName: "KvlistValue",
protoID: 6,
returnMessage: keyValueListStruct,
},
&OneOfPrimitiveValue{
fieldName: "BytesValue",
protoID: 7,
originFieldName: "BytesValue",
protoType: proto.TypeBytes,
},
},
},
},
hasOnlyOrig: true,
}

var keyValueListStruct = &messageStruct{
structName: "KeyValueList",
description: "KeyValueList is a list of KeyValue messages. We need KeyValueList as a message since oneof in AnyValue does not allow repeated fields.",
Expand Down Expand Up @@ -145,12 +209,6 @@ var timestampType = &TypedType{
testVal: "1234567890",
}

var anyValue = &messageStruct{
structName: "Value",
packageName: "pcommon",
originFullName: "otlpcommon.AnyValue",
}

var traceIDType = &TypedType{
structName: "TraceID",
packageName: "pcommon",
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/pdatagen/internal/proto/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (pf *Field) DefaultValue() string {
case TypeBool:
return `false`
case TypeBytes:
return `[]byte{}`
return `nil`
case TypeString:
return `""`
case TypeMessage:
Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/pdatagen/internal/proto/json_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ const marshalJSONBytes = `{{ if .repeated -}}
}
dest.WriteArrayEnd()
}
{{- else }}
{{- else }}{{ if not .nullable }}
if len(orig.{{ .fieldName }}) > 0 {
{{- end }}
dest.WriteObjectField("{{ .jsonTag }}")
dest.WriteBytes(orig.{{ .fieldName }})
{{- if not .nullable -}}
}
{{- end }}`
{{- end }}{{- end }}`

func (pf *Field) GenMarshalJSON() string {
tf := pf.getTemplateFields()
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/pdatagen/internal/proto/json_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ const unmarshalJSONBytes = ` case {{ .allJSONTags }}:
for iter.ReadArray() {
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, iter.ReadBytes())
}
{{ else if ne .oneOfGroup "" -}}
{
var ov *{{ .oneOfMessageFullName }}
if !UseProtoPooling.IsEnabled() {
ov = &{{ .oneOfMessageFullName }}{}
} else {
ov = ProtoPool{{ .oneOfMessageName }}.Get().(*{{ .oneOfMessageFullName }})
}
ov.{{ .fieldName }} = iter.ReadBytes()
orig.{{ .oneOfGroup }} = ov
}
{{ else -}}
orig.{{ .fieldName }} = iter.ReadBytes()
{{- end }}`
Expand Down
20 changes: 14 additions & 6 deletions internal/cmd/pdatagen/internal/proto/proto_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,23 @@ const unmarshalProtoBytes = `
} else {
ov = ProtoPool{{ .oneOfMessageName }}.Get().(*{{ .oneOfMessageFullName }})
}
ov.{{ .fieldName }} = make([]byte, length)
copy(ofv.{{ .fieldName }}, buf[startPos:pos])
if length != 0 {
ov.{{ .fieldName }} = make([]byte, length)
copy(ov.{{ .fieldName }}, buf[startPos:pos])
}
orig.{{ .oneOfGroup }} = ov
{{- else if .repeated -}}
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, make([]byte, length))
copy(orig.{{ .fieldName }}[len(orig.{{ .fieldName }}) - 1], buf[startPos:pos])
if length != 0 {
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, make([]byte, length))
copy(orig.{{ .fieldName }}[len(orig.{{ .fieldName }}) - 1], buf[startPos:pos])
} else {
orig.{{ .fieldName }} = append(orig.{{ .fieldName }}, nil)
}
{{- else -}}
orig.{{ .fieldName }} = make([]byte, length)
copy(orig.{{ .fieldName }}, buf[startPos:pos])
if length != 0 {
orig.{{ .fieldName }} = make([]byte, length)
copy(orig.{{ .fieldName }}, buf[startPos:pos])
}
{{- end }}`

const unmarshalProtoMessage = `
Expand Down
Loading
Loading