// ToWire generates an expression of type (Value, error) object containing the
// wire representation of the variable $varName of type $spec or an error.
550731
func (w *WireGenerator) ToWire(g Generator, spec compile.TypeSpec, varName string) (string, error) {
	wire := g.Import("go.uber.org/thriftrw/wire")
	switch s := spec.(type) {
	case *compile.BoolSpec:
		return fmt.Sprintf("%s.NewValueBool(%s), error(nil)", wire, varName), nil
	case *compile.I8Spec:
		return fmt.Sprintf("%s.NewValueI8(%s), error(nil)", wire, varName), nil
	case *compile.I16Spec:
		return fmt.Sprintf("%s.NewValueI16(%s), error(nil)", wire, varName), nil
	case *compile.I32Spec:
		return fmt.Sprintf("%s.NewValueI32(%s), error(nil)", wire, varName), nil
	case *compile.I64Spec:
		return fmt.Sprintf("%s.NewValueI64(%s), error(nil)", wire, varName), nil
	case *compile.DoubleSpec:
		return fmt.Sprintf("%s.NewValueDouble(%s), error(nil)", wire, varName), nil
	case *compile.StringSpec:
		return fmt.Sprintf("%s.NewValueString(%s), error(nil)", wire, varName), nil
	case *compile.BinarySpec:
		return fmt.Sprintf("%s.NewValueBinary(%s), error(nil)", wire, varName), nil
	case *compile.MapSpec:
		mapItemList, err := w.mapG.ItemList(g, s)
		if err != nil {
			return "", err
		}

		return g.TextTemplate(
			`<.Wire>.NewValueMap(<.MapItemList>(<.Name>)), error(nil)`,
			struct {
				Wire        string
				Name        string
				Spec        *compile.MapSpec
				MapItemList string
			}{Wire: wire, Name: varName, Spec: s, MapItemList: mapItemList},
		)
	case *compile.ListSpec:
		valueList, err := w.listG.ValueList(g, s)
		if err != nil {
			return "", err
		}

		return g.TextTemplate(
			`<.Wire>.NewValueList(<.ValueList>(<.Name>)), error(nil)`,
			struct {
				Wire      string
				Name      string
				Spec      *compile.ListSpec
				ValueList string
			}{Wire: wire, Name: varName, Spec: s, ValueList: valueList},
		)
	case *compile.SetSpec:
		valueList, err := w.setG.ValueList(g, s)
		if err != nil {
			return "", err
		}

		return g.TextTemplate(
			`<.Wire>.NewValueSet(<.ValueList>(<.Name>)), error(nil)`,
			struct {
				Wire      string
				Name      string
				Spec      *compile.SetSpec
				ValueList string
			}{Wire: wire, Name: varName, Spec: s, ValueList: valueList},
		)
	default:
		// Custom defined type
		return fmt.Sprintf("%s.ToWire()", varName), nil
	}
}
102473
func (f genHelperDecoder) Extension(rtid uintptr) (xfn *extTypeTagFn) {
	return f.d.h.getExt(rtid)
}
193966
func (err *ErrEvidenceInvalid) Error() string {
	return fmt.Sprintf("Invalid evidence: %v. Evidence: %v", err.ErrorValue, err.Evidence)
}
786254
func (f InterceptorFunc) Intercept(c Chain) (*http.Response, error) {
	return f(c)
}
293832
func (env *joyentEnviron) ConstraintsValidator(ctx context.ProviderCallContext) (constraints.Validator, error) {
	validator := constraints.NewValidator()
	validator.RegisterUnsupported(unsupportedConstraints)
	packages, err := env.compute.cloudapi.ListPackages(nil)
	if err != nil {
		return nil, err
	}
	instTypeNames := make([]string, len(packages))
	for i, pkg := range packages {
		instTypeNames[i] = pkg.Name
	}
	validator.RegisterVocabulary(constraints.InstanceType, instTypeNames)
	return validator, nil
}
948306
func (c *SubjectError) Error() string {
	return fmt.Sprintf("%s: %s", c.error.Error(), c.subject)
}
217668
func makeError(m []KV) error {
	kv := findRec(m, "ERROR")
	if kv.Key == "" {
		return &Error{Message: "generic error"}
	}
	return &Error{Message: string(kv.Value)}
}
824115
func TooShort(name, in string, min int64) *Validation {
	var msg string
	if in == "" {
		msg = fmt.Sprintf(tooShortMessageNoIn, name, min)
	} else {
		msg = fmt.Sprintf(tooShortMessage, name, in, min)
	}

	return &Validation{
		code:    TooShortFailCode,
		Name:    name,
		In:      in,
		message: msg,
	}
}
877549
func HeaderExtractor(headers []string) func(*http.Request) string {
	return func(r *http.Request) string {
		for _, h := range headers {
			v := r.Header.Get(h)
			if v != "" {
				return v
			}
		}

		return ""
	}
}
433497
func (r Spec) String() string {
	if r.Object == "" {
		return r.Locator
	}
	if r.Object[:1] == "@" {
		return fmt.Sprintf("%v%v", r.Locator, r.Object)
	}

	return fmt.Sprintf("%v:%v", r.Locator, r.Object)
}
349814
func ToWikiPageName(urlString string) string {
	name, _ := url.QueryUnescape(urlString)
	return strings.Replace(strings.TrimLeft(path.Clean("/"+name), "/"), "/", " ", -1)
}
1097610
func RequiredValidator(input string) (interface{}, error) {
	if input == "" {
		return nil, fmt.Errorf("This option is required")
	}

	return input, nil
}
641250
func baseType(t reflect.Type, expected reflect.Kind) (reflect.Type, error) {
	t = reflectx.Deref(t)
	if t.Kind() != expected {
		return nil, fmt.Errorf("expected %s but got %s", expected, t.Kind())
	}
	return t, nil
}
95949
func (x *genRunner) dec(varname string, t reflect.Type, isptr bool) {
	// assumptions:
	//   - the varname is to a pointer already. No need to take address of it
	//   - t is always a baseType T (not a *T, etc).
	rtid := rt2id(t)
	ti2 := x.ti.get(rtid, t)
	// tptr := reflect.PtrTo(t)
	if x.checkForSelfer(t, varname) {
		if ti2.cs || ti2.csp { // t.Implements(selferTyp) || tptr.Implements(selferTyp) {
			x.line(varname + ".CodecDecodeSelf(d)")
			return
		}
		if _, ok := x.td[rtid]; ok {
			x.line(varname + ".CodecDecodeSelf(d)")
			return
		}
	}

	inlist := false
	for _, t0 := range x.t {
		if t == t0 {
			inlist = true
			if x.checkForSelfer(t, varname) {
				x.line(varname + ".CodecDecodeSelf(d)")
				return
			}
			break
		}
	}

	var rtidAdded bool
	if t == x.tc {
		x.td[rtid] = true
		rtidAdded = true
	}

	// check if
	//   - type is time.Time, Raw, RawExt
	//   - the type implements (Text|JSON|Binary)(Unm|M)arshal

	mi := x.varsfx()
	// x.linef("%sm%s := z.DecBinary()", genTempVarPfx, mi)
	// x.linef("_ = %sm%s", genTempVarPfx, mi)
	x.line("if false {")           //start if block
	defer func() { x.line("}") }() //end if block

	var ptrPfx, addrPfx string
	if isptr {
		ptrPfx = "*"
	} else {
		addrPfx = "&"
	}
	if t == timeTyp {
		x.linef("} else if !z.DecBasicHandle().TimeNotBuiltin { %s%v = r.DecodeTime()", ptrPfx, varname)
		// return
	}
	if t == rawTyp {
		x.linef("} else { %s%v = z.DecRaw()", ptrPfx, varname)
		return
	}

	if t == rawExtTyp {
		x.linef("} else { r.DecodeExt(%s%v, 0, nil)", addrPfx, varname)
		return
	}

	// only check for extensions if the type is named, and has a packagePath.
	if !x.nx && genImportPath(t) != "" && t.Name() != "" {
		// first check if extensions are configued, before doing the interface conversion
		// x.linef("} else if z.HasExtensions() && z.DecExt(%s) {", varname)
		yy := fmt.Sprintf("%sxt%s", genTempVarPfx, mi)
		x.linef("} else if %s := z.Extension(z.I2Rtid(%s)); %s != nil { z.DecExtension(%s, %s) ", yy, varname, yy, varname, yy)
	}

	if ti2.bu || ti2.bup { // t.Implements(binaryUnmarshalerTyp) || tptr.Implements(binaryUnmarshalerTyp) {
		x.linef("} else if z.DecBinary() { z.DecBinaryUnmarshal(%s%v) ", addrPfx, varname)
	}
	if ti2.ju || ti2.jup { // t.Implements(jsonUnmarshalerTyp) || tptr.Implements(jsonUnmarshalerTyp) {
		x.linef("} else if !z.DecBinary() && z.IsJSONHandle() { z.DecJSONUnmarshal(%s%v)", addrPfx, varname)
	} else if ti2.tu || ti2.tup { // t.Implements(textUnmarshalerTyp) || tptr.Implements(textUnmarshalerTyp) {
		x.linef("} else if !z.DecBinary() { z.DecTextUnmarshal(%s%v)", addrPfx, varname)
	}

	x.line("} else {")

	if x.decTryAssignPrimitive(varname, t, isptr) {
		return
	}

	switch t.Kind() {
	case reflect.Array, reflect.Chan:
		x.xtraSM(varname, t, false, isptr)
	case reflect.Slice:
		// if a []uint8, call dedicated function
		// if a known fastpath slice, call dedicated function
		// else write encode function in-line.
		// - if elements are primitives or Selfers, call dedicated function on each member.
		// - else call Encoder.encode(XXX) on it.
		if rtid == uint8SliceTypId {
			x.linef("%s%s = r.DecodeBytes(%s(%s[]byte)(%s), false)",
				ptrPfx, varname, ptrPfx, ptrPfx, varname)
		} else if fastpathAV.index(rtid) != -1 {
			g := x.newGenV(t)
			x.linef("z.F.%sX(%s%s, d)", g.MethodNamePfx("Dec", false), addrPfx, varname)
		} else {
			x.xtraSM(varname, t, false, isptr)
			// x.decListFallback(varname, rtid, false, t)
		}
	case reflect.Map:
		// if a known fastpath map, call dedicated function
		// else write encode function in-line.
		// - if elements are primitives or Selfers, call dedicated function on each member.
		// - else call Encoder.encode(XXX) on it.
		if fastpathAV.index(rtid) != -1 {
			g := x.newGenV(t)
			x.linef("z.F.%sX(%s%s, d)", g.MethodNamePfx("Dec", false), addrPfx, varname)
		} else {
			x.xtraSM(varname, t, false, isptr)
			// x.decMapFallback(varname, rtid, t)
		}
	case reflect.Struct:
		if inlist {
			// no need to create temp variable if isptr, or x.F or x[F]
			if isptr || strings.IndexByte(varname, '.') != -1 || strings.IndexByte(varname, '[') != -1 {
				x.decStruct(varname, rtid, t)
			} else {
				varname2 := genTempVarPfx + "j" + mi
				x.line(varname2 + " := &" + varname)
				x.decStruct(varname2, rtid, t)
			}
		} else {
			// delete(x.td, rtid)
			x.line("z.DecFallback(" + addrPfx + varname + ", false)")
		}
	default:
		if rtidAdded {
			delete(x.te, rtid)
		}
		x.line("z.DecFallback(" + addrPfx + varname + ", true)")
	}
}
207039
func AssertType(v interface{}, t reflect.Type) reflect.Value {
	rv := reflect.ValueOf(v)
	tv := rv.Type()
	if tv != t {
		ppe("Value '%v' has type '%s' but expected '%s'.", v, tv, t)
	}
	return rv
}
778058
func (v ModuleID) ToWire() (wire.Value, error) {
	x := (int32)(v)
	return wire.NewValueI32(x), error(nil)
}
213689
func SpecificationError(detail string) *Error {
	return &Error{
		Title:  "JSON API Specification Error",
		Detail: detail,
		Status: http.StatusNotAcceptable,
	}
}