本文整理汇总了Golang中reflect.Type.Name方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Name方法的具体用法?Golang Type.Name怎么用?Golang Type.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.Name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getReflectTypeName
func getReflectTypeName(reflectType reflect.Type) (string, error) {
buffer := bytes.NewBuffer(nil)
for reflectType.Kind() == reflect.Ptr {
if _, err := buffer.WriteString("*"); err != nil {
return "", err
}
reflectType = reflectType.Elem()
}
pkgPath := reflectType.PkgPath()
if pkgPath == "" {
return "", fmt.Errorf("ledge: no package for type %v", reflectType)
}
if _, err := buffer.WriteString("\""); err != nil {
return "", err
}
if _, err := buffer.WriteString(pkgPath); err != nil {
return "", err
}
if _, err := buffer.WriteString("\""); err != nil {
return "", err
}
name := reflectType.Name()
if name == "" {
return "", fmt.Errorf("ledge: no name for type %v", reflectType)
}
if _, err := buffer.WriteString("."); err != nil {
return "", err
}
if _, err := buffer.WriteString(name); err != nil {
return "", err
}
return buffer.String(), nil
}
示例2: getTypName
// getTypName returns a string representing the name of the object typ.
// if the name is defined then it is used, otherwise, the name is derived from the
// Stringer interface.
//
// the stringer returns something like *somepkg.MyStruct, so skip
// the *somepkg and return MyStruct
func getTypName(typ reflect.Type) string {
if typ.Name() != "" {
return typ.Name()
}
split := strings.Split(typ.String(), ".")
return split[len(split)-1]
}
示例3: ToSqlType
func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string {
switch val.Kind() {
case reflect.Ptr:
return d.ToSqlType(val.Elem(), maxsize, isAutoIncr)
case reflect.Bool:
return "integer"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "integer"
case reflect.Float64, reflect.Float32:
return "real"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "blob"
}
}
switch val.Name() {
case "NullInt64":
return "integer"
case "NullFloat64":
return "real"
case "NullBool":
return "integer"
case "Time":
return "datetime"
}
if maxsize < 1 {
maxsize = 255
}
return fmt.Sprintf("varchar(%d)", maxsize)
}
示例4: rawSelectByStruct
func (db *DB) rawSelectByStruct(structType reflect.Type, qi SqlQueryInfo) (rows *sql.Rows, fields []string, err error) {
// nums of struct's fields
lf := structType.NumField()
// type's fields
fields = make([]string, 0, lf)
// sql select columns, it's Snake Cased
columns := make([]string, 0, lf)
// get fields in structType,
// and convert to sql query column name
for i := 0; i < lf; i++ {
structField := structType.Field(i)
fieldName := structField.Name
fields = append(fields, fieldName)
columns = append(columns, utils.SnakeCasedName(fieldName))
}
// tableName := utils.SnakeCasedName(utils.StructName(s))
tableName := utils.SnakeCasedName(structType.Name())
// TODO: check the fileds has specified ?
qi.Fields = strings.Join(columns, ", ")
// run query from db
rows, err = db.Select(tableName, qi)
return
}
示例5: defaultType
func defaultType(t reflect.Type) TypeDesc {
switch t.Kind() {
case reflect.Bool:
return BooleanType
case reflect.String:
return UTF8Type
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return LongType
case reflect.Float32:
return FloatType
case reflect.Float64:
return DoubleType
case reflect.Array:
if t.Name() == "UUID" && t.Size() == 16 {
return UUIDType
}
return UnknownType
case reflect.Struct:
if t.Name() == "Time" && t.PkgPath() == "time" {
return DateType
}
return UnknownType
case reflect.Slice:
if et := t.Elem(); et.Kind() == reflect.Uint8 {
return BytesType
}
return UnknownType
}
return UnknownType
}
示例6: generate
func (g *schemaGenerator) generate(t reflect.Type) (*JSONSchema, error) {
if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("Only struct types can be converted.")
}
s := JSONSchema{
ID: "http://fabric8.io/fabric8/v2/" + t.Name() + "#",
Schema: "http://json-schema.org/schema#",
JSONDescriptor: JSONDescriptor{
Type: "object",
},
}
s.JSONObjectDescriptor = g.generateObjectDescriptor(t)
if len(g.types) > 0 {
s.Definitions = make(map[string]JSONPropertyDescriptor)
for k, v := range g.types {
name := g.qualifiedName(k)
value := JSONPropertyDescriptor{
JSONDescriptor: &JSONDescriptor{
Type: "object",
},
JSONObjectDescriptor: v,
JavaTypeDescriptor: &JavaTypeDescriptor{
JavaType: g.javaType(k),
},
JavaInterfacesDescriptor: &JavaInterfacesDescriptor{
JavaInterfaces: g.javaInterfaces(k),
},
}
s.Definitions[name] = value
}
}
return &s, nil
}
示例7: getTypeInfo
// The reflection type must have all its indirections processed out.
// typeLock must be held.
func getTypeInfo(rt reflect.Type) (*typeInfo, os.Error) {
if _, ok := rt.(*reflect.PtrType); ok {
panic("pointer type in getTypeInfo: " + rt.String())
}
info, ok := typeInfoMap[rt]
if !ok {
info = new(typeInfo)
name := rt.Name()
gt, err := getType(name, rt)
if err != nil {
return nil, err
}
info.id = gt.id()
t := info.id.gobType()
switch typ := rt.(type) {
case *reflect.ArrayType:
info.wire = &wireType{arrayT: t.(*arrayType)}
case *reflect.MapType:
info.wire = &wireType{mapT: t.(*mapType)}
case *reflect.SliceType:
// []byte == []uint8 is a special case handled separately
if _, ok := typ.Elem().(*reflect.Uint8Type); !ok {
info.wire = &wireType{sliceT: t.(*sliceType)}
}
case *reflect.StructType:
info.wire = &wireType{structT: t.(*structType)}
}
typeInfoMap[rt] = info
}
return info, nil
}
示例8: FieldType
func (b *Backend) FieldType(typ reflect.Type, t *structs.Tag) (string, error) {
if c := codec.FromTag(t); c != nil {
if c.Binary || t.PipeName() != "" {
return "BLOB", nil
}
return "TEXT", nil
}
switch typ.Kind() {
case reflect.Bool:
return "BOOLEAN", nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "INTEGER", nil
case reflect.Float32, reflect.Float64:
return "REAL", nil
case reflect.String:
return "TEXT", nil
case reflect.Slice:
// []byte
if typ.Elem().Kind() == reflect.Uint8 {
return "BLOB", nil
}
case reflect.Struct:
if typ.Name() == "Time" && typ.PkgPath() == "time" {
return "INTEGER", nil
}
}
return "", fmt.Errorf("can't map field type %v to a database type", typ)
}
示例9: isExportedOrBuiltinType
// Is this typoe exported or a builtin?
func isExportedOrBuiltinType(t reflect.Type) bool {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return isExported(t.Name()) || t.PkgPath() == ""
}
示例10: fromStruct
func fromStruct(name string, s reflect.Type) (t Table, err error) {
if name == "" {
if s.Name() == "" {
err = errors.New("please specify a name or use name type.")
return t, err
}
name = s.Name()
}
t.name = strings.ToLower(name)
n := s.NumField()
if n <= 0 {
return t, errors.New("struct doesn't have a field.")
}
for i := 0; i < n; i++ {
field := s.Field(i)
if field.Anonymous || !isExported(field) {
continue
}
t.fields = append(t.fields, strings.ToLower(field.Name))
t.fieldsIndex = append(t.fieldsIndex, i)
}
t.primaryKey, err = primaryKeys(s)
return t, err
}
示例11: GetTypeHash
// GetTypeHash returns the TypeHash for a given reflect.Type
func GetTypeHash(t reflect.Type) TypeHash {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return TypeHash(fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()))
}
示例12: createIndexImpl
func (m *DbMap) createIndexImpl(dialect reflect.Type,
table *TableMap,
index *IndexMap) error {
s := bytes.Buffer{}
s.WriteString("create")
if index.Unique {
s.WriteString(" unique")
}
s.WriteString(" index")
s.WriteString(fmt.Sprintf(" %s on %s", index.IndexName, table.TableName))
if dname := dialect.Name(); dname == "PostgresDialect" && index.IndexType != "" {
s.WriteString(fmt.Sprintf(" %s %s", m.Dialect.CreateIndexSuffix(), index.IndexType))
}
s.WriteString(" (")
for x, col := range index.columns {
if x > 0 {
s.WriteString(", ")
}
s.WriteString(m.Dialect.QuoteField(col))
}
s.WriteString(")")
if dname := dialect.Name(); dname == "MySQLDialect" && index.IndexType != "" {
s.WriteString(fmt.Sprintf(" %s %s", m.Dialect.CreateIndexSuffix(), index.IndexType))
}
s.WriteString(";")
_, err := m.Exec(s.String())
return err
}
示例13: emit
func (self *DB) emit(typ reflect.Type, oldValue, newValue *reflect.Value) (err error) {
if oldValue != nil && newValue != nil {
if chain := newValue.Addr().MethodByName("Updated"); chain.IsValid() {
if err = callErr(chain, []reflect.Value{reflect.ValueOf(self), oldValue.Addr()}); err != nil {
return
}
}
} else if newValue != nil {
if chain := newValue.Addr().MethodByName("Created"); chain.IsValid() {
if err = callErr(chain, []reflect.Value{reflect.ValueOf(self)}); err != nil {
return
}
}
} else if oldValue != nil {
if chain := oldValue.Addr().MethodByName("Deleted"); chain.IsValid() {
if err = callErr(chain, []reflect.Value{reflect.ValueOf(self)}); err != nil {
return
}
}
}
self.lock.RLock()
defer self.lock.RUnlock()
for _, subscription := range self.subscriptions[typ.Name()] {
go subscription.handle(typ, oldValue, newValue)
}
return
}
示例14: newStructEncoder
func newStructEncoder(t reflect.Type) encoderFunc {
n := t.NumField()
fields := make([]reflect.StructField, n)
for i := 0; i < n; i++ {
fields[i] = t.Field(i)
}
return func(e *encodeState, v reflect.Value, _ []byte) {
e.HeaderWrite(t.Name(), v)
for i := 0; i < n; i++ {
sf := fields[i]
f := v.Field(i)
name, mandatory := getName(sf)
if !f.IsValid() || isEmptyValue(f) || len(name) == 0 {
if mandatory {
// TODO this must be an error
panic("missing mandatory field")
}
continue
}
newTypeEncoder(sf.Type, false)(e, f, name)
}
e.WriteByte(']')
}
}
示例15: typeName
func (g *conversionGenerator) typeName(inType reflect.Type) string {
switch inType.Kind() {
case reflect.Slice:
return fmt.Sprintf("[]%s", g.typeName(inType.Elem()))
case reflect.Ptr:
return fmt.Sprintf("*%s", g.typeName(inType.Elem()))
case reflect.Map:
if len(inType.Name()) == 0 {
return fmt.Sprintf("map[%s]%s", g.typeName(inType.Key()), g.typeName(inType.Elem()))
}
fallthrough
default:
pkg, name := inType.PkgPath(), inType.Name()
if len(name) == 0 && inType.Kind() == reflect.Struct {
return "struct{}"
}
if len(pkg) == 0 {
// Default package.
return name
}
if val, found := g.pkgOverwrites[pkg]; found {
pkg = val
}
if len(pkg) == 0 {
return name
}
short := g.addImportByPath(pkg)
if len(short) > 0 {
return fmt.Sprintf("%s.%s", short, name)
}
return name
}
}