当前位置: 首页>>代码示例>>Golang>>正文


Golang Field.GetParam方法代码示例

本文整理汇总了Golang中rsprd/com/spread/pkg/spreadproto.Field.GetParam方法的典型用法代码示例。如果您正苦于以下问题:Golang Field.GetParam方法的具体用法?Golang Field.GetParam怎么用?Golang Field.GetParam使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rsprd/com/spread/pkg/spreadproto.Field的用法示例。


在下文中一共展示了Field.GetParam方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ApplyArguments

// ApplyArguments takes the given arguments and uses them to satisfy a field parameter. If a single argument and no
// formatting pattern are given the single argument is used as the field value. Otherwise the arguments will be used as
// arguments to Printf with the formatting string as the pattern.
func ApplyArguments(field *pb.Field, args ...*pb.Argument) error {
	if field == nil {
		return errors.New("field was nil")
	} else if field.GetParam() == nil {
		return fmt.Errorf("field %s does not have a parameter", field.Key)
	} else if len(args) < 1 && field.GetParam().GetDefault() == nil {
		return errors.New("an argument must be specified if no default is given")
	} else if len(args) < 1 {
		return applyDefault(field)
	} else if len(args) == 1 && len(field.GetParam().Pattern) == 0 {
		return simpleArgApply(field, args[0])
	} else if len(args) > 1 && len(field.GetParam().Pattern) == 0 {
		return errors.New("may only use multiple arguments if a string template is provided")
	}

	argVals := make([]interface{}, len(args))
	for i, v := range args {
		switch val := v.GetValue().(type) {
		case *pb.Argument_Number:
			argVals[i] = val.Number
		case *pb.Argument_Str:
			argVals[i] = val.Str
		case *pb.Argument_Boolean:
			argVals[i] = val.Boolean
		}
	}

	val := fmt.Sprintf(field.GetParam().Pattern, argVals...)
	field.Value = &pb.Field_Str{Str: val}
	return nil
}
开发者ID:redspread,项目名称:spread,代码行数:34,代码来源:parameter.go

示例2: InteractiveArgs

// InteractiveArgs hosts an interactive session using a Reader and Writer which prompts for input which is used to
// populate the provided field. If required is true then only fields without defaults will be prompted for.
func InteractiveArgs(r io.ReadCloser, w io.Writer, field *pb.Field, required bool) error {
	param := field.GetParam()

	defaultVal := param.GetDefault()
	// don't prompt if only checking for required and has default
	if required && defaultVal != nil {
		return nil
	}

	fmt.Fprintln(w, "Name: ", param.Name)
	fmt.Fprintln(w, "Prompt: ", param.Prompt)
	fmt.Fprint(w, "Input: ", displayDefault(defaultVal))
	reader := bufio.NewReader(r)
	text, err := reader.ReadString('\n')
	if err != nil {
		return err
	}

	// use default if no input given
	args := []*pb.Argument{defaultVal}
	if len(text) > 1 {
		_, str := field.GetValue().(*pb.Field_Str)
		args, err = ParseArguments(text[:len(text)-1], str)
		if err != nil {
			return err
		}
	}

	return ApplyArguments(field, args...)
}
开发者ID:redspread,项目名称:spread,代码行数:32,代码来源:parameter.go

示例3: AddParameterFields

// AddParameterFields adds fields with parameters from the given field (and its subfields) to the map given. The name of the parameter is the key.
func AddParameterFields(field *pb.Field, params map[string]*pb.Field) {
	param := field.GetParam()
	// add to map if has parameter
	if param != nil {
		params[param.Name] = field
	}

	switch val := field.GetValue().(type) {
	case *pb.Field_Object:
		for _, objField := range val.Object.GetItems() {
			AddParameterFields(objField, params)
		}
	case *pb.Field_Array:
		for _, arrField := range val.Array.GetItems() {
			AddParameterFields(arrField, params)
		}
	}
}
开发者ID:redspread,项目名称:spread,代码行数:19,代码来源:parameter.go

示例4: applyDefault

func applyDefault(field *pb.Field) error {
	if field == nil {
		return errors.New("field cannot be nil")
	} else if field.GetParam() == nil {
		return errors.New("field does not have parameters")
	} else if field.GetParam().GetDefault() == nil {
		return errors.New("fields has paramaters but default was nil")
	}

	switch d := field.GetParam().GetDefault().GetValue().(type) {
	case *pb.Argument_Number:
		field.Value = &pb.Field_Number{Number: d.Number}
	case *pb.Argument_Str:
		field.Value = &pb.Field_Str{Str: d.Str}
	case *pb.Argument_Boolean:
		field.Value = &pb.Field_Boolean{Boolean: d.Boolean}
	}
	return nil
}
开发者ID:redspread,项目名称:spread,代码行数:19,代码来源:parameter.go


注:本文中的rsprd/com/spread/pkg/spreadproto.Field.GetParam方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。