Skip to content

Commit

Permalink
Skip TopKind in maps (#70)
Browse files Browse the repository at this point in the history
* Skip TopKind in maps

* Use default any for TopKind

* Remove maturity attribute
  • Loading branch information
spinillos authored Dec 23, 2022
1 parent 9df4a17 commit 77a030e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
7 changes: 4 additions & 3 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,10 @@ func tsprintField(v cue.Value, isType bool) (ts.Expr, error) {
case cue.StructKind:
switch op {
case cue.SelectorOp, cue.AndOp, cue.NoOp:
// Checks [string]something and {...}
// Checks [string]something only.
// It skips structs like {...} (cue.TopKind) to avoid undesired results.
val := v.LookupPath(cue.MakePath(cue.AnyString))
if val.Exists() {
if val.Exists() && val.IncompleteKind() != cue.TopKind {
expr, err := tsprintField(val, isType)
if err != nil {
return nil, valError(v, err.Error())
Expand Down Expand Up @@ -1122,7 +1123,7 @@ func tsprintType(k cue.Kind) ts.Expr {
case cue.NumberKind, cue.FloatKind, cue.IntKind:
return ts.Ident("number")
case cue.TopKind:
return ts.Ident("unknown")
return ts.Ident("any")
default:
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions testdata/array_with_structs.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- cue --
package cuetsy

List: {
test?: [...(#StructTest | #DefinedStructTest)]

#StructTest: {
a: string
}

#DefinedStructTest: {
type: "hola"
...
}
} @cuetsy(kind="interface")

-- ts --

export interface List {
test?: Array<({
a: string;
} | {
type: 'hola';
})>;
}

export const defaultList: Partial<List> = {
test: [],
};
2 changes: 1 addition & 1 deletion testdata/interfaces.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export enum E2 {
export interface I1 {
I1_FloatLiteral: 4.4;
I1_OptionalDisjunctionLiteral?: ('other' | 'values' | 2);
I1_Top: unknown;
I1_Top: any;
}

export interface I2 {
Expand Down
11 changes: 11 additions & 0 deletions testdata/map_to_type.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ package cuetsy
a: string
}

#StructWithDots: {
type: "hola"
...
}

Map: {
boolTest: [string]: bool
numberTest: [string]: int64
Expand All @@ -15,6 +20,8 @@ Map: {
mapTest: [string]: [string]: string
structTest: [string]: #StructTest
optionalTest?: [string]: string
emptyStructMapTest: [string]: {...}
structWithDotsTest: [string]: #StructWithDots
} @cuetsy(kind="interface")


Expand All @@ -23,6 +30,7 @@ Map: {

export interface Map {
boolTest: Record<string, boolean>;
emptyStructMapTest: Record<string, Record<string, unknown>>;
emptyStructTest: Record<string, unknown>;
listTest: Record<string, Array<string>>;
listWithStructTest: Record<string, Array<{
Expand All @@ -34,5 +42,8 @@ export interface Map {
stringTest: Record<string, string>;
structTest: Record<string, {
a: string,
}>;
structWithDotsTest: Record<string, {
type: 'hola',
}>;
}

0 comments on commit 77a030e

Please sign in to comment.