Example #1
0
// 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...)
}
Example #2
0
func getFromArrayField(field *pb.Field, index int) (*pb.Field, error) {
	fieldArr := field.GetArray()
	if fieldArr == nil {
		return nil, fmt.Errorf("field '%s' isn't an array, cannot access %s[%d]", field.Key, field.Key, index)
	}

	items := fieldArr.GetItems()
	if items == nil {
		return nil, fmt.Errorf("the array wrapper struct for the value of field '%s' had nil for items, cannot access %s[%d]", field.Key, field.Key, index)
	} else if len(items)-1 < index {
		return nil, fmt.Errorf("could not access %s[%d], the size of '%s' is %d", field.Key, index, field.Key, len(items))
	}
	return items[index], nil
}
Example #3
0
// simpleArgApply is used when no formatting template string is given.
func simpleArgApply(field *pb.Field, arg *pb.Argument) error {
	switch val := arg.GetValue().(type) {
	case *pb.Argument_Number:
		field.Value = &pb.Field_Number{Number: val.Number}
	case *pb.Argument_Str:
		field.Value = &pb.Field_Str{Str: val.Str}
	case *pb.Argument_Boolean:
		field.Value = &pb.Field_Boolean{Boolean: val.Boolean}
	default:
		field.Value = nil
	}

	return nil
}
Example #4
0
func getFromMapField(field *pb.Field, key string) (*pb.Field, error) {
	fieldMap := field.GetObject()
	if fieldMap == nil {
		return nil, fmt.Errorf("field '%s' isn't an object, cannot access %s['%s']", field.Key, field.Key, key)
	}

	items := fieldMap.GetItems()
	if items == nil {
		return nil, fmt.Errorf("the object wrapper struct for the value of field '%s' had nil for items, cannot access %s[%s]", field.Key, field.Key, key)
	}

	item, ok := items[key]
	if !ok {
		return nil, fmt.Errorf("no key '%s' in map for field '%s", key, field.Key)
	}
	return item, nil
}
Example #5
0
// 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)
		}
	}
}
Example #6
0
func decodeField(field *pb.Field) (interface{}, error) {
	val := field.GetValue()
	if val == nil {
		return nil, nil
	}

	switch v := val.(type) {
	case *pb.Field_Number:
		return v.Number, nil
	case *pb.Field_Str:
		return v.Str, nil
	case *pb.Field_Boolean:
		return v.Boolean, nil
	case *pb.Field_Object:
		return decodeObject(v.Object.GetItems())
	case *pb.Field_Array:
		return decodeArray(v.Array.GetItems())
	case *pb.Field_Link:
		// TODO: IMPLEMENT FOLLOWING LINKS
		return nil, nil
	}

	return nil, fmt.Errorf("unknown type for Field '%s'", field.Key)
}
Example #7
0
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
}
Example #8
0
// FieldValueEquals returns true if the value of the given fields is the same.
func FieldValueEquals(this, other *pb.Field) bool {
	// check for pointer + primitive  matches and nil values
	switch {
	case this == other:
		return true
	case this == nil || other == nil:
		return false
	case this.GetValue() == other.GetValue():
		return true
	case this.GetValue() == nil || other.GetValue() == nil:
		return false
	}

	// check for matches with objects and arrays
	switch val := this.GetValue().(type) {
	case *pb.Field_Number:
		otherVal, ok := other.GetValue().(*pb.Field_Number)
		if !ok {
			return false
		}
		return val.Number == otherVal.Number
	case *pb.Field_Str:
		otherVal, ok := other.GetValue().(*pb.Field_Str)
		if !ok {
			return false
		}
		return val.Str == otherVal.Str
	case *pb.Field_Boolean:
		otherVal, ok := other.GetValue().(*pb.Field_Boolean)
		if !ok {
			return false
		}
		return val.Boolean == otherVal.Boolean
	case *pb.Field_Object:
		otherVal, ok := other.GetValue().(*pb.Field_Object)
		if !ok {
			return false
		}
		items, otherItems := val.Object.GetItems(), otherVal.Object.GetItems()

		if len(items) != len(otherItems) {
			return false
		}

		for k, v := range items {
			otherV, ok := otherItems[k]
			if !ok {
				return false
			}

			return FieldValueEquals(v, otherV)
		}
	case *pb.Field_Array:
		otherVal, ok := other.GetValue().(*pb.Field_Array)
		if !ok {
			return false
		}
		items, otherItems := val.Array.GetItems(), otherVal.Array.GetItems()

		if len(items) != len(otherItems) {
			return false
		}

		for k, v := range items {
			return FieldValueEquals(v, otherItems[k])
		}
	}

	return false
}
Example #9
0
// 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
}