Skip to content

Commit

Permalink
first cut for yaml controls
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Jun 23, 2021
1 parent 3047fbf commit b8536e8
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 160 deletions.
4 changes: 2 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func convert(stdin bool, templateFilePath string, json, split bool, subpath stri
flowed := templateYAML
if subpath != "" {
comps := dynaml.PathComponents(subpath, false)
node, ok := yaml.FindR(true, flowed, comps...)
node, ok := yaml.FindR(true, flowed, nil, comps...)
if !ok {
log.Fatalln(fmt.Sprintf("path %q not found%s", subpath, doc))
}
Expand All @@ -85,7 +85,7 @@ func convert(stdin bool, templateFilePath string, json, split bool, subpath stri
new := map[string]yaml.Node{}
for _, p := range selection {
comps := dynaml.PathComponents(p, false)
node, ok := yaml.FindR(true, flowed, comps...)
node, ok := yaml.FindR(true, flowed, nil, comps...)
if !ok {
log.Fatalln(fmt.Sprintf("path %q not found%s", subpath, doc))
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func merge(stdin bool, templateFilePath string, opts flow.Options, json, split b
" -: depending on a node with an error"

var binding dynaml.Binding
features := features.Features()
if bindingYAML != nil || interpolation || len(tags) > 0 || len(templateYAMLs) > 1 {
defstate := flow.NewDefaultState().SetInterpolation(interpolation)
defstate.SetTags(tags...)
Expand All @@ -243,6 +244,7 @@ func merge(stdin bool, templateFilePath string, opts flow.Options, json, split b
}
binding = binding.WithLocalScope(values)
}
features = binding.GetFeatures()
}

prepared, err := flow.PrepareStubs(binding, processingOptions.Partial, stubs...)
Expand Down Expand Up @@ -272,7 +274,7 @@ func merge(stdin bool, templateFilePath string, opts flow.Options, json, split b
}
if subpath != "" {
comps := dynaml.PathComponents(subpath, false)
node, ok := yaml.FindR(true, flowed, comps...)
node, ok := yaml.FindR(true, flowed, features, comps...)
if !ok {
log.Fatalln(fmt.Sprintf("path %q not found%s", subpath, doc))
}
Expand Down Expand Up @@ -330,7 +332,7 @@ func merge(stdin bool, templateFilePath string, opts flow.Options, json, split b
new := map[string]yaml.Node{}
for _, p := range selection {
comps := dynaml.PathComponents(p, false)
node, ok := yaml.FindR(true, flowed, comps...)
node, ok := yaml.FindR(true, flowed, features, comps...)
if !ok {
log.Fatalln(fmt.Sprintf("path %q not found%s", subpath, doc))
}
Expand Down
8 changes: 4 additions & 4 deletions compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func listToMap(list []yaml.Node) map[string]yaml.Node {
toMap := make(map[string]yaml.Node)

for _, val := range list {
name, ok := yaml.FindString(val, "name")
name, ok := yaml.FindString(val, nil, "name")
if !ok {
return nil
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func jobMap(jobs []yaml.Node) map[string]yaml.Node {
attrs, ok := job.Value().(map[string]yaml.Node)
attrs["index"] = yaml.NewNode(index, job.SourceName())

name, ok := yaml.FindString(job, "name")
name, ok := yaml.FindString(job, nil, "name")
if !ok {
panic("job without string name")
}
Expand All @@ -167,7 +167,7 @@ func jobMap(jobs []yaml.Node) map[string]yaml.Node {
}

func findByNameOrIndex(node yaml.Node, others []yaml.Node, index int) (string, yaml.Node, bool) {
name, ok := yaml.FindString(node, "name")
name, ok := yaml.FindString(node, nil, "name")
if !ok {
return findByIndex(others, index)
}
Expand All @@ -182,7 +182,7 @@ func findByNameOrIndex(node yaml.Node, others []yaml.Node, index int) (string, y

func findByName(name string, nodes []yaml.Node) (string, yaml.Node, bool) {
for _, node := range nodes {
otherName, ok := yaml.FindString(node, "name")
otherName, ok := yaml.FindString(node, nil, "name")
if !ok {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions dynaml/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (e AutoExpr) Evaluate(binding Binding, locally bool) (interface{}, Evaluati
return nil, info, false
}

if !isResolvedValue(jobs) {
if !isResolvedValue(jobs, binding) {
return e, info, true
}
jobsList, ok := jobs.([]yaml.Node)
Expand All @@ -33,7 +33,7 @@ func (e AutoExpr) Evaluate(binding Binding, locally bool) (interface{}, Evaluati
var size int64

for _, job := range jobsList {
poolName, ok := yaml.FindString(job, "resource_pool")
poolName, ok := yaml.FindString(job, binding.GetFeatures(), "resource_pool")
if !ok {
continue
}
Expand All @@ -42,7 +42,7 @@ func (e AutoExpr) Evaluate(binding Binding, locally bool) (interface{}, Evaluati
continue
}

instances, ok := yaml.FindInt(job, "instances")
instances, ok := yaml.FindInt(job, binding.GetFeatures(), "instances")
if !ok {
return nil, info, false
}
Expand Down
8 changes: 4 additions & 4 deletions dynaml/dynamic_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func (e DynamicExpr) Evaluate(binding Binding, locally bool) (interface{}, Evalu

locally = locally || info.Raw

if !isLocallyResolvedValue(root) {
if !isLocallyResolvedValue(root, binding) {
return e, info, true
}

if !locally && !isResolvedValue(root) {
if !locally && !isResolvedValue(root, binding) {
return e, info, true
}

Expand All @@ -34,7 +34,7 @@ func (e DynamicExpr) Evaluate(binding Binding, locally bool) (interface{}, Evalu
if !ok {
return nil, info, false
}
if !isResolvedValue(dyn) {
if !isResolvedValue(dyn, binding) {
return e, info, true
}

Expand Down Expand Up @@ -66,7 +66,7 @@ func (e DynamicExpr) Evaluate(binding Binding, locally bool) (interface{}, Evalu
return info.Error("index or field name required for reference qualifier")
}
return NewReferenceExpr(qual...).find(func(end int, path []string) (yaml.Node, bool) {
return yaml.Find(NewNode(root, nil), path[:end+1]...)
return yaml.Find(NewNode(root, nil), binding.GetFeatures(), path[:end+1]...)
}, binding, locally)
}

Expand Down
3 changes: 3 additions & 0 deletions dynaml/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dynaml
import (
"github.com/mandelsoft/vfs/pkg/vfs"

"github.com/mandelsoft/spiff/features"
"github.com/mandelsoft/spiff/yaml"
)

Expand All @@ -24,6 +25,7 @@ type State interface {
FileAccessAllowed() bool
FileSystem() vfs.VFS
GetFunctions() Registry
GetFeatures() features.FeatureFlags
InterpolationEnabled() bool
ControlEnabled() bool
SetTag(name string, node yaml.Node, path []string, scope TagScope) error
Expand Down Expand Up @@ -53,6 +55,7 @@ type Binding interface {
NoMerge() bool

GetState() State
GetFeatures() features.FeatureFlags
GetTempName(data []byte) (string, error)
GetFileContent(file string, cached bool) ([]byte, error)

Expand Down
5 changes: 5 additions & 0 deletions dynaml/fake_binding_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/mandelsoft/spiff/features"
"github.com/mandelsoft/spiff/yaml"
)

Expand All @@ -28,6 +29,10 @@ func (c FakeBinding) GetState() State {
return nil
}

func (c FakeBinding) GetFeatures() features.FeatureFlags {
return nil
}

func (c FakeBinding) Outer() Binding {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions dynaml/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func (e ProjectionExpr) Evaluate(binding Binding, locally bool) (interface{}, Ev
if !ok {
return nil, infoa, false
}
if !isLocallyResolvedValue(newList[index]) {
if !isLocallyResolvedValue(newList[index], binding) {
return e, infoa, true
}
if !locally && !isResolvedValue(newList[index]) {
if !locally && !isResolvedValue(newList[index], binding) {
return e, infoa, true
}
newList[index] = NewNode(result, binding)
Expand All @@ -50,10 +50,10 @@ func (e ProjectionExpr) Evaluate(binding Binding, locally bool) (interface{}, Ev
if !ok {
return nil, infoa, false
}
if !isLocallyResolvedValue(newList[index]) {
if !isLocallyResolvedValue(newList[index], binding) {
return e, infoa, true
}
if !locally && !isResolvedValue(newList[index]) {
if !locally && !isResolvedValue(newList[index], binding) {
return e, infoa, true
}
newList[index] = NewNode(result, binding)
Expand Down
6 changes: 3 additions & 3 deletions dynaml/qualified_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (e QualifiedExpr) Evaluate(binding Binding, locally bool) (interface{}, Eva
return nil, info, false
}
locally = locally || info.Raw
if !isLocallyResolvedValue(root) {
if !isLocallyResolvedValue(root, binding) {
debug.Debug("not locally resolved: %v\n", root)
if root != nil {
if ex, ok := root.(Expression); ok {
Expand All @@ -28,14 +28,14 @@ func (e QualifiedExpr) Evaluate(binding Binding, locally bool) (interface{}, Eva
}
return e, info, true
}
if !locally && !isResolvedValue(root) {
if !locally && !isResolvedValue(root, binding) {
debug.Debug("not resoved: %v\n", root)
return e, info, true
}

debug.Debug("qualified reference (%t): %v\n", locally, e.Reference.Path)
return e.Reference.find(func(end int, path []string) (yaml.Node, bool) {
return yaml.Find(NewNode(root, nil), path[:end+1]...)
return yaml.Find(NewNode(root, nil), binding.GetFeatures(), path[:end+1]...)
}, binding, locally)
}

Expand Down
6 changes: 3 additions & 3 deletions dynaml/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (e ReferenceExpr) Evaluate(binding Binding, locally bool) (interface{}, Eva
return binding.FindFromRoot(path[start : end+1])
} else {
if tag != nil {
return yaml.Find(tag.Node(), path...)
return yaml.Find(tag.Node(), binding.GetFeatures(), path...)
}
return binding.FindReference(path[:end+1])
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (e ReferenceExpr) find(f func(int, []string) (node yaml.Node, x bool), bind
return info.Error("'%s' not found", strings.Join(e.Path[0:i+1], "."))
}

if !isLocallyResolved(step) {
if !isLocallyResolved(step, binding) {
debug.Debug(" locally unresolved %T\n", step.Value())
if _, ok := step.Value().(Expression); ok {
info.Issue = yaml.NewIssue("'%s' unresolved", strings.Join(e.Path[0:i+1], "."))
Expand All @@ -122,7 +122,7 @@ func (e ReferenceExpr) find(f func(int, []string) (node yaml.Node, x bool), bind
}
}

if !locally && !isResolvedValue(step.Value()) {
if !locally && !isResolvedValue(step.Value(), binding) {
debug.Debug(" unresolved\n")
info.Issue = yaml.NewIssue("'%s' unresolved", strings.Join(e.Path, "."))
info.Failed = step.Failed() || step.HasError()
Expand Down
4 changes: 2 additions & 2 deletions dynaml/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func (e SliceExpr) Evaluate(binding Binding, locally bool) (interface{}, Evaluat
if !ok {
return nil, info, false
}
if !isLocallyResolvedValue(root) {
if !isLocallyResolvedValue(root, binding) {
return e, info, true
}
if !locally && !isResolvedValue(root) {
if !locally && !isResolvedValue(root, binding) {
return e, info, true
}

Expand Down
24 changes: 12 additions & 12 deletions dynaml/unresolved_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,20 @@ func isExpression(val interface{}) bool {
return ok
}

func isLocallyResolved(node yaml.Node) bool {
return isLocallyResolvedValue(node.Value())
func isLocallyResolved(node yaml.Node, binding Binding) bool {
return isLocallyResolvedValue(node.Value(), binding)
}

func isLocallyResolvedValue(value interface{}) bool {
func isLocallyResolvedValue(value interface{}, binding Binding) bool {
switch v := value.(type) {
case Expression:
return false
case map[string]yaml.Node:
if !yaml.IsMapResolved(v) {
if !yaml.IsMapResolved(v, binding.GetFeatures()) {
return false
}
case []yaml.Node:
if !yaml.IsListResolved(v) {
if !yaml.IsListResolved(v, binding.GetFeatures()) {
return false
}
default:
Expand All @@ -296,21 +296,21 @@ func isLocallyResolvedValue(value interface{}) bool {
return true
}

func IsResolvedNode(node yaml.Node) bool {
func IsResolvedNode(node yaml.Node, binding Binding) bool {
if node == nil {
return false
}
if node.Failed() || node.Undefined() {
return false
}
return isResolvedValue(node.Value())
return isResolvedValue(node.Value(), binding)
}

func isResolved(node yaml.Node) bool {
return node == nil || isResolvedValue(node.Value())
func isResolved(node yaml.Node, binding Binding) bool {
return node == nil || isResolvedValue(node.Value(), binding)
}

func isResolvedValue(val interface{}) bool {
func isResolvedValue(val interface{}, binding Binding) bool {
if val == nil {
return true
}
Expand All @@ -319,14 +319,14 @@ func isResolvedValue(val interface{}) bool {
return false
case []yaml.Node:
for _, n := range v {
if !isResolved(n) {
if !isResolved(n, binding) {
return false
}
}
return true
case map[string]yaml.Node:
for _, n := range v {
if !isResolved(n) {
if !isResolved(n, binding) {
return false
}
}
Expand Down
3 changes: 3 additions & 0 deletions features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const CONTROL = "control"
type FeatureFlags map[string]struct{}

func (this FeatureFlags) Enabled(name string) bool {
if this == nil {
return false
}
_, ok := this[name]
return ok
}
Expand Down
Loading

0 comments on commit b8536e8

Please sign in to comment.