add document state transformer interface
This commit is contained in:
parent
452695c8f5
commit
f1a56b0968
@ -150,7 +150,7 @@ func (d *Declaration) Resource() data.Resource {
|
|||||||
return d.Attributes
|
return d.Attributes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Declaration) Apply() (result error) {
|
func (d *Declaration) Apply(stateTransition string) (result error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
slog.Info("Declaration.Apply()", "error", r, "stacktrace", string(debug.Stack()))
|
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()
|
stater := d.Attributes.StateMachine()
|
||||||
slog.Info("Declaration.Apply()", "machine", stater, "machine.state", stater.CurrentState(), "uri", d.Attributes.URI())
|
slog.Info("Declaration.Apply()", "machine", stater, "machine.state", stater.CurrentState(), "uri", d.Attributes.URI())
|
||||||
switch d.Transition {
|
switch stateTransition {
|
||||||
case "construct":
|
case "construct":
|
||||||
if doc, ok := DocumentRegistry.DeclarationMap[d]; ok {
|
if doc, ok := DocumentRegistry.DeclarationMap[d]; ok {
|
||||||
d.SetDocument(doc)
|
d.SetDocument(doc)
|
||||||
|
@ -9,7 +9,6 @@ _ "encoding/json"
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
_ "log"
|
_ "log"
|
||||||
_ "os"
|
_ "os"
|
||||||
"path/filepath"
|
|
||||||
_ "decl/internal/types"
|
_ "decl/internal/types"
|
||||||
"decl/internal/codec"
|
"decl/internal/codec"
|
||||||
"testing"
|
"testing"
|
||||||
@ -18,7 +17,7 @@ _ "decl/internal/types"
|
|||||||
/*
|
/*
|
||||||
func TestYamlLoadDecl(t *testing.T) {
|
func TestYamlLoadDecl(t *testing.T) {
|
||||||
|
|
||||||
file := filepath.Join(TempDir, "fooread.txt")
|
file := TempDir.FilePath("fooread.txt")
|
||||||
|
|
||||||
resourceAttributes := make(map[string]any)
|
resourceAttributes := make(map[string]any)
|
||||||
decl := fmt.Sprintf(`
|
decl := fmt.Sprintf(`
|
||||||
@ -44,7 +43,7 @@ func TestNewResourceDeclaration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewResourceDeclarationType(t *testing.T) {
|
func TestNewResourceDeclarationType(t *testing.T) {
|
||||||
file := filepath.Join(TempDir, "fooread.txt")
|
file := TempDir.FilePath("fooread.txt")
|
||||||
|
|
||||||
decl := fmt.Sprintf(`
|
decl := fmt.Sprintf(`
|
||||||
type: foo
|
type: foo
|
||||||
@ -61,6 +60,35 @@ func TestNewResourceDeclarationType(t *testing.T) {
|
|||||||
assert.Equal(t, TypeName("foo"), resourceDeclaration.Type)
|
assert.Equal(t, TypeName("foo"), resourceDeclaration.Type)
|
||||||
assert.NotNil(t, resourceDeclaration.Attributes)
|
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 '<eof>', 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) {
|
func TestDeclarationNewResource(t *testing.T) {
|
||||||
resourceDeclaration := NewDeclaration()
|
resourceDeclaration := NewDeclaration()
|
||||||
|
@ -268,16 +268,13 @@ func (d *Document) Apply(state string) error {
|
|||||||
if idx < 0 { idx = - idx }
|
if idx < 0 { idx = - idx }
|
||||||
|
|
||||||
slog.Info("Document.Apply() applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI(), "resource", d.ResourceDeclarations[idx].Resource())
|
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)
|
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())
|
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 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())
|
slog.Error("Document.Apply() error applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI())
|
||||||
|
|
||||||
d.ResourceDeclarations[idx].Error = e.Error()
|
d.ResourceDeclarations[idx].Error = e.Error()
|
||||||
@ -560,8 +557,7 @@ func (d *Document) UnmarshalYAML(value *yaml.Node) error {
|
|||||||
}
|
}
|
||||||
d.assignConfigurationsDocument()
|
d.assignConfigurationsDocument()
|
||||||
d.assignResourcesDocument()
|
d.assignResourcesDocument()
|
||||||
d.loadImports()
|
return d.loadImports()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Document) UnmarshalJSON(data []byte) error {
|
func (d *Document) UnmarshalJSON(data []byte) error {
|
||||||
@ -572,7 +568,6 @@ func (d *Document) UnmarshalJSON(data []byte) error {
|
|||||||
}
|
}
|
||||||
d.assignConfigurationsDocument()
|
d.assignConfigurationsDocument()
|
||||||
d.assignResourcesDocument()
|
d.assignResourcesDocument()
|
||||||
d.loadImports()
|
return d.loadImports()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,8 +20,7 @@ var ProcessTestUserName string
|
|||||||
var ProcessTestGroupName string
|
var ProcessTestGroupName string
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
var err error
|
err := TempDir.Create()
|
||||||
err = TempDir.Create()
|
|
||||||
if err != nil || TempDir == "" {
|
if err != nil || TempDir == "" {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user