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

Commit 253499f

Browse files
authored
Fix shorthand structs (#98)
* Iterate struct to find missing attributes * Remove print
1 parent 7f2d44d commit 253499f

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

analyze.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"cuelang.org/go/cue"
8+
"cuelang.org/go/cue/ast"
89
"cuelang.org/go/cue/format"
910
)
1011

@@ -31,7 +32,11 @@ func getKindFor(v cue.Value) (TSType, error) {
3132
attr = a
3233
}
3334
}
35+
3436
if !found {
37+
if t, ok := getKindForField(v); ok {
38+
return t, nil
39+
}
3540
return "", valError(v, "value has no \"@%s\" attribute", attrname)
3641
}
3742

@@ -46,6 +51,42 @@ func getKindFor(v cue.Value) (TSType, error) {
4651
return TSType(tt), nil
4752
}
4853

54+
func getKindForField(v cue.Value) (TSType, bool) {
55+
if field, ok := v.Source().(*ast.Field); ok {
56+
if s, ok := field.Value.(*ast.StructLit); ok {
57+
return iterateField(s)
58+
}
59+
}
60+
return "", false
61+
}
62+
63+
func iterateField(s *ast.StructLit) (TSType, bool) {
64+
for _, el := range s.Elts {
65+
if field, ok := el.(*ast.Field); ok {
66+
if len(field.Attrs) > 0 {
67+
return parseStringAttribute(field.Attrs[0].Text), true
68+
}
69+
if s, ok := field.Value.(*ast.StructLit); ok {
70+
return iterateField(s)
71+
}
72+
}
73+
}
74+
return "", false
75+
}
76+
77+
func parseStringAttribute(attr string) TSType {
78+
switch attr {
79+
case "@cuetsy(kind=\"interface\")":
80+
return TypeInterface
81+
case "@cuetsy(kind=\"enum\")":
82+
return TypeEnum
83+
case "@cuetsy(kind=\"type\")":
84+
return TypeAlias
85+
default:
86+
return ""
87+
}
88+
}
89+
4990
func targetsKind(v cue.Value, kinds ...TSType) bool {
5091
vkind, err := getKindFor(v)
5192
if err != nil {

generate_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ func TestGenerateWithImports(t *testing.T) {
4444
Name: "gen",
4545
Update: *updateGolden,
4646
ToDo: map[string]string{
47-
"imports/oneref_verbose": "Figure out how to disambiguate struct literals from the struct-with-braces-and-one-element case",
48-
"imports/struct_shorthand": "Shorthand struct notation is currently unsupported, needs fixing",
47+
"imports/oneref_verbose": "Figure out how to disambiguate struct literals from the struct-with-braces-and-one-element case",
4948
},
5049
}
5150

testdata/imports/struct_shorthand.txtar

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ TwoLevel: NestedDecl: {
88
InnerTwo: number
99
} @cuetsy(kind="interface")
1010

11+
ThreeLevel: TwoLevel: NestedDecl: {
12+
InnerOne: number
13+
InnerTwo: number
14+
} @cuetsy(kind="interface")
15+
1116
-- out/gen --
1217

1318
export interface TwoLevel {
@@ -16,3 +21,12 @@ export interface TwoLevel {
1621
InnerTwo: number;
1722
};
1823
}
24+
25+
export interface ThreeLevel {
26+
TwoLevel: {
27+
NestedDecl: {
28+
InnerOne: number;
29+
InnerTwo: number;
30+
};
31+
};
32+
}

0 commit comments

Comments
 (0)