Ejemplo n.º 1
0
// 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
		}
	}
}
Ejemplo n.º 2
0
Archivo: parser.go Proyecto: h12w/gombi
func (p *parser) parseParamDecl(n *parse.Node, scope *ast.Scope) *ast.Field {
	field := ast.Field{}
	if n.Child(0).Is(type_) {
		field.Type = p.parseType(n.Child(0))
	} else {
		field.Names = p.parseIdentList(n.Child(0))
		if n.Child(1).Is(type_) {
			field.Type = p.parseType(n.Child(1))
		} else {
			// TODO ...
			field.Type = p.parseType(n.Child(2))
		}
	}
	p.declare(field, nil, scope, ast.Var, field.Names...)
	if field.Type != nil {
		p.resolve(field.Type)
	}
	return &field
}
Ejemplo n.º 3
0
// 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
	}
}
Ejemplo n.º 4
0
// Struct creates a struct{} expression. The arguments are a series
// of name/type/tag tuples. Name must be of type *ast.Ident, type
// must be of type ast.Expr, and tag must be of type *ast.BasicLit,
// The number of arguments must be a multiple of 3, or a run-time
// panic will occur.
func Struct(args ...ast.Expr) *ast.StructType {
	fields := new(ast.FieldList)
	if len(args)%3 != 0 {
		panic("Number of args to FieldList must be a multiple of 3, got " + strconv.Itoa(len(args)))
	}
	for i := 0; i < len(args); i += 3 {
		var field ast.Field
		name, typ, tag := args[i], args[i+1], args[i+2]
		if name != nil {
			field.Names = []*ast.Ident{name.(*ast.Ident)}
		}
		if typ != nil {
			field.Type = typ
		}
		if tag != nil {
			field.Tag = tag.(*ast.BasicLit)
		}
		fields.List = append(fields.List, &field)
	}
	return &ast.StructType{Fields: fields}
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
0
Archivo: method.go Proyecto: nelsam/hel
func mockField(idx int, f *ast.Field) *ast.Field {
	if f.Names == nil {
		if idx < 0 {
			return f
		}
		// Edit the field directly to ensure the same name is used in the mock
		// struct.
		f.Names = []*ast.Ident{{Name: fmt.Sprintf(inputFmt, idx)}}
		return f
	}

	// Here, we want a copy, so that we can use altered names without affecting
	// field names in the mock struct.
	newField := &ast.Field{Type: f.Type}
	for _, n := range f.Names {
		name := n.Name
		if name == receiverName {
			name += "_"
		}
		newField.Names = append(newField.Names, &ast.Ident{Name: name})
	}
	return newField
}
Ejemplo n.º 7
0
// AppendStructTag adds an additional tag to a struct tag
func AppendStructTag(field *ast.Field, tagName string, offset *int, data []byte) []byte {
	start := int(field.End()) + *offset - 1
	tag := fmt.Sprintf(" `%s:\"%s\"`", options.Tag, tagName)
	*offset += len(tag)
	return Insert(data, []byte(tag), start)
}