• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang hcl.Object类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/hashicorp/hcl/hcl.Object的典型用法代码示例。如果您正苦于以下问题:Golang Object类的具体用法?Golang Object怎么用?Golang Object使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Object类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:nvartolomei,项目名称:otto,代码行数:25,代码来源:parse.go


示例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
}
开发者ID:rbramwell,项目名称:nomad,代码行数:34,代码来源:parse.go


示例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
}
开发者ID:kuuyee,项目名称:otto-learn,代码行数:32,代码来源:parse.go


示例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
}
开发者ID:rowhit,项目名称:nomad,代码行数:26,代码来源:parse.go


示例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
}
开发者ID:nvartolomei,项目名称:otto,代码行数:34,代码来源:parse.go


示例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
}
开发者ID:rowhit,项目名称:nomad,代码行数:35,代码来源:parse.go


示例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
}
开发者ID:rbramwell,项目名称:nomad,代码行数:29,代码来源:parse.go


示例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
}
开发者ID:kapalhq,项目名称:envoy,代码行数:29,代码来源:hcl_parser.go


示例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
}
开发者ID:nvartolomei,项目名称:otto,代码行数:58,代码来源:parse.go


示例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
}
开发者ID:riddopic,项目名称:nomad,代码行数:57,代码来源:parse.go


示例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
}
开发者ID:rbramwell,项目名称:nomad,代码行数:56,代码来源:parse.go


示例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
}
开发者ID:kapalhq,项目名称:envoy,代码行数:10,代码来源:hcl_parser.go


示例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
}
开发者ID:rgl,项目名称:terraform,代码行数:57,代码来源:loader_hcl.go


示例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
}
开发者ID:kapalhq,项目名称:envoy,代码行数:12,代码来源:hcl_parser.go


示例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
}
开发者ID:kapalhq,项目名称:envoy,代码行数:14,代码来源:hcl_parser.go


示例16: parseVersion

func parseVersion(hcltree *hclObj.Object, errors *multierror.Error) string {
	if rawVersion := hcltree.Get("version", false); rawVersion != nil {
		if rawVersion.Len() > 1 {
			errors = multierror.Append(errors, fmt.Errorf("Version was specified more than once"))
		} else {
			if rawVersion.Type != hclObj.ValueTypeString {
				errors = multierror.Append(errors, fmt.Errorf("Version was specified as an invalid type - expected string, found %s", rawVersion.Type))
			} else {
				return rawVersion.Value.(string)
			}
		}
	} else {
		errors = multierror.Append(errors, fmt.Errorf("No version was specified in the configuration"))
	}
	return "*** Error"
}
开发者ID:kapalhq,项目名称:mozo,代码行数:16,代码来源:parser.go


示例17: parseMaxExecutions

func parseMaxExecutions(jobNode *hclObj.Object, errors *multierror.Error) string {
	if rawCron := jobNode.Get("max_executions", false); rawCron != nil {
		if rawCron.Len() > 1 {
			errors = multierror.Append(errors, fmt.Errorf("max_executions was specified more than once"))
		} else {
			if rawCron.Type != hclObj.ValueTypeString {
				errors = multierror.Append(errors, fmt.Errorf("max_executions was specified as an invalid type - expected string, found %s", rawCron.Type))
			} else {
				return rawCron.Value.(string)
			}
		}
	} else {
		errors = multierror.Append(errors, fmt.Errorf("No max_executions was specified in the configuration"))
	}
	return ""
}
开发者ID:kapalhq,项目名称:mozo,代码行数:16,代码来源:parser.go


示例18: parseJobTrigger

func parseJobTrigger(jobNode *hclObj.Object, errors *multierror.Error) TriggerConfig {
	var triggerConfig TriggerConfig
	if t := jobNode.Get("trigger", false); t != nil {
		err := hcl.DecodeObject(&triggerConfig, t)
		if err != nil {
			errors = multierror.Append(errors, err)
		}
	}
	return triggerConfig
	// cron := parseCron(jobNode.Get("trigger", false), errors)
	// maxExecs := parseMaxExecutions(jobNode.Get("trigger", false), errors)
	// return TriggerConfig{
	// 	Cron:          cron,
	// 	MaxExecutions: maxExecs,
	// }
}
开发者ID:kapalhq,项目名称:mozo,代码行数:16,代码来源:parser.go


示例19: 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
}
开发者ID:nvartolomei,项目名称:otto,代码行数:16,代码来源:parse.go


示例20: parseJob

func parseJob(jobNode *hclObj.Object, errors *multierror.Error) JobConfig {
	config := JobConfig{}

	if jobNode.Len() > 1 {
		errors = multierror.Append(errors, fmt.Errorf("job was specified more than once"))
	} else {
		if jobNode.Type != hclObj.ValueTypeObject {
			errors = multierror.Append(errors, fmt.Errorf("job was specified as an invalid type - expected object, found %s", jobNode.Type))
		} else {
			config.Name = jobNode.Key
			config.Trigger = parseJobTrigger(jobNode, errors)
			config.Exec = parseExec(jobNode, errors)
		}
	}
	return config
}
开发者ID:kapalhq,项目名称:mozo,代码行数:16,代码来源:parser.go



注:本文中的github.com/hashicorp/hcl/hcl.Object类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang ast.Node类代码示例发布时间:2022-05-23
下一篇:
Golang hcl.Parse函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap