本文整理汇总了Golang中condense/template.Rules类的典型用法代码示例。如果您正苦于以下问题:Golang Rules类的具体用法?Golang Rules怎么用?Golang Rules使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Rules类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestMakeFnIncludeFile_BasicRule
func TestMakeFnIncludeFile_BasicRule(t *testing.T) {
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
if key == "content" {
return key, interface{}("replaced")
}
return key, node
})
fs := mapfs.New(map[string]string{"a": "{\"content\": 1}"})
fnIncludeFile := MakeFnIncludeFile(fs, &templateRules)
input := interface{}(map[string]interface{}{
"Fn::IncludeFile": "/a",
})
expected := interface{}(map[string]interface{}{"content": "replaced"})
newKey, newNode := fnIncludeFile([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnIncludeFile modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected) {
t.Fatalf("FnIncludeFile did not return the expected result (%#v instead of %#v)", newNode, expected)
}
}
示例2: TestFnFor_BasicRule
func TestFnFor_BasicRule(t *testing.T) {
stack := deepstack.DeepStack{}
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
if key == 1 {
return key, interface{}("replaced")
}
return key, node
})
fnFor := MakeFnFor(&stack, &templateRules)
input := interface{}(map[string]interface{}{
"Fn::For": []interface{}{[]interface{}{"key", "value"}, []interface{}{"a", "b"}, "aTemplate"},
})
expected := []interface{}{"aTemplate", "replaced"}
newKey, newNode := fnFor([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnFor modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected) {
t.Fatalf("FnFor with did not return the expected result (%#v instead of %#v)", newNode, expected)
}
}
示例3: TestFnWith_BasicRule
func TestFnWith_BasicRule(t *testing.T) {
stack := deepstack.DeepStack{}
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
return key, interface{}(map[string]interface{}{"a": "one"})
})
fnWith := MakeFnWith(&stack, &templateRules)
input := interface{}(map[string]interface{}{
"Fn::With": []interface{}{map[string]interface{}{"a": "one"}, "aTemplate"},
})
expected := interface{}(map[string]interface{}{"a": "one"})
newKey, newNode := fnWith([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnWith modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected) {
t.Fatalf("FnWith with did not return the expected result (%#v instead of %#v)", newNode, expected)
}
}
示例4: TestFnFor_PreProcessArgs
func TestFnFor_PreProcessArgs(t *testing.T) {
stack := deepstack.DeepStack{}
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
aString, ok := node.(string)
if !ok {
return key, node
}
if aString == "skip" {
return true, nil
}
if aString == "refNames" {
return key, []interface{}{"key", "value"}
}
if aString == "values" {
return key, []interface{}{"a"}
}
if aString == "template" {
_, hasKey := stack.Get([]string{"value"})
if hasKey {
return key, "processed-template"
}
return key, "preprocessed-template"
}
return key, node
})
fnFor := MakeFnFor(&stack, &templateRules)
input := interface{}(map[string]interface{}{
"Fn::For": []interface{}{"skip", "refNames", "skip", "values", "skip", "template", "skip"},
})
expected := []interface{}{"processed-template"}
newKey, newNode := fnFor([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnFor modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected) {
t.Fatalf("FnFor with did not return the expected result (%#v instead of %#v)", newNode, expected)
}
}
示例5: TestFnGetAtt_ProcessBound
func TestFnGetAtt_ProcessBound(t *testing.T) {
stack := deepstack.DeepStack{}
stack.Push(fallbackmap.DeepMap(map[string]interface{}{
"FakeResource": map[string]interface{}{
"FakeProperty": map[string]interface{}{"FakeSub": "FakeValue"},
},
}))
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
if stringVal, ok := node.(string); ok && stringVal == "FakeValue" {
return key, interface{}("ProcessedFakeValue")
}
return key, node
})
inputs := []interface{}{
[]interface{}{"FakeResource", "FakeProperty.FakeSub"},
}
expected := []interface{}{
"ProcessedFakeValue",
}
for i, input := range inputs {
fnGetAtt := MakeFnGetAtt(&stack, &templateRules)
input := interface{}(map[string]interface{}{
"Fn::GetAtt": input,
})
newKey, newNode := fnGetAtt([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnGetAtt modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected[i]) {
t.Fatalf("FnGetAtt for %v did not return the expected result (%#v instead of %#v)", input, newNode, expected[i])
}
}
}
示例6: Test_Lazy_traversesTemplateRuleResults
func Test_Lazy_traversesTemplateRuleResults(t *testing.T) {
testRules := template.Rules{}
testRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
if key == "replaceThis" {
return key, map[string]interface{}{
"replacedDeep": "replacedDeepValue",
}
}
return key, node
})
lazy := NewLazyMap(fallbackmap.DeepMap(map[string]interface{}{
"a": "anA",
"b": interface{}(map[string]interface{}{
"innerA": "anInnerA",
"replaceThis": "this should be replaced",
}),
"replaceThis": "this should also be replaced",
}), &testRules)
for path, expected := range map[string]string{
"b.replaceThis.replacedDeep": "replacedDeepValue",
"replaceThis.replacedDeep": "replacedDeepValue",
} {
result, has_key := lazy.Get(strings.Split(path, "."))
if !has_key {
t.Fatalf("Get() of a rule-modified map did not claim to have a key for path %#v", path)
}
resultString, ok := result.(string)
if !ok {
t.Fatalf("Get() of a rule-modified map did not return the correct type of value for path %#v", path)
}
if resultString != expected {
t.Fatalf("Get() of a rule-modified map did not return the expected value '%s' (got '%s' instead) for path %#v", expected, resultString, path)
}
}
}
示例7: TestFnFor_StackWithArray
func TestFnFor_StackWithArray(t *testing.T) {
stack := deepstack.DeepStack{}
stack.Push(fallbackmap.DeepMap(map[string]interface{}{"outer": "outerValue", "masked": "outerMasked"}))
testRefNames := []interface{}{
[]interface{}{"key", "masked"},
[]interface{}{nil, "masked"},
[]interface{}{"key", nil},
[]interface{}{nil, nil},
[]interface{}{"masked"},
"masked",
}
expected := []interface{}{
[]interface{}{
map[string]interface{}{
"outer": []interface{}{"outerValue", true},
"masked": []interface{}{"innerMasking", true},
"key": []interface{}{float64(0), true},
},
},
[]interface{}{
map[string]interface{}{
"outer": []interface{}{"outerValue", true},
"masked": []interface{}{"innerMasking", true},
},
},
[]interface{}{
map[string]interface{}{
"outer": []interface{}{"outerValue", true},
"masked": []interface{}{"outerMasked", true},
"key": []interface{}{float64(0), true},
},
},
[]interface{}{
map[string]interface{}{
"outer": []interface{}{"outerValue", true},
"masked": []interface{}{"outerMasked", true},
},
},
[]interface{}{
map[string]interface{}{
"outer": []interface{}{"outerValue", true},
"masked": []interface{}{"innerMasking", true},
},
},
[]interface{}{
map[string]interface{}{
"outer": []interface{}{"outerValue", true},
"masked": []interface{}{"innerMasking", true},
},
},
}
for i, refNames := range testRefNames {
input := interface{}(map[string]interface{}{
"Fn::For": []interface{}{
refNames,
[]interface{}{"innerMasking"},
"aTemplate",
},
})
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
if stringVal, ok := node.(string); !ok || stringVal != "aTemplate" {
return key, node
}
generated := map[string]interface{}{}
for binding, _ := range expected[i].([]interface{})[0].(map[string]interface{}) {
value, has_key := stack.Get([]string{binding})
generated[binding] = []interface{}{value, has_key}
}
return key, generated
})
fnFor := MakeFnFor(&stack, &templateRules)
newKey, newNode := fnFor([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnFor modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected[i]) {
t.Fatalf("FnFor did not have the correct stack values with refNames %v during templateRule (%#v instead of %#v)",
refNames,
newNode,
expected[i],
)
}
}
}
示例8: TestFnWith_StackWithArray
func TestFnWith_StackWithArray(t *testing.T) {
stack := deepstack.DeepStack{}
stack.Push(fallbackmap.DeepMap(map[string]interface{}{"outer": "outer-value", "masked": "masked-value"}))
input := interface{}(map[string]interface{}{
"Fn::With": []interface{}{
map[string]interface{}{
"masked": "masking-value",
"inner": "inner-value",
},
map[string]interface{}{
"outer": "replace-with-outer",
"masked": "replace-with-masked",
"inner": "replace-with-inner",
"untouched": "stay-the-same",
},
},
})
expected := interface{}(map[string]interface{}{
"outer": "outer-value",
"masked": "masking-value",
"inner": "inner-value",
"untouched": "stay-the-same",
})
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
newNode := make(map[string]interface{})
if nodeMap, ok := node.(map[string]interface{}); ok {
if _, ok := node.(map[string]interface{})["untouched"]; ok {
for key, value := range nodeMap {
newValue, hasKey := stack.Get([]string{key})
if hasKey {
newNode[key] = newValue
} else {
newNode[key] = value
}
}
return key, newNode
}
}
return key, node
})
fnWith := MakeFnWith(&stack, &templateRules)
newKey, newNode := fnWith([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnWith modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected) {
t.Fatalf("FnWith did not have the correct stack values during templateRule (%#v instead of %#v)",
newNode,
expected,
)
}
}
示例9: main
func main() {
templateRules := template.Rules{}
inputParameters := NewInputsFlag(&templateRules)
var templateFilename string
var outputWhat OutputWhatFlag
flag.StringVar(&templateFilename,
"template", "-",
"CloudFormation Template to process")
flag.Var(&inputParameters,
"parameters",
"File to use of input parameters (can be specified multiple times)")
flag.Var(&outputWhat,
"output",
"What to output after processing the Template")
flag.Parse()
var jsonStream io.Reader
var err error
if templateFilename == "-" {
jsonStream = os.Stdin
} else if jsonStream, err = os.Open(templateFilename); err != nil {
panic(err)
}
dec := json.NewDecoder(jsonStream)
t := make(map[string]interface{})
if err := dec.Decode(&t); err != nil {
panic(err)
}
sources := fallbackmap.FallbackMap{}
stack := deepstack.DeepStack{}
sources.Attach(inputParameters.Get())
sources.Attach(deepalias.DeepAlias{&stack})
sources.Attach(deepcloudformationoutputs.NewDeepCloudFormationOutputs("eu-west-1"))
sources.Attach(deepcloudformationresources.NewDeepCloudFormationResources("eu-west-1"))
stack.Push(&sources)
templateRules.AttachEarly(rules.ExcludeComments)
templateRules.AttachEarly(rules.MakeFnFor(&stack, &templateRules))
templateRules.AttachEarly(rules.MakeFnWith(&stack, &templateRules))
templateRules.Attach(rules.FnAdd)
templateRules.Attach(rules.FnIf)
templateRules.Attach(rules.FnAnd)
templateRules.Attach(rules.FnOr)
templateRules.Attach(rules.FnNot)
templateRules.Attach(rules.FnEquals)
templateRules.Attach(rules.FnConcat)
templateRules.Attach(rules.FnFromEntries)
templateRules.Attach(rules.FnHasKey)
templateRules.Attach(rules.FnJoin)
templateRules.Attach(rules.FnKeys)
templateRules.Attach(rules.FnLength)
templateRules.Attach(rules.FnMerge)
templateRules.Attach(rules.FnMergeDeep)
templateRules.Attach(rules.FnMod)
templateRules.Attach(rules.FnSplit)
templateRules.Attach(rules.FnToEntries)
templateRules.Attach(rules.FnUnique)
templateRules.Attach(rules.MakeFnGetAtt(&stack, &templateRules))
templateRules.Attach(rules.MakeRef(&stack, &templateRules))
templateRules.Attach(rules.MakeFnHasRef(&stack))
templateRules.Attach(rules.MakeFnIncludeFile(vfs.OS("/"), &templateRules))
templateRules.Attach(rules.MakeFnIncludeFileRaw(vfs.OS("/")))
templateRules.Attach(rules.ReduceConditions)
// First Pass (to collect Parameter names)
processed := template.Process(t, &templateRules)
parameterRefs := map[string]interface{}{}
if processedMap, ok := processed.(map[string]interface{}); ok {
if processedParameters, ok := processedMap["Parameters"]; ok {
if processedParametersMap, ok := processedParameters.(map[string]interface{}); ok {
for parameterName, _ := range processedParametersMap {
parameterRefs[parameterName] = map[string]interface{}{
"ParamRef": parameterName,
}
}
}
}
}
stack.Push(fallbackmap.DeepMap(parameterRefs))
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
if nodeMap, ok := node.(map[string]interface{}); !ok || len(nodeMap) != 1 {
return key, node //passthru
}
//.........这里部分代码省略.........