Skip to content

Commit

Permalink
Little cosmetic rearrangement
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Sep 17, 2023
1 parent f62cf3c commit 808744c
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions internal/ast/compiler/disjunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,46 +132,50 @@ func (pass *DisjunctionToType) processDisjunction(file *ast.File, def ast.Disjun
// add it to preprocessor.types, and use it instead.
newTypeName := pass.disjunctionTypeName(def)

if _, ok := pass.newObjects[newTypeName]; !ok {
/*
TODO: return an error here. Some jennies won't be able to handle
this type of disjunction.
if !def.Branches.HasOnlyScalarOrArray() || !def.Branches.HasOnlyRefs() {
}
*/

fields := make([]ast.StructField, 0, len(def.Branches))
for _, branch := range def.Branches {
// FIXME: should ignore this completely.
// ie: if there was a nullable branch, the whole resulting type should be nullable.
if branch.IsNull() {
continue
}
// if we already generated a new object for this disjunction, let's return
// a reference to it.
if _, ok := pass.newObjects[newTypeName]; ok {
return ast.NewRef(newTypeName), nil
}

fields = append(fields, ast.StructField{
Name: "Val" + tools.UpperCamelCase(pass.typeName(branch)),
Type: branch,
Required: false,
})
/*
TODO: return an error here. Some jennies won't be able to handle
this type of disjunction.
if !def.Branches.HasOnlyScalarOrArray() || !def.Branches.HasOnlyRefs() {
}
*/

structType := ast.NewStruct(fields)
if def.Branches.HasOnlyScalarOrArray() {
structType.Struct.Hint[ast.HintDisjunctionOfScalars] = def
fields := make([]ast.StructField, 0, len(def.Branches))
for _, branch := range def.Branches {
// FIXME: should ignore this completely.
// ie: if there was a nullable branch, the whole resulting type should be nullable.
if branch.IsNull() {
continue
}
if def.Branches.HasOnlyRefs() {
newDisjunctionDef, err := pass.ensureDiscriminator(file, def)
if err != nil {
return ast.Type{}, err
}

structType.Struct.Hint[ast.HintDiscriminatedDisjunctionOfStructs] = newDisjunctionDef
}
fields = append(fields, ast.StructField{
Name: "Val" + tools.UpperCamelCase(pass.typeName(branch)),
Type: branch,
Required: false,
})
}

pass.newObjects[newTypeName] = ast.Object{
Name: newTypeName,
Type: structType,
structType := ast.NewStruct(fields)
if def.Branches.HasOnlyScalarOrArray() {
structType.Struct.Hint[ast.HintDisjunctionOfScalars] = def
}
if def.Branches.HasOnlyRefs() {
newDisjunctionDef, err := pass.ensureDiscriminator(file, def)
if err != nil {
return ast.Type{}, err
}

structType.Struct.Hint[ast.HintDiscriminatedDisjunctionOfStructs] = newDisjunctionDef
}

pass.newObjects[newTypeName] = ast.Object{
Name: newTypeName,
Type: structType,
}

return ast.NewRef(newTypeName), nil
Expand Down

0 comments on commit 808744c

Please sign in to comment.