Skip to content

Commit

Permalink
Rename generator structs
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Sep 11, 2023
1 parent 71be6a3 commit 3c93617
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
30 changes: 15 additions & 15 deletions internal/jsonschema/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ type Config struct {
Package string
}

type newGenerator struct {
type generator struct {
file *ast.File
}

func GenerateAST(schemaReader io.Reader, c Config) (*ast.File, error) {
g := &newGenerator{
g := &generator{
file: &ast.File{
Package: c.Package,
},
Expand Down Expand Up @@ -65,7 +65,7 @@ func GenerateAST(schemaReader io.Reader, c Config) (*ast.File, error) {
return g.file, nil
}

func (g *newGenerator) declareDefinition(definitionName string, schema *schemaparser.Schema) error {
func (g *generator) declareDefinition(definitionName string, schema *schemaparser.Schema) error {
def, err := g.walkDefinition(schema)
if err != nil {
return fmt.Errorf("%s: %w", definitionName, err)
Expand All @@ -79,7 +79,7 @@ func (g *newGenerator) declareDefinition(definitionName string, schema *schemapa
return nil
}

func (g *newGenerator) walkDefinition(schema *schemaparser.Schema) (ast.Type, error) {
func (g *generator) walkDefinition(schema *schemaparser.Schema) (ast.Type, error) {
var def ast.Type
var err error

Expand Down Expand Up @@ -139,12 +139,12 @@ func (g *newGenerator) walkDefinition(schema *schemaparser.Schema) (ast.Type, er
return def, err
}

func (g *newGenerator) walkDisjunction(_ *schemaparser.Schema) (ast.DisjunctionType, error) {
func (g *generator) walkDisjunction(_ *schemaparser.Schema) (ast.DisjunctionType, error) {
// TODO: finish implementation
return ast.DisjunctionType{}, nil
}

func (g *newGenerator) walkDisjunctionBranches(branches []*schemaparser.Schema) ([]ast.Type, error) {
func (g *generator) walkDisjunctionBranches(branches []*schemaparser.Schema) ([]ast.Type, error) {
definitions := make([]ast.Type, 0, len(branches))
for _, oneOf := range branches {
branch, err := g.walkDefinition(oneOf)
Expand All @@ -158,7 +158,7 @@ func (g *newGenerator) walkDisjunctionBranches(branches []*schemaparser.Schema)
return definitions, nil
}

func (g *newGenerator) walkOneOf(schema *schemaparser.Schema) (ast.DisjunctionType, error) {
func (g *generator) walkOneOf(schema *schemaparser.Schema) (ast.DisjunctionType, error) {
if len(schema.OneOf) == 0 {
return ast.DisjunctionType{}, fmt.Errorf("oneOf with no branches")
}
Expand All @@ -174,7 +174,7 @@ func (g *newGenerator) walkOneOf(schema *schemaparser.Schema) (ast.DisjunctionTy
}

// TODO: what's the difference between oneOf and anyOf?
func (g *newGenerator) walkAnyOf(schema *schemaparser.Schema) (ast.DisjunctionType, error) {
func (g *generator) walkAnyOf(schema *schemaparser.Schema) (ast.DisjunctionType, error) {
if len(schema.AnyOf) == 0 {
return ast.DisjunctionType{}, fmt.Errorf("anyOf with no branches")
}
Expand All @@ -189,12 +189,12 @@ func (g *newGenerator) walkAnyOf(schema *schemaparser.Schema) (ast.DisjunctionTy
}, nil
}

func (g *newGenerator) walkAllOf(_ *schemaparser.Schema) (ast.DisjunctionType, error) {
func (g *generator) walkAllOf(_ *schemaparser.Schema) (ast.DisjunctionType, error) {
// TODO: finish implementation and use correct type
return ast.DisjunctionType{}, nil
}

func (g *newGenerator) walkRef(schema *schemaparser.Schema) (ast.RefType, error) {
func (g *generator) walkRef(schema *schemaparser.Schema) (ast.RefType, error) {
parts := strings.Split(schema.Ref.Ptr, "/")
referredKindName := parts[len(parts)-1] // Very naive

Expand All @@ -208,7 +208,7 @@ func (g *newGenerator) walkRef(schema *schemaparser.Schema) (ast.RefType, error)
}, nil
}

func (g *newGenerator) walkString(_ *schemaparser.Schema) (ast.ScalarType, error) {
func (g *generator) walkString(_ *schemaparser.Schema) (ast.ScalarType, error) {
def := ast.ScalarType{ScalarKind: ast.KindString}

/*
Expand All @@ -223,12 +223,12 @@ func (g *newGenerator) walkString(_ *schemaparser.Schema) (ast.ScalarType, error
return def, nil
}

func (g *newGenerator) walkNumber(_ *schemaparser.Schema) (ast.ScalarType, error) {
func (g *generator) walkNumber(_ *schemaparser.Schema) (ast.ScalarType, error) {
// TODO: finish implementation
return ast.ScalarType{ScalarKind: ast.KindInt64}, nil
}

func (g *newGenerator) walkList(schema *schemaparser.Schema) (ast.ArrayType, error) {
func (g *generator) walkList(schema *schemaparser.Schema) (ast.ArrayType, error) {
var itemsDef ast.Type
var err error

Expand All @@ -254,7 +254,7 @@ func (g *newGenerator) walkList(schema *schemaparser.Schema) (ast.ArrayType, err
}, nil
}

func (g *newGenerator) walkEnum(schema *schemaparser.Schema) (ast.EnumType, error) {
func (g *generator) walkEnum(schema *schemaparser.Schema) (ast.EnumType, error) {
if len(schema.Enum) == 0 {
return ast.EnumType{}, fmt.Errorf("enum with no values")
}
Expand All @@ -277,7 +277,7 @@ func (g *newGenerator) walkEnum(schema *schemaparser.Schema) (ast.EnumType, erro
}, nil
}

func (g *newGenerator) walkObject(schema *schemaparser.Schema) (ast.StructType, error) {
func (g *generator) walkObject(schema *schemaparser.Schema) (ast.StructType, error) {
// TODO: finish implementation
fields := make([]ast.StructField, 0, len(schema.Properties))
for name, property := range schema.Properties {
Expand Down
34 changes: 17 additions & 17 deletions internal/simplecue/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ type Config struct {
Package string
}

type newGenerator struct {
type generator struct {
file *ast.File
}

func GenerateAST(val cue.Value, c Config) (*ast.File, error) {
g := &newGenerator{
g := &generator{
file: &ast.File{
Package: c.Package,
},
Expand All @@ -49,7 +49,7 @@ func GenerateAST(val cue.Value, c Config) (*ast.File, error) {
}

// Do we really need to distinguish top-level types with others?
func (g *newGenerator) declareTopLevelType(name string, v cue.Value) (ast.Object, error) {
func (g *generator) declareTopLevelType(name string, v cue.Value) (ast.Object, error) {
typeHint, err := getTypeHint(v)
if err != nil {
return ast.Object{}, err
Expand Down Expand Up @@ -78,7 +78,7 @@ func (g *newGenerator) declareTopLevelType(name string, v cue.Value) (ast.Object
}
}

func (g *newGenerator) declareTopLevelString(name string, v cue.Value) (ast.Object, error) {
func (g *generator) declareTopLevelString(name string, v cue.Value) (ast.Object, error) {
ik := v.IncompleteKind()
if ik&cue.StringKind != ik {
return ast.Object{}, errorWithCueRef(v, "top-level strings may only be generated from concrete strings")
Expand All @@ -96,7 +96,7 @@ func (g *newGenerator) declareTopLevelString(name string, v cue.Value) (ast.Obje
}, nil
}

func (g *newGenerator) declareEnum(name string, v cue.Value) (ast.Object, error) {
func (g *generator) declareEnum(name string, v cue.Value) (ast.Object, error) {
// Restrict the expression of enums to ints or strings.
allowed := cue.StringKind | cue.IntKind
ik := v.IncompleteKind()
Expand All @@ -118,7 +118,7 @@ func (g *newGenerator) declareEnum(name string, v cue.Value) (ast.Object, error)
}, nil
}

func (g *newGenerator) extractEnumValues(v cue.Value) ([]ast.EnumValue, error) {
func (g *generator) extractEnumValues(v cue.Value) ([]ast.EnumValue, error) {
_, dvals := v.Expr()
a := v.Attribute(annotationName)

Expand Down Expand Up @@ -175,7 +175,7 @@ func (g *newGenerator) extractEnumValues(v cue.Value) ([]ast.EnumValue, error) {
return fields, nil
}

func (g *newGenerator) declareTopLevelStruct(name string, v cue.Value) (ast.Object, error) {
func (g *generator) declareTopLevelStruct(name string, v cue.Value) (ast.Object, error) {
// This check might be too restrictive
if v.IncompleteKind() != cue.StructKind {
return ast.Object{}, errorWithCueRef(v, "top-level type definitions may only be generated from structs")
Expand All @@ -195,7 +195,7 @@ func (g *newGenerator) declareTopLevelStruct(name string, v cue.Value) (ast.Obje
return typeDef, nil
}

func (g *newGenerator) structFields(v cue.Value) ([]ast.StructField, error) {
func (g *generator) structFields(v cue.Value) ([]ast.StructField, error) {
// This check might be too restrictive
if v.IncompleteKind() != cue.StructKind {
return nil, errorWithCueRef(v, "top-level type definitions may only be generated from structs")
Expand Down Expand Up @@ -230,7 +230,7 @@ func (g *newGenerator) structFields(v cue.Value) ([]ast.StructField, error) {
return fields, nil
}

func (g *newGenerator) declareNode(v cue.Value) (ast.Type, error) {
func (g *generator) declareNode(v cue.Value) (ast.Type, error) {
// This node is referring to another definition
_, path := v.ReferencePath()
if path.String() != "" {
Expand Down Expand Up @@ -320,7 +320,7 @@ func (g *newGenerator) declareNode(v cue.Value) (ast.Type, error) {
}
}

func (g *newGenerator) declareAnonymousEnum(v cue.Value) (ast.Type, error) {
func (g *generator) declareAnonymousEnum(v cue.Value) (ast.Type, error) {
allowed := cue.StringKind | cue.IntKind
ik := v.IncompleteKind()
if ik&allowed != ik {
Expand All @@ -337,7 +337,7 @@ func (g *newGenerator) declareAnonymousEnum(v cue.Value) (ast.Type, error) {
}, nil
}

func (g *newGenerator) declareString(v cue.Value) (ast.ScalarType, error) {
func (g *generator) declareString(v cue.Value) (ast.ScalarType, error) {
typeDef := ast.ScalarType{
ScalarKind: ast.KindString,
}
Expand All @@ -363,7 +363,7 @@ func (g *newGenerator) declareString(v cue.Value) (ast.ScalarType, error) {
return typeDef, nil
}

func (g *newGenerator) extractDefault(v cue.Value) (any, error) {
func (g *generator) extractDefault(v cue.Value) (any, error) {
defaultVal, ok := v.Default()
if !ok {
//nolint: nilnil
Expand All @@ -378,7 +378,7 @@ func (g *newGenerator) extractDefault(v cue.Value) (any, error) {
return def, nil
}

func (g *newGenerator) declareStringConstraints(v cue.Value) ([]ast.TypeConstraint, error) {
func (g *generator) declareStringConstraints(v cue.Value) ([]ast.TypeConstraint, error) {
typeAndConstraints := appendSplit(nil, cue.AndOp, v)

// nothing to do
Expand Down Expand Up @@ -441,7 +441,7 @@ func (g *newGenerator) declareStringConstraints(v cue.Value) ([]ast.TypeConstrai
return constraints, nil
}

func (g *newGenerator) declareNumber(v cue.Value) (ast.ScalarType, error) {
func (g *generator) declareNumber(v cue.Value) (ast.ScalarType, error) {
numberTypeWithConstraintsAsString, err := format.Node(v.Syntax())
if err != nil {
return ast.ScalarType{}, err
Expand Down Expand Up @@ -506,7 +506,7 @@ func (g *newGenerator) declareNumber(v cue.Value) (ast.ScalarType, error) {
return typeDef, nil
}

func (g *newGenerator) declareNumberConstraints(v cue.Value) ([]ast.TypeConstraint, error) {
func (g *generator) declareNumberConstraints(v cue.Value) ([]ast.TypeConstraint, error) {
// typeAndConstraints can contain the following cue expressions:
// - number
// - int|float, number, upper bound, lower bound
Expand Down Expand Up @@ -538,7 +538,7 @@ func (g *newGenerator) declareNumberConstraints(v cue.Value) ([]ast.TypeConstrai
return constraints, nil
}

func (g *newGenerator) extractConstraint(v cue.Value) (ast.TypeConstraint, error) {
func (g *generator) extractConstraint(v cue.Value) (ast.TypeConstraint, error) {
toConstraint := func(operator string, arg cue.Value) (ast.TypeConstraint, error) {
scalar, err := cueConcreteToScalar(arg)
if err != nil {
Expand Down Expand Up @@ -567,7 +567,7 @@ func (g *newGenerator) extractConstraint(v cue.Value) (ast.TypeConstraint, error
}
}

func (g *newGenerator) declareList(v cue.Value) (ast.Type, error) {
func (g *generator) declareList(v cue.Value) (ast.Type, error) {
i, err := v.List()
if err != nil {
return nil, err
Expand Down

0 comments on commit 3c93617

Please sign in to comment.