Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit a320b05

Browse files
author
sam boyer
authored
Merge pull request #67 from grafana/allow-multiple-types-string-map
Allow multiple types string map
2 parents 05aae59 + 4c8848f commit a320b05

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

generator.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,20 @@ func tsprintField(v cue.Value, isType bool) (ts.Expr, error) {
911911
case cue.StructKind:
912912
switch op {
913913
case cue.SelectorOp, cue.AndOp, cue.NoOp:
914+
// Checks [string]something and {...}
914915
val := v.LookupPath(cue.MakePath(cue.AnyString))
915916
if val.Exists() {
917+
expr, err := tsprintField(val, isType)
918+
if err != nil {
919+
return nil, valError(v, err.Error())
920+
}
916921
kvs := []tsast.KeyValueExpr{
917922
{
918923
Key: ts.Ident("string"),
919-
Value: tsprintType(val.IncompleteKind()),
924+
Value: expr,
920925
CommentList: commentsFor(val.Value(), true),
921926
},
922927
}
923-
924928
return tsast.ObjectLit{Elems: kvs, IsType: isType, IsMap: true}, nil
925929
}
926930

@@ -1118,7 +1122,7 @@ func tsprintType(k cue.Kind) ts.Expr {
11181122
case cue.NumberKind, cue.FloatKind, cue.IntKind:
11191123
return ts.Ident("number")
11201124
case cue.TopKind:
1121-
return ts.Ident("any")
1125+
return ts.Ident("unknown")
11221126
default:
11231127
return nil
11241128
}

testdata/interfaces.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export enum E2 {
5555
export interface I1 {
5656
I1_FloatLiteral: 4.4;
5757
I1_OptionalDisjunctionLiteral?: ('other' | 'values' | 2);
58-
I1_Top: any;
58+
I1_Top: unknown;
5959
}
6060

6161
export interface I2 {

testdata/map_to_type.txtar

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
-- cue --
22
package cuetsy
33

4+
#StructTest: {
5+
a: string
6+
}
7+
48
Map: {
59
boolTest: [string]: bool
610
numberTest: [string]: int64
711
stringTest: [string]: string
12+
emptyStructTest: {...}
13+
listTest: [string]: [...string]
14+
listWithStructTest: [string]: [...#StructTest]
15+
mapTest: [string]: [string]: string
16+
structTest: [string]: #StructTest
17+
optionalTest?: [string]: string
818
} @cuetsy(kind="interface")
919

20+
21+
1022
-- ts --
1123

1224
export interface Map {
1325
boolTest: Record<string, boolean>;
26+
emptyStructTest: Record<string, unknown>;
27+
listTest: Record<string, Array<string>>;
28+
listWithStructTest: Record<string, Array<{
29+
a: string,
30+
}>>;
31+
mapTest: Record<string, Record<string, string>>;
1432
numberTest: Record<string, number>;
33+
optionalTest?: Record<string, string>;
1534
stringTest: Record<string, string>;
35+
structTest: Record<string, {
36+
a: string,
37+
}>;
1638
}

0 commit comments

Comments
 (0)