diff --git a/internal/folio/declaration.go b/internal/folio/declaration.go index 01eb9ab..9c0a166 100644 --- a/internal/folio/declaration.go +++ b/internal/folio/declaration.go @@ -150,7 +150,7 @@ func (d *Declaration) Resource() data.Resource { return d.Attributes } -func (d *Declaration) Apply() (result error) { +func (d *Declaration) Apply(stateTransition string) (result error) { defer func() { if r := recover(); r != nil { slog.Info("Declaration.Apply()", "error", r, "stacktrace", string(debug.Stack())) @@ -161,9 +161,13 @@ func (d *Declaration) Apply() (result error) { } }() + if stateTransition == "" { + stateTransition = d.Transition + } + stater := d.Attributes.StateMachine() slog.Info("Declaration.Apply()", "machine", stater, "machine.state", stater.CurrentState(), "uri", d.Attributes.URI()) - switch d.Transition { + switch stateTransition { case "construct": if doc, ok := DocumentRegistry.DeclarationMap[d]; ok { d.SetDocument(doc) diff --git a/internal/folio/declaration_test.go b/internal/folio/declaration_test.go index 437e9ac..1de5f6e 100644 --- a/internal/folio/declaration_test.go +++ b/internal/folio/declaration_test.go @@ -9,7 +9,6 @@ _ "encoding/json" "github.com/stretchr/testify/assert" _ "log" _ "os" - "path/filepath" _ "decl/internal/types" "decl/internal/codec" "testing" @@ -18,7 +17,7 @@ _ "decl/internal/types" /* func TestYamlLoadDecl(t *testing.T) { - file := filepath.Join(TempDir, "fooread.txt") + file := TempDir.FilePath("fooread.txt") resourceAttributes := make(map[string]any) decl := fmt.Sprintf(` @@ -44,7 +43,7 @@ func TestNewResourceDeclaration(t *testing.T) { } func TestNewResourceDeclarationType(t *testing.T) { - file := filepath.Join(TempDir, "fooread.txt") + file := TempDir.FilePath("fooread.txt") decl := fmt.Sprintf(` type: foo @@ -61,6 +60,35 @@ func TestNewResourceDeclarationType(t *testing.T) { assert.Equal(t, TypeName("foo"), resourceDeclaration.Type) assert.NotNil(t, resourceDeclaration.Attributes) } + +/* XXX +func TestEventTypeLoad(t *testing.T) { + file := TempDir.FilePath("fooread.txt") + + fooTemplate := ` + type: foo + on: + load: | + %s + attributes: + name: "%s" +` + + for _, v := range []struct{ decl string; expected string } { + { decl: fmt.Sprintf(fooTemplate, "print('Hello world!')", file), expected: "" }, + { decl: fmt.Sprintf(fooTemplate, "print('Hello world!') fail", file), expected: "Lua error: luaL_loadstring(): failed loading, [string \"print('Hello world!') fail...\"]:2: '=' expected near '', return code: 3" }, + } { + resourceDeclaration := NewDeclaration() + resourceDeclaration.ResourceTypes = TestResourceTypes + assert.NotNil(t, resourceDeclaration) + + e := resourceDeclaration.LoadString(v.decl, codec.FormatYaml) + assert.Nil(t, e) + assert.Equal(t, v.expected, resourceDeclaration.Error) + } +} +*/ + /* func TestDeclarationNewResource(t *testing.T) { resourceDeclaration := NewDeclaration() @@ -98,7 +126,7 @@ func TestDeclarationJson(t *testing.T) { "type": "user", "attributes": { "name": "testuser", - "uid": "10012" + "uid": "10012" } } ` diff --git a/internal/folio/document.go b/internal/folio/document.go index 1b8e0dd..2e2836b 100644 --- a/internal/folio/document.go +++ b/internal/folio/document.go @@ -268,16 +268,13 @@ func (d *Document) Apply(state string) error { if idx < 0 { idx = - idx } slog.Info("Document.Apply() applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI(), "resource", d.ResourceDeclarations[idx].Resource()) - if state != "" { - d.ResourceDeclarations[idx].Transition = state - } d.ResourceDeclarations[idx].SetConfig(d.config) slog.Info("Document.Apply() applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI(), "resource", d.ResourceDeclarations[idx].Resource()) if d.ResourceDeclarations[idx].Requires.Check() { - if e := d.ResourceDeclarations[idx].Apply(); e != nil { + if e := d.ResourceDeclarations[idx].Apply(state); e != nil { slog.Error("Document.Apply() error applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI()) d.ResourceDeclarations[idx].Error = e.Error() @@ -560,8 +557,7 @@ func (d *Document) UnmarshalYAML(value *yaml.Node) error { } d.assignConfigurationsDocument() d.assignResourcesDocument() - d.loadImports() - return nil + return d.loadImports() } func (d *Document) UnmarshalJSON(data []byte) error { @@ -572,7 +568,6 @@ func (d *Document) UnmarshalJSON(data []byte) error { } d.assignConfigurationsDocument() d.assignResourcesDocument() - d.loadImports() - return nil + return d.loadImports() } diff --git a/internal/folio/folio_test.go b/internal/folio/folio_test.go index 034b4dc..e0d2c31 100644 --- a/internal/folio/folio_test.go +++ b/internal/folio/folio_test.go @@ -20,8 +20,7 @@ var ProcessTestUserName string var ProcessTestGroupName string func TestMain(m *testing.M) { - var err error - err = TempDir.Create() + err := TempDir.Create() if err != nil || TempDir == "" { log.Fatal(err) } diff --git a/internal/folio/mock_resource_test.go b/internal/folio/mock_resource_test.go index 4a056a0..7e8f0a1 100644 --- a/internal/folio/mock_resource_test.go +++ b/internal/folio/mock_resource_test.go @@ -15,12 +15,12 @@ _ "fmt" ) type MockResource struct { - InjectURI func() string `json:"-" yaml:"-"` - InjectType func() string `json:"-" yaml:"-"` - InjectResolveId func(ctx context.Context) string `json:"-" yaml:"-"` - InjectLoadDecl func(string) error `json:"-" yaml:"-"` - InjectValidate func() error `json:"-" yaml:"-"` - InjectApply func() error `json:"-" yaml:"-"` + InjectURI func() string `json:"-" yaml:"-"` + InjectType func() string `json:"-" yaml:"-"` + InjectResolveId func(ctx context.Context) string `json:"-" yaml:"-"` + InjectLoadDecl func(string) error `json:"-" yaml:"-"` + InjectValidate func() error `json:"-" yaml:"-"` + InjectApply func() error `json:"-" yaml:"-"` InjectJSON func() ([]byte, error) `json:"-" yaml:"-"` InjectYAML func() ([]byte, error) `json:"-" yaml:"-"` InjectPB func() ([]byte, error) `json:"-" yaml:"-"` @@ -32,7 +32,7 @@ type MockResource struct { InjectRead func(context.Context) ([]byte, error) `json:"-" yaml:"-"` InjectUpdate func(context.Context) error `json:"-" yaml:"-"` InjectDelete func(context.Context) error `json:"-" yaml:"-"` - InjectStateMachine func() machine.Stater `json:"-" yaml:"-"` + InjectStateMachine func() machine.Stater `json:"-" yaml:"-"` InjectSetResourceMapper func(data.ResourceMapper) `json:"-" yaml:"-"` InjectUseConfig func(data.ConfigurationValueGetter) `json:"-" yaml:"-"` InjectNotify func(*machine.EventMessage) `json:"-" yaml:"-"`