本文整理匯總了Golang中github.com/hashicorp/hcl/hcl.Object類的典型用法代碼示例。如果您正苦於以下問題:Golang Object類的具體用法?Golang Object怎麽用?Golang Object使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Object類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: parseProject
func parseProject(result *File, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'project' block allowed")
}
// Check for invalid keys
valid := []string{"name", "infrastructure"}
if err := checkHCLKeys(obj, valid); err != nil {
return multierror.Prefix(err, "project:")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, obj); err != nil {
return err
}
// Parse the project
var proj Project
result.Project = &proj
if err := mapstructure.WeakDecode(m, &proj); 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 {
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
}
示例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: 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
}
示例5: 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
}
示例6: parseRestartPolicy
func parseRestartPolicy(result *structs.RestartPolicy, obj *hclobj.Object) error {
var restartHclObj *hclobj.Object
var m map[string]interface{}
if restartHclObj = obj.Get("restart", false); restartHclObj == nil {
return nil
}
if err := hcl.DecodeObject(&m, restartHclObj); err != nil {
return err
}
if delay, ok := m["delay"]; ok {
d, err := toDuration(delay)
if err != nil {
return fmt.Errorf("Invalid Delay time in restart policy: %v", err)
}
result.Delay = d
}
if interval, ok := m["interval"]; ok {
i, err := toDuration(interval)
if err != nil {
return fmt.Errorf("Invalid Interval time in restart policy: %v", err)
}
result.Interval = i
}
if attempts, ok := m["attempts"]; ok {
a, err := toInteger(attempts)
if err != nil {
return fmt.Errorf("Invalid value in attempts: %v", err)
}
result.Attempts = a
}
return nil
}
示例7: 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
}
示例8: 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
}
示例9: 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
}
示例10: 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
}
示例11: 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
}
示例12: parseRawObject
func parseRawObject(key string, hcltree *hclObj.Object, errors *multierror.Error) (apisOut []*hclObj.Object) {
if apis := hcltree.Get(key, false); apis != nil {
for _, api := range apis.Elem(true) {
apisOut = append(apisOut, api)
}
} else {
errors = multierror.Append(errors, fmt.Errorf("No job_store was specified in the configuration"))
}
return apisOut
}
示例13: 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
}
示例14: parseRawBool
func parseRawBool(key string, hcltree *hclObj.Object, errors *multierror.Error) bool {
if rawValue := hcltree.Get(key, false); rawValue != nil {
if rawValue.Type != hclObj.ValueTypeBool {
errors = multierror.Append(errors, fmt.Errorf("Invalid type assigned to property: %s. Expected string type, Found %s", key, rawValue.Type))
} else {
return rawValue.Value.(bool)
}
} else {
errors = multierror.Append(errors, fmt.Errorf("Property: %s not specified in the configuration", key))
}
return false
}
示例15: parseRawArray
func parseRawArray(key string, object *hclObj.Object, errors *multierror.Error) []string {
output := []string{}
if rawValue := object.Get(key, false); rawValue != nil {
switch rawValue.Type {
case hclObj.ValueTypeString:
output = append(output, rawValue.Value.(string))
case hclObj.ValueTypeList:
for _, m := range rawValue.Elem(true) {
output = append(output, m.Value.(string))
}
}
}
return output
}