// simplifyImpliedValueType attempts to heuristically simplify a value type
// derived from a legacy stored output value into something simpler that
// is closer to what would've fitted into the pre-v0.12 value type system.
972878
func simplifyImpliedValueType(ty cty.Type) cty.Type {
	switch {
	case ty.IsTupleType():
		// If all of the element types are the same then we'll make this
		// a list instead. This is very likely to be true, since prior versions
		// of Terraform did not officially support mixed-type collections.

		if ty.Equals(cty.EmptyTuple) {
			// Don't know what the element type would be, then.
			return ty
		}

		etys := ty.TupleElementTypes()
		ety := etys[0]
		for _, other := range etys[1:] {
			if !other.Equals(ety) {
				// inconsistent types
				return ty
			}
		}
		ety = simplifyImpliedValueType(ety)
		return cty.List(ety)

	case ty.IsObjectType():
		// If all of the attribute types are the same then we'll make this
		// a map instead. This is very likely to be true, since prior versions
		// of Terraform did not officially support mixed-type collections.

		if ty.Equals(cty.EmptyObject) {
			// Don't know what the element type would be, then.
			return ty
		}

		atys := ty.AttributeTypes()
		var ety cty.Type
		for _, other := range atys {
			if ety == cty.NilType {
				ety = other
				continue
			}
			if !other.Equals(ety) {
				// inconsistent types
				return ty
			}
		}
		ety = simplifyImpliedValueType(ety)
		return cty.Map(ety)

	default:
		// No other normalizations are possible
		return ty
	}
}
809422
func (a *CompositeAggregationTermsValuesSource) ValueType(valueType string) *CompositeAggregationTermsValuesSource {
	a.valueType = valueType
	return a
}
296137
func (c *Cell) SafeFormattedValue() (string, error) {
	var numberFormat = c.GetNumberFormat()
	if isTimeFormat(numberFormat) {
		return parseTime(c)
	}
	switch numberFormat {
	case builtInNumFmt[builtInNumFmtIndex_GENERAL], builtInNumFmt[builtInNumFmtIndex_STRING]:
		return c.Value, nil
	case builtInNumFmt[builtInNumFmtIndex_INT], "#,##0":
		return c.formatToInt("%d")
	case builtInNumFmt[builtInNumFmtIndex_FLOAT], "#,##0.00":
		return c.formatToFloat("%.2f")
	case "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)":
		f, err := strconv.ParseFloat(c.Value, 64)
		if err != nil {
			return c.Value, err
		}
		if f < 0 {
			i := int(math.Abs(f))
			return fmt.Sprintf("(%d)", i), nil
		}
		i := int(f)
		return fmt.Sprintf("%d", i), nil
	case "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)":
		f, err := strconv.ParseFloat(c.Value, 64)
		if err != nil {
			return c.Value, err
		}
		if f < 0 {
			return fmt.Sprintf("(%.2f)", f), nil
		}
		return fmt.Sprintf("%.2f", f), nil
	case "0%":
		f, err := strconv.ParseFloat(c.Value, 64)
		if err != nil {
			return c.Value, err
		}
		f = f * 100
		return fmt.Sprintf("%d%%", int(f)), nil
	case "0.00%":
		f, err := strconv.ParseFloat(c.Value, 64)
		if err != nil {
			return c.Value, err
		}
		f = f * 100
		return fmt.Sprintf("%.2f%%", f), nil
	case "0.00e+00", "##0.0e+0":
		return c.formatToFloat("%e")
	}
	return c.Value, nil

}
1307034
func (t SimpleType) Validate() error {
	if !t.Kind.IsSimpleType() {
		return errs.New("a simple type can only have a simple type (e.g. no list or enum)")
	}
	_, err := t.SetDefaultValue(t.DefaultValue)
	if err != nil {
		return errs.Wrapf(err, "failed to validate default value for kind %s: %+v (%[1]T)", t.Kind, t.DefaultValue)
	}
	return nil
}
1075434
func (v *Variable) ValidateTypeAndDefault() error {
	// If an explicit type is declared, ensure it is valid
	if v.DeclaredType != "" {
		if _, ok := typeStringMap[v.DeclaredType]; !ok {
			validTypes := []string{}
			for k := range typeStringMap {
				validTypes = append(validTypes, k)
			}
			return fmt.Errorf(
				"Variable '%s' type must be one of [%s] - '%s' is not a valid type",
				v.Name,
				strings.Join(validTypes, ", "),
				v.DeclaredType,
			)
		}
	}

	if v.DeclaredType == "" || v.Default == nil {
		return nil
	}

	if v.inferTypeFromDefault() != v.Type() {
		return fmt.Errorf("'%s' has a default value which is not of type '%s' (got '%s')",
			v.Name, v.DeclaredType, v.inferTypeFromDefault().Printable())
	}

	return nil
}
546738
func (a *CompositeAggregationHistogramValuesSource) ValueType(valueType string) *CompositeAggregationHistogramValuesSource {
	a.valueType = valueType
	return a
}
1346255
func convertComplexValue(record *skyrecord.Record) error {
	for idx, val := range record.Data {
		valStr, ok := val.(string)
		if !ok {
			continue
		}

		for _, complexType := range ComplexTypeList {
			if complexType.Validate(valStr) {
				convert, err := complexValueConfirmation(valStr)
				if err != nil {
					return err
				}
				if !convert {
					continue
				}

				result, err := complexType.Convert(valStr)
				if err != nil {
					return err
				}
				record.Data[idx] = result
			}
		}
	}
	return nil
}
1115422
func UnpackValue(v reflect.Value) (reflect.Value, error) {
	if v.IsNil() {
		return v, nil
	}
	k := v.Type().Kind()
	if k == reflect.Map {
		return v, nil
	} else if k == reflect.Ptr {
		return UnpackValue(v.Elem())
	} else if k == reflect.Interface {
		return UnpackValue(v.Elem())
	}
	return v, fmt.Errorf("Unsupported type")
}
879388
func DoubleValue(val string) (*wrappers.DoubleValue, error) {
	parsedVal, err := Float64(val)
	return &wrappers.DoubleValue{Value: parsedVal}, err
}
809625
func (b *PCMBuffer) SwitchPrimaryType(t DataFormat) {
	if b == nil || t == b.DataType {
		return
	}
	switch t {
	case Integer:
		b.Ints = b.AsInts()
		b.Floats = nil
		b.Bytes = nil
	case Float:
		b.Floats = b.AsFloat64s()
		b.Ints = nil
		b.Bytes = nil
	case Byte:
		// TODO: get as bytes
		b.Floats = nil
		b.Ints = nil
	}
	b.DataType = t
}
824951
func SimpleType_Values() []SimpleType {
	return []SimpleType{
		SimpleTypeBool,
		SimpleTypeByte,
		SimpleTypeInt8,
		SimpleTypeInt16,
		SimpleTypeInt32,
		SimpleTypeInt64,
		SimpleTypeFloat64,
		SimpleTypeString,
		SimpleTypeStructEmpty,
	}
}
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())
	}
}
843333
func (a UnsignedValues) Deduplicate() UnsignedValues {
	if len(a) <= 1 {
		return a
	}

	// See if we're already sorted and deduped
	var needSort bool
	for i := 1; i < len(a); i++ {
		if a[i-1].UnixNano() >= a[i].UnixNano() {
			needSort = true
			break
		}
	}

	if !needSort {
		return a
	}

	sort.Stable(a)
	var i int
	for j := 1; j < len(a); j++ {
		v := a[j]
		if v.UnixNano() != a[i].UnixNano() {
			i++
		}
		a[i] = v

	}
	return a[:i+1]
}
613293
func flagTypeVal(a *design.AttributeDefinition, key string, field string) string {
	switch a.Type {
	case design.Integer:
		return `intFlagVal("` + key + `", ` + field + ")"
	case design.String:
		return `stringFlagVal("` + key + `", ` + field + ")"
	case design.Number, design.Boolean, design.UUID, design.DateTime, design.Any:
		return "%s"
	default:
		return "&" + field
	}
}
793463
func Type(v interface{}) ValueType {
	t := Unknown
	switch v.(type) {
	case nil:
		t = Null
	case bool:
		t = Bool
	case string:
		t = String
	case float64:
		t = Number
	case []interface{}:
		t = Array
	case map[string]interface{}:
		t = Object
	}
	return t
}
999521
func DriverTypeSimpleValueOf(s string) (DriverType, error) {
	obj, err := simpleValueOf("driver_type", DriverType_value, s)
	return DriverType(obj), err
}
1268392
func typeOfValue(t *ast.ValueSpec) string {
	switch t := t.Type.(type) {
	case *ast.Ident:
		return t.String()
	}
	return ""
}