Skip to content

Commit

Permalink
Merge pull request #94 from grafana/allow-null-types
Browse files Browse the repository at this point in the history
Fix null types generation
  • Loading branch information
sam boyer authored Apr 4, 2023
2 parents 7d314ea + d443b17 commit 0bfb025
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 0 additions & 1 deletion generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestGenerateWithImports(t *testing.T) {
"imports/oneref_verbose": "Figure out how to disambiguate struct literals from the struct-with-braces-and-one-element case",
"imports/struct_shorthand": "Shorthand struct notation is currently unsupported, needs fixing",
"imports/inline_comments": "Inline comments do not appear be retrievable from cue.Value.Doc()",
"imports/nulltype": "null types are not handled correctly",
},
}

Expand Down
10 changes: 9 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,13 @@ func (g generator) tsprintField(v cue.Value, isType bool) (ts.Expr, error) {
return nil, valError(v, "bounds constraints are not supported as they lack a direct typescript equivalent")
}
fallthrough
case cue.FloatKind, cue.IntKind, cue.BoolKind, cue.NullKind, cue.StructKind:
case cue.NullKind:
// It evaluates single null value
if op == cue.NoOp && len(dvals) == 0 {
return tsprintType(cue.NullKind), nil
}
fallthrough
case cue.FloatKind, cue.IntKind, cue.BoolKind, cue.StructKind:
// Having eliminated the possibility of bounds/constraints, we're left
// with disjunctions and basic types.
switch op {
Expand Down Expand Up @@ -1273,6 +1279,8 @@ func tsprintType(k cue.Kind) ts.Expr {
return ts.Ident("number")
case cue.TopKind:
return ts.Ident("unknown")
case cue.NullKind:
return ts.Ident("null")
default:
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions testdata/imports/nulltype.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ obj: {

export type nullType = null;

export type nullInUnion = string | null;
export type nullInUnion = (string | null);

export type nullDefault = "foo" | "bar" | null;
export type nullDefault = ('foo' | 'bar' | null);

export const defaultnullDefault: nullDefault = null;

export interface obj {
nullField: null
}
nullField: null;
}

0 comments on commit 0bfb025

Please sign in to comment.