lang: Plumb through the unified state facility

This commit is contained in:
James Shubin
2024-07-01 14:47:04 -04:00
parent d326917432
commit 4e18c9c67a
6 changed files with 49 additions and 29 deletions

View File

@@ -49,6 +49,7 @@ import (
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/interpolate" "github.com/purpleidea/mgmt/lang/interpolate"
"github.com/purpleidea/mgmt/lang/parser" "github.com/purpleidea/mgmt/lang/parser"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/lang/unification" "github.com/purpleidea/mgmt/lang/unification"
"github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/util" "github.com/purpleidea/mgmt/util"
@@ -287,11 +288,12 @@ func (obj *GAPI) Cli(info *gapi.Info) (*gapi.Deploy, error) {
return nil, errwrap.Wrapf(err, "could not get default solver") return nil, errwrap.Wrapf(err, "could not get default solver")
} }
unifier := &unification.Unifier{ unifier := &unification.Unifier{
AST: iast, AST: iast,
Solver: solver, Solver: solver,
Strategy: unificationStrategy, Strategy: unificationStrategy,
Debug: debug, UnifiedState: types.NewUnifiedState(),
Logf: unificationLogf, Debug: debug,
Logf: unificationLogf,
} }
startTime := time.Now() startTime := time.Now()
unifyErr := unifier.Unify(context.TODO()) unifyErr := unifier.Unify(context.TODO())

View File

@@ -60,6 +60,7 @@ import (
"github.com/purpleidea/mgmt/lang/interpolate" "github.com/purpleidea/mgmt/lang/interpolate"
"github.com/purpleidea/mgmt/lang/interpret" "github.com/purpleidea/mgmt/lang/interpret"
"github.com/purpleidea/mgmt/lang/parser" "github.com/purpleidea/mgmt/lang/parser"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/lang/unification" "github.com/purpleidea/mgmt/lang/unification"
"github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/util" "github.com/purpleidea/mgmt/util"
@@ -465,10 +466,11 @@ func TestAstFunc1(t *testing.T) {
return return
} }
unifier := &unification.Unifier{ unifier := &unification.Unifier{
AST: iast, AST: iast,
Solver: solver, Solver: solver,
Debug: testing.Verbose(), UnifiedState: types.NewUnifiedState(),
Logf: xlogf, Debug: testing.Verbose(),
Logf: xlogf,
} }
err = unifier.Unify(context.TODO()) err = unifier.Unify(context.TODO())
if (!fail || !failUnify) && err != nil { if (!fail || !failUnify) && err != nil {
@@ -1041,10 +1043,11 @@ func TestAstFunc2(t *testing.T) {
return return
} }
unifier := &unification.Unifier{ unifier := &unification.Unifier{
AST: iast, AST: iast,
Solver: solver, Solver: solver,
Debug: testing.Verbose(), UnifiedState: types.NewUnifiedState(),
Logf: xlogf, Debug: testing.Verbose(),
Logf: xlogf,
} }
err = unifier.Unify(context.TODO()) err = unifier.Unify(context.TODO())
if (!fail || !failUnify) && err != nil { if (!fail || !failUnify) && err != nil {
@@ -1849,10 +1852,11 @@ func TestAstFunc3(t *testing.T) {
return return
} }
unifier := &unification.Unifier{ unifier := &unification.Unifier{
AST: iast, AST: iast,
Solver: solver, Solver: solver,
Debug: testing.Verbose(), UnifiedState: types.NewUnifiedState(),
Logf: xlogf, Debug: testing.Verbose(),
Logf: xlogf,
} }
err = unifier.Unify(context.TODO()) err = unifier.Unify(context.TODO())
if (!fail || !failUnify) && err != nil { if (!fail || !failUnify) && err != nil {

View File

@@ -49,6 +49,7 @@ import (
"github.com/purpleidea/mgmt/lang/interpolate" "github.com/purpleidea/mgmt/lang/interpolate"
"github.com/purpleidea/mgmt/lang/interpret" "github.com/purpleidea/mgmt/lang/interpret"
"github.com/purpleidea/mgmt/lang/parser" "github.com/purpleidea/mgmt/lang/parser"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/lang/unification" "github.com/purpleidea/mgmt/lang/unification"
_ "github.com/purpleidea/mgmt/lang/unification/solvers" // import so the solvers register _ "github.com/purpleidea/mgmt/lang/unification/solvers" // import so the solvers register
"github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/pgraph"
@@ -258,11 +259,12 @@ func (obj *Lang) Init(ctx context.Context) error {
return errwrap.Wrapf(err, "could not get default solver") return errwrap.Wrapf(err, "could not get default solver")
} }
unifier := &unification.Unifier{ unifier := &unification.Unifier{
AST: obj.ast, AST: obj.ast,
Solver: solver, Solver: solver,
Strategy: obj.Data.UnificationStrategy, Strategy: obj.Data.UnificationStrategy,
Debug: obj.Debug, UnifiedState: types.NewUnifiedState(),
Logf: logf, Debug: obj.Debug,
Logf: logf,
} }
timing = time.Now() timing = time.Now()
// NOTE: This is the "real" Unify that runs. (This is not for deploy.) // NOTE: This is the "real" Unify that runs. (This is not for deploy.)

View File

@@ -59,6 +59,9 @@ type Init struct {
// overall cleaner unification algorithm in place. // overall cleaner unification algorithm in place.
Strategy map[string]string Strategy map[string]string
// UnifiedState stores a common representation of our unification vars.
UnifiedState *types.UnifiedState
Debug bool Debug bool
Logf func(format string, v ...interface{}) Logf func(format string, v ...interface{})
} }

View File

@@ -861,10 +861,11 @@ func TestUnification1(t *testing.T) {
return return
} }
unifier := &unification.Unifier{ unifier := &unification.Unifier{
AST: xast, AST: xast,
Solver: solver, Solver: solver,
Debug: debug, UnifiedState: types.NewUnifiedState(),
Logf: logf, Debug: debug,
Logf: logf,
} }
err = unifier.Unify(context.TODO()) err = unifier.Unify(context.TODO())

View File

@@ -38,6 +38,7 @@ import (
"strings" "strings"
"github.com/purpleidea/mgmt/lang/interfaces" "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. // Unifier holds all the data that the Unify function will need for it to run.
@@ -52,6 +53,9 @@ type Unifier struct {
// overall cleaner unification algorithm in place. // overall cleaner unification algorithm in place.
Strategy map[string]string Strategy map[string]string
// UnifiedState stores a common representation of our unification vars.
UnifiedState *types.UnifiedState
Debug bool Debug bool
Logf func(format string, v ...interface{}) Logf func(format string, v ...interface{})
} }
@@ -75,14 +79,18 @@ func (obj *Unifier) Unify(ctx context.Context) error {
if obj.Solver == nil { if obj.Solver == nil {
return fmt.Errorf("the Solver is missing") return fmt.Errorf("the Solver is missing")
} }
if obj.UnifiedState == nil {
return fmt.Errorf("the UnifiedState table is missing")
}
if obj.Logf == nil { if obj.Logf == nil {
return fmt.Errorf("the Logf function is missing") return fmt.Errorf("the Logf function is missing")
} }
init := &Init{ init := &Init{
Strategy: obj.Strategy, Strategy: obj.Strategy,
Logf: obj.Logf, UnifiedState: obj.UnifiedState,
Debug: obj.Debug, Logf: obj.Logf,
Debug: obj.Debug,
} }
if err := obj.Solver.Init(init); err != nil { if err := obj.Solver.Init(init); err != nil {
return err return err