当前位置: 首页>>代码示例>>Golang>>正文


Golang Field.Pos方法代码示例

本文整理汇总了Golang中go/ast.Field.Pos方法的典型用法代码示例。如果您正苦于以下问题:Golang Field.Pos方法的具体用法?Golang Field.Pos怎么用?Golang Field.Pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在go/ast.Field的用法示例。


在下文中一共展示了Field.Pos方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: checkCanonicalFieldTag

// checkCanonicalFieldTag checks a single struct field tag.
func checkCanonicalFieldTag(f *File, field *ast.Field, seen *map[[2]string]token.Pos) {
	if field.Tag == nil {
		return
	}

	tag, err := strconv.Unquote(field.Tag.Value)
	if err != nil {
		f.Badf(field.Pos(), "unable to read struct tag %s", field.Tag.Value)
		return
	}

	if err := validateStructTag(tag); err != nil {
		raw, _ := strconv.Unquote(field.Tag.Value) // field.Tag.Value is known to be a quoted string
		f.Badf(field.Pos(), "struct field tag %#q not compatible with reflect.StructTag.Get: %s", raw, err)
	}

	for _, key := range checkTagDups {
		val := reflect.StructTag(tag).Get(key)
		if val == "" || val == "-" || val[0] == ',' {
			continue
		}
		if i := strings.Index(val, ","); i >= 0 {
			val = val[:i]
		}
		if *seen == nil {
			*seen = map[[2]string]token.Pos{}
		}
		if pos, ok := (*seen)[[2]string{key, val}]; ok {
			f.Badf(field.Pos(), "struct field %s repeats %s tag %q also at %s", field.Names[0].Name, key, val, f.loc(pos))
		} else {
			(*seen)[[2]string{key, val}] = field.Pos()
		}
	}

	// Check for use of json or xml tags with unexported fields.

	// Embedded struct. Nothing to do for now, but that
	// may change, depending on what happens with issue 7363.
	if len(field.Names) == 0 {
		return
	}

	if field.Names[0].IsExported() {
		return
	}

	for _, enc := range [...]string{"json", "xml"} {
		if reflect.StructTag(tag).Get(enc) != "" {
			f.Badf(field.Pos(), "struct field %s has %s tag but is not exported", field.Names[0].Name, enc)
			return
		}
	}
}
开发者ID:achanda,项目名称:go,代码行数:54,代码来源:structtag.go

示例2: checkCanonicalFieldTag

// checkField checks a struct field tag.
func (f *File) checkCanonicalFieldTag(field *ast.Field) {
	if field.Tag == nil {
		return
	}

	tag, err := strconv.Unquote(field.Tag.Value)
	if err != nil {
		f.Warnf(field.Pos(), "unable to read struct tag %s", field.Tag.Value)
		return
	}

	// Check tag for validity by appending
	// new key:value to end and checking that
	// the tag parsing code can find it.
	if reflect.StructTag(tag+` _gofix:"_magic"`).Get("_gofix") != "_magic" {
		f.Warnf(field.Pos(), "struct field tag %s not compatible with reflect.StructTag.Get", field.Tag.Value)
		return
	}
}
开发者ID:krasin,项目名称:go-deflate,代码行数:20,代码来源:structtag.go

示例3: NewPolyTypeFromField

func NewPolyTypeFromField(v *Visitor, f *ast.Field) (PolyType, *PolyError) {
	switch t := f.Type.(type) {
	case *ast.MapType:
		kname := fmt.Sprintf("%v", t.Key)
		vname := fmt.Sprintf("%v", t.Value)
		if kname == "string" {
			if _, ok := t.Value.(*ast.MapType); ok {
				line := v.fs.Position(f.Pos()).Line
				return PolyType{}, &PolyError{Line: line, Message: "Maps may not be nested"}
			} else {
				return PolyType{vname, kname, false, true, false}, nil
			}
		} else {
			line := v.fs.Position(f.Pos()).Line
			return PolyType{}, &PolyError{Line: line, Message: "Map keys must be type string (not " + kname + ")"}
		}
	case *ast.ArrayType:
		tname := fmt.Sprintf("%v", t.Elt)
		return PolyType{tname, "", false, false, true}, nil
	case *ast.SliceExpr:
		tname := fmt.Sprintf("%v", t.X)
		return PolyType{tname, "", false, false, true}, nil
	default:
		stype := fmt.Sprintf("%v", t)
		switch stype {
		case "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64",
			"float32", "float64", "complex64", "complex128", "byte", "uint", "uintptr":
			line := v.fs.Position(f.Pos()).Line
			return PolyType{}, &PolyError{Line: line, Message: "Illegal type: " + stype}
		default:
			if _, ok := t.(*ast.Ellipsis); ok {
				line := v.fs.Position(f.Pos()).Line
				return PolyType{}, &PolyError{Line: line, Message: "Variadics are not allowed. Use [] instead"}
			}
		}
		//fmt.Printf("NewPoly. type: %v\n", reflect.TypeOf(t))
	}

	tname := fmt.Sprintf("%v", f.Type)
	return PolyType{tname, "", false, false, false}, nil
}
开发者ID:coopernurse,项目名称:polygen,代码行数:41,代码来源:parser.go


注:本文中的go/ast.Field.Pos方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。