本文整理匯總了Golang中github.com/hashicorp/hcl/hcl/ast.ObjectList.Children方法的典型用法代碼示例。如果您正苦於以下問題:Golang ObjectList.Children方法的具體用法?Golang ObjectList.Children怎麽用?Golang ObjectList.Children使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hashicorp/hcl/hcl/ast.ObjectList
的用法示例。
在下文中一共展示了ObjectList.Children方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: parseFoundations
func parseFoundations(result *Infrastructure, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Foundation, 0, len(list.Items))
seen := make(map[string]struct{})
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
// Make sure we haven't already found this
if _, ok := seen[n]; ok {
return fmt.Errorf("foundation '%s' defined more than once", n)
}
seen[n] = struct{}{}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var f Foundation
f.Name = n
f.Config = m
collection = append(collection, &f)
}
// Set the results
result.Foundations = collection
return nil
}
示例2: loadOutputsHcl
// LoadOutputsHcl recurses into the given HCL object and turns
// it into a mapping of outputs.
func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
list = list.Children()
if len(list.Items) == 0 {
return nil, nil
}
// Go through each object and turn it into an actual result.
result := make([]*Output, 0, len(list.Items))
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, err
}
rawConfig, err := NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"Error reading config for output %s: %s",
n,
err)
}
result = append(result, &Output{
Name: n,
RawConfig: rawConfig,
})
}
return result, nil
}
示例3: parseGroups
func (j *Job) parseGroups(list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
seen := make(map[string]struct{})
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
// Make sure we haven't already found this
if _, ok := seen[n]; ok {
return fmt.Errorf("group '%s' defined more than once", n)
}
seen[n] = struct{}{}
// We need this later
obj, ok := item.Val.(*ast.ObjectType)
if !ok {
return fmt.Errorf("group '%s': should be an object", n)
}
// Build the group with the basic decode
tg := &TaskGroup{}
tg.Name = TaskGroupName(n)
if err := tg.parse(obj); err != nil {
return maskAny(err)
}
j.Groups = append(j.Groups, tg)
}
return nil
}
示例4: parseImport
func parseImport(result *File, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Import, 0, len(list.Items))
seen := make(map[string]struct{})
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
// Make sure we haven't already found this import
if _, ok := seen[key]; ok {
return fmt.Errorf("import '%s' defined more than once", key)
}
seen[key] = struct{}{}
// Check for invalid keys
if err := checkHCLKeys(item.Val, nil); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"import '%s':", key))
}
collection = append(collection, &Import{
Source: key,
})
}
result.Imports = collection
return nil
}
示例5: loadConfigOutputs
func loadConfigOutputs(hclConfig *ast.ObjectList) ([]*tfcfg.Output, error) {
hclConfig = hclConfig.Children()
result := make([]*tfcfg.Output, 0, len(hclConfig.Items))
if len(hclConfig.Items) == 0 {
return result, nil
}
for _, item := range hclConfig.Items {
n := item.Keys[0].Token.Value().(string)
if _, ok := item.Val.(*ast.ObjectType); !ok {
return nil, fmt.Errorf("output '%s': should be a block", n)
}
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, err
}
rawConfig, err := tfcfg.NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"error reading output config %s: %s", n, err,
)
}
result = append(result, &tfcfg.Output{
Name: n,
RawConfig: rawConfig,
})
}
return result, nil
}
示例6: parseTasks
// parse a list of tasks
func (tasks *parseTaskList) parseTasks(list *ast.ObjectList, anonymousGroup bool) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
// Get all the maps of keys to the actual object
seen := make(map[string]struct{})
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
if _, ok := seen[n]; ok {
return fmt.Errorf("task '%s' defined more than once", n)
}
seen[n] = struct{}{}
obj, ok := item.Val.(*ast.ObjectType)
if !ok {
return fmt.Errorf("task '%s': should be an object", n)
}
t := &parseTask{}
t.Name = TaskName(n)
if err := t.parse(obj, anonymousGroup); err != nil {
return maskAny(err)
}
*tasks = append(*tasks, t)
}
return nil
}
示例7: loadOutputsHcl
// LoadOutputsHcl recurses into the given HCL object and turns
// it into a mapping of outputs.
func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
list = list.Children()
if len(list.Items) == 0 {
return nil, fmt.Errorf(
"'output' must be followed by exactly one string: a name")
}
// Go through each object and turn it into an actual result.
result := make([]*Output, 0, len(list.Items))
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("output '%s': should be an object", n)
}
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, err
}
// Delete special keys
delete(config, "depends_on")
rawConfig, err := NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"Error reading config for output %s: %s",
n,
err)
}
// If we have depends fields, then add those in
var dependsOn []string
if o := listVal.Filter("depends_on"); len(o.Items) > 0 {
err := hcl.DecodeObject(&dependsOn, o.Items[0].Val)
if err != nil {
return nil, fmt.Errorf(
"Error reading depends_on for output %q: %s",
n,
err)
}
}
result = append(result, &Output{
Name: n,
RawConfig: rawConfig,
DependsOn: dependsOn,
})
}
return result, nil
}
示例8: loadConfigVariables
func loadConfigVariables(hclConfig *ast.ObjectList) ([]*tfcfg.Variable, error) {
hclConfig = hclConfig.Children()
result := make([]*tfcfg.Variable, 0, len(hclConfig.Items))
if len(hclConfig.Items) == 0 {
return result, nil
}
for _, item := range hclConfig.Items {
n := item.Keys[0].Token.Value().(string)
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("variable '%s': should be a block", n)
}
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, err
}
variable := &tfcfg.Variable{
Name: n,
}
if a := listVal.Filter("default"); len(a.Items) > 0 {
err := hcl.DecodeObject(&variable.Default, a.Items[0].Val)
if err != nil {
return nil, fmt.Errorf(
"error reading variable %s default: %s", n, err,
)
}
}
if a := listVal.Filter("description"); len(a.Items) > 0 {
err := hcl.DecodeObject(&variable.Description, a.Items[0].Val)
if err != nil {
return nil, fmt.Errorf(
"error reading variable %s description: %s", n, err,
)
}
}
if a := listVal.Filter("type"); len(a.Items) > 0 {
err := hcl.DecodeObject(&variable.DeclaredType, a.Items[0].Val)
if err != nil {
return nil, fmt.Errorf(
"error reading variable %s type: %s", n, err,
)
}
}
result = append(result, variable)
}
return result, nil
}
示例9: loadProvidersHcl
// LoadProvidersHcl recurses into the given HCL object and turns
// it into a mapping of provider configs.
func loadProvidersHcl(list *ast.ObjectList) ([]*ProviderConfig, error) {
list = list.Children()
if len(list.Items) == 0 {
return nil, nil
}
// Go through each object and turn it into an actual result.
result := make([]*ProviderConfig, 0, len(list.Items))
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("module '%s': should be an object", n)
}
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, err
}
delete(config, "alias")
rawConfig, err := NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"Error reading config for provider config %s: %s",
n,
err)
}
// If we have an alias field, then add those in
var alias string
if a := listVal.Filter("alias"); len(a.Items) > 0 {
err := hcl.DecodeObject(&alias, a.Items[0].Val)
if err != nil {
return nil, fmt.Errorf(
"Error reading alias for provider[%s]: %s",
n,
err)
}
}
result = append(result, &ProviderConfig{
Name: n,
Alias: alias,
RawConfig: rawConfig,
})
}
return result, nil
}
示例10: loadConfigProviders
func loadConfigProviders(hclConfig *ast.ObjectList) ([]*tfcfg.ProviderConfig, error) {
hclConfig = hclConfig.Children()
result := make([]*tfcfg.ProviderConfig, 0, len(hclConfig.Items))
if len(hclConfig.Items) == 0 {
return result, nil
}
for _, item := range hclConfig.Items {
n := item.Keys[0].Token.Value().(string)
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("provider '%s': should be a block", n)
}
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, err
}
delete(config, "alias")
rawConfig, err := tfcfg.NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"error reading provider config %s: %s", n, err,
)
}
// If we have an alias, add it in
var alias string
if a := listVal.Filter("alias"); len(a.Items) > 0 {
err := hcl.DecodeObject(&alias, a.Items[0].Val)
if err != nil {
return nil, fmt.Errorf(
"error reading provider %s alias: %s", n, err,
)
}
}
result = append(result, &tfcfg.ProviderConfig{
Name: n,
Alias: alias,
RawConfig: rawConfig,
})
}
return result, nil
}
示例11: loadConfigModules
func loadConfigModules(hclConfig *ast.ObjectList) ([]*tfcfg.Module, error) {
hclConfig = hclConfig.Children()
result := make([]*tfcfg.Module, 0, len(hclConfig.Items))
if len(hclConfig.Items) == 0 {
return result, nil
}
for _, item := range hclConfig.Items {
n := item.Keys[0].Token.Value().(string)
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("module '%s': should be a block", n)
}
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, err
}
delete(config, "source")
rawConfig, err := tfcfg.NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"error reading module config %s: %s", n, err,
)
}
var source string
if a := listVal.Filter("source"); len(a.Items) > 0 {
err := hcl.DecodeObject(&source, a.Items[0].Val)
if err != nil {
return nil, fmt.Errorf(
"error reading module %s source: %s", n, err,
)
}
}
result = append(result, &tfcfg.Module{
Name: n,
Source: source,
RawConfig: rawConfig,
})
}
return result, nil
}
示例12: parse
// Parse a Cluster
func (c *Cluster) parse(list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) != 1 {
return fmt.Errorf("only one 'cluster' block allowed")
}
// Get our cluster object
obj := list.Items[0]
// Decode the object
excludeList := []string{
"default-options",
"docker",
"fleet",
"quark",
}
if err := Decode(obj.Val, excludeList, nil, c); err != nil {
return maskAny(err)
}
c.Stack = obj.Keys[0].Token.Value().(string)
// Value should be an object
var listVal *ast.ObjectList
if ot, ok := obj.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return errgo.Newf("cluster '%s' value: should be an object", c.Stack)
}
// If we have quark object, parse it
if o := listVal.Filter("quark"); len(o.Items) > 0 {
for _, o := range o.Elem().Items {
if obj, ok := o.Val.(*ast.ObjectType); ok {
if err := c.QuarkOptions.parse(obj, *c); err != nil {
return maskAny(err)
}
} else {
return maskAny(errgo.WithCausef(nil, ValidationError, "docker of cluster '%s' is not an object", c.Stack))
}
}
}
return nil
}
示例13: loadConfigTargets
func loadConfigTargets(hclConfig *ast.ObjectList) ([]*TargetConfig, error) {
hclConfig = hclConfig.Children()
result := make([]*TargetConfig, 0, len(hclConfig.Items))
if len(hclConfig.Items) == 0 {
return result, nil
}
for _, item := range hclConfig.Items {
n := item.Keys[0].Token.Value().(string)
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("target '%s': should be a block", n)
}
target := &TargetConfig{
Name: n,
}
var err error
target.Providers, err = loadConfigProviders(listVal.Filter("provider"))
if err != nil {
return nil, err
}
target.Modules, err = loadConfigModules(listVal.Filter("module"))
if err != nil {
return nil, err
}
target.Outputs, err = loadConfigOutputs(listVal.Filter("output"))
if err != nil {
return nil, err
}
result = append(result, target)
}
return result, nil
}
示例14: loadManagedResourcesHcl
// Given a handle to a HCL object, this recurses into the structure
// and pulls out a list of managed resources.
//
// The resulting resources may not be unique, but each resource
// represents exactly one "resource" block in the HCL configuration.
// We leave it up to another pass to merge them together.
func loadManagedResourcesHcl(list *ast.ObjectList) ([]*Resource, error) {
list = list.Children()
if len(list.Items) == 0 {
return nil, nil
}
// Where all the results will go
var result []*Resource
// Now go over all the types and their children in order to get
// all of the actual resources.
for _, item := range list.Items {
// GH-4385: We detect a pure provisioner resource and give the user
// an error about how to do it cleanly.
if len(item.Keys) == 4 && item.Keys[2].Token.Value().(string) == "provisioner" {
return nil, fmt.Errorf(
"position %s: provisioners in a resource should be wrapped in a list\n\n"+
"Example: \"provisioner\": [ { \"local-exec\": ... } ]",
item.Pos())
}
// HCL special case: if we're parsing JSON then directly nested
// items will show up as additional "keys". We need to unwrap them
// since we expect only two keys. Example:
//
// { "foo": { "bar": { "baz": {} } } }
//
// Will show up with Keys being: []string{"foo", "bar", "baz"}
// when we really just want the first two. To fix this we unwrap
// them into the right value.
if len(item.Keys) > 2 && item.Keys[0].Token.JSON {
for len(item.Keys) > 2 {
// Pop off the last key
n := len(item.Keys)
key := item.Keys[n-1]
item.Keys[n-1] = nil
item.Keys = item.Keys[:n-1]
// Wrap our value in a list
item.Val = &ast.ObjectType{
List: &ast.ObjectList{
Items: []*ast.ObjectItem{
&ast.ObjectItem{
Keys: []*ast.ObjectKey{key},
Val: item.Val,
},
},
},
}
}
}
if len(item.Keys) != 2 {
return nil, fmt.Errorf(
"position %s: resource must be followed by exactly two strings, a type and a name",
item.Pos())
}
t := item.Keys[0].Token.Value().(string)
k := item.Keys[1].Token.Value().(string)
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("resources %s[%s]: should be an object", t, k)
}
var config map[string]interface{}
if err := hcl.DecodeObject(&config, item.Val); err != nil {
return nil, fmt.Errorf(
"Error reading config for %s[%s]: %s",
t,
k,
err)
}
// Remove the fields we handle specially
delete(config, "connection")
delete(config, "count")
delete(config, "depends_on")
delete(config, "provisioner")
delete(config, "provider")
delete(config, "lifecycle")
rawConfig, err := NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"Error reading config for %s[%s]: %s",
t,
k,
err)
}
//.........這裏部分代碼省略.........
示例15: parse
// Parse a Cluster
func (c *Cluster) parse(list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) != 1 {
return fmt.Errorf("only one 'cluster' block allowed")
}
// Get our cluster object
obj := list.Items[0]
// Decode the object
excludeList := []string{
"default-options",
"docker",
"fleet",
"quark",
}
if err := hclutil.Decode(obj.Val, excludeList, nil, c); err != nil {
return maskAny(err)
}
c.Stack = obj.Keys[0].Token.Value().(string)
// Value should be an object
var listVal *ast.ObjectList
if ot, ok := obj.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return errgo.Newf("cluster '%s' value: should be an object", c.Stack)
}
// If we have docker object, parse it
if o := listVal.Filter("docker"); len(o.Items) > 0 {
for _, o := range o.Elem().Items {
if obj, ok := o.Val.(*ast.ObjectType); ok {
if err := c.DockerOptions.parse(obj, *c); err != nil {
return maskAny(err)
}
} else {
return maskAny(errgo.WithCausef(nil, ValidationError, "docker of cluster '%s' is not an object", c.Stack))
}
}
}
// If we have fleet object, parse it
if o := listVal.Filter("fleet"); len(o.Items) > 0 {
for _, o := range o.Elem().Items {
if obj, ok := o.Val.(*ast.ObjectType); ok {
if err := c.FleetOptions.parse(obj, *c); err != nil {
return maskAny(err)
}
} else {
return maskAny(errgo.WithCausef(nil, ValidationError, "fleet of cluster '%s' is not an object", c.Stack))
}
}
}
// Parse default-options
if o := listVal.Filter("default-options"); len(o.Items) > 0 {
for _, o := range o.Elem().Items {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o.Val); err != nil {
return maskAny(err)
}
// Merge key/value pairs into myself
for k, v := range m {
c.DefaultOptions.SetKeyValue(k, v)
}
}
}
return nil
}