本文整理匯總了Golang中github.com/hashicorp/hcl/hcl.Object.Elem方法的典型用法代碼示例。如果您正苦於以下問題:Golang Object.Elem方法的具體用法?Golang Object.Elem怎麽用?Golang Object.Elem使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hashicorp/hcl/hcl.Object
的用法示例。
在下文中一共展示了Object.Elem方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: parseUpdate
func parseUpdate(result *structs.UpdateStrategy, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'update' block allowed per job")
}
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
for _, key := range []string{"stagger", "Stagger"} {
if raw, ok := m[key]; ok {
switch v := raw.(type) {
case string:
dur, err := time.ParseDuration(v)
if err != nil {
return fmt.Errorf("invalid stagger time '%s'", raw)
}
m[key] = dur
case int:
m[key] = time.Duration(v) * time.Second
default:
return fmt.Errorf("invalid type for stagger time '%s'",
raw)
}
}
}
if err := mapstructure.WeakDecode(m, result); err != nil {
return err
}
}
return nil
}
示例2: parseUpdate
func parseUpdate(result *structs.UpdateStrategy, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'update' block allowed per job")
}
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
for _, key := range []string{"stagger", "Stagger"} {
if raw, ok := m[key]; ok {
staggerTime, err := toDuration(raw)
if err != nil {
return fmt.Errorf("Invalid stagger time: %v", err)
}
m[key] = staggerTime
}
}
if err := mapstructure.WeakDecode(m, result); err != nil {
return err
}
}
return nil
}
示例3: parseDetect
func parseDetect(result *Config, obj *hclobj.Object) error {
// 從map中獲取所有實際對象的key值
objects := make([]*hclobj.Object, 0, 2)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
objects = append(objects, o2)
}
}
if len(objects) == 0 {
return nil
}
// 檢查每個對象,返回實際結果
collection := make([]*Detector, 0, len(objects))
for _, o := range objects {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
var d Detector
if err := mapstructure.WeakDecode(m, &d); err != nil {
return fmt.Errorf("解析detector錯誤 '%s' : %s", o.Key, err)
}
d.Type = o.Key
collection = append(collection, &d)
}
result.Detectors = collection
return nil
}
示例4: parseDetect
func parseDetect(result *Config, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make([]*hclobj.Object, 0, 2)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
objects = append(objects, o2)
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Detector, 0, len(objects))
for _, o := range objects {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
var d Detector
if err := mapstructure.WeakDecode(m, &d); err != nil {
return fmt.Errorf(
"error parsing detector '%s': %s", o.Key, err)
}
d.Type = o.Key
collection = append(collection, &d)
}
result.Detectors = collection
return nil
}
示例5: parseConstraints
func parseConstraints(result *[]*structs.Constraint, obj *hclobj.Object) error {
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
m["LTarget"] = m["attribute"]
m["RTarget"] = m["value"]
m["Operand"] = m["operator"]
// Default constraint to being hard
if _, ok := m["hard"]; !ok {
m["hard"] = true
}
// Build the constraint
var c structs.Constraint
if err := mapstructure.WeakDecode(m, &c); err != nil {
return err
}
if c.Operand == "" {
c.Operand = "="
}
*result = append(*result, &c)
}
return nil
}
示例6: parseRawModules
func parseRawModules(objModules *hclObj.Object, errors *multierror.Error) (output []modules.ModuleSpec) {
//iterate over each module
for _, ms := range objModules.Elem(true) {
// lets build the mod params ...
rawParams := ms.Elem(true)
params := params.NewModuleParams()
for _, p := range rawParams {
switch p.Value.(type) {
case string:
params[p.Key] = p.Value
case int:
params[p.Key] = p.Value
case []*hclObj.Object:
propertyValues := make([]interface{}, 0)
for _, pv := range p.Elem(true) {
propertyValues = append(propertyValues, pv.Value)
}
params[p.Key] = propertyValues
}
}
// build the module
module := modprobe.Find(ms.Key, params)
output = append(output, module)
}
return output
}
示例7: parseInfra
func parseInfra(result *File, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make(map[string]*hclobj.Object)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
if _, ok := objects[o2.Key]; ok {
return fmt.Errorf(
"infrastructure '%s' defined more than once",
o2.Key)
}
objects[o2.Key] = o2
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Infrastructure, 0, len(objects))
for n, o := range objects {
// Check for invalid keys
valid := []string{"name", "type", "flavor", "foundation"}
if err := checkHCLKeys(o, valid); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"infrastructure '%s':", n))
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
var infra Infrastructure
if err := mapstructure.WeakDecode(m, &infra); err != nil {
return fmt.Errorf(
"error parsing infrastructure '%s': %s", n, err)
}
infra.Name = n
if infra.Type == "" {
infra.Type = infra.Name
}
// Parse the foundations if we have any
if o2 := o.Get("foundation", false); o != nil {
if err := parseFoundations(&infra, o2); err != nil {
return fmt.Errorf("error parsing 'foundation': %s", err)
}
}
collection = append(collection, &infra)
}
result.Infrastructure = collection
return nil
}
示例8: parseConstraints
func parseConstraints(result *[]*structs.Constraint, obj *hclobj.Object) error {
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
m["LTarget"] = m["attribute"]
m["RTarget"] = m["value"]
m["Operand"] = m["operator"]
// Default constraint to being hard
if _, ok := m["hard"]; !ok {
m["hard"] = true
}
// If "version" is provided, set the operand
// to "version" and the value to the "RTarget"
if constraint, ok := m[structs.ConstraintVersion]; ok {
m["Operand"] = structs.ConstraintVersion
m["RTarget"] = constraint
}
// If "regexp" is provided, set the operand
// to "regexp" and the value to the "RTarget"
if constraint, ok := m[structs.ConstraintRegex]; ok {
m["Operand"] = structs.ConstraintRegex
m["RTarget"] = constraint
}
if value, ok := m[structs.ConstraintDistinctHosts]; ok {
enabled, err := strconv.ParseBool(value.(string))
if err != nil {
return err
}
// If it is not enabled, skip the constraint.
if !enabled {
continue
}
m["Operand"] = structs.ConstraintDistinctHosts
}
// Build the constraint
var c structs.Constraint
if err := mapstructure.WeakDecode(m, &c); err != nil {
return err
}
if c.Operand == "" {
c.Operand = "="
}
*result = append(*result, &c)
}
return nil
}
示例9: parseResources
func parseResources(result *structs.Resources, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'resource' block allowed per task")
}
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
delete(m, "network")
if err := mapstructure.WeakDecode(m, result); err != nil {
return err
}
// Parse the network resources
if o := o.Get("network", false); o != nil {
if o.Len() > 1 {
return fmt.Errorf("only one 'network' resource allowed")
}
var r structs.NetworkResource
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
if err := mapstructure.WeakDecode(m, &r); err != nil {
return err
}
// Keep track of labels we've already seen so we can ensure there
// are no collisions when we turn them into environment variables.
// lowercase:NomalCase so we can get the first for the error message
seenLabel := map[string]string{}
for _, label := range r.DynamicPorts {
if !reDynamicPorts.MatchString(label) {
return errDynamicPorts
}
first, seen := seenLabel[strings.ToLower(label)]
if seen {
return fmt.Errorf("Found a port label collision: `%s` overlaps with previous `%s`", label, first)
} else {
seenLabel[strings.ToLower(label)] = label
}
}
result.Networks = []*structs.NetworkResource{&r}
}
}
return nil
}
示例10: loadProvidersHcl
// LoadProvidersHcl recurses into the given HCL object and turns
// it into a mapping of provider configs.
func loadProvidersHcl(os *hclobj.Object) ([]*ProviderConfig, error) {
var objects []*hclobj.Object
// Iterate over all the "provider" blocks and get the keys along with
// their raw configuration objects. We'll parse those later.
for _, o1 := range os.Elem(false) {
for _, o2 := range o1.Elem(true) {
objects = append(objects, o2)
}
}
if len(objects) == 0 {
return nil, nil
}
// Go through each object and turn it into an actual result.
result := make([]*ProviderConfig, 0, len(objects))
for _, o := range objects {
var config map[string]interface{}
if err := hcl.DecodeObject(&config, o); 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",
o.Key,
err)
}
// If we have an alias field, then add those in
var alias string
if a := o.Get("alias", false); a != nil {
err := hcl.DecodeObject(&alias, a)
if err != nil {
return nil, fmt.Errorf(
"Error reading alias for provider[%s]: %s",
o.Key,
err)
}
}
result = append(result, &ProviderConfig{
Name: o.Key,
Alias: alias,
RawConfig: rawConfig,
})
}
return result, nil
}
示例11: checkHCLKeys
func checkHCLKeys(obj *hclobj.Object, valid []string) error {
validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}
var result error
for _, o := range obj.Elem(true) {
if _, ok := validMap[o.Key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invald key: %s", o.Key))
}
}
return result
}
示例12: decodeSlice
func (d *decoder) decodeSlice(name string, o *hcl.Object, result reflect.Value) error {
// If we have an interface, then we can address the interface,
// but not the slice itself, so get the element but set the interface
set := result
if result.Kind() == reflect.Interface {
result = result.Elem()
}
// Create the slice if it isn't nil
resultType := result.Type()
resultElemType := resultType.Elem()
if result.IsNil() {
resultSliceType := reflect.SliceOf(resultElemType)
result = reflect.MakeSlice(
resultSliceType, 0, 0)
}
// Determine how we're doing this
expand := true
switch o.Type {
case hcl.ValueTypeObject:
expand = false
default:
// Array or anything else: we expand values and take it all
}
i := 0
for _, o := range o.Elem(expand) {
fieldName := fmt.Sprintf("%s[%d]", name, i)
// Decode
val := reflect.Indirect(reflect.New(resultElemType))
if err := d.decode(fieldName, o, val); err != nil {
return err
}
// Append it onto the slice
result = reflect.Append(result, val)
i += 1
}
set.Set(result)
return nil
}
示例13: loadListeners
func loadListeners(os *hclobj.Object) ([]*Listener, error) {
var allNames []*hclobj.Object
// Really confusing iteration. The key is the false/true parameter
// of whether we're expanding or not. We first iterate over all
// the "listeners"
for _, o1 := range os.Elem(false) {
// Iterate expand to get the list of types
for _, o2 := range o1.Elem(true) {
switch o2.Type {
case hclobj.ValueTypeList:
// This switch is for JSON, to allow them to do this:
//
// "tcp": [{ ... }, { ... }]
//
// To configure multiple listeners of the same type.
for _, o3 := range o2.Elem(true) {
o3.Key = o2.Key
allNames = append(allNames, o3)
}
case hclobj.ValueTypeObject:
// This is for the standard `listener "tcp" { ... }` syntax
allNames = append(allNames, o2)
}
}
}
if len(allNames) == 0 {
return nil, nil
}
// Now go over all the types and their children in order to get
// all of the actual resources.
result := make([]*Listener, 0, len(allNames))
for _, obj := range allNames {
k := obj.Key
var config map[string]string
if err := hcl.DecodeObject(&config, obj); err != nil {
return nil, fmt.Errorf(
"Error reading config for %s: %s",
k,
err)
}
result = append(result, &Listener{
Type: k,
Config: config,
})
}
return result, nil
}
示例14: loadProvidersHcl
// LoadProvidersHcl recurses into the given HCL object and turns
// it into a mapping of provider configs.
func loadProvidersHcl(os *hclobj.Object) ([]*ProviderConfig, error) {
objects := make(map[string]*hclobj.Object)
// Iterate over all the "provider" blocks and get the keys along with
// their raw configuration objects. We'll parse those later.
for _, o1 := range os.Elem(false) {
for _, o2 := range o1.Elem(true) {
objects[o2.Key] = o2
}
}
if len(objects) == 0 {
return nil, nil
}
// Go through each object and turn it into an actual result.
result := make([]*ProviderConfig, 0, len(objects))
for n, o := range objects {
var config map[string]interface{}
if err := hcl.DecodeObject(&config, o); err != nil {
return nil, err
}
rawConfig, err := NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"Error reading config for provider config %s: %s",
n,
err)
}
result = append(result, &ProviderConfig{
Name: n,
RawConfig: rawConfig,
})
}
return result, nil
}
示例15: parseImport
func parseImport(result *File, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make([]*hclobj.Object, 0, 3)
set := make(map[string]struct{})
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
if _, ok := set[o2.Key]; ok {
return fmt.Errorf(
"imported '%s' more than once",
o2.Key)
}
objects = append(objects, o2)
set[o2.Key] = struct{}{}
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Import, 0, len(objects))
for _, o := range objects {
// Check for invalid keys
if err := checkHCLKeys(o, nil); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"import '%s':", o.Key))
}
collection = append(collection, &Import{
Source: o.Key,
})
}
result.Imports = collection
return nil
}