Skip to content

Commit

Permalink
lang: Plumb through the unified state facility
Browse files Browse the repository at this point in the history
  • Loading branch information
purpleidea committed Jul 1, 2024
1 parent d326917 commit 4e18c9c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 29 deletions.
12 changes: 7 additions & 5 deletions lang/gapi/gapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/interpolate"
"github.com/purpleidea/mgmt/lang/parser"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/lang/unification"
"github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/util"
Expand Down Expand Up @@ -287,11 +288,12 @@ func (obj *GAPI) Cli(info *gapi.Info) (*gapi.Deploy, error) {
return nil, errwrap.Wrapf(err, "could not get default solver")
}
unifier := &unification.Unifier{
AST: iast,
Solver: solver,
Strategy: unificationStrategy,
Debug: debug,
Logf: unificationLogf,
AST: iast,
Solver: solver,
Strategy: unificationStrategy,
UnifiedState: types.NewUnifiedState(),
Debug: debug,
Logf: unificationLogf,
}
startTime := time.Now()
unifyErr := unifier.Unify(context.TODO())
Expand Down
28 changes: 16 additions & 12 deletions lang/interpret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"github.com/purpleidea/mgmt/lang/interpolate"
"github.com/purpleidea/mgmt/lang/interpret"
"github.com/purpleidea/mgmt/lang/parser"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/lang/unification"
"github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/util"
Expand Down Expand Up @@ -465,10 +466,11 @@ func TestAstFunc1(t *testing.T) {
return
}
unifier := &unification.Unifier{
AST: iast,
Solver: solver,
Debug: testing.Verbose(),
Logf: xlogf,
AST: iast,
Solver: solver,
UnifiedState: types.NewUnifiedState(),
Debug: testing.Verbose(),
Logf: xlogf,
}
err = unifier.Unify(context.TODO())
if (!fail || !failUnify) && err != nil {
Expand Down Expand Up @@ -1041,10 +1043,11 @@ func TestAstFunc2(t *testing.T) {
return
}
unifier := &unification.Unifier{
AST: iast,
Solver: solver,
Debug: testing.Verbose(),
Logf: xlogf,
AST: iast,
Solver: solver,
UnifiedState: types.NewUnifiedState(),
Debug: testing.Verbose(),
Logf: xlogf,
}
err = unifier.Unify(context.TODO())
if (!fail || !failUnify) && err != nil {
Expand Down Expand Up @@ -1849,10 +1852,11 @@ func TestAstFunc3(t *testing.T) {
return
}
unifier := &unification.Unifier{
AST: iast,
Solver: solver,
Debug: testing.Verbose(),
Logf: xlogf,
AST: iast,
Solver: solver,
UnifiedState: types.NewUnifiedState(),
Debug: testing.Verbose(),
Logf: xlogf,
}
err = unifier.Unify(context.TODO())
if (!fail || !failUnify) && err != nil {
Expand Down
12 changes: 7 additions & 5 deletions lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/purpleidea/mgmt/lang/interpolate"
"github.com/purpleidea/mgmt/lang/interpret"
"github.com/purpleidea/mgmt/lang/parser"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/lang/unification"
_ "github.com/purpleidea/mgmt/lang/unification/solvers" // import so the solvers register
"github.com/purpleidea/mgmt/pgraph"
Expand Down Expand Up @@ -258,11 +259,12 @@ func (obj *Lang) Init(ctx context.Context) error {
return errwrap.Wrapf(err, "could not get default solver")
}
unifier := &unification.Unifier{
AST: obj.ast,
Solver: solver,
Strategy: obj.Data.UnificationStrategy,
Debug: obj.Debug,
Logf: logf,
AST: obj.ast,
Solver: solver,
Strategy: obj.Data.UnificationStrategy,
UnifiedState: types.NewUnifiedState(),
Debug: obj.Debug,
Logf: logf,
}
timing = time.Now()
// NOTE: This is the "real" Unify that runs. (This is not for deploy.)
Expand Down
3 changes: 3 additions & 0 deletions lang/unification/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type Init struct {
// overall cleaner unification algorithm in place.
Strategy map[string]string

// UnifiedState stores a common representation of our unification vars.
UnifiedState *types.UnifiedState

Debug bool
Logf func(format string, v ...interface{})
}
Expand Down
9 changes: 5 additions & 4 deletions lang/unification/solvers/unification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,10 +861,11 @@ func TestUnification1(t *testing.T) {
return
}
unifier := &unification.Unifier{
AST: xast,
Solver: solver,
Debug: debug,
Logf: logf,
AST: xast,
Solver: solver,
UnifiedState: types.NewUnifiedState(),
Debug: debug,
Logf: logf,
}
err = unifier.Unify(context.TODO())

Expand Down
14 changes: 11 additions & 3 deletions lang/unification/unification.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"strings"

"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
)

// Unifier holds all the data that the Unify function will need for it to run.
Expand All @@ -52,6 +53,9 @@ type Unifier struct {
// overall cleaner unification algorithm in place.
Strategy map[string]string

// UnifiedState stores a common representation of our unification vars.
UnifiedState *types.UnifiedState

Debug bool
Logf func(format string, v ...interface{})
}
Expand All @@ -75,14 +79,18 @@ func (obj *Unifier) Unify(ctx context.Context) error {
if obj.Solver == nil {
return fmt.Errorf("the Solver is missing")
}
if obj.UnifiedState == nil {
return fmt.Errorf("the UnifiedState table is missing")
}
if obj.Logf == nil {
return fmt.Errorf("the Logf function is missing")
}

init := &Init{
Strategy: obj.Strategy,
Logf: obj.Logf,
Debug: obj.Debug,
Strategy: obj.Strategy,
UnifiedState: obj.UnifiedState,
Logf: obj.Logf,
Debug: obj.Debug,
}
if err := obj.Solver.Init(init); err != nil {
return err
Expand Down

0 comments on commit 4e18c9c

Please sign in to comment.