本文整理汇总了Golang中reflect.Type.PkgPath方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.PkgPath方法的具体用法?Golang Type.PkgPath怎么用?Golang Type.PkgPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.PkgPath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: defaultTableName
// Returns the default table name for a type
func defaultTableName(typ reflect.Type) string {
n := typ.Name()
if p := typ.PkgPath(); !strings.HasPrefix(p, "main") {
n = strings.Replace(p, "/", "_", -1) + n
}
return stringutil.CamelCaseToLower(n, "_")
}
示例2: 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() == ""
}
示例3: addImportForType
// Add an import for the supplied type, without recursing.
func addImportForType(imports importMap, t reflect.Type) {
// If there is no package path, this is a built-in type and we don't need an
// import.
pkgPath := t.PkgPath()
if pkgPath == "" {
return
}
// Work around a bug in Go:
//
// http://code.google.com/p/go/issues/detail?id=2660
//
var errorPtr *error
if t == reflect.TypeOf(errorPtr).Elem() {
return
}
// Use the identifier that's part of the type's string representation as the
// import identifier. This means that we'll do the right thing for package
// "foo/bar" with declaration "package baz".
match := typePackageIdentifierRegexp.FindStringSubmatch(t.String())
if match == nil {
return
}
imports[match[1]] = pkgPath
}
示例4: typeNamesEqual
func typeNamesEqual(methVal reflect.Type, name2 string) bool {
if strings.Index(name2, ".") == -1 {
return methVal.Name() == name2
}
fullName := strings.Replace(methVal.PkgPath(), "/", ".", -1) + "." + methVal.Name()
return fullName == name2
}
示例5: checkStructureIsInTheSamePackage
func checkStructureIsInTheSamePackage(packagePath string, basicType reflect.Type, encountered map[reflect.Type]bool) error {
if encountered == nil {
encountered = make(map[reflect.Type]bool)
}
if encountered[basicType] {
return nil
}
encountered[basicType] = true
pkgPath := basicType.PkgPath()
if basicType.Kind() == reflect.Slice {
return checkStructureIsInTheSamePackage(packagePath, basicType.Elem(), encountered)
} else if basicType.Kind() == reflect.Ptr {
return checkStructureIsInTheSamePackage(packagePath, basicType.Elem(), encountered)
} else if len(pkgPath) == 0 || isPrimitiveType(basicType) {
return nil
} else if _, exception := isSamePackagePathException[pkgPath]; exception {
return nil
} else if pkgPath != packagePath {
return fmt.Errorf(`Structure must be fully defined in the same package. Type '%s' is not.`, basicType)
} else if basicType.Kind() == reflect.Struct {
for i := 0; i < basicType.NumField(); i++ {
err := checkStructureIsInTheSamePackage(packagePath, basicType.Field(i).Type, encountered)
if err != nil {
return err
}
}
return nil
}
return errors.New(`Unreachable code`)
}
示例6: checkExternalJsonTags
func checkExternalJsonTags(objType reflect.Type, seen *map[reflect.Type]bool, t *testing.T) {
if _, exists := (*seen)[objType]; exists {
return
}
(*seen)[objType] = true
if !strings.Contains(objType.PkgPath(), "github.com/openshift/origin/pkg") {
return
}
for i := 0; i < objType.NumField(); i++ {
structField := objType.FieldByIndex([]int{i})
jsonTag := structField.Tag.Get("json")
if len(jsonTag) == 0 {
t.Errorf("%v.%v should have a json tag", objType, structField.Name)
}
switch structField.Type.Kind() {
case reflect.Struct:
checkExternalJsonTags(structField.Type, seen, t)
case reflect.Ptr:
checkExternalJsonTags(structField.Type.Elem(), seen, t)
}
}
}
示例7: 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
}
示例8: 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()))
}
示例9: 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)
}
示例10: 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
}
示例11: getOrCreateSchema
func getOrCreateSchema(definitions Definitions, t reflect.Type) *Schema {
var result Schema
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Map {
if t.Key().Kind() != reflect.String {
panic("swagger supports only maps with string keys")
}
result.Type = "object"
result.AdditionalProperties = getOrCreateSchema(definitions, t.Elem())
return &result
}
if t.Kind() == reflect.Interface {
result.Type = "object"
return &result
}
result.Type = typeName(t)
if result.Type == "object" {
name := t.PkgPath() + "/" + t.String()
if _, ok := definitions[name]; ok {
result = Schema{Ref: "#/definitions/" + name}
return &result
}
definitions[name] = result
if t.NumField() > 0 {
result.Properties = Properties{}
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.PkgPath != "" {
continue
}
name := field.Tag.Get("json")
if name == "" {
name = field.Tag.Get("key")
if name == "" {
name = field.Name
}
}
if field.Type.Kind() != reflect.Ptr {
result.Required = append(result.Required, name)
}
fieldSchema := getOrCreateSchema(definitions, field.Type)
fieldSchema.Description = field.Tag.Get("description")
result.Properties[name] = fieldSchema
}
definitions[name] = result
result = Schema{Ref: "#/definitions/" + name}
} else if result.Type == "array" {
itemsSchema := getOrCreateSchema(definitions, t.Elem())
result.Items = &Items{*itemsSchema}
}
return &result
}
示例12: GetTypeTemplate
// GetObjectTemplate retrieves a template given a certain type
func (m *Manager) GetTypeTemplate(themeName, packageName string, t reflect.Type, view string) (*template.Template, error) {
logger.Debug("Getting type template=%v+%v", t.PkgPath(), t.Name())
name := filepath.Join(t.PkgPath(), t.Name())
logger.Debug("Theme: %v, Package: %v, View: %v, method: %v, tname: %v, pkg: %v", themeName, packageName, name, view, t.Name(), t.PkgPath())
return m.GetTemplate(themeName, packageName, name, view+m.extension)
}
示例13: progType
// progType returns the go/types type for the given reflect.Type,
// which must represent a named non-predeclared Go type.
func progType(prog *loader.Program, t reflect.Type) (*types.TypeName, error) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
typeName := t.Name()
if typeName == "" {
return nil, errgo.Newf("type %s is not named", t)
}
pkgPath := t.PkgPath()
if pkgPath == "" {
// TODO could return types.Basic type here if we needed to.
return nil, errgo.Newf("type %s not declared in package", t)
}
pkgInfo := prog.Package(pkgPath)
if pkgInfo == nil {
return nil, errgo.Newf("cannot find %q in imported code", pkgPath)
}
pkg := pkgInfo.Pkg
obj := pkg.Scope().Lookup(typeName)
if obj == nil {
return nil, errgo.Newf("type %s not found in %s", typeName, pkgPath)
}
objTypeName, ok := obj.(*types.TypeName)
if !ok {
return nil, errgo.Newf("%s is not a type", typeName)
}
return objTypeName, nil
}
示例14: getAlias
func (this *FileInfo) getAlias(packet string, fieldType reflect.Type) (string, bool) {
stdType := true
alias := fieldType.String()
pktPath := fieldType.PkgPath()
if len(pktPath) > 0 {
stdType = false
if fieldType.Kind() == reflect.Ptr {
if pktPath != "" && pktPath != packet {
if packet, found := this.Imports[pktPath]; !found {
ref := alias
if idx := strings.Index(ref, "."); idx >= 0 {
ref = ref[:idx]
}
if _, found := this.RefImport[ref]; found {
ref = genPacketAliasMD5(pktPath)
alias = ref + "." + fieldType.Name()
}
this.RefImport[ref] = pktPath
this.Imports[pktPath] = packet
} else {
alias = packet + "." + pktPath
}
}
}
}
return alias, stdType
}
示例15: AddExt
// AddExt registers an encode and decode function for a reflect.Type.
// Note that the type must be a named type, and specifically not
// a pointer or Interface. An error is returned if that is not honored.
func (o *extHandle) AddExt(
rt reflect.Type,
tag byte,
encfn func(reflect.Value) ([]byte, error),
decfn func(reflect.Value, []byte) error,
) (err error) {
// o is a pointer, because we may need to initialize it
if rt.PkgPath() == "" || rt.Kind() == reflect.Interface {
err = fmt.Errorf("codec.Handle.AddExt: Takes a named type, especially not a pointer or interface: %T",
reflect.Zero(rt).Interface())
return
}
if o == nil {
err = errors.New("codec.Handle.AddExt: Nil (should never happen)")
return
}
rtid := reflect.ValueOf(rt).Pointer()
if *o == nil {
*o = make(map[uintptr]*extTypeTagFn, 4)
}
m := *o
if encfn == nil || decfn == nil {
delete(m, rtid)
} else {
m[rtid] = &extTypeTagFn{rtid, rt, tag, encfn, decfn}
}
return
}