@@ -506,10 +506,11 @@ func (g *generator) genInterface(name string, v cue.Value) []ts.Decl {
506
506
}
507
507
508
508
sub := nolit .LookupPath (cue .MakePath (sel ))
509
- // Theoretically, lattice equality can be defined as bijective
510
- // subsumption. In practice, Subsume() seems to ignore optional
511
- // fields, and Equals() doesn't. So, use Equals().
512
- if sub .Exists () && sub .Equals (iter .Value ()) {
509
+ op , _ := iter .Value ().Expr ()
510
+ // Also we need to check if the sub operator to discard the one that have validators and if it has a default
511
+ subOp , _ := sub .Expr ()
512
+ _ , def := iter .Value ().Default ()
513
+ if sub .Exists () && sub .Equals (iter .Value ()) && (subOp == cue .AndOp || op != cue .AndOp || ! def ) {
513
514
continue
514
515
}
515
516
}
@@ -656,52 +657,14 @@ func (g *generator) genInterfaceField(v cue.Value) (*typeRef, error) {
656
657
return g .genEnumReference (v )
657
658
}
658
659
659
- // One path for when there's a ref to a cuetsy node, and a separate one otherwise
660
- if ! containsCuetsyReference (v ) {
661
- tref .T , err = tsprintField (v , true )
662
- if err != nil {
660
+ tref .T , err = tsprintField (v , true )
661
+ if err != nil {
662
+ if ! containsCuetsyReference (v ) {
663
663
g .addErr (valError (v , "could not generate field: %w" , err ))
664
664
return nil , err
665
665
}
666
- } else {
667
- expr , err := tsprintField (v , true )
668
- if err != nil {
669
- g .addErr (err )
670
- return nil , nil
671
- }
672
- tref .T = expr
673
-
674
- // Deconstruct the field's expressions.
675
- // conjuncts := appendSplit(nil, cue.AndOp, v)
676
-
677
- // var expr ts.Expr
678
- //
679
- // for _, cv := range conjuncts {
680
- // disjuncts := appendSplit(nil, cue.OrOp, cv)
681
- // for i, dv := range disjuncts {
682
- // if _, r := dv.Reference(); len(r) == 0 {
683
- // disjuncts[i] = dv.Eval()
684
- // }
685
- // }
686
- // switch len(disjuncts) {
687
- // case 0:
688
- // // conjunct eliminated - need more preprocessing to actually make this possible
689
- // panic("TODO, unreachable")
690
- // case 1:
691
- // err := disjuncts[0].Err()
692
- // if err != nil {
693
- // g.addErr(valError(v, "invalid value"))
694
- // return nil
695
- // }
696
- // expr, err = tsprintField(disjuncts[0])
697
- // if err != nil {
698
- // g.addErr(valError(v, "invalid value"))
699
- // return nil
700
- // }
701
- // default:
702
- // // TODO create disjunction handler
703
- // }
704
- // }
666
+ g .addErr (err )
667
+ return nil , nil
705
668
}
706
669
707
670
exists , defExpr , err := tsPrintDefault (v )
@@ -859,6 +822,7 @@ func tsPrintDefault(v cue.Value) (bool, ts.Expr, error) {
859
822
// }
860
823
861
824
if ok {
825
+ d = getTypeDefaultOverride (d )
862
826
expr , err := tsprintField (d , false )
863
827
if err != nil {
864
828
return false , nil , err
@@ -879,9 +843,22 @@ func tsPrintDefault(v cue.Value) (bool, ts.Expr, error) {
879
843
880
844
return true , expr , nil
881
845
}
846
+
882
847
return false , nil , nil
883
848
}
884
849
850
+ func getTypeDefaultOverride (v cue.Value ) cue.Value {
851
+ op , expr := v .Expr ()
852
+ if op != cue .AndOp || len (expr ) < 2 {
853
+ return v
854
+ }
855
+
856
+ if def , ok := expr [1 ].Default (); ok {
857
+ return def
858
+ }
859
+ return v
860
+ }
861
+
885
862
// Render a string containing a Typescript semantic equivalent to the provided
886
863
// Value for placement in a single field, if possible.
887
864
func tsprintField (v cue.Value , isType bool ) (ts.Expr , error ) {
@@ -1198,7 +1175,7 @@ func refAsInterface(v cue.Value) (ts.Expr, error) {
1198
1175
// attribute. The variadic parameter determines which kinds will be treated as
1199
1176
// permissible. By default, all kinds are permitted.
1200
1177
//
1201
- // An nil expr indicates a reference is not allowable, including the case
1178
+ // A nil expr indicates a reference is not allowable, including the case
1202
1179
// that the provided Value is not actually a reference. A non-nil error
1203
1180
// indicates a deeper problem.
1204
1181
func referenceValueAs (v cue.Value , kinds ... TSType ) (ts.Expr , error ) {
@@ -1269,6 +1246,8 @@ func referenceValueAs(v cue.Value, kinds ...TSType) (ts.Expr, error) {
1269
1246
Sel : ts .Ident (dstr ),
1270
1247
}, nil
1271
1248
}
1249
+
1250
+ return ts .Ident (dstr ), nil
1272
1251
default :
1273
1252
return nil , valError (v , "unknown selector subject type %T, cannot translate path %s" , dvals [0 ].Source (), v .Path ().String ())
1274
1253
}
0 commit comments