Skip to content

Commit

Permalink
release v1.7.0-beta-3
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Jan 16, 2022
2 parents 15de963 + f2e84cc commit 364c4d4
Show file tree
Hide file tree
Showing 90 changed files with 9,174 additions and 2,150 deletions.
1,009 changes: 984 additions & 25 deletions README.md

Large diffs are not rendered by default.

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
15 changes: 13 additions & 2 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var tagdefs []string
var expr string
var split bool
var interpolation bool
var featureFlags []string
var processingOptions flow.Options
var state string
var bindings string
Expand Down Expand Up @@ -72,6 +73,7 @@ func init() {
mergeCmd.Flags().StringArrayVarP(&values, "define", "D", nil, "key/value bindings")
mergeCmd.Flags().StringArrayVar(&selection, "select", []string{}, "filter dedicated output fields")
mergeCmd.Flags().StringArrayVar(&tagdefs, "tag", []string{}, "tag files (tag:path)")
mergeCmd.Flags().StringArrayVar(&featureFlags, "features", []string{}, "set feature flags")
mergeCmd.Flags().StringVar(&expr, "evaluate", "", "evaluation expression")
}

Expand Down Expand Up @@ -231,6 +233,14 @@ 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()
for _, list := range featureFlags {
for _, f := range strings.Split(list, ",") {
if err := features.Set(strings.TrimSpace(f), true); err != nil {
log.Fatalln(err.Error())
}
}
}
if bindingYAML != nil || interpolation || len(tags) > 0 || len(templateYAMLs) > 1 {
defstate := flow.NewDefaultState().SetInterpolation(interpolation)
defstate.SetTags(tags...)
Expand All @@ -243,6 +253,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 +283,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 +341,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
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/mandelsoft/spiff/flow"
)

var cfgFile string
Expand All @@ -17,7 +19,7 @@ var cfgFile string
var rootCmd = &cobra.Command{
Use: "spiff",
Short: "YAML in-domain templating processor",
Version: "v1.7.0-beta-2",
Version: flow.VERSION,
}

// Execute adds all child commands to the root command and sets flags appropriately.
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
17 changes: 15 additions & 2 deletions dynaml/addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,28 @@ func (e AdditionExpr) Evaluate(binding Binding, locally bool) (interface{}, Eval

str, ok := a.(string)
if ok {
var cidr *net.IPNet
var err error
ip := net.ParseIP(str)
if ip == nil {
return info.Error("first argument for addition must be IP address or number")
ip, cidr, err = net.ParseCIDR(str)
if err != nil {
return info.Error("first argument for addition must be IP address, CIDR or number")
}
}
bint, ok := b.(int64)
if !ok {
return info.Error("addition argument for an IP address requires an integer argument")
}
return IPAdd(ip, bint).String(), info, true
ip = IPAdd(ip, bint)
if cidr != nil {
if !cidr.Contains(ip) {
return info.Error("resulting ip address not in CIDR range")
}
cidr.IP = ip
return cidr.String(), info, true
}
return ip.String(), info, true
}
a, b, err := NumberOperands(a, b)
if err != nil {
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
36 changes: 17 additions & 19 deletions dynaml/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,37 @@ import (

type Function func(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool)

type Registry interface {
type Functions interface {
RegisterFunction(name string, f Function)
LookupFunction(name string) Function
}
type registry struct {

type functionRegistry struct {
functions map[string]Function
}

func NewRegistry() Registry {
return &registry{map[string]Function{}}
func NewFunctions() Functions {
return &functionRegistry{map[string]Function{}}
}

func (r *registry) RegisterFunction(name string, f Function) {
func (r *functionRegistry) RegisterFunction(name string, f Function) {
r.functions[name] = f
}

func (r *registry) LookupFunction(name string) Function {
return r.functions[name]
func (r *functionRegistry) LookupFunction(name string) Function {
f := r.functions[name]
if f != nil || r == function_registry {
return f
}
return function_registry.(*functionRegistry).functions[name]
}

var functions = NewRegistry()

func RegisterFunction(name string, f Function) {
functions.RegisterFunction(name, f)
function_registry.RegisterFunction(name, f)
}

var function_registry = NewFunctions()

type NameArgument struct {
Name string
Expression
Expand Down Expand Up @@ -324,14 +329,7 @@ func (e CallExpr) Evaluate(binding Binding, locally bool) (interface{}, Evaluati
}

default:
var f Function
ext := binding.GetState().GetFunctions()
if ext != nil {
f = ext.LookupFunction(funcName)
}
if f == nil {
f = functions.LookupFunction(funcName)
}
f := binding.GetState().GetRegistry().LookupFunction(funcName)
if f == nil {
return info.Error("unknown function '%s'", funcName)
}
Expand All @@ -341,7 +339,7 @@ func (e CallExpr) Evaluate(binding Binding, locally bool) (interface{}, Evaluati
if cleaned {
info.Cleanup()
}
if ok && (!resolved || isExpression(result)) {
if ok && (!resolved || IsExpression(result)) {
return e, sub.Join(info), true
}
return result, sub.Join(info), ok
Expand Down
Loading

0 comments on commit 364c4d4

Please sign in to comment.