Skip to content

Commit

Permalink
Sort declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
atombender committed Mar 12, 2019
1 parent 8355fe7 commit 871b21b
Show file tree
Hide file tree
Showing 16 changed files with 400 additions and 192 deletions.
41 changes: 39 additions & 2 deletions pkg/codegen/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codegen

import (
"sort"
"strings"

"github.com/sanity-io/litter"
Expand All @@ -10,6 +11,11 @@ type Decl interface {
Generate(out *Emitter)
}

type Named interface {
Decl
GetName() string
}

type File struct {
FileName string
Package Package
Expand Down Expand Up @@ -68,7 +74,18 @@ func (p *Package) Generate(out *Emitter) {
}
}
out.Newline()
for i, t := range p.Decls {

sorted := make([]Decl, len(p.Decls))
copy(sorted, p.Decls)
sort.Slice(sorted, func(i, j int) bool {
if a, ok := sorted[i].(Named); ok {
if b, ok := sorted[j].(Named); ok {
return a.GetName() < b.GetName()
}
}
return false
})
for i, t := range sorted {
if i > 0 {
out.Newline()
}
Expand All @@ -83,6 +100,10 @@ type Var struct {
Value interface{}
}

func (v *Var) GetName() string {
return v.Name
}

func (v *Var) Generate(out *Emitter) {
out.Print("var %s ", v.Name)
if v.Type != nil {
Expand All @@ -98,6 +119,10 @@ type Constant struct {
Value interface{}
}

func (c *Constant) GetName() string {
return c.Name
}

func (c *Constant) Generate(out *Emitter) {
out.Print("const %s ", c.Name)
if c.Type != nil {
Expand Down Expand Up @@ -145,6 +170,10 @@ type TypeDecl struct {
Comment string
}

func (td *TypeDecl) GetName() string {
return td.Name
}

func (td *TypeDecl) Generate(out *Emitter) {
out.Comment(td.Comment)
out.Print("type %s ", td.Name)
Expand Down Expand Up @@ -184,8 +213,12 @@ type NamedType struct {
Decl *TypeDecl
}

func (t NamedType) GetName() string {
return t.Decl.Name
}

func (t NamedType) IsNillable() bool {
return t.Decl.Type.IsNillable()
return t.Decl.Type != nil && t.Decl.Type.IsNillable()
}

func (t NamedType) Generate(out *Emitter) {
Expand Down Expand Up @@ -263,6 +296,10 @@ type StructField struct {
DefaultValue interface{}
}

func (f *StructField) GetName() string {
return f.Name
}

func (f *StructField) Generate(out *Emitter) {
out.Comment(f.Comment)
out.Print("%s ", f.Name)
Expand Down
14 changes: 7 additions & 7 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ func (g *schemaGenerator) generateRootType() error {
return errors.New("schema has no root")
}

if len(g.schema.Type.Type) == 0 {
for _, name := range sortDefinitionsByName(g.schema.Definitions) {
def := g.schema.Definitions[name]
_, err := g.generateDeclaredType(def, newNameScope(g.identifierize(name)))
if err != nil {
return err
}
for _, name := range sortDefinitionsByName(g.schema.Definitions) {
def := g.schema.Definitions[name]
_, err := g.generateDeclaredType(def, newNameScope(g.identifierize(name)))
if err != nil {
return err
}
}
if len(g.schema.Type.Type) == 0 {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions tests/data/core/4.2.1_array.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

package test

type A421ArrayMyObjectArrayElem map[string]interface{}

type A421Array struct {
// MyBooleanArray corresponds to the JSON schema field "myBooleanArray".
MyBooleanArray []bool `json:"myBooleanArray,omitempty"`
Expand All @@ -23,3 +21,5 @@ type A421Array struct {
// MyStringArray corresponds to the JSON schema field "myStringArray".
MyStringArray []string `json:"myStringArray,omitempty"`
}

type A421ArrayMyObjectArrayElem map[string]interface{}
4 changes: 2 additions & 2 deletions tests/data/core/objectEmpty.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

package test

type ObjectEmptyFoo map[string]interface{}

type ObjectEmpty struct {
// Foo corresponds to the JSON schema field "foo".
Foo ObjectEmptyFoo `json:"foo,omitempty"`
}

type ObjectEmptyFoo map[string]interface{}
12 changes: 6 additions & 6 deletions tests/data/core/objectNested.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

package test

type ObjectNestedMyObjectMyObject struct {
// MyString corresponds to the JSON schema field "myString".
MyString *string `json:"myString,omitempty"`
type ObjectNested struct {
// MyObject corresponds to the JSON schema field "myObject".
MyObject *ObjectNestedMyObject `json:"myObject,omitempty"`
}

type ObjectNestedMyObject struct {
// MyObject corresponds to the JSON schema field "myObject".
MyObject *ObjectNestedMyObjectMyObject `json:"myObject,omitempty"`
}

type ObjectNested struct {
// MyObject corresponds to the JSON schema field "myObject".
MyObject *ObjectNestedMyObject `json:"myObject,omitempty"`
type ObjectNestedMyObjectMyObject struct {
// MyString corresponds to the JSON schema field "myString".
MyString *string `json:"myString,omitempty"`
}
10 changes: 5 additions & 5 deletions tests/data/core/ref.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

package test

type Thing struct {
// Name corresponds to the JSON schema field "name".
Name *string `json:"name,omitempty"`
}

type Ref struct {
// MyThing corresponds to the JSON schema field "myThing".
MyThing *Thing `json:"myThing,omitempty"`

// MyThing2 corresponds to the JSON schema field "myThing2".
MyThing2 *Thing `json:"myThing2,omitempty"`
}

type Thing struct {
// Name corresponds to the JSON schema field "name".
Name *string `json:"name,omitempty"`
}
10 changes: 5 additions & 5 deletions tests/data/core/refExternalFile.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

package test

type Thing struct {
// Name corresponds to the JSON schema field "name".
Name *string `json:"name,omitempty"`
}

type Ref struct {
// MyThing corresponds to the JSON schema field "myThing".
MyThing *Thing `json:"myThing,omitempty"`
Expand All @@ -23,3 +18,8 @@ type RefExternalFile struct {
// "someOtherExternalThing".
SomeOtherExternalThing *Thing `json:"someOtherExternalThing,omitempty"`
}

type Thing struct {
// Name corresponds to the JSON schema field "name".
Name *string `json:"name,omitempty"`
}
28 changes: 14 additions & 14 deletions tests/data/core/refExternalFileWithDupe.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

package test

type Thing struct {
// Name corresponds to the JSON schema field "name".
Name *string `json:"name,omitempty"`
}

type Ref struct {
// MyThing corresponds to the JSON schema field "myThing".
MyThing *Thing `json:"myThing,omitempty"`
MyThing *Thing_1 `json:"myThing,omitempty"`

// MyThing2 corresponds to the JSON schema field "myThing2".
MyThing2 *Thing `json:"myThing2,omitempty"`
}

type Thing_1 struct {
// Something corresponds to the JSON schema field "something".
Something *string `json:"something,omitempty"`
MyThing2 *Thing_1 `json:"myThing2,omitempty"`
}

type RefExternalFileWithDupe struct {
// MyExternalThing corresponds to the JSON schema field "myExternalThing".
MyExternalThing *Thing `json:"myExternalThing,omitempty"`
MyExternalThing *Thing_1 `json:"myExternalThing,omitempty"`

// MyThing corresponds to the JSON schema field "myThing".
MyThing *Thing_1 `json:"myThing,omitempty"`
MyThing *Thing `json:"myThing,omitempty"`
}

type Thing struct {
// Something corresponds to the JSON schema field "something".
Something *string `json:"something,omitempty"`
}

type Thing_1 struct {
// Name corresponds to the JSON schema field "name".
Name *string `json:"name,omitempty"`
}
38 changes: 34 additions & 4 deletions tests/data/core/refToEnum.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,40 @@ func (j *Thing) UnmarshalJSON(b []byte) error {
return nil
}

const ThingX Thing = "x"
const ThingY Thing = "y"

type RefToEnum struct {
// MyThing corresponds to the JSON schema field "myThing".
MyThing *Thing `json:"myThing,omitempty"`
MyThing *Thing_1 `json:"myThing,omitempty"`
}

const ThingX Thing = "x"

type Thing_1 string

var enumValues_Thing_1 = []interface{}{
"x",
"y",
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *Thing_1) UnmarshalJSON(b []byte) error {
var v string
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var ok bool
for _, expected := range enumValues_Thing_1 {
if reflect.DeepEqual(v, expected) {
ok = true
break
}
}
if !ok {
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_Thing_1, v)
}
*j = Thing_1(v)
return nil
}

const ThingY Thing = "y"
const Thing_1_X Thing_1 = "x"
const Thing_1_Y Thing_1 = "y"
4 changes: 2 additions & 2 deletions tests/data/core/refToPrimitiveString.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

package test

type Thing string

type RefToPrimitiveString struct {
// MyThing corresponds to the JSON schema field "myThing".
MyThing *Thing `json:"myThing,omitempty"`
}

type Thing string
10 changes: 5 additions & 5 deletions tests/data/crossPackage/schema.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package schema

import other "github.com/example/other"

type Thing struct {
// S corresponds to the JSON schema field "s".
S *string `json:"s,omitempty"`
}

type Schema struct {
// DefInOtherSchema corresponds to the JSON schema field "defInOtherSchema".
DefInOtherSchema *other.Thing `json:"defInOtherSchema,omitempty"`

// DefInSameSchema corresponds to the JSON schema field "defInSameSchema".
DefInSameSchema *Thing `json:"defInSameSchema,omitempty"`
}

type Thing struct {
// S corresponds to the JSON schema field "s".
S *string `json:"s,omitempty"`
}
10 changes: 5 additions & 5 deletions tests/data/crossPackageNoOutput/schema.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package schema

import other "github.com/example/other"

type Thing struct {
// S corresponds to the JSON schema field "s".
S *string `json:"s,omitempty"`
}

type Schema struct {
// DefInOtherSchema corresponds to the JSON schema field "defInOtherSchema".
DefInOtherSchema *other.Thing `json:"defInOtherSchema,omitempty"`

// DefInSameSchema corresponds to the JSON schema field "defInSameSchema".
DefInSameSchema *Thing `json:"defInSameSchema,omitempty"`
}

type Thing struct {
// S corresponds to the JSON schema field "s".
S *string `json:"s,omitempty"`
}
10 changes: 5 additions & 5 deletions tests/data/miscWithDefaults/cyclic.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ type Bar struct {
RefToFoo *Foo `json:"refToFoo,omitempty"`
}

type Foo struct {
// RefToBar corresponds to the JSON schema field "refToBar".
RefToBar *Bar `json:"refToBar,omitempty"`
}

type Cyclic struct {
// A corresponds to the JSON schema field "a".
A *Foo `json:"a,omitempty"`
}

type Foo struct {
// RefToBar corresponds to the JSON schema field "refToBar".
RefToBar *Bar `json:"refToBar,omitempty"`
}
10 changes: 5 additions & 5 deletions tests/data/miscWithDefaults/cyclicAndRequired1.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ package test
import "fmt"
import "encoding/json"

type Bar struct {
// RefToFoo corresponds to the JSON schema field "refToFoo".
RefToFoo *Foo `json:"refToFoo,omitempty"`
}

type Foo struct {
// RefToBar corresponds to the JSON schema field "refToBar".
RefToBar Bar `json:"refToBar"`
Expand All @@ -33,6 +28,11 @@ func (j *Foo) UnmarshalJSON(b []byte) error {
return nil
}

type Bar struct {
// RefToFoo corresponds to the JSON schema field "refToFoo".
RefToFoo *Foo `json:"refToFoo,omitempty"`
}

type CyclicAndRequired1 struct {
// A corresponds to the JSON schema field "a".
A *Foo `json:"a,omitempty"`
Expand Down
Loading

0 comments on commit 871b21b

Please sign in to comment.