Skip to content

Commit

Permalink
fix: set consistent var names in unmarshallers, introduce support for…
Browse files Browse the repository at this point in the history
… both yaml and json in validators
  • Loading branch information
omissis committed Oct 7, 2023
1 parent 0e79c59 commit 4684bc6
Show file tree
Hide file tree
Showing 38 changed files with 1,624 additions and 593 deletions.
2 changes: 2 additions & 0 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,8 @@ func (g *schemaGenerator) generateDeclaredType(t *schemas.Type, scope nameScope)
if t.IsSubSchemaTypeElem() || len(validators) > 0 {
g.generateUnmarshaler(decl, validators)
}

return &codegen.NamedType{Decl: &decl}, nil
}

if _, ok := theType.(*codegen.MapType); ok {
Expand Down
14 changes: 7 additions & 7 deletions pkg/generator/json_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func (jf *jsonFormatter) generate(
) func(*codegen.Emitter) {
return func(out *codegen.Emitter) {
out.Commentf("Unmarshal%s implements %s.Unmarshaler.", strings.ToUpper(formatJSON), formatJSON)
out.Printlnf("func (j *%s) Unmarshal%s(b []byte) error {", declType.Name, strings.ToUpper(formatJSON))
out.Printlnf("func (j *%s) Unmarshal%s(value []byte) error {", declType.Name, strings.ToUpper(formatJSON))
out.Indent(1)
out.Printlnf("var %s map[string]interface{}", varNameRawMap)
out.Printlnf("if err := %s.Unmarshal(b, &%s); err != nil { return err }",
out.Printlnf("if err := %s.Unmarshal(value, &%s); err != nil { return err }",
formatJSON, varNameRawMap)

for _, v := range validators {
if v.desc().beforeJSONUnmarshal {
v.generate(out)
if v.desc().beforeUnmarshal {
v.generate(out, "json")
}
}

Expand All @@ -43,12 +43,12 @@ func (jf *jsonFormatter) generate(

out.Printlnf("type %s %s", tp, declType.Name)
out.Printlnf("var %s %s", varNamePlainStruct, tp)
out.Printlnf("if err := %s.Unmarshal(b, &%s); err != nil { return err }",
out.Printlnf("if err := %s.Unmarshal(value, &%s); err != nil { return err }",
formatJSON, varNamePlainStruct)

for _, v := range validators {
if !v.desc().beforeJSONUnmarshal {
v.generate(out)
if !v.desc().beforeUnmarshal {
v.generate(out, "json")
}
}

Expand Down
49 changes: 27 additions & 22 deletions pkg/generator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

type validator interface {
generate(out *codegen.Emitter)
generate(out *codegen.Emitter, format string)
desc() *validatorDesc
}

type validatorDesc struct {
hasError bool
beforeJSONUnmarshal bool
hasError bool
beforeUnmarshal bool
}

var (
Expand All @@ -35,7 +35,7 @@ type requiredValidator struct {
declName string
}

func (v *requiredValidator) generate(out *codegen.Emitter) {
func (v *requiredValidator) generate(out *codegen.Emitter, format string) {
out.Printlnf(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, v.jsonName)
out.Indent(1)
out.Printlnf(`return fmt.Errorf("field %s in %s: required")`, v.jsonName, v.declName)
Expand All @@ -45,8 +45,8 @@ func (v *requiredValidator) generate(out *codegen.Emitter) {

func (v *requiredValidator) desc() *validatorDesc {
return &validatorDesc{
hasError: true,
beforeJSONUnmarshal: true,
hasError: true,
beforeUnmarshal: true,
}
}

Expand All @@ -56,7 +56,7 @@ type nullTypeValidator struct {
arrayDepth int
}

func (v *nullTypeValidator) generate(out *codegen.Emitter) {
func (v *nullTypeValidator) generate(out *codegen.Emitter, format string) {
value := fmt.Sprintf("%s.%s", varNamePlainStruct, v.fieldName)
fieldName := v.jsonName

Expand Down Expand Up @@ -91,8 +91,8 @@ func (v *nullTypeValidator) generate(out *codegen.Emitter) {

func (v *nullTypeValidator) desc() *validatorDesc {
return &validatorDesc{
hasError: true,
beforeJSONUnmarshal: false,
hasError: true,
beforeUnmarshal: false,
}
}

Expand All @@ -103,7 +103,7 @@ type defaultValidator struct {
defaultValue interface{}
}

func (v *defaultValidator) generate(out *codegen.Emitter) {
func (v *defaultValidator) generate(out *codegen.Emitter, format string) {
defaultValue := v.dumpDefaultValue(out)

out.Printlnf(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, v.jsonName)
Expand Down Expand Up @@ -165,8 +165,8 @@ func (v *defaultValidator) tryDumpDefaultSlice(maxLineLen uint) (string, error)

func (v *defaultValidator) desc() *validatorDesc {
return &validatorDesc{
hasError: false,
beforeJSONUnmarshal: false,
hasError: false,
beforeUnmarshal: false,
}
}

Expand All @@ -178,7 +178,7 @@ type arrayValidator struct {
maxItems int
}

func (v *arrayValidator) generate(out *codegen.Emitter) {
func (v *arrayValidator) generate(out *codegen.Emitter, format string) {
if v.minItems == 0 && v.maxItems == 0 {
return
}
Expand Down Expand Up @@ -227,8 +227,8 @@ func (v *arrayValidator) generate(out *codegen.Emitter) {

func (v *arrayValidator) desc() *validatorDesc {
return &validatorDesc{
hasError: true,
beforeJSONUnmarshal: false,
hasError: true,
beforeUnmarshal: false,
}
}

Expand All @@ -240,7 +240,7 @@ type stringValidator struct {
isNillable bool
}

func (v *stringValidator) generate(out *codegen.Emitter) {
func (v *stringValidator) generate(out *codegen.Emitter, format string) {
if v.minLength == 0 && v.maxLength == 0 {
return
}
Expand Down Expand Up @@ -275,8 +275,8 @@ func (v *stringValidator) generate(out *codegen.Emitter) {

func (v *stringValidator) desc() *validatorDesc {
return &validatorDesc{
hasError: true,
beforeJSONUnmarshal: false,
hasError: true,
beforeUnmarshal: false,
}
}

Expand All @@ -285,15 +285,20 @@ type anyOfValidator struct {
elemCount int
}

func (v *anyOfValidator) generate(out *codegen.Emitter) {
func (v *anyOfValidator) generate(out *codegen.Emitter, format string) {
for i := 0; i < v.elemCount; i++ {
out.Printlnf(`var %s_%d %s_%d`, lowerFirst(v.fieldName), i, upperFirst(v.fieldName), i)
}

out.Printlnf(`var errs []error`)

for i := 0; i < v.elemCount; i++ {
out.Printlnf(`if err := %s_%d.UnmarshalJSON(b); err != nil {`, lowerFirst(v.fieldName), i)
out.Printlnf(
`if err := %s_%d.Unmarshal%s(value); err != nil {`,
lowerFirst(v.fieldName),
i,
strings.ToUpper(format),
)
out.Indent(1)
out.Printlnf(`errs = append(errs, err)`)
out.Indent(-1)
Expand All @@ -309,8 +314,8 @@ func (v *anyOfValidator) generate(out *codegen.Emitter) {

func (v *anyOfValidator) desc() *validatorDesc {
return &validatorDesc{
hasError: true,
beforeJSONUnmarshal: true,
hasError: true,
beforeUnmarshal: true,
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/generator/yaml_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (yf *yamlFormatter) generate(
out.Printlnf("if err := value.Decode(&%s); err != nil { return err }", varNameRawMap)

for _, v := range validators {
if v.desc().beforeJSONUnmarshal {
v.generate(out)
if v.desc().beforeUnmarshal {
v.generate(out, "yaml")
}
}

Expand All @@ -48,8 +48,8 @@ func (yf *yamlFormatter) generate(
out.Printlnf("if err := value.Decode(&%s); err != nil { return err }", varNamePlainStruct)

for _, v := range validators {
if !v.desc().beforeJSONUnmarshal {
v.generate(out)
if !v.desc().beforeUnmarshal {
v.generate(out, "yaml")
}
}

Expand Down
61 changes: 0 additions & 61 deletions tests/data/core/10.2.1.1_allOf.go

This file was deleted.

Loading

0 comments on commit 4684bc6

Please sign in to comment.