Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Place goccy/go-json behind built tags and revert default encoder to encoder/json package #2270

Merged
merged 11 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .generator/src/generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def cli(specs, output):
"utils.go": env.get_template("utils.j2"),
"zstd.go": env.get_template("zstd.j2"),
"no_zstd.go": env.get_template("no_zstd.j2"),
"encoding_json.go": env.get_template("encoding_json.j2"),
"goccy_gojson.go": env.get_template("goccy_gojson.j2"),
}

test_scenarios_files = {
Expand Down
5 changes: 2 additions & 3 deletions .generator/src/generator/templates/client.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"time"
"unicode/utf8"

"github.com/goccy/go-json"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -437,7 +436,7 @@ func (c *APIClient) Decode(v interface{}, b []byte, contentType string) (err err
} else {
return errors.New("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
}
} else if err = json.Unmarshal(b, v); err != nil { // simple model
} else if err = Unmarshal(b, v); err != nil { // simple model
return err
}
return nil
Expand Down Expand Up @@ -486,7 +485,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
} else if s, ok := body.(*string); ok {
_, err = bodyBuf.WriteString(*s)
} else if jsonCheck.MatchString(contentType) {
err = json.NewEncoder(bodyBuf).Encode(body)
err = newEncoder(bodyBuf).Encode(body)
} else if xmlCheck.MatchString(contentType) {
err = xml.NewEncoder(bodyBuf).Encode(body)
}
Expand Down
21 changes: 21 additions & 0 deletions .generator/src/generator/templates/encoding_json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% include "partial_header.j2" %}
//go:build !goccy_gojson

package {{ common_package_name }}

import (
"encoding/json"
"io"
)

func Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

func Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}

func newEncoder(w io.Writer) *json.Encoder {
return json.NewEncoder(w)
}
22 changes: 22 additions & 0 deletions .generator/src/generator/templates/goccy_gojson.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% include "partial_header.j2" %}
//go:build goccy_gojson

package {{ common_package_name }}

import (
"io"

"github.com/goccy/go-json"
)

func Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

func Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}

func newEncoder(w io.Writer) *json.Encoder {
return json.NewEncoder(w)
}
1 change: 0 additions & 1 deletion .generator/src/generator/templates/model.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package {{ package_name }}

import (
"github.com/goccy/go-json"
"github.com/google/uuid"
"fmt"

Expand Down
6 changes: 3 additions & 3 deletions .generator/src/generator/templates/model_enum.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (v *{{ name }}) GetAllowedValues() []{{ name }} {
// UnmarshalJSON deserializes the given payload.
func (v *{{ name }}) UnmarshalJSON(src []byte) error {
var value {{ model|simple_type }}
err := json.Unmarshal(src, &value)
err := datadog.Unmarshal(src, &value)
if err != nil {
return err
}
Expand Down Expand Up @@ -91,12 +91,12 @@ func NewNullable{{ name }}(val *{{ name }}) *Nullable{{ name }} {

// MarshalJSON serializes the associated value.
func (v Nullable{{ name }}) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return datadog.Marshal(v.value)
}

// UnmarshalJSON deserializes the payload and sets the flag as if Set has been called.
func (v *Nullable{{ name }}) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return datadog.Unmarshal(src, &v.value)
}
{%- endif %}
14 changes: 7 additions & 7 deletions .generator/src/generator/templates/model_oneof.j2
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (obj *{{ name }}) UnmarshalJSON(data []byte) error {
{{#-first}}
// use discriminator value to speed up the lookup
var jsonDict map[string]interface{}
err = json.Unmarshal(data, &jsonDict)
err = datadog.Unmarshal(data, &jsonDict)
if err != nil {
return fmt.Errorf("failed to unmarshal JSON into map for the discriminator lookup.")
}
Expand All @@ -39,7 +39,7 @@ func (obj *{{ name }}) UnmarshalJSON(data []byte) error {
// check if the discriminator value is '{{{mappingName}}}'
if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
// try to unmarshal JSON data into {{{modelName}}}
err = json.Unmarshal(data, &obj.{{{modelName}}})
err = datadog.Unmarshal(data, &obj.{{{modelName}}})
if err == nil {
return nil // data stored in obj.{{{modelName}}}, return on the first match
} else {
Expand All @@ -59,10 +59,10 @@ func (obj *{{ name }}) UnmarshalJSON(data []byte) error {
{%- set dataType = get_type(oneOf) %}
{%- set attributeName = (get_name(oneOf) or dataType)|upperfirst %}
// try to unmarshal data into {{ attributeName }}
err = json.Unmarshal(data, &obj.{{ attributeName }})
err = datadog.Unmarshal(data, &obj.{{ attributeName }})
if err == nil {
if obj.{{ attributeName }} != nil {% if oneOf.get("type", "object") == "object" %}&& obj.{{ attributeName }}.UnparsedObject == nil {% endif %}{
json{{ attributeName }}, _ := json.Marshal(obj.{{ attributeName }})
json{{ attributeName }}, _ := datadog.Marshal(obj.{{ attributeName }})
if string(json{{ attributeName }}) == "{}" { // empty struct
obj.{{ attributeName }} = nil
} else {
Expand All @@ -82,7 +82,7 @@ func (obj *{{ name }}) UnmarshalJSON(data []byte) error {
{%- set attributeName = (get_name(oneOf) or dataType)|upperfirst %}
obj.{{ attributeName }} = nil
{%- endfor %}
return json.Unmarshal(data, &obj.UnparsedObject)
return datadog.Unmarshal(data, &obj.UnparsedObject)
}
return nil // exactly one match
{%- endif %}
Expand All @@ -94,12 +94,12 @@ func (obj {{ name }}) MarshalJSON() ([]byte, error) {
{%- set dataType = get_type(oneOf) %}
{%- set attributeName = (get_name(oneOf) or dataType)|upperfirst %}
if obj.{{ attributeName }} != nil {
return json.Marshal(&obj.{{ attributeName }})
return datadog.Marshal(&obj.{{ attributeName }})
}

{% endfor %}
if obj.UnparsedObject != nil {
return json.Marshal(obj.UnparsedObject)
return datadog.Marshal(obj.UnparsedObject)
}
return nil, nil // no data in oneOf schemas
}
Expand Down
18 changes: 9 additions & 9 deletions .generator/src/generator/templates/model_simple.j2
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (o *{{ name }}) Unset{{ propertyName }}() {
func (o {{ name }}) MarshalJSON() ([]byte, error) {
toSerialize := {% if model.type == "array" %}make([]interface{}, len(o.Items)){% else %}map[string]interface{}{}{% endif %}
if o.UnparsedObject != nil {
return json.Marshal(o.UnparsedObject)
return datadog.Marshal(o.UnparsedObject)
}
{%- if model.type == "array" %}
for i, item := range o.Items {
Expand Down Expand Up @@ -312,23 +312,23 @@ toSerialize["{{ attr }}"] = o.{{ propertyName }}
}

{%- endif %}
return json.Marshal(toSerialize)
return datadog.Marshal(toSerialize)
}

// UnmarshalJSON deserializes the given payload.
{%- if model.type == "array" %}
{%- set isPrimitiveContainer = model.get("items")|is_primitive %}
func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
if err = json.Unmarshal(bytes, &o.Items); err != nil {
return json.Unmarshal(bytes, &o.UnparsedObject)
if err = datadog.Unmarshal(bytes, &o.Items); err != nil {
return datadog.Unmarshal(bytes, &o.UnparsedObject)
}

{%- if not isPrimitiveContainer %}

if o.Items != nil && len(o.Items) > 0 {
for _, v := range o.Items {
if v.UnparsedObject != nil {
return json.Unmarshal(bytes, &o.UnparsedObject)
return datadog.Unmarshal(bytes, &o.UnparsedObject)
}
}
}
Expand All @@ -350,8 +350,8 @@ func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
{%- endif %}
{%- endfor %}
}{}
if err = json.Unmarshal(bytes, &all); err != nil {
return json.Unmarshal(bytes, &o.UnparsedObject)
if err = datadog.Unmarshal(bytes, &all); err != nil {
return datadog.Unmarshal(bytes, &o.UnparsedObject)
}
{%- for attr, spec in model.get("properties", {}).items() if attr in model.get("required", []) %}
{%- set propertyName = attr|attribute_name %}
Expand All @@ -366,7 +366,7 @@ func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {

{%- if model.additionalProperties is not false %}
additionalProperties := make(map[string]{{ get_type(model.get("additionalProperties", True)) }})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
if err = datadog.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{ {% for attr, schema in model.get("properties", {}).items()%}"{{ attr }}", {% endfor %} })
} else {
return err
Expand Down Expand Up @@ -436,7 +436,7 @@ func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
{%- if ns.hasCheckedFields %}

if hasInvalidField {
return json.Unmarshal(bytes, &o.UnparsedObject)
return datadog.Unmarshal(bytes, &o.UnparsedObject)
}
{%- endif %}

Expand Down
4 changes: 2 additions & 2 deletions .generator/src/generator/templates/nullable_model.j2
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewNullable{{ name }}(val *{{ name }}) *Nullable{{ name }} {

// MarshalJSON serializes the associated value.
func (v Nullable{{ name }}) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return datadog.Marshal(v.value)
}

// UnmarshalJSON deserializes the payload and sets the flag as if Set has been called.
Expand All @@ -45,5 +45,5 @@ func (v *Nullable{{ name }}) UnmarshalJSON(src []byte) error {
return nil
}

return json.Unmarshal(src, &v.value)
return datadog.Unmarshal(src, &v.value)
}
35 changes: 17 additions & 18 deletions .generator/src/generator/templates/utils.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package {{ common_package_name }}

import (
"bytes"
"github.com/goccy/go-json"
"io"
"net/http"
"reflect"
Expand Down Expand Up @@ -76,13 +75,13 @@ func NewNullableBool(val *bool) *NullableBool {

// MarshalJSON serializes the associated value.
func (v NullableBool) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableBool) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableInt is a struct to hold a nullable int value.
Expand Down Expand Up @@ -120,13 +119,13 @@ func NewNullableInt(val *int) *NullableInt {

// MarshalJSON serializes the associated value.
func (v NullableInt) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableInt) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableInt32 is a struct to hold a nullable int32 value.
Expand Down Expand Up @@ -164,13 +163,13 @@ func NewNullableInt32(val *int32) *NullableInt32 {

// MarshalJSON serializes the associated value.
func (v NullableInt32) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableInt32) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableInt64 is a struct to hold a nullable int64 value.
Expand Down Expand Up @@ -208,13 +207,13 @@ func NewNullableInt64(val *int64) *NullableInt64 {

// MarshalJSON serializes the associated value.
func (v NullableInt64) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableInt64) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableFloat32 is a struct to hold a nullable float32 value.
Expand Down Expand Up @@ -252,13 +251,13 @@ func NewNullableFloat32(val *float32) *NullableFloat32 {

// MarshalJSON serializes the associated value.
func (v NullableFloat32) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableFloat32) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableFloat64 is a struct to hold a nullable float64 value.
Expand Down Expand Up @@ -296,13 +295,13 @@ func NewNullableFloat64(val *float64) *NullableFloat64 {

// MarshalJSON serializes the associated value.
func (v NullableFloat64) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableFloat64) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableString is a struct to hold a nullable string value.
Expand Down Expand Up @@ -340,13 +339,13 @@ func NewNullableString(val *string) *NullableString {

// MarshalJSON serializes the associated value.
func (v NullableString) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableString) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableTime is a struct to hold a nullable Time value.
Expand Down Expand Up @@ -390,7 +389,7 @@ func (v NullableTime) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes to the associated value.
func (v *NullableTime) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// NullableList struct to hold nullable list value.
Expand Down Expand Up @@ -428,13 +427,13 @@ func NewNullableList[T any](val *[]T) *NullableList[T] {

// MarshalJSON serializes the associated value.
func (v NullableList[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
return Marshal(v.value)
}

// UnmarshalJSON deserializes to the associated value.
func (v *NullableList[T]) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
return Unmarshal(src, &v.value)
}

// DeleteKeys helper method to delete keys from a map
Expand Down
Loading