Пример #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
		}
	}
}
Пример #2
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
	}
}
Пример #3
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
}