// Given a type and any value, return a runtime value coerced to match the type.
555581
func coerceValue(ttype Input, value interface{}) interface{} {
	if isNullish(value) {
		return nil
	}
	switch ttype := ttype.(type) {
	case *NonNull:
		return coerceValue(ttype.OfType, value)
	case *List:
		var values = []interface{}{}
		valType := reflect.ValueOf(value)
		if valType.Kind() == reflect.Slice {
			for i := 0; i < valType.Len(); i++ {
				val := valType.Index(i).Interface()
				values = append(values, coerceValue(ttype.OfType, val))
			}
			return values
		}
		return append(values, coerceValue(ttype.OfType, value))
	case *InputObject:
		var obj = map[string]interface{}{}
		valueMap, _ := value.(map[string]interface{})
		if valueMap == nil {
			valueMap = map[string]interface{}{}
		}

		for name, field := range ttype.Fields() {
			fieldValue := coerceValue(field.Type, valueMap[name])
			if isNullish(fieldValue) {
				fieldValue = field.DefaultValue
			}
			if !isNullish(fieldValue) {
				obj[name] = fieldValue
			}
		}
		return obj
	case *Scalar:
		if parsed := ttype.ParseValue(value); !isNullish(parsed) {
			return parsed
		}
	case *Enum:
		if parsed := ttype.ParseValue(value); !isNullish(parsed) {
			return parsed
		}
	}

	return nil
}
1069712
func (c *TsColumnDescription) GetType() string {
	return riak_ts.TsColumnType_name[int32(c.column.GetType())]
}
598592
func (t EnumType) GetDefaultValue() interface{} {
	// manual default value has precedence over first value in list of allowed
	// values
	if t.DefaultValue != nil {
		return t.DefaultValue
	}
	// fallback to first permitted element
	if len(t.Values) > 0 {
		return t.Values[0]
	}
	return nil
}
352086
func (p plugin) getType() Type {
	var typeFn func() uint16
	fnName := "_Type"
	p.lib.Sym(fnName, &typeFn)
	if typeFn == nil {
		panic("Failed to load plugin: Missing _Type function")
	}
	return Type(typeFn())
}
506481
func TypeMatch(this interface{}, that interface{}) bool {
	t1 := reflect.TypeOf(this)
	t2 := reflect.TypeOf(that)
	if t1 == t2 {
		return true
	}
	if reflect.PtrTo(t1) == t2 || t1 == reflect.PtrTo(t2) {
		return true
	}
	return false
}
192643
func (list *List) GetValue() interface{} {
	var values []interface{}
	for _, val := range list.values {
		values = append(values, val.GetValue())
	}
	return values
}
1256044
func (s *GetService) Type(typ string) *GetService {
	s.typ = typ
	return s
}
1195686
func typeFor(k reflect.Kind) reflect.Type {
	if k > 0 && int(k) < len(kindToType) {
		return kindToType[k]
	}
	return nil
}
82728
func FSTypeSimpleValueOf(s string) (FSType, error) {
	obj, err := simpleValueOf("fs_type", FSType_value, s)
	return FSType(obj), err
}
126826
func (s TypedString) ParseValue() (Value, error) {
	fnc := knownConversions[s.Type.Full()]
	if fnc == nil {
		return s, nil
	}
	return fnc(string(s.Value))
}
809883
func (s Slice) IsType(t ValueType) bool {
	return typeMap[s.head()] == t
}
774082
func (item *MultiGetItem) Type(typ string) *MultiGetItem {
	item.typ = typ
	return item
}
236482
func nameOfRuntimeType(_type *Variable) (typename string, kind int64, err error) {
	if e, ok := _type.bi.nameOfRuntimeType[_type.Addr]; ok {
		return e.typename, e.kind, nil
	}

	var tflag int64

	if tflagField := _type.loadFieldNamed("tflag"); tflagField != nil && tflagField.Value != nil {
		tflag, _ = constant.Int64Val(tflagField.Value)
	}
	if kindField := _type.loadFieldNamed("kind"); kindField != nil && kindField.Value != nil {
		kind, _ = constant.Int64Val(kindField.Value)
	}

	// Named types are defined by a 'type' expression, everything else
	// (for example pointers to named types) are not considered named.
	if tflag&tflagNamed != 0 {
		typename, err = nameOfNamedRuntimeType(_type, kind, tflag)
		return typename, kind, err
	}

	typename, err = nameOfUnnamedRuntimeType(_type, kind, tflag)
	return typename, kind, err
}
466598
func (self *_runtime) convertCallParameter(v Value, t reflect.Type) reflect.Value {
	if t == typeOfValue {
		return reflect.ValueOf(v)
	}

	if v.kind == valueObject {
		if gso, ok := v._object().value.(*_goStructObject); ok {
			if gso.value.Type().AssignableTo(t) {
				// please see TestDynamicFunctionReturningInterface for why this exists
				if t.Kind() == reflect.Interface && gso.value.Type().ConvertibleTo(t) {
					return gso.value.Convert(t)
				} else {
					return gso.value
				}
			}
		}

		if gao, ok := v._object().value.(*_goArrayObject); ok {
			if gao.value.Type().AssignableTo(t) {
				// please see TestDynamicFunctionReturningInterface for why this exists
				if t.Kind() == reflect.Interface && gao.value.Type().ConvertibleTo(t) {
					return gao.value.Convert(t)
				} else {
					return gao.value
				}
			}
		}
	}

	if t.Kind() == reflect.Interface {
		e := v.export()
		if e == nil {
			return reflect.Zero(t)
		}
		iv := reflect.ValueOf(e)
		if iv.Type().AssignableTo(t) {
			return iv
		}
	}

	tk := t.Kind()

	if tk == reflect.Ptr {
		switch v.kind {
		case valueEmpty, valueNull, valueUndefined:
			return reflect.Zero(t)
		default:
			var vv reflect.Value
			if err := catchPanic(func() { vv = self.convertCallParameter(v, t.Elem()) }); err == nil {
				if vv.CanAddr() {
					return vv.Addr()
				}

				pv := reflect.New(vv.Type())
				pv.Elem().Set(vv)
				return pv
			}
		}
	}

	switch tk {
	case reflect.Bool:
		return reflect.ValueOf(v.bool())
	case reflect.String:
		switch v.kind {
		case valueString:
			return reflect.ValueOf(v.value)
		case valueNumber:
			return reflect.ValueOf(fmt.Sprintf("%v", v.value))
		}
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
		switch v.kind {
		case valueNumber:
			return self.convertNumeric(v, t)
		}
	case reflect.Slice:
		if o := v._object(); o != nil {
			if lv := o.get("length"); lv.IsNumber() {
				l := lv.number().int64

				s := reflect.MakeSlice(t, int(l), int(l))

				tt := t.Elem()

				if o.class == "Array" {
					for i := int64(0); i < l; i++ {
						p, ok := o.property[strconv.FormatInt(i, 10)]
						if !ok {
							continue
						}

						e, ok := p.value.(Value)
						if !ok {
							continue
						}

						ev := self.convertCallParameter(e, tt)

						s.Index(int(i)).Set(ev)
					}
				} else if o.class == "GoArray" {

					var gslice bool
					switch o.value.(type) {
					case *_goSliceObject:
						gslice = true
					case *_goArrayObject:
						gslice = false
					}

					for i := int64(0); i < l; i++ {
						var p *_property
						if gslice {
							p = goSliceGetOwnProperty(o, strconv.FormatInt(i, 10))
						} else {
							p = goArrayGetOwnProperty(o, strconv.FormatInt(i, 10))
						}
						if p == nil {
							continue
						}

						e, ok := p.value.(Value)
						if !ok {
							continue
						}

						ev := self.convertCallParameter(e, tt)

						s.Index(int(i)).Set(ev)
					}
				}

				return s
			}
		}
	case reflect.Map:
		if o := v._object(); o != nil && t.Key().Kind() == reflect.String {
			m := reflect.MakeMap(t)

			o.enumerate(false, func(k string) bool {
				m.SetMapIndex(reflect.ValueOf(k), self.convertCallParameter(o.get(k), t.Elem()))
				return true
			})

			return m
		}
	case reflect.Func:
		if t.NumOut() > 1 {
			panic(self.panicTypeError("converting JavaScript values to Go functions with more than one return value is currently not supported"))
		}

		if o := v._object(); o != nil && o.class == "Function" {
			return reflect.MakeFunc(t, func(args []reflect.Value) []reflect.Value {
				l := make([]interface{}, len(args))
				for i, a := range args {
					if a.CanInterface() {
						l[i] = a.Interface()
					}
				}

				rv, err := v.Call(nullValue, l...)
				if err != nil {
					panic(err)
				}

				if t.NumOut() == 0 {
					return nil
				}

				return []reflect.Value{self.convertCallParameter(rv, t.Out(0))}
			})
		}
	case reflect.Struct:
		if o := v._object(); o != nil && o.class == "Object" {
			s := reflect.New(t)

			for _, k := range o.propertyOrder {
				var f *reflect.StructField

				for i := 0; i < t.NumField(); i++ {
					ff := t.Field(i)

					if j := ff.Tag.Get("json"); j != "" {
						if j == "-" {
							continue
						}

						a := strings.Split(j, ",")

						if a[0] == k {
							f = &ff
							break
						}
					}

					if ff.Name == k {
						f = &ff
						break
					}

					if strings.EqualFold(ff.Name, k) {
						f = &ff
					}
				}

				if f == nil {
					panic(self.panicTypeError("can't convert object; field %q was supplied but does not exist on target %v", k, t))
				}

				ss := s

				for _, i := range f.Index {
					if ss.Kind() == reflect.Ptr {
						if ss.IsNil() {
							if !ss.CanSet() {
								panic(self.panicTypeError("can't set embedded pointer to unexported struct: %v", ss.Type().Elem()))
							}

							ss.Set(reflect.New(ss.Type().Elem()))
						}

						ss = ss.Elem()
					}

					ss = ss.Field(i)
				}

				ss.Set(self.convertCallParameter(o.get(k), ss.Type()))
			}

			return s.Elem()
		}
	}

	if tk == reflect.String {
		if o := v._object(); o != nil && o.hasProperty("toString") {
			if fn := o.get("toString"); fn.IsFunction() {
				sv, err := fn.Call(v)
				if err != nil {
					panic(err)
				}

				var r reflect.Value
				if err := catchPanic(func() { r = self.convertCallParameter(sv, t) }); err == nil {
					return r
				}
			}
		}

		return reflect.ValueOf(v.String())
	}

	if v.kind == valueString {
		var s encoding.TextUnmarshaler

		if reflect.PtrTo(t).Implements(reflect.TypeOf(&s).Elem()) {
			r := reflect.New(t)

			if err := r.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(v.string())); err != nil {
				panic(self.panicSyntaxError("can't convert to %s: %s", t.String(), err.Error()))
			}

			return r.Elem()
		}
	}

	s := "OTTO DOES NOT UNDERSTAND THIS TYPE"
	switch v.kind {
	case valueBoolean:
		s = "boolean"
	case valueNull:
		s = "null"
	case valueNumber:
		s = "number"
	case valueString:
		s = "string"
	case valueUndefined:
		s = "undefined"
	case valueObject:
		s = v.Class()
	}

	panic(self.panicTypeError("can't convert from %q to %q", s, t.String()))
}
74155
func Runtime(term *ast.Term) func(*Basic) {
	return func(b *Basic) {
		b.runtime = term
	}
}
736110
func (s *CountService) Type(typ ...string) *CountService {
	if s.typ == nil {
		s.typ = make([]string, 0)
	}
	s.typ = append(s.typ, typ...)
	return s
}
1106847
func coerce(v interface{}) interface{} {
	switch val := v.(type) {
	case string:
		return v
	// Yes, these are all the same, however we can't put them into a single
	// case, or the value becomes interface{}, which can't be converted to a
	// float64.
	case int:
		return float64(val)
	case int8:
		return float64(val)
	case int16:
		return float64(val)
	case int32:
		return float64(val)
	case int64:
		return float64(val)
	case uint:
		return float64(val)
	case uint8:
		return float64(val)
	case uint16:
		return float64(val)
	case uint32:
		return float64(val)
	case uint64:
		return float64(val)
	case float32:
		return float64(val)
	case float64:
		return val
	}
	return v
}