/
convert.go
69 lines (59 loc) · 1.74 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cue
import (
"fmt"
"os"
"cuelang.org/go/cue"
)
type Parameter struct {
Name string `json:"name"`
Short string `json:"short,omitempty"`
Required bool `json:"required,omitempty"`
Default interface{} `json:"default,omitempty"`
Usage string `json:"usage,omitempty"`
Ignore bool `json:"ignore,omitempty"`
Type cue.Kind `json:"type,omitempty"`
Alias string `json:"alias,omitempty"`
JSONType string `json:"jsonType,omitempty"`
}
func GetParameter() {
content, err := os.ReadFile("D:\\code\\go\\src\\kosmos\\self\\kompass\\example\\pod.cue")
if err != nil {
fmt.Errorf("the cue script is invalid:%w", err)
}
value, err := ParseToTemplateValue(content)
if err != nil {
fmt.Errorf("the cue script is invalid:%w", err)
}
templateValue, err := value.LookupValue("template")
if err != nil {
fmt.Errorf("the cue script is invalid:%w", err)
}
paramVal, err := templateValue.LookupValue("parameter")
if err != nil || !paramVal.CueValue().Exists() {
fmt.Errorf("the cue script is invalid:%w", err)
}
iter, err := paramVal.CueValue().Fields(cue.Definitions(true), cue.Hidden(true), cue.All())
if err != nil {
fmt.Errorf("the cue script is invalid:%w", err)
}
// parse each fields in the parameter fields
var params []Parameter
for iter.Next() {
if iter.Selector().IsDefinition() {
continue
}
var param = Parameter{
Name: iter.Label(),
Required: !iter.IsOptional(),
}
val := iter.Value()
param.Type = val.IncompleteKind()
if def, ok := val.Default(); ok && def.IsConcrete() {
param.Required = false
param.Type = def.Kind()
}
params = append(params, param)
}
fmt.Println("【TemplateValue】:", templateValue)
fmt.Println("【Result】:", "")
}