Skip to content

Commit

Permalink
It's working! Just slop it all in
Browse files Browse the repository at this point in the history
Really, really, truly, it's cleanup time, Coming Soon™
  • Loading branch information
sam boyer committed May 5, 2022
1 parent d5fada1 commit e218883
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 163 deletions.
3 changes: 1 addition & 2 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestGenerateWithImports(t *testing.T) {
Name: "gen",
Update: *updateGolden,
ToDo: map[string]string{
"imports/empty_default": "Various fixes needed to emit empty types and defaults correctly",
"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",
},
Expand Down Expand Up @@ -115,7 +114,7 @@ func loadCases(dir string) ([]Case, error) {
}

if len(a.Files) != 2 {
return nil, fmt.Errorf("Malformed test case '%s': Must contain exactly two files (CUE and TS/ERR), but has %d", file, len(a.Files))
return nil, fmt.Errorf("malformed test case '%s': Must contain exactly two files (CUE and TS/ERR), but has %d", file, len(a.Files))
}
name := strings.TrimSuffix(fi.Name(), ".txtar")
if strings.HasSuffix(name, "error") {
Expand Down
90 changes: 51 additions & 39 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,30 +798,30 @@ type typeRef struct {
func tsPrintDefault(v cue.Value) (bool, ts.Expr, error) {
d, ok := v.Default()
// [...number] results in [], which is a fake default, we need to correct it here.
if ok && d.Kind() == cue.ListKind {
len, err := d.Len().Int64()
if err != nil {
return false, nil, err
}
var defaultExist bool
if len <= 0 {
op, vals := v.Expr()
if op == cue.OrOp {
for _, val := range vals {
vallen, _ := d.Len().Int64()
if val.Kind() == cue.ListKind && vallen <= 0 {
defaultExist = true
break
}
}
if !defaultExist {
ok = false
}
} else {
ok = false
}
}
}
// if ok && d.Kind() == cue.ListKind {
// len, err := d.Len().Int64()
// if err != nil {
// return false, nil, err
// }
// var defaultExist bool
// if len <= 0 {
// op, vals := v.Expr()
// if op == cue.OrOp {
// for _, val := range vals {
// vallen, _ := d.Len().Int64()
// if val.Kind() == cue.ListKind && vallen <= 0 {
// defaultExist = true
// break
// }
// }
// if !defaultExist {
// ok = false
// }
// } else {
// ok = false
// }
// }
// }

if ok {
expr, err := tsprintField(d)
Expand Down Expand Up @@ -913,7 +913,6 @@ func tsprintField(v cue.Value) (ts.Expr, error) {
//
// For closed lists, we simply iterate over its component elements and
// print their typescript representation.

iter, _ := v.List()
var elems []ts.Expr
for iter.Next() {
Expand Down Expand Up @@ -952,27 +951,40 @@ func tsprintField(v cue.Value) (ts.Expr, error) {
// This list is open - its final element is ...<value> - and we can only
// meaningfully convert open lists to typescript if there are zero other
// elements.

// First, peel off a simple default, if one exists.
// dlist, has := v.Default()
// if has && op == cue.OrOp {
// di := analyzeList(dlist)
// if len(dvals) != 2 {
// panic(fmt.Sprintf("%v branches on list disjunct, can only handle 2", len(dvals)))
// }
// if di.eq(analyzeList(dvals[1])) {
// v = dvals[0]
// } else if di.eq(analyzeList(dvals[0])) {
// v = dvals[1]
// } else {
// panic("wat - list kind had default but analysis did not match for either disjunct branch")
// }
// }

// If the default (all lists have a default, usually self, ugh) differs from the
// input list, peel it off. Otherwise our AnyIndex lookup may end up getting
// sent on the wrong path.
defv, _ := v.Default()
if !defv.Equals(v) {
v = dvals[0]
}

e := v.LookupPath(cue.MakePath(cue.AnyIndex))
has := e.Exists()
if has {
if e.Exists() {
expr, err := tsprintField(e)
if err != nil {
return nil, err
}
return tsast.ListExpr{Expr: expr}, nil
} else {
// When it is a concrete list.
iter, _ := v.List()
if iter.Next() {
expr := tsprintType(iter.Value().Kind())
if expr == nil {
label, _ := v.Label()
return nil, valError(v, "can't convert list element of %v to typescript", label)
}
return tsast.ListExpr{Expr: expr}, nil
}

panic("💩")
panic("unreachable - open list must have a type")
}
case cue.NumberKind, cue.StringKind:
// It appears there are only three cases in which we can have an
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/grafana/cuetsy
go 1.16

require (
cuelang.org/go v0.4.2
cuelang.org/go v0.4.3
github.com/google/go-cmp v0.5.5
github.com/matryer/is v1.4.0
github.com/rogpeppe/go-internal v1.8.0
github.com/xlab/treeprint v1.1.0 // indirect
github.com/rogpeppe/go-internal v1.8.1
github.com/stretchr/testify v1.7.1 // indirect
github.com/xlab/treeprint v1.1.0
golang.org/x/tools v0.1.10
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
gotest.tools v2.2.0+incompatible
Expand Down
Loading

0 comments on commit e218883

Please sign in to comment.