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

Commit

Permalink
Merge pull request #2329 from ibuildthecloud/main
Browse files Browse the repository at this point in the history
Update to new AML version
  • Loading branch information
ibuildthecloud committed Nov 14, 2023
2 parents f4d08b8 + e10ddfb commit 8fd5bd1
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 75 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ replace (
require (
cuelang.org/go v0.6.0
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/acorn-io/aml v0.0.0-20231017143906-5a6098a42249
github.com/acorn-io/aml/cli v0.0.0-20231024210332-d1516a4da692
github.com/acorn-io/aml v0.0.0-20231113235531-f2769ecb3c7f
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e
github.com/acorn-io/baaah v0.0.0-20231009165317-af2b68361b8a
github.com/acorn-io/mink v0.0.0-20230804175412-8d121aae112c
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpH
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/acorn-io/aml v0.0.0-20231017143906-5a6098a42249 h1:Mclb6bw5z+L78eRW7qAZKVxDnGrWw9F1iabSaIK9Dtg=
github.com/acorn-io/aml v0.0.0-20231017143906-5a6098a42249/go.mod h1:I1qN++bfN+6sOV/FiACluqj2Eu5YaHOy82cTAnjLMf0=
github.com/acorn-io/aml/cli v0.0.0-20231024210332-d1516a4da692 h1:qnhI1/hbVCDMvVqCgqlVhSIUEPJpa8Fa4PlJPD5P5U4=
github.com/acorn-io/aml/cli v0.0.0-20231024210332-d1516a4da692/go.mod h1:/BeWMP1u6zv/s3x+hWxm+buKqKXnFKuEbqEBGpzcKi4=
github.com/acorn-io/aml v0.0.0-20231113235531-f2769ecb3c7f h1:6hsD027mQIrqSGyWxB09hJa5uDwJsr7tJ4H9rIrIW4U=
github.com/acorn-io/aml v0.0.0-20231113235531-f2769ecb3c7f/go.mod h1:jImvyZGYOReKDJEEjS2SLjvVAEFcXFMA7OPQ96l1JYA=
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2 h1:CtflOEPAvtpALuC3SM1WApsfTuECXcDuq25E3fZaPdg=
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2/go.mod h1:D4tWmJlLdsmMbQ/MI4T+Tj4j2PjgTAdde2QDGkeWH20=
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e h1:W67DG9AcoNvBwIOR9OFUCZlSJBaHuvM2kXQ2+C6EnLk=
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e/go.mod h1:XnJZSZq/tG/jWPE/tmm2zy90gOZrJRIaOyKpoMulxfE=
github.com/acorn-io/baaah v0.0.0-20231009165317-af2b68361b8a h1:0bHfiYUw4ojXCUfGHUPmRewrgJ/EpLQ4BaR4yEy8BC4=
Expand Down
8 changes: 3 additions & 5 deletions pkg/apis/internal.acorn.io/v1/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ type FieldType struct {
}

type Constraint struct {
Description string `json:"description,omitempty"`
Op string `json:"op,omitempty"`
ID string `json:"id,omitempty"`
Right string `json:"right,omitempty"`
Type *FieldType `json:"type,omitempty"`
Op string `json:"op,omitempty"`
Right string `json:"right,omitempty"`
Type *FieldType `json:"type,omitempty"`
}

type Object struct {
Expand Down
File renamed without changes.
44 changes: 30 additions & 14 deletions pkg/appdefinition/appdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"path/filepath"
"sort"
"strings"
"sync"
"time"

"github.com/acorn-io/aml"
amllegacy "github.com/acorn-io/aml/legacy"
"github.com/acorn-io/aml/pkg/eval"
"github.com/acorn-io/aml/pkg/schema"
"github.com/acorn-io/aml/pkg/value"
"github.com/acorn-io/baaah/pkg/typed"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"sigs.k8s.io/yaml"
Expand All @@ -39,13 +40,34 @@ const (

var (
ErrInvalidInput = errors.New("invalid input")
schema value.Schema
schemaOnce sync.Once
)

func init() {
// Disable AML debug printing
eval.DebugEnabled = false
}

func getSchema() value.Schema {
schemaOnce.Do(func() {
f, err := fs.Open(schemaFile)
if err != nil {
// this shouldn't happen, this an embedded FS
panic(err)
}
defer f.Close()

err = aml.NewDecoder(f, aml.DecoderOption{
SchemaSourceName: schemaFile,
}).Decode(&schema)
if err != nil {
panic(err)
}
})
return schema
}

type DataFiles struct {
IconSuffix string
Icon []byte
Expand Down Expand Up @@ -200,25 +222,25 @@ func (a *AppDefinition) decodeLegacy(out any) error {
Acornfile: true,
})

if f, ok := out.(*schema.File); ok {
if f, ok := out.(*value.FuncSchema); ok {
args, err := decoder.Args()
if err != nil {
return err
}

for _, profile := range args.Profiles {
f.ProfileNames = append(f.ProfileNames, schema.Name{
f.ProfileNames = append(f.ProfileNames, value.Name{
Name: profile.Name,
Description: profile.Description,
})
}

for _, param := range args.Params {
f.Args.Fields = append(f.Args.Fields, schema.Field{
Name: param.Name,
f.Args = append(f.Args, value.ObjectSchemaField{
Key: param.Name,
Description: param.Description,
Type: schema.FieldType{
Kind: schema.Kind(param.Type),
Schema: &value.TypeSchema{
KindValue: value.Kind(param.Type),
},
})
}
Expand All @@ -233,12 +255,6 @@ func (a *AppDefinition) decode(out any) error {
if a.acornfileV0 {
return a.decodeLegacy(out)
}
f, err := fs.Open(schemaFile)
if err != nil {
// this shouldn't happen, this an embedded FS
panic(err)
}
defer f.Close()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand All @@ -249,7 +265,7 @@ func (a *AppDefinition) decode(out any) error {
Args: a.args,
Profiles: a.profiles,
SchemaSourceName: "acornfile-schema.acorn",
Schema: f,
SchemaValue: getSchema(),
}).Decode(out)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/appdefinition/appdefinition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ for i in std.range(1,args.scale+1) {
}

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

appSpec, err := devApp.AppSpec()
if err != nil {
Expand Down Expand Up @@ -2583,12 +2583,12 @@ localData: {

func TestStdMissing(t *testing.T) {
data := `
foo : std.toyaml({})
localData: foo : std.toyaml({})
`

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

func TestArgsTopCondition(t *testing.T) {
Expand Down
110 changes: 77 additions & 33 deletions pkg/appdefinition/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"

"github.com/acorn-io/aml/cli/pkg/flagargs"
"github.com/acorn-io/aml/pkg/schema"
"github.com/acorn-io/aml/pkg/value"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
)

Expand All @@ -19,14 +19,14 @@ var (
}
)

func fromNames(names schema.Names) (result []v1.Profile) {
func fromNames(names value.Names) (result []v1.Profile) {
for _, name := range names {
result = append(result, v1.Profile(name))
}
return
}

func dropHiddenProfiles(names schema.Names) (result schema.Names) {
func dropHiddenProfiles(names value.Names) (result value.Names) {
for _, profile := range names {
if _, ok := hiddenProfiles[profile.Name]; ok {
continue
Expand All @@ -36,24 +36,33 @@ func dropHiddenProfiles(names schema.Names) (result schema.Names) {
return
}

func dropHiddenArgs(args []schema.Field) (result []schema.Field) {
func dropHiddenArgs(args []value.ObjectSchemaField) (result []value.ObjectSchemaField) {
for _, arg := range args {
if _, ok := hiddenArgs[arg.Name]; ok {
if _, ok := hiddenArgs[arg.Key]; ok {
continue
}
result = append(result, arg)
}
return
}

func fromFields(in []schema.Field) (result []v1.Field) {
func fromFields(in []value.ObjectSchemaField) (result []v1.Field) {
for _, item := range in {
result = append(result, fromField(item))
}
return
}

func anyToString(v any) string {
var (
ts *value.TypeSchema
)
ts, ok := v.(*value.TypeSchema)
if ok {
v = ts.DefaultValue
} else {
v = nil
}
if v == nil {
return ""
}
Expand All @@ -64,36 +73,59 @@ func anyToString(v any) string {
return string(d)
}

func fromObject(in *schema.Object) *v1.Object {
func fromObject(s value.Schema) *v1.Object {
var (
in *value.ObjectSchema
ts *value.TypeSchema
)
ts, ok := s.(*value.TypeSchema)
if ok {
in = ts.Object
}

if in == nil {
return nil
}
return &v1.Object{
Path: in.Path,
Reference: in.Reference,
Path: ts.Path.String(),
Reference: ts.Reference,
Description: in.Description,
Fields: fromFields(in.Fields),
AllowNewKeys: in.AllowNewKeys,
}
}

func fromArray(in *schema.Array) *v1.Array {
func fromArray(s value.Schema) *v1.Array {
var (
in *value.ArraySchema
ts *value.TypeSchema
)
ts, ok := s.(*value.TypeSchema)
if ok {
in = ts.Array
}
if in == nil {
return nil
}
return &v1.Array{
Types: fromFieldTypes(in.Types),
Types: fromFieldTypes(in.Valid),
}
}

func fromConstraints(in []schema.Constraint) (result []v1.Constraint) {
func fromConstraints(s value.Schema) (result []v1.Constraint) {
var (
in []value.Constraint
ts *value.TypeSchema
)
ts, ok := s.(*value.TypeSchema)
if ok {
in = ts.Constraints
}
for _, item := range in {
result = append(result, v1.Constraint{
Description: item.Description,
Op: item.Op,
ID: item.ID,
Right: anyToString(item.Right),
Type: anyToFieldType(item.Right),
Op: item.Op,
Right: anyToString(item.Right),
Type: anyToFieldType(item.Right),
})
}
return
Expand All @@ -104,32 +136,44 @@ func anyToFieldType(v any) *v1.FieldType {
return rt
}

func fromFieldTypes(in []schema.FieldType) (out []v1.FieldType) {
func fromAlternates(s value.Schema) (out []v1.FieldType) {
var (
in []value.Schema
ts *value.TypeSchema
)
ts, ok := s.(*value.TypeSchema)
if ok {
in = ts.Alternates
}
return fromFieldTypes(in)
}

func fromFieldTypes(in []value.Schema) (out []v1.FieldType) {
for _, fieldType := range in {
out = append(out, *fromFieldType(&fieldType))
out = append(out, *fromFieldType(fieldType))
}
return
}

func fromFieldType(in *schema.FieldType) *v1.FieldType {
func fromFieldType(in value.Schema) *v1.FieldType {
if in == nil {
return nil
}
return &v1.FieldType{
Kind: string(in.Kind),
Object: fromObject(in.Object),
Array: fromArray(in.Array),
Constraints: fromConstraints(in.Contstraints),
Default: anyToString(in.Default),
Alternates: fromFieldTypes(in.Alternates),
Kind: string(in.TargetKind()),
Object: fromObject(in),
Array: fromArray(in),
Constraints: fromConstraints(in),
Default: anyToString(in),
Alternates: fromAlternates(in),
}
}

func fromField(in schema.Field) v1.Field {
func fromField(in value.ObjectSchemaField) v1.Field {
return v1.Field{
Name: in.Name,
Name: in.Key,
Description: in.Description,
Type: *fromFieldType(&in.Type),
Type: *fromFieldType(in.Schema),
Match: in.Match,
Optional: in.Optional,
}
Expand All @@ -140,27 +184,27 @@ type Flags interface {
}

func (a *AppDefinition) ToFlags(programName, argsFile string, usage func()) (Flags, error) {
var file schema.File
var file value.FuncSchema
err := a.decode(&file)
if err != nil {
return nil, err
}

args := flagargs.New(argsFile, programName,
dropHiddenProfiles(file.ProfileNames),
dropHiddenArgs(file.Args.Fields))
dropHiddenArgs(file.Args))
args.Usage = usage
return args, nil
}

func (a *AppDefinition) ToParamSpec() (*v1.ParamSpec, error) {
var file schema.File
var file value.FuncSchema
err := a.decode(&file)
if err != nil {
return nil, err
}
result := &v1.ParamSpec{
Args: fromFields(dropHiddenArgs(file.Args.Fields)),
Args: fromFields(dropHiddenArgs(file.Args)),
Profiles: fromNames(dropHiddenProfiles(file.ProfileNames)),
}

Expand Down
Loading

0 comments on commit 8fd5bd1

Please sign in to comment.