// EnumName is a helper function to simplify printing protocol buffer enums
// by name.  Given an enum map and a value, it returns a useful string.
487309
func EnumName(m map[int32]string, v int32) string {
	s, ok := m[v]
	if ok {
		return s
	}
	return strconv.Itoa(int(v))
}
205902
func (n *lowercaseSingularNamer) Name(t *types.Type) string {
	return strings.ToLower(t.Name.Name)
}
1215475
func ToJSONName(name string) string {
	in := split(name)
	out := make([]string, 0, len(in))

	for i, w := range in {
		if i == 0 {
			out = append(out, lower(w))
			continue
		}
		out = append(out, Camelize(w))
	}
	return strings.Join(out, "")
}
896816
func (f *FileDescriptor) GetEnum(name string) *EnumDescriptor {
	for _, e := range f.GetEnums() {
		if e.GetName() == name || e.GetLongName() == name {
			return e
		}
	}

	return nil
}
1164872
func (r *rawNamer) Name(t *types.Type) string {
	if r.Names == nil {
		r.Names = Names{}
	}
	if name, ok := r.Names[t]; ok {
		return name
	}
	if t.Name.Package != "" {
		var name string
		if r.tracker != nil {
			r.tracker.AddType(t)
			if t.Name.Package == r.pkg {
				name = t.Name.Name
			} else {
				name = r.tracker.LocalNameOf(t.Name.Package) + "." + t.Name.Name
			}
		} else {
			if t.Name.Package == r.pkg {
				name = t.Name.Name
			} else {
				name = filepath.Base(t.Name.Package) + "." + t.Name.Name
			}
		}
		r.Names[t] = name
		return name
	}
	var name string
	switch t.Kind {
	case types.Builtin:
		name = t.Name.Name
	case types.Map:
		name = "map[" + r.Name(t.Key) + "]" + r.Name(t.Elem)
	case types.Slice:
		name = "[]" + r.Name(t.Elem)
	case types.Pointer:
		name = "*" + r.Name(t.Elem)
	case types.Struct:
		elems := []string{}
		for _, m := range t.Members {
			elems = append(elems, m.Name+" "+r.Name(m.Type))
		}
		name = "struct{" + strings.Join(elems, "; ") + "}"
	case types.Chan:
		// TODO: include directionality
		name = "chan " + r.Name(t.Elem)
	case types.Interface:
		// TODO: add to name test
		elems := []string{}
		for _, m := range t.Methods {
			// TODO: include function signature
			elems = append(elems, m.Name.Name)
		}
		name = "interface{" + strings.Join(elems, "; ") + "}"
	case types.Func:
		// TODO: add to name test
		params := []string{}
		for _, pt := range t.Signature.Parameters {
			params = append(params, r.Name(pt))
		}
		results := []string{}
		for _, rt := range t.Signature.Results {
			results = append(results, r.Name(rt))
		}
		name = "func(" + strings.Join(params, ",") + ")"
		if len(results) == 1 {
			name += " " + results[0]
		} else if len(results) > 1 {
			name += " (" + strings.Join(results, ",") + ")"
		}
	default:
		name = "unnameable_" + string(t.Kind)
	}
	r.Names[t] = name
	return name
}
216592
func typeName(g Generator, spec compile.TypeSpec) (string, error) {
	switch s := spec.(type) {
	case *compile.BoolSpec:
		return "bool", nil
	case *compile.I8Spec:
		return "int8", nil
	case *compile.I16Spec:
		return "int16", nil
	case *compile.I32Spec:
		return "int32", nil
	case *compile.I64Spec:
		return "int64", nil
	case *compile.DoubleSpec:
		return "float64", nil
	case *compile.StringSpec:
		return "string", nil
	case *compile.BinarySpec:
		return "[]byte", nil
	case *compile.MapSpec:
		k, err := typeReference(g, s.KeySpec)
		if err != nil {
			return "", err
		}
		v, err := typeReference(g, s.ValueSpec)
		if err != nil {
			return "", err
		}
		if !isHashable(s.KeySpec) {
			// unhashable type
			return fmt.Sprintf("[]struct{Key %s; Value %s}", k, v), nil
		}
		return fmt.Sprintf("map[%s]%s", k, v), nil
	case *compile.ListSpec:
		v, err := typeReference(g, s.ValueSpec)
		if err != nil {
			return "", err
		}
		return "[]" + v, nil
	case *compile.SetSpec:
		v, err := typeReference(g, s.ValueSpec)
		if err != nil {
			return "", err
		}
		// not annotated to be slice and hashable value type
		if setUsesMap(s) {
			return fmt.Sprintf("map[%s]struct{}", v), nil
		}
		return fmt.Sprintf("[]%s", v), nil
	case *compile.EnumSpec, *compile.StructSpec, *compile.TypedefSpec:
		return g.LookupTypeName(spec)
	default:
		panic(fmt.Sprintf("Unknown type (%T) %v", spec, spec))
	}
}
7635
func GetScancodeFromName(name string) Scancode {
	_name := C.CString(name)
	defer C.free(unsafe.Pointer(_name))
	return (Scancode)(C.SDL_GetScancodeFromName(_name))
}
14769
func serviceName(name string) string {
	aname := make([]byte, len(name))

	for i, r := range []byte(name) {
		if !labelRe.Match([]byte{r}) {
			aname[i] = '_'
			continue
		}
		aname[i] = r
	}

	return string(aname)
}
663236
func EnumValidator(param string, v reflect.Value) (ValidationResult, error) {
	if param == "" {
		return 0, ErrInvalidConfigValue
	}

	params := strings.Split(param, "|")

	var enum func(v reflect.Value) (ValidationResult, error)
	enum = func(v reflect.Value) (ValidationResult, error) {
		if v.Kind() == reflect.Ptr {
			v = v.Elem()
		}
		switch v.Kind() {
		case reflect.String:
			val := v.String()
			if val == "" {
				// need empty checking? use req :)
				return ValidationOK, nil
			}
			for _, value := range params {
				if val == value {
					return ValidationOK, nil
				}
			}
			return ValidationNG, nil
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			val := v.Int()
			for _, value := range params {
				value2, err := strconv.ParseInt(value, 10, 0)
				if err != nil {
					return 0, ErrInvalidConfigValue
				}
				if val == value2 {
					return ValidationOK, nil
				}
			}
			return ValidationNG, nil
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
			val := v.Uint()
			for _, value := range params {
				value2, err := strconv.ParseUint(value, 10, 0)
				if err != nil {
					return 0, ErrInvalidConfigValue
				}
				if val == value2 {
					return ValidationOK, nil
				}
			}
			return ValidationNG, nil
		case reflect.Array, reflect.Slice:
			for i := 0; i < v.Len(); i++ {
				e := v.Index(i)
				return enum(e)
			}
		default:
			return 0, ErrValidateUnsupportedType
		}

		return ValidationOK, nil
	}

	return enum(v)
}
605975
func PgEnumByEnumtypidEnumlabel(db XODB, enumtypid pgtypes.Oid, enumlabel pgtypes.Name) (*PgEnum, error) {
	var err error

	// sql query
	const sqlstr = `SELECT ` +
		`tableoid, cmax, xmax, cmin, xmin, oid, ctid, enumtypid, enumsortorder, enumlabel ` +
		`FROM pg_catalog.pg_enum ` +
		`WHERE enumtypid = $1 AND enumlabel = $2`

	// run query
	XOLog(sqlstr, enumtypid, enumlabel)
	pe := PgEnum{}

	err = db.QueryRow(sqlstr, enumtypid, enumlabel).Scan(&pe.Tableoid, &pe.Cmax, &pe.Xmax, &pe.Cmin, &pe.Xmin, &pe.Oid, &pe.Ctid, &pe.Enumtypid, &pe.Enumsortorder, &pe.Enumlabel)
	if err != nil {
		return nil, err
	}

	return &pe, nil
}
66914
func Enum(path, in string, data interface{}, enum interface{}) *errors.Validation {
	val := reflect.ValueOf(enum)
	if val.Kind() != reflect.Slice {
		return nil
	}

	var values []interface{}
	for i := 0; i < val.Len(); i++ {
		ele := val.Index(i)
		enumValue := ele.Interface()
		if data != nil {
			if reflect.DeepEqual(data, enumValue) {
				return nil
			}
			actualType := reflect.TypeOf(enumValue)
			if actualType == nil { // Safeguard. Frankly, I don't know how we may get a nil
				continue
			}
			expectedValue := reflect.ValueOf(data)
			if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
				// Attempt comparison after type conversion
				if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), enumValue) {
					return nil
				}
			}
		}
		values = append(values, enumValue)
	}
	return errors.EnumFail(path, in, data, values)
}
33606
func newEnum(structName string, prop raml.Property, pkg string, fromStruct bool) *enum {
	e := enum{
		Name: strings.Title(structName) + strings.Title(prop.Name),
		Type: convertToGoType(prop.TypeString(), prop.Items.Type),
		Pkg:  pkg,
	}
	if !fromStruct {
		e.Name = "Enum" + e.Name
	}
	for _, v := range prop.Enum.([]interface{}) {
		e.Fields = append(e.Fields, newEnumField(v, e))
	}
	return &e
}
748564
func structName(input string) string {

	// Remove ::
	output := strings.Replace(input, "::", "", -1)

	// Remove .
	output = strings.Replace(output, ".", "_", -1)

	return output

}
257406
func FromName(name string) (serviceName string, methodName string) {
	parts := strings.SplitN(name, "::", 2)
	if len(parts) == 1 {
		return parts[0], ""
	}
	return parts[0], parts[1]
}
351032
func (r Row) GetEnum(colIdx int) types.Enum {
	name, val := r.getNameValue(colIdx)
	return types.Enum{Name: name, Value: val}
}
911136
func handlerName(h handlerFunc) string {
	name := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
	parts := nameRegexp.FindStringSubmatch(name)
	if len(parts) != 2 {
		return ""
	}
	return parts[1]
}
787312
func (r *Registry) LookupEnum(location, name string) (*Enum, error) {
	glog.V(1).Infof("lookup enum %s from %s", name, location)
	if strings.HasPrefix(name, ".") {
		e, ok := r.enums[name]
		if !ok {
			return nil, fmt.Errorf("no enum found: %s", name)
		}
		return e, nil
	}

	if !strings.HasPrefix(location, ".") {
		location = fmt.Sprintf(".%s", location)
	}
	components := strings.Split(location, ".")
	for len(components) > 0 {
		fqen := strings.Join(append(components, name), ".")
		if e, ok := r.enums[fqen]; ok {
			return e, nil
		}
		components = components[:len(components)-1]
	}
	return nil, fmt.Errorf("no enum found: %s", name)
}