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
25 changes: 25 additions & 0 deletions .chloggen/gen-marshal-json-stream.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Use pdatagen to generate marshalJSON without using gogo proto jsonpb.

Check warning on line 10 in .chloggen/gen-marshal-json-stream.yaml

View workflow job for this annotation

GitHub Actions / spell-check

Unknown word (jsonpb)
Comment thread
bogdandrutu marked this conversation as resolved.

# One or more tracking issues or pull requests related to the change
issues: [13450]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
14 changes: 14 additions & 0 deletions internal/cmd/pdatagen/internal/message_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const messageSetTestTemplate = `{{ if .isCommon -}}
const messageCopyOrigTemplate = `{{ if .isCommon }}{{ if not .isBaseStructCommon }}internal.{{ end }}CopyOrig{{ else }}copyOrig{{ end }}
{{- .returnType }}(&dest.{{ .originFieldName }}, &src.{{ .originFieldName }})`

const messageMarshalJSONTemplate = `{{- if eq .returnType "TraceState" }} if ms.orig.{{ .originFieldName }} != "" { {{ end -}}
dest.WriteObjectField("{{ lowerFirst .originFieldName }}")
{{- if .isCommon }}
{{ if not .isBaseStructCommon }}internal.{{ end }}MarshalJSONStream{{ .returnType }}(
{{- if not .isBaseStructCommon }}internal.{{ end }}New{{ .returnType }}(&ms.orig.{{ .originFieldName }}, ms.state), dest)
{{- else }}
ms.{{ .fieldName }}().marshalJSONStream(dest)
{{- end }}{{ if eq .returnType "TraceState" -}} } {{- end }}`

type MessageField struct {
fieldName string
returnMessage *messageStruct
Expand All @@ -64,6 +73,11 @@ func (mf *MessageField) GenerateCopyOrig(ms *messageStruct) string {
return executeTemplate(t, mf.templateFields(ms))
}

func (mf *MessageField) GenerateMarshalJSON(ms *messageStruct) string {
t := template.Must(templateNew("messageMarshalJSONTemplate").Parse(messageMarshalJSONTemplate))
return executeTemplate(t, mf.templateFields(ms))
}

func (mf *MessageField) templateFields(ms *messageStruct) map[string]any {
return map[string]any{
"isCommon": usedByOtherDataTypes(mf.returnMessage.packageName),
Expand Down
12 changes: 12 additions & 0 deletions internal/cmd/pdatagen/internal/one_of_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ const oneOfCopyOrigTemplate = `switch t := src.{{ .originFieldName }}.(type) {
{{- end }}
}`

const oneOfMarshalJSONTemplate = `switch ov := ms.{{ .origAccessor }}.{{ .originFieldName }}.(type) {
{{- range .values }}
{{ .GenerateMarshalJSON $.baseStruct $.OneOfField }}
{{- end }}
}`

type OneOfField struct {
originFieldName string
typeName string
Expand Down Expand Up @@ -74,6 +80,11 @@ func (of *OneOfField) GenerateCopyOrig(ms *messageStruct) string {
return executeTemplate(t, of.templateFields(ms))
}

func (of *OneOfField) GenerateMarshalJSON(ms *messageStruct) string {
t := template.Must(templateNew("oneOfMarshalJSONTemplate").Parse(oneOfMarshalJSONTemplate))
return executeTemplate(t, of.templateFields(ms))
}

func (of *OneOfField) templateFields(ms *messageStruct) map[string]any {
return map[string]any{
"baseStruct": ms,
Expand All @@ -99,4 +110,5 @@ type oneOfValue interface {
GenerateSetWithTestValue(ms *messageStruct, of *OneOfField) string
GenerateCopyOrig(ms *messageStruct, of *OneOfField) string
GenerateType(ms *messageStruct, of *OneOfField) string
GenerateMarshalJSON(ms *messageStruct, of *OneOfField) string
}
9 changes: 9 additions & 0 deletions internal/cmd/pdatagen/internal/one_of_message_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const oneOfMessageCopyOrigTemplate = ` case *{{ .originStructType }}:
const oneOfMessageTypeTemplate = `case *{{ .originStructName }}_{{ .originFieldName }}:
return {{ .typeName }}`

const oneOfMessageMarshalJSONTemplate = `case *{{ .originStructName }}_{{ .originFieldName }}:
dest.WriteObjectField("{{ lowerFirst .originFieldName }}")
new{{ .returnType }}(ov.{{ .fieldName }}, ms.state).marshalJSONStream(dest)`

type OneOfMessageValue struct {
fieldName string
originFieldPackageName string
Expand Down Expand Up @@ -99,6 +103,11 @@ func (omv *OneOfMessageValue) GenerateType(ms *messageStruct, of *OneOfField) st
return executeTemplate(t, omv.templateFields(ms, of))
}

func (omv *OneOfMessageValue) GenerateMarshalJSON(ms *messageStruct, of *OneOfField) string {
t := template.Must(templateNew("oneOfMessageMarshalJSONTemplate").Parse(oneOfMessageMarshalJSONTemplate))
return executeTemplate(t, omv.templateFields(ms, of))
}

func (omv *OneOfMessageValue) templateFields(ms *messageStruct, of *OneOfField) map[string]any {
return map[string]any{
"fieldName": omv.fieldName,
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/pdatagen/internal/one_of_primitive_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ const oneOfPrimitiveCopyOrigTemplate = `case *{{ .originStructName }}_{{ .origin
const oneOfPrimitiveTypeTemplate = `case *{{ .originStructName }}_{{ .originFieldName }}:
return {{ .typeName }}`

const oneOfPrimitiveMarshalJSONTemplate = `case *{{ .originStructName }}_{{ .originFieldName }}:
dest.WriteObjectField("{{ lowerFirst .originFieldName }}")
dest.Write{{ upperFirst .returnType }}(ov.{{ .originFieldName }})`

type OneOfPrimitiveValue struct {
fieldName string
defaultVal string
Expand Down Expand Up @@ -87,6 +91,11 @@ func (opv *OneOfPrimitiveValue) GenerateType(ms *messageStruct, of *OneOfField)
return executeTemplate(t, opv.templateFields(ms, of))
}

func (opv *OneOfPrimitiveValue) GenerateMarshalJSON(ms *messageStruct, of *OneOfField) string {
t := template.Must(templateNew("oneOfPrimitiveMarshalJSONTemplate").Parse(oneOfPrimitiveMarshalJSONTemplate))
return executeTemplate(t, opv.templateFields(ms, of))
}

func (opv *OneOfPrimitiveValue) templateFields(ms *messageStruct, of *OneOfField) map[string]any {
return map[string]any{
"structName": ms.getName(),
Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/pdatagen/internal/optional_primitive_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ const optionalPrimitiveCopyOrigTemplate = `if src{{ .fieldName }}, ok := src.{{
dest.{{ .fieldName }}_ = nil
}`

const optionalPrimitiveMarshalJSONTemplate = `if ms.Has{{ .fieldName }}() {
dest.WriteObjectField("{{ lowerFirst .fieldName }}")
dest.Write{{ upperFirst .returnType }}(ms.{{ .fieldName }}())
}`

type OptionalPrimitiveField struct {
fieldName string
defaultVal string
Expand Down Expand Up @@ -94,6 +99,11 @@ func (opv *OptionalPrimitiveField) GenerateCopyOrig(ms *messageStruct) string {
return executeTemplate(t, opv.templateFields(ms))
}

func (opv *OptionalPrimitiveField) GenerateMarshalJSON(ms *messageStruct) string {
t := template.Must(templateNew("optionalPrimitiveMarshalJSONTemplate").Parse(optionalPrimitiveMarshalJSONTemplate))
return executeTemplate(t, opv.templateFields(ms))
}

func (opv *OptionalPrimitiveField) templateFields(ms *messageStruct) map[string]any {
return map[string]any{
"structName": ms.getName(),
Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/pdatagen/internal/primitive_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ const primitiveSetTestTemplate = `tv.orig.{{ .originFieldName }} = {{ .testValue

const primitiveCopyOrigTemplate = `dest.{{ .originFieldName }} = src.{{ .originFieldName }}`

const primitiveMarshalJSONTemplate = `if ms.orig.{{ .originFieldName }} != {{ .defaultVal }} {
dest.WriteObjectField("{{ lowerFirst .originFieldName }}")
dest.Write{{ upperFirst .returnType }}(ms.orig.{{ .originFieldName }})
}`

type PrimitiveField struct {
fieldName string
originFieldName string
Expand Down Expand Up @@ -76,6 +81,11 @@ func (pf *PrimitiveField) GenerateCopyOrig(ms *messageStruct) string {
return executeTemplate(t, pf.templateFields(ms))
}

func (pf *PrimitiveField) GenerateMarshalJSON(ms *messageStruct) string {
t := template.Must(templateNew("primitiveMarshalJSONTemplate").Parse(primitiveMarshalJSONTemplate))
return executeTemplate(t, pf.templateFields(ms))
}

func (pf *PrimitiveField) templateFields(ms *messageStruct) map[string]any {
return map[string]any{
"structName": ms.getName(),
Expand Down
13 changes: 13 additions & 0 deletions internal/cmd/pdatagen/internal/primitive_slice_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const primitiveSliceCopyOrigTemplate = `dest.{{ .originFieldName }} =
{{- if .isCommon }}{{ if not .isBaseStructCommon }}internal.{{ end }}CopyOrig{{ else }}copyOrig{{ end }}
{{- .returnType }}(dest.{{ .originFieldName }}, src.{{ .originFieldName }})`

const primitiveSliceMarshalJSONTemplate = `dest.WriteObjectField("{{ lowerFirst .originFieldName }}")
{{- if .isCommon }}
{{ if not .isBaseStructCommon }}internal.{{ end }}MarshalJSONStream{{ .returnType }}(
{{- if not .isBaseStructCommon }}internal.{{ end }}New{{ .returnType }}(&ms.orig.{{ .originFieldName }}, ms.state), dest)
{{- else }}
ms.{{ .fieldName }}().marshalJSONStream(dest)
{{- end }}`

// PrimitiveSliceField is used to generate fields for slice of primitive types
type PrimitiveSliceField struct {
fieldName string
Expand Down Expand Up @@ -56,6 +64,11 @@ func (psf *PrimitiveSliceField) GenerateCopyOrig(ms *messageStruct) string {
return executeTemplate(t, psf.templateFields(ms))
}

func (psf *PrimitiveSliceField) GenerateMarshalJSON(ms *messageStruct) string {
t := template.Must(templateNew("primitiveSliceMarshalJSONTemplate").Parse(primitiveSliceMarshalJSONTemplate))
return executeTemplate(t, psf.templateFields(ms))
}

func (psf *PrimitiveSliceField) templateFields(ms *messageStruct) map[string]any {
return map[string]any{
"structName": ms.getName(),
Expand Down
15 changes: 15 additions & 0 deletions internal/cmd/pdatagen/internal/slice_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ const sliceCopyOrigTemplate = `dest.{{ .originFieldName }} =
{{- if .isCommon }}{{ if not .isBaseStructCommon }}internal.{{ end }}CopyOrig{{ else }}copyOrig{{ end }}
{{- .returnType }}(dest.{{ .originFieldName }}, src.{{ .originFieldName }})`

const sliceMarshalJSONTemplate = `if len(ms.orig.{{ .originFieldName }}) > 0 {
dest.WriteObjectField("{{ lowerFirst .originFieldName }}")
{{- if .isCommon }}
{{ if not .isBaseStructCommon }}internal.{{ end }}MarshalJSONStream{{ .returnType }}(
{{- if not .isBaseStructCommon }}internal.{{ end }}New{{ .returnType }}(&ms.orig.{{ .originFieldName }}, ms.state), dest)
{{- else }}
ms.{{ .fieldName }}().marshalJSONStream(dest)
{{- end }}
}`

type SliceField struct {
fieldName string
originFieldName string
Expand Down Expand Up @@ -79,6 +89,11 @@ func (sf *SliceField) GenerateCopyOrig(ms *messageStruct) string {
return executeTemplate(t, sf.templateFields(ms))
}

func (sf *SliceField) GenerateMarshalJSON(ms *messageStruct) string {
t := template.Must(templateNew("sliceMarshalJSONTemplate").Parse(sliceMarshalJSONTemplate))
return executeTemplate(t, sf.templateFields(ms))
}

func (sf *SliceField) templateFields(ms *messageStruct) map[string]any {
return map[string]any{
"structName": ms.getName(),
Expand Down
15 changes: 12 additions & 3 deletions internal/cmd/pdatagen/internal/templates/message.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package {{ .packageName }}

import (
{{ range $index, $element := .imports -}}
{{ $element }}
{{ end }}
{{ range $index, $element := .imports -}}
{{ $element }}
{{ end }}
)

{{ .description }}
Expand Down Expand Up @@ -81,6 +81,15 @@ func (ms {{ .structName }}) getState() *internal.State {
return internal.Get{{ .structName }}State(internal.{{ .structName }}(ms))
}
{{- else }}
// marshalJSONStream marshals all properties from the current struct to the destination stream.
func (ms {{ .structName }}) marshalJSONStream(dest *json.Stream) {
dest.WriteObjectStart()
{{ range .fields -}}
{{ .GenerateMarshalJSON $.messageStruct }}
{{ end -}}
dest.WriteObjectEnd()
}

func copyOrig{{ .structName }}(dest, src *{{ .originName }}) {
{{- range .fields }}
{{ .GenerateCopyOrig $.messageStruct }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
package internal

import (
{{ range $index, $element := .imports -}}
{{ if ne $element "go.opentelemetry.io/collector/pdata/internal" }}
{{ $element }}
{{ end }}
{{ end }}
{{ range $index, $element := .imports -}}
{{ if ne $element "go.opentelemetry.io/collector/pdata/internal" }}
{{ $element }}
{{- end }}
{{- end }}
)

type {{ .structName }} struct {
Expand Down Expand Up @@ -50,3 +50,12 @@ func FillTest{{ .structName }}(tv {{ .structName }}) {
{{ .GenerateSetWithTestValue $.messageStruct }}
{{- end }}
}

// MarshalJSONStream marshals all properties from the current struct to the destination stream.
func MarshalJSONStream{{ .structName }}(ms {{ .structName }}, dest *json.Stream) {
dest.WriteObjectStart()
{{ range .fields -}}
{{ .GenerateMarshalJSON $.messageStruct }}
{{ end -}}
dest.WriteObjectEnd()
}
28 changes: 25 additions & 3 deletions internal/cmd/pdatagen/internal/templates/message_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package {{ .packageName }}

import (
{{ range $index, $element := .testImports -}}
{{ $element }}
{{ end }}
{{ range $index, $element := .testImports -}}
{{ $element }}
{{ end }}
)

func Test{{ .structName }}_MoveTo(t *testing.T) {
Expand Down Expand Up @@ -37,6 +37,28 @@ func Test{{ .structName }}_CopyTo(t *testing.T) {
assert.Panics(t, func() { ms.CopyTo(new{{ .structName }}(&{{ .originName }}{}, &sharedState)) })
}

func Test{{ .structName }}_MarshalAndUnmarshal(t *testing.T) {
stream := json.BorrowStream(nil)
defer json.ReturnStream(stream)
src := generateTest{{ .structName }}()
{{- if .isCommon }}
internal.MarshalJSONStream{{ .structName }}(internal.{{ .structName }}(src), stream)
{{- else }}
src.marshalJSONStream(stream)
{{- end }}

iter := json.BorrowIterator(stream.Buffer())
defer json.ReturnIterator(iter)
dest := New{{ .structName }}()
{{- if .isCommon }}
internal.UnmarshalJSONIter{{ .structName }}(internal.{{ .structName }}(dest), iter)
{{- else }}
dest.unmarshalJsoniter(iter)
{{- end }}

assert.Equal(t, src, dest)
}

{{ range .fields }}
{{ .GenerateAccessorsTest $.messageStruct }}
{{ end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package {{ .packageName }}

import (
{{ range $index, $element := .imports -}}
{{ $element }}
{{ end }}
{{ range $index, $element := .imports -}}
{{ $element }}
{{ end }}
)

// {{ .structName }} represents a []{{ .itemType }} slice.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
package internal

import (
{{ range $index, $element := .imports -}}
{{ if ne $element "go.opentelemetry.io/collector/pdata/internal" }}
{{ $element }}
{{ end }}
{{ end }}
{{ range $index, $element := .imports -}}
{{ if ne $element "go.opentelemetry.io/collector/pdata/internal" }}
{{ $element }}
{{- end }}
{{- end }}
)

type {{ .structName }} struct {
Expand Down Expand Up @@ -47,3 +47,20 @@ func GenerateTest{{ .structName }}() {{ .structName }} {
FillTest{{ .structName }}(ms)
return ms
}

// MarshalJSONStream marshals all properties from the current struct to the destination stream.
func MarshalJSONStream{{ .structName }}(ms {{ .structName }}, dest *json.Stream) {
{{- if eq .itemType "byte" }}
dest.WriteString(base64.StdEncoding.EncodeToString(*ms.orig))
{{- else }}
dest.WriteArrayStart()
if len(*ms.orig) > 0 {
dest.Write{{ upperFirst .itemType }}((*ms.orig)[0])
}
for i := 1; i < len((*ms.orig)); i++ {
dest.WriteMore()
dest.Write{{ upperFirst .itemType }}((*ms.orig)[i])
}
dest.WriteArrayEnd()
{{- end }}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package {{ .packageName }}

import (
{{ range $index, $element := .testImports -}}
{{ $element }}
{{ end }}
{{ range $index, $element := .testImports -}}
{{ $element }}
{{ end }}
)

func TestNew{{ .structName }}(t *testing.T) {
Expand Down
Loading
Loading