Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 8fd5bd1

Browse files
Merge pull request #2329 from ibuildthecloud/main
Update to new AML version
2 parents f4d08b8 + e10ddfb commit 8fd5bd1

File tree

10 files changed

+130
-75
lines changed

10 files changed

+130
-75
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ replace (
1212
require (
1313
cuelang.org/go v0.6.0
1414
github.com/AlecAivazis/survey/v2 v2.3.6
15-
github.com/acorn-io/aml v0.0.0-20231017143906-5a6098a42249
16-
github.com/acorn-io/aml/cli v0.0.0-20231024210332-d1516a4da692
15+
github.com/acorn-io/aml v0.0.0-20231113235531-f2769ecb3c7f
16+
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2
1717
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e
1818
github.com/acorn-io/baaah v0.0.0-20231009165317-af2b68361b8a
1919
github.com/acorn-io/mink v0.0.0-20230804175412-8d121aae112c

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpH
107107
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
108108
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
109109
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
110-
github.com/acorn-io/aml v0.0.0-20231017143906-5a6098a42249 h1:Mclb6bw5z+L78eRW7qAZKVxDnGrWw9F1iabSaIK9Dtg=
111-
github.com/acorn-io/aml v0.0.0-20231017143906-5a6098a42249/go.mod h1:I1qN++bfN+6sOV/FiACluqj2Eu5YaHOy82cTAnjLMf0=
112-
github.com/acorn-io/aml/cli v0.0.0-20231024210332-d1516a4da692 h1:qnhI1/hbVCDMvVqCgqlVhSIUEPJpa8Fa4PlJPD5P5U4=
113-
github.com/acorn-io/aml/cli v0.0.0-20231024210332-d1516a4da692/go.mod h1:/BeWMP1u6zv/s3x+hWxm+buKqKXnFKuEbqEBGpzcKi4=
110+
github.com/acorn-io/aml v0.0.0-20231113235531-f2769ecb3c7f h1:6hsD027mQIrqSGyWxB09hJa5uDwJsr7tJ4H9rIrIW4U=
111+
github.com/acorn-io/aml v0.0.0-20231113235531-f2769ecb3c7f/go.mod h1:jImvyZGYOReKDJEEjS2SLjvVAEFcXFMA7OPQ96l1JYA=
112+
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2 h1:CtflOEPAvtpALuC3SM1WApsfTuECXcDuq25E3fZaPdg=
113+
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2/go.mod h1:D4tWmJlLdsmMbQ/MI4T+Tj4j2PjgTAdde2QDGkeWH20=
114114
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e h1:W67DG9AcoNvBwIOR9OFUCZlSJBaHuvM2kXQ2+C6EnLk=
115115
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e/go.mod h1:XnJZSZq/tG/jWPE/tmm2zy90gOZrJRIaOyKpoMulxfE=
116116
github.com/acorn-io/baaah v0.0.0-20231009165317-af2b68361b8a h1:0bHfiYUw4ojXCUfGHUPmRewrgJ/EpLQ4BaR4yEy8BC4=

pkg/apis/internal.acorn.io/v1/schema.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ type FieldType struct {
1818
}
1919

2020
type Constraint struct {
21-
Description string `json:"description,omitempty"`
22-
Op string `json:"op,omitempty"`
23-
ID string `json:"id,omitempty"`
24-
Right string `json:"right,omitempty"`
25-
Type *FieldType `json:"type,omitempty"`
21+
Op string `json:"op,omitempty"`
22+
Right string `json:"right,omitempty"`
23+
Type *FieldType `json:"type,omitempty"`
2624
}
2725

2826
type Object struct {

pkg/appdefinition/appdefinition.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111
"path/filepath"
1212
"sort"
1313
"strings"
14+
"sync"
1415
"time"
1516

1617
"github.com/acorn-io/aml"
1718
amllegacy "github.com/acorn-io/aml/legacy"
1819
"github.com/acorn-io/aml/pkg/eval"
19-
"github.com/acorn-io/aml/pkg/schema"
20+
"github.com/acorn-io/aml/pkg/value"
2021
"github.com/acorn-io/baaah/pkg/typed"
2122
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
2223
"sigs.k8s.io/yaml"
@@ -39,13 +40,34 @@ const (
3940

4041
var (
4142
ErrInvalidInput = errors.New("invalid input")
43+
schema value.Schema
44+
schemaOnce sync.Once
4245
)
4346

4447
func init() {
4548
// Disable AML debug printing
4649
eval.DebugEnabled = false
4750
}
4851

52+
func getSchema() value.Schema {
53+
schemaOnce.Do(func() {
54+
f, err := fs.Open(schemaFile)
55+
if err != nil {
56+
// this shouldn't happen, this an embedded FS
57+
panic(err)
58+
}
59+
defer f.Close()
60+
61+
err = aml.NewDecoder(f, aml.DecoderOption{
62+
SchemaSourceName: schemaFile,
63+
}).Decode(&schema)
64+
if err != nil {
65+
panic(err)
66+
}
67+
})
68+
return schema
69+
}
70+
4971
type DataFiles struct {
5072
IconSuffix string
5173
Icon []byte
@@ -200,25 +222,25 @@ func (a *AppDefinition) decodeLegacy(out any) error {
200222
Acornfile: true,
201223
})
202224

203-
if f, ok := out.(*schema.File); ok {
225+
if f, ok := out.(*value.FuncSchema); ok {
204226
args, err := decoder.Args()
205227
if err != nil {
206228
return err
207229
}
208230

209231
for _, profile := range args.Profiles {
210-
f.ProfileNames = append(f.ProfileNames, schema.Name{
232+
f.ProfileNames = append(f.ProfileNames, value.Name{
211233
Name: profile.Name,
212234
Description: profile.Description,
213235
})
214236
}
215237

216238
for _, param := range args.Params {
217-
f.Args.Fields = append(f.Args.Fields, schema.Field{
218-
Name: param.Name,
239+
f.Args = append(f.Args, value.ObjectSchemaField{
240+
Key: param.Name,
219241
Description: param.Description,
220-
Type: schema.FieldType{
221-
Kind: schema.Kind(param.Type),
242+
Schema: &value.TypeSchema{
243+
KindValue: value.Kind(param.Type),
222244
},
223245
})
224246
}
@@ -233,12 +255,6 @@ func (a *AppDefinition) decode(out any) error {
233255
if a.acornfileV0 {
234256
return a.decodeLegacy(out)
235257
}
236-
f, err := fs.Open(schemaFile)
237-
if err != nil {
238-
// this shouldn't happen, this an embedded FS
239-
panic(err)
240-
}
241-
defer f.Close()
242258

243259
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
244260
defer cancel()
@@ -249,7 +265,7 @@ func (a *AppDefinition) decode(out any) error {
249265
Args: a.args,
250266
Profiles: a.profiles,
251267
SchemaSourceName: "acornfile-schema.acorn",
252-
Schema: f,
268+
SchemaValue: getSchema(),
253269
}).Decode(out)
254270
}
255271

pkg/appdefinition/appdefinition_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ for i in std.range(1,args.scale+1) {
15891589
}
15901590

15911591
app = app.WithImageData(*image)
1592-
devApp := app.WithArgs(map[string]any{"scale": 2}, nil)
1592+
devApp := app.WithArgs(map[string]any{"scale": 4}, nil)
15931593

15941594
appSpec, err := devApp.AppSpec()
15951595
if err != nil {
@@ -2583,12 +2583,12 @@ localData: {
25832583

25842584
func TestStdMissing(t *testing.T) {
25852585
data := `
2586-
foo : std.toyaml({})
2586+
localData: foo : std.toyaml({})
25872587
`
25882588

25892589
_, err := NewAppDefinition([]byte(data))
25902590
require.Error(t, err)
2591-
assert.Contains(t, err.Error(), "key not found \"toyaml\": Acornfile:2:11")
2591+
assert.Contains(t, err.Error(), "key not found \"toyaml\": Acornfile:2:22")
25922592
}
25932593

25942594
func TestArgsTopCondition(t *testing.T) {

pkg/appdefinition/params.go

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"encoding/json"
55

66
"github.com/acorn-io/aml/cli/pkg/flagargs"
7-
"github.com/acorn-io/aml/pkg/schema"
7+
"github.com/acorn-io/aml/pkg/value"
88
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
99
)
1010

@@ -19,14 +19,14 @@ var (
1919
}
2020
)
2121

22-
func fromNames(names schema.Names) (result []v1.Profile) {
22+
func fromNames(names value.Names) (result []v1.Profile) {
2323
for _, name := range names {
2424
result = append(result, v1.Profile(name))
2525
}
2626
return
2727
}
2828

29-
func dropHiddenProfiles(names schema.Names) (result schema.Names) {
29+
func dropHiddenProfiles(names value.Names) (result value.Names) {
3030
for _, profile := range names {
3131
if _, ok := hiddenProfiles[profile.Name]; ok {
3232
continue
@@ -36,24 +36,33 @@ func dropHiddenProfiles(names schema.Names) (result schema.Names) {
3636
return
3737
}
3838

39-
func dropHiddenArgs(args []schema.Field) (result []schema.Field) {
39+
func dropHiddenArgs(args []value.ObjectSchemaField) (result []value.ObjectSchemaField) {
4040
for _, arg := range args {
41-
if _, ok := hiddenArgs[arg.Name]; ok {
41+
if _, ok := hiddenArgs[arg.Key]; ok {
4242
continue
4343
}
4444
result = append(result, arg)
4545
}
4646
return
4747
}
4848

49-
func fromFields(in []schema.Field) (result []v1.Field) {
49+
func fromFields(in []value.ObjectSchemaField) (result []v1.Field) {
5050
for _, item := range in {
5151
result = append(result, fromField(item))
5252
}
5353
return
5454
}
5555

5656
func anyToString(v any) string {
57+
var (
58+
ts *value.TypeSchema
59+
)
60+
ts, ok := v.(*value.TypeSchema)
61+
if ok {
62+
v = ts.DefaultValue
63+
} else {
64+
v = nil
65+
}
5766
if v == nil {
5867
return ""
5968
}
@@ -64,36 +73,59 @@ func anyToString(v any) string {
6473
return string(d)
6574
}
6675

67-
func fromObject(in *schema.Object) *v1.Object {
76+
func fromObject(s value.Schema) *v1.Object {
77+
var (
78+
in *value.ObjectSchema
79+
ts *value.TypeSchema
80+
)
81+
ts, ok := s.(*value.TypeSchema)
82+
if ok {
83+
in = ts.Object
84+
}
85+
6886
if in == nil {
6987
return nil
7088
}
7189
return &v1.Object{
72-
Path: in.Path,
73-
Reference: in.Reference,
90+
Path: ts.Path.String(),
91+
Reference: ts.Reference,
7492
Description: in.Description,
7593
Fields: fromFields(in.Fields),
7694
AllowNewKeys: in.AllowNewKeys,
7795
}
7896
}
7997

80-
func fromArray(in *schema.Array) *v1.Array {
98+
func fromArray(s value.Schema) *v1.Array {
99+
var (
100+
in *value.ArraySchema
101+
ts *value.TypeSchema
102+
)
103+
ts, ok := s.(*value.TypeSchema)
104+
if ok {
105+
in = ts.Array
106+
}
81107
if in == nil {
82108
return nil
83109
}
84110
return &v1.Array{
85-
Types: fromFieldTypes(in.Types),
111+
Types: fromFieldTypes(in.Valid),
86112
}
87113
}
88114

89-
func fromConstraints(in []schema.Constraint) (result []v1.Constraint) {
115+
func fromConstraints(s value.Schema) (result []v1.Constraint) {
116+
var (
117+
in []value.Constraint
118+
ts *value.TypeSchema
119+
)
120+
ts, ok := s.(*value.TypeSchema)
121+
if ok {
122+
in = ts.Constraints
123+
}
90124
for _, item := range in {
91125
result = append(result, v1.Constraint{
92-
Description: item.Description,
93-
Op: item.Op,
94-
ID: item.ID,
95-
Right: anyToString(item.Right),
96-
Type: anyToFieldType(item.Right),
126+
Op: item.Op,
127+
Right: anyToString(item.Right),
128+
Type: anyToFieldType(item.Right),
97129
})
98130
}
99131
return
@@ -104,32 +136,44 @@ func anyToFieldType(v any) *v1.FieldType {
104136
return rt
105137
}
106138

107-
func fromFieldTypes(in []schema.FieldType) (out []v1.FieldType) {
139+
func fromAlternates(s value.Schema) (out []v1.FieldType) {
140+
var (
141+
in []value.Schema
142+
ts *value.TypeSchema
143+
)
144+
ts, ok := s.(*value.TypeSchema)
145+
if ok {
146+
in = ts.Alternates
147+
}
148+
return fromFieldTypes(in)
149+
}
150+
151+
func fromFieldTypes(in []value.Schema) (out []v1.FieldType) {
108152
for _, fieldType := range in {
109-
out = append(out, *fromFieldType(&fieldType))
153+
out = append(out, *fromFieldType(fieldType))
110154
}
111155
return
112156
}
113157

114-
func fromFieldType(in *schema.FieldType) *v1.FieldType {
158+
func fromFieldType(in value.Schema) *v1.FieldType {
115159
if in == nil {
116160
return nil
117161
}
118162
return &v1.FieldType{
119-
Kind: string(in.Kind),
120-
Object: fromObject(in.Object),
121-
Array: fromArray(in.Array),
122-
Constraints: fromConstraints(in.Contstraints),
123-
Default: anyToString(in.Default),
124-
Alternates: fromFieldTypes(in.Alternates),
163+
Kind: string(in.TargetKind()),
164+
Object: fromObject(in),
165+
Array: fromArray(in),
166+
Constraints: fromConstraints(in),
167+
Default: anyToString(in),
168+
Alternates: fromAlternates(in),
125169
}
126170
}
127171

128-
func fromField(in schema.Field) v1.Field {
172+
func fromField(in value.ObjectSchemaField) v1.Field {
129173
return v1.Field{
130-
Name: in.Name,
174+
Name: in.Key,
131175
Description: in.Description,
132-
Type: *fromFieldType(&in.Type),
176+
Type: *fromFieldType(in.Schema),
133177
Match: in.Match,
134178
Optional: in.Optional,
135179
}
@@ -140,27 +184,27 @@ type Flags interface {
140184
}
141185

142186
func (a *AppDefinition) ToFlags(programName, argsFile string, usage func()) (Flags, error) {
143-
var file schema.File
187+
var file value.FuncSchema
144188
err := a.decode(&file)
145189
if err != nil {
146190
return nil, err
147191
}
148192

149193
args := flagargs.New(argsFile, programName,
150194
dropHiddenProfiles(file.ProfileNames),
151-
dropHiddenArgs(file.Args.Fields))
195+
dropHiddenArgs(file.Args))
152196
args.Usage = usage
153197
return args, nil
154198
}
155199

156200
func (a *AppDefinition) ToParamSpec() (*v1.ParamSpec, error) {
157-
var file schema.File
201+
var file value.FuncSchema
158202
err := a.decode(&file)
159203
if err != nil {
160204
return nil, err
161205
}
162206
result := &v1.ParamSpec{
163-
Args: fromFields(dropHiddenArgs(file.Args.Fields)),
207+
Args: fromFields(dropHiddenArgs(file.Args)),
164208
Profiles: fromNames(dropHiddenProfiles(file.ProfileNames)),
165209
}
166210

0 commit comments

Comments
 (0)