// ConvertFromModel converts a field value for use in the REST API layer
767722
func (f FieldDefinition) ConvertFromModel(name string, value interface{}) (interface{}, error) {
	if f.Required && value == nil {
		return nil, fmt.Errorf("Value %s is required", name)
	}
	return f.Type.ConvertFromModel(value)
}
238096
func (t SimpleType) ConvertToModel(value interface{}) (interface{}, error) {
	if value == nil {
		return nil, nil
	}
	valueType := reflect.TypeOf(value)
	switch t.GetKind() {
	case KindString, KindUser, KindIteration, KindArea, KindLabel, KindBoardColumn:
		if valueType.Kind() != reflect.String {
			return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %s", value, "string", valueType.Name())
		}
		return value, nil
	case KindRemoteTracker:
		return AnyToUUID(value)
	case KindURL:
		if valueType.Kind() == reflect.String && govalidator.IsURL(value.(string)) {
			return value, nil
		}
		return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %q", value, "URL", valueType.Name())
	case KindFloat:
		if valueType.Kind() != reflect.Float64 {
			return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %q", value, "float64", valueType.Name())
		}
		return value, nil
	case KindInteger:
		// NOTE(kwk): This will change soon to be more consistent.
		switch valueType.Kind() {
		case reflect.Int,
			reflect.Int64:
			return value, nil
		case reflect.Float64:
			fval, ok := value.(float64)
			if !ok {
				return nil, errs.Errorf("failed to cast value %+v (%[1]T) to float64", value)
			}
			if fval != math.Trunc(fval) {
				return nil, errs.Errorf("float64 value %+v (%[1]T) has digits after the decimal point and therefore cannot be represented by an integer", value)
			}
			return int(fval), nil
		default:
			return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %s ", value, "int or float", valueType.Name())
		}
	case KindInstant:
		// instant == milliseconds
		// if !valueType.Implements(timeType) {
		if valueType.Kind() != timeType.Kind() {
			return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %s", value, "time.Time", valueType.Name())
		}
		return value.(time.Time).UnixNano(), nil
	case KindList:
		if (valueType.Kind() != reflect.Array) && (valueType.Kind() != reflect.Slice) {
			return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %s,", value, "array/slice", valueType.Kind())
		}
		return value, nil
	case KindEnum:
		// to be done yet | not sure what to write here as of now.
		return value, nil
	case KindMarkup:
		// 'markup' is just a string in the API layer for now:
		// it corresponds to the MarkupContent.Content field. The MarkupContent.Markup is set to the default value
		switch value.(type) {
		case rendering.MarkupContent:
			markupContent := value.(rendering.MarkupContent)
			if !rendering.IsMarkupSupported(markupContent.Markup) {
				return nil, errs.Errorf("value %v (%[1]T) has no valid markup type %s", value, markupContent.Markup)
			}
			return markupContent.ToMap(), nil
		case map[string]interface{}:
			markupContent := rendering.NewMarkupContentFromValue(value)
			if !rendering.IsMarkupSupported(markupContent.Markup) {
				return nil, errs.Errorf("value %v (%[1]T) has no valid markup type %s", value, markupContent.Markup)
			}
			return markupContent.ToMap(), nil
		default:
			return nil, errs.Errorf("value %v (%[1]T) should be rendering.MarkupContent, but is %s", value, valueType)
		}
	case KindCodebase:
		switch value.(type) {
		case codebase.Content:
			cb := value.(codebase.Content)
			if err := cb.IsValid(); err != nil {
				return nil, errs.Wrapf(err, "value %v (%[1]T) is invalid %s", value, cb)
			}
			return cb.ToMap(), nil
		default:
			return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %s", value, "CodebaseContent", valueType)
		}
	case KindBoolean:
		if valueType.Kind() != reflect.Bool {
			return nil, errs.Errorf("value %v (%[1]T) should be %s, but is %s", value, "boolean", valueType.Name())
		}
		return value, nil
	default:
		return nil, errs.Errorf("unexpected type constant: '%s'", t.GetKind())
	}
}
689931
func (t SimpleType) ConvertFromModel(value interface{}) (interface{}, error) {
	if value == nil {
		return nil, nil
	}
	valueType := reflect.TypeOf(value)
	switch t.GetKind() {
	case KindString, KindURL, KindUser, KindInteger, KindFloat, KindIteration, KindArea, KindLabel, KindBoardColumn, KindBoolean:
		return value, nil
	case KindRemoteTracker:
		return AnyToUUID(value)
	case KindInstant:
		switch valueType.Kind() {
		case reflect.Float64:
			v, ok := value.(float64)
			if !ok {
				return nil, errs.Errorf("value %v could not be converted into an %s", value, reflect.Float64)
			}
			if v != math.Trunc(v) {
				return nil, errs.Errorf("value %v is not a whole number", value)
			}
			return time.Unix(0, int64(v)), nil
		case reflect.Int64:
			v, ok := value.(int64)
			if !ok {
				return nil, errs.Errorf("value %v could not be converted into an %s", value, reflect.Int64)
			}
			return time.Unix(0, v), nil
		default:
			return nil, errs.Errorf("value %v must be either %s or %s but has an unknown type %s", value, reflect.Int64, reflect.Float64, valueType.Name())
		}
	case KindMarkup:
		if valueType.Kind() != reflect.Map {
			return nil, errs.Errorf("value %v should be %s, but is %s", value, reflect.Map, valueType.Name())
		}
		markupContent := rendering.NewMarkupContentFromMap(value.(map[string]interface{}))
		return markupContent, nil
	case KindCodebase:
		if valueType.Kind() != reflect.Map {
			return nil, errs.Errorf("value %v should be %s, but is %s", value, reflect.Map, valueType.Name())
		}
		cb, err := codebase.NewCodebaseContent(value.(map[string]interface{}))
		if err != nil {
			return nil, err
		}
		return cb, nil
	default:
		return nil, errs.Errorf("unexpected field type: %s", t.GetKind())
	}
}
233865
func (jsonList FooJSONList) Convert() ([]*Foo, error) {
	orig := ([]*FooJSON)(jsonList)

	list := make([]*Foo, len(orig))
	for idx, or := range orig {
		obj, err := or.Convert()
		if err != nil {
			return nil, err
		}
		list[idx] = obj
	}

	return list, nil
}
1142522
func (f *CustomUplinkFunctions) convert(fields map[string]interface{}, port uint8) (map[string]interface{}, error) {
	if f.Converter == "" {
		return fields, nil
	}

	env := map[string]interface{}{
		"fields": fields,
		"port":   port,
	}

	code := fmt.Sprintf(`
		%s;
		Converter(fields, port)
	`, f.Converter)

	value, err := functions.RunCode("Converter", code, env, timeOut, f.Logger)
	if err != nil {
		return nil, err
	}

	m, ok := value.(map[string]interface{})
	if !ok {
		return nil, errors.NewErrInvalidArgument("Converter", "does not return an object")
	}

	return m, nil
}
874704
func HtmlIdFromModel(tag string, m ModelName) HtmlId {
	id := fmt.Sprintf("%s-%s", strings.ToLower(tag), m.Id())
	return NewHtmlId(tag, id)
}
326812
func (self *Collection) ConvertValue(name string, value interface{}) interface{} {
	if field, ok := self.GetField(name); ok {
		if v, err := field.ConvertValue(value); err == nil {
			return v
		}
	}

	return value
}
327029
func (o *IntOptions) GetMutableModel() *models.ConfigurationMap {
	mutableCfg := make(models.ConfigurationMap)
	o.optsMU.RLock()
	for k, v := range o.Opts {
		_, config := o.Library.Lookup(k)

		// It's possible that an option has since been removed and thus has
		// no corresponding configuration; need to check if configuration is
		// nil accordingly.
		if config != nil {
			if config.Format == nil {
				if v == OptionDisabled {
					mutableCfg[k] = fmt.Sprintf("Disabled")
				} else {
					mutableCfg[k] = fmt.Sprintf("Enabled")
				}
			} else {
				mutableCfg[k] = config.Format(v)
			}
		}
	}
	o.optsMU.RUnlock()

	return &mutableCfg
}
878378
func (v *versionConverter) Convert(object typed.TypedValue, version fieldpath.APIVersion) (typed.TypedValue, error) {
	// Convert the smd typed value to a kubernetes object.
	objectToConvert, err := v.typeConverter.TypedToObject(object)
	if err != nil {
		return object, err
	}

	// Parse the target groupVersion.
	groupVersion, err := schema.ParseGroupVersion(string(version))
	if err != nil {
		return object, err
	}

	// If attempting to convert to the same version as we already have, just return it.
	fromVersion := objectToConvert.GetObjectKind().GroupVersionKind().GroupVersion()
	if fromVersion == groupVersion {
		return object, nil
	}

	// Convert to internal
	internalObject, err := v.objectConvertor.ConvertToVersion(objectToConvert, v.hubGetter(fromVersion))
	if err != nil {
		return object, err
	}

	// Convert the object into the target version
	convertedObject, err := v.objectConvertor.ConvertToVersion(internalObject, groupVersion)
	if err != nil {
		return object, err
	}

	// Convert the object back to a smd typed value and return it.
	return v.typeConverter.ObjectToTyped(convertedObject)
}
1364390
func Convert(obj interface{}) (url.Values, error) {
	result := url.Values{}
	if obj == nil {
		return result, nil
	}
	var sv reflect.Value
	switch reflect.TypeOf(obj).Kind() {
	case reflect.Ptr, reflect.Interface:
		sv = reflect.ValueOf(obj).Elem()
	default:
		return nil, fmt.Errorf("expecting a pointer or interface")
	}
	st := sv.Type()
	if !isStructKind(st.Kind()) {
		return nil, fmt.Errorf("expecting a pointer to a struct")
	}

	// Check all object fields
	convertStruct(result, st, sv)

	return result, nil
}
160144
func (ls LabelArray) GetModel() []string {
	res := []string{}
	for l := range ls {
		res = append(res, ls[l].String())
	}
	return res
}
980686
func (f FieldDefinition) ConvertToModel(name string, value interface{}) (interface{}, error) {
	// Overwrite value if default value if none was provided
	if value == nil {
		value = f.Type.GetDefaultValue()
	}

	if f.Required {
		if value == nil {
			return nil, fmt.Errorf("value for field %q must not be nil", name)
		}
		if f.Type.GetKind() == KindString {
			sVal, ok := value.(string)
			if !ok {
				return nil, errs.Errorf("failed to convert '%+v' to string", spew.Sdump(value))
			}
			if strings.TrimSpace(sVal) == "" {
				return nil, errs.Errorf("value for field %q must not be empty: \"%+v\"", name, value)
			}
		}
	}
	return f.Type.ConvertToModel(value)
}
922022
func (e *Enforcer) LoadModel() {
	e.model = NewModel()
	e.model.LoadModel(e.modelPath)
	e.model.PrintModel()
	e.fm = model.LoadFunctionMap()
}
989539
func (self *Collection) MapFromRecord(record *Record, fields ...string) (map[string]interface{}, error) {
	rv := make(map[string]interface{})

	for _, field := range self.Fields {
		if len(fields) > 0 && !sliceutil.ContainsString(fields, field.Name) {
			continue
		}

		if dv := field.GetDefaultValue(); dv != nil {
			rv[field.Name] = dv
		}
	}

	if record != nil {
		if record.ID != nil {
			if id, err := self.formatAndValidateId(record.ID, RetrieveOperation, record); err == nil {
				rv[self.GetIdentityFieldName()] = id
			} else {
				return nil, err
			}
		}

		if len(self.Fields) > 0 {
			for _, field := range self.Fields {
				if v := record.Get(field.Name); v != nil {
					if len(fields) > 0 && !sliceutil.ContainsString(fields, field.Name) {
						continue
					}

					rv[field.Name] = v
				}
			}
		} else {
			for k, v := range record.Fields {
				rv[k] = v
			}
		}
	}

	return rv, nil
}
863597
func (r *GormWorkItemTypeRepository) CreateFromModel(ctx context.Context, model WorkItemType) (*WorkItemType, error) {
	defer goa.MeasureSince([]string{"goa", "db", "workitemtype", "create"}, time.Now())

	if model.ID == uuid.Nil {
		model.ID = uuid.NewV4()
	}

	allFields := map[string]FieldDefinition{}
	path := LtreeSafeID(model.ID)
	if model.Extends != uuid.Nil {
		extendedType := WorkItemType{}
		db := r.db.Model(&extendedType).Where("id=?", model.Extends).First(&extendedType)
		if db.RecordNotFound() {
			return nil, errors.NewBadParameterError("extendedTypeID", model.Extends)
		}
		if err := db.Error; err != nil {
			return nil, errors.NewInternalError(ctx, err)
		}
		// copy fields from extended type
		for key, value := range extendedType.Fields {
			allFields[key] = value
		}
		path = extendedType.Path + pathSep + path
	}
	// now process new fields, checking whether they are already there.
	for field, definition := range model.Fields {
		existing, exists := allFields[field]
		if exists && !compatibleFields(existing, definition) {
			return nil, errs.Errorf("incompatible change for field %s", field)
		}
		allFields[field] = definition
	}

	model.Version = 0
	model.Path = path
	model.Fields = allFields

	db := r.db.Create(&model)
	if db.Error != nil {
		return nil, errors.NewInternalError(ctx, db.Error)
	}
	return &model, nil
}
322890
func ConvertIdentifierToUsername(identifier string, valMgr *validation.Manager) (string, error) {
	email, err := valMgr.GetByEmailAddress(identifier)
	if err == nil {
		return email.Username, err
	} else if valMgr.IsErrNotFound(err) {
		phone, err := valMgr.GetByPhoneNumber(identifier)
		if err == nil || db.IsNotFound(err) {
			return phone.Username, nil
		}
		return identifier, err

	}
	return identifier, err
}
295032
func ConvertProtoStatObject(in *proto.StatObject) *StatObject {
	if in == nil {
		return nil
	}

	out := &StatObject{
		Nested:     make(map[string]*StatObject, len(in.Nested)),
		Attributes: make(map[string]*StatValue, len(in.Attributes)),
	}

	for k, v := range in.Nested {
		out.Nested[k] = ConvertProtoStatObject(v)
	}

	for k, v := range in.Attributes {
		out.Attributes[k] = ConvertProtoStatValue(v)
	}

	return out
}