Skip to content

Commit

Permalink
Update a couple of txtar tests outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Sep 17, 2023
1 parent 808744c commit 266d089
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 14 deletions.
3 changes: 3 additions & 0 deletions internal/jennies/golang/rawtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func (jenny RawTypes) formatObject(def ast.Object) ([]byte, error) {
case ast.KindScalar:
scalarType := def.Type.AsScalar()

//nolint: gocritic
if scalarType.Value != nil {
buffer.WriteString(fmt.Sprintf("const %s = %s", defName, formatScalar(scalarType.Value)))
} else if scalarType.ScalarKind == ast.KindBytes {
buffer.WriteString(fmt.Sprintf("type %s %s", defName, "[]byte"))
} else {
buffer.WriteString(fmt.Sprintf("type %s %s", defName, formatType(def.Type, true, "")))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (resource *{{ .def.Name|formatIdentifier }}) UnmarshalJSON(raw []byte) erro
case "{{ $discriminatorValue }}":
var val{{ $typeName|formatIdentifier }} {{ $typeName|formatIdentifier }}
if err := json.Unmarshal(raw, &val{{ $typeName|formatIdentifier }}); err != nil {
return err
return err
}

resource.Val{{ $typeName|formatIdentifier }} = &val{{ $typeName|formatIdentifier }}
Expand Down
118 changes: 108 additions & 10 deletions testdata/jennies/rawtypes/disjunctions.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
"Kind": "struct",
"Struct": {
"Fields": [
{
"Name": "Type",
"Required": true,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "string", "Value": "some-struct"}
}
},
{
"Name": "FieldAny",
"Required": true,
Expand Down Expand Up @@ -85,11 +93,19 @@
"Struct": {
"Fields": [
{
"Name": "FieldAny",
"Name": "Type",
"Required": true,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "any"}
"Scalar": {"ScalarKind": "string", "Value": "some-other-struct"}
}
},
{
"Name": "Foo",
"Required": true,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "bytes"}
}
}
]
Expand All @@ -103,11 +119,19 @@
"Struct": {
"Fields": [
{
"Name": "FieldAny",
"Name": "Type",
"Required": true,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "any"}
"Scalar": {"ScalarKind": "string", "Value": "yet-another-struct"}
}
},
{
"Name": "Bar",
"Required": true,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "uint8"}
}
}
]
Expand Down Expand Up @@ -146,17 +170,20 @@ export type RefreshRate = string | boolean;
export type StringOrNull = string | null;

export interface SomeStruct {
type: "some-struct";
fieldAny: any;
}

export type BoolOrRef = boolean | SomeStruct;

export interface SomeOtherStruct {
fieldAny: any;
type: "some-other-struct";
foo: string;
}

export interface YetAnotherStruct {
fieldAny: any;
type: "yet-another-struct";
bar: number;
}

export type SeveralRefs = SomeStruct | SomeOtherStruct | YetAnotherStruct;
Expand All @@ -171,17 +198,20 @@ type RefreshRate StringOrBool
type StringOrNull *string

type SomeStruct struct {
Type string `json:"Type"`
FieldAny any `json:"FieldAny"`
}

type BoolOrRef BoolOrSomeStruct

type SomeOtherStruct struct {
FieldAny any `json:"FieldAny"`
Type string `json:"Type"`
Foo bytes `json:"Foo"`
}

type YetAnotherStruct struct {
FieldAny any `json:"FieldAny"`
Type string `json:"Type"`
Bar uint8 `json:"Bar"`
}

type SeveralRefs SomeStructOrSomeOtherStructOrYetAnotherStruct
Expand All @@ -197,12 +227,80 @@ type SomeStructOrSomeOtherStructOrYetAnotherStruct struct {
ValYetAnotherStruct *YetAnotherStruct `json:"ValYetAnotherStruct,omitempty"`
}

func (resource *SomeStructOrSomeOtherStructOrYetAnotherStruct) MarshalJSON() ([]byte, error) {

if resource.ValSomeStruct != nil {
return json.Marshal(resource.ValSomeStruct)
}

if resource.ValSomeOtherStruct != nil {
return json.Marshal(resource.ValSomeOtherStruct)
}

if resource.ValYetAnotherStruct != nil {
return json.Marshal(resource.ValYetAnotherStruct)
}


return nil, nil
}

func (resource *SomeStructOrSomeOtherStructOrYetAnotherStruct) UnmarshalJSON(raw []byte) error {
if raw == nil {
return nil
}

// FIXME: this is wasteful, we need to find a more efficient way to unmarshal this.
parsedAsMap := make(map[string]any)
if err := json.Unmarshal(raw, &parsedAsMap); err != nil {
return err
}

discriminator, found := parsedAsMap["Type"]
if !found {
return errors.New("discriminator field 'Type' not found in payload")
}

switch discriminator {

case "some-other-struct":
var valSomeOtherStruct SomeOtherStruct
if err := json.Unmarshal(raw, &valSomeOtherStruct); err != nil {
return err
}

resource.ValSomeOtherStruct = &valSomeOtherStruct
return nil

case "some-struct":
var valSomeStruct SomeStruct
if err := json.Unmarshal(raw, &valSomeStruct); err != nil {
return err
}

resource.ValSomeStruct = &valSomeStruct
return nil

case "yet-another-struct":
var valYetAnotherStruct YetAnotherStruct
if err := json.Unmarshal(raw, &valYetAnotherStruct); err != nil {
return err
}

resource.ValYetAnotherStruct = &valYetAnotherStruct
return nil

}

return fmt.Errorf("could not unmarshal resource with `Type = %v`", discriminator)
}

type StringOrBool struct {
ValString *string `json:"ValString,omitempty"`
ValBool *bool `json:"ValBool,omitempty"`
}

func (resource StringOrBool) MarshalJSON() ([]byte, error) {
func (resource *StringOrBool) MarshalJSON() ([]byte, error) {

if resource.ValString != nil {
return json.Marshal(resource.ValString)
Expand All @@ -216,7 +314,7 @@ func (resource StringOrBool) MarshalJSON() ([]byte, error) {
return nil, nil
}

func (resource StringOrBool) UnmarshalJSON(raw []byte) error {
func (resource *StringOrBool) UnmarshalJSON(raw []byte) error {
if raw == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/jennies/rawtypes/scalars.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ type ScalarTypeAny any

type ScalarTypeBool bool

type ScalarTypeBytes bytes
type ScalarTypeBytes []byte

type ScalarTypeString string

Expand Down
4 changes: 2 additions & 2 deletions testdata/jennies/rawtypes/struct_with_complex_fields.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ type StringOrBool struct {
ValBool *bool `json:"ValBool,omitempty"`
}

func (resource StringOrBool) MarshalJSON() ([]byte, error) {
func (resource *StringOrBool) MarshalJSON() ([]byte, error) {

if resource.ValString != nil {
return json.Marshal(resource.ValString)
Expand All @@ -244,7 +244,7 @@ func (resource StringOrBool) MarshalJSON() ([]byte, error) {
return nil, nil
}

func (resource StringOrBool) UnmarshalJSON(raw []byte) error {
func (resource *StringOrBool) UnmarshalJSON(raw []byte) error {
if raw == nil {
return nil
}
Expand Down

0 comments on commit 266d089

Please sign in to comment.