本文整理汇总了Golang中reflect.StructTag类的典型用法代码示例。如果您正苦于以下问题:Golang StructTag类的具体用法?Golang StructTag怎么用?Golang StructTag使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StructTag类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseTag
func parseTag(tag reflect.StructTag) packopts {
var opts packopts
bpTag := tag.Get("binpack")
for _, t := range strings.Split(string(bpTag), ",") {
if t == "-" {
opts.skip = true
}
if strings.HasPrefix(t, "lenprefix=") {
opts.lenprefix = strings.TrimPrefix(t, "lenprefix=")
}
if strings.HasPrefix(t, "endian=") {
endian := strings.TrimPrefix(t, "endian=")
switch endian {
case "little":
opts.endian = binary.LittleEndian
case "big":
opts.endian = binary.BigEndian
default:
panic("unknown endian type in struct: " + endian)
}
}
}
return opts
}
示例2: parseValue
func (q *queryParser) parseValue(v url.Values, value reflect.Value, prefix string, tag reflect.StructTag) error {
value = elemOf(value)
// no need to handle zero values
if !value.IsValid() {
return nil
}
t := tag.Get("type")
if t == "" {
switch value.Kind() {
case reflect.Struct:
t = "structure"
case reflect.Slice:
t = "list"
case reflect.Map:
t = "map"
}
}
switch t {
case "structure":
return q.parseStruct(v, value, prefix)
case "list":
return q.parseList(v, value, prefix, tag)
case "map":
return q.parseMap(v, value, prefix, tag)
default:
return q.parseScalar(v, value, prefix, tag)
}
}
示例3: dump
func dump(path string, typ types.Type, st reflect.StructTag) IObj {
named, _ := typ.(*types.Named)
if named != nil {
typ = typ.Underlying()
}
if strings.Split(st.Get("json"), ",")[0] == "" {
if _, ok := typ.(*types.Struct); !ok {
if _, ok := typ.(*types.Pointer); !ok {
return nil
}
}
}
switch u := typ.(type) {
case *types.Struct:
return Struct(path, st, u, named)
case *types.Map:
return Map(path, st, u, named)
case *types.Slice:
return Slice(path, st, u)
case *types.Pointer:
return Pointer(path, st, u)
case *types.Basic:
return Basic(path, st, u, named)
default:
panic("unsupported")
}
}
示例4: HasFilter
func HasFilter(field reflect.StructTag) (k, v string, err error) {
tag := field.Get("blueprint")
for _, entry := range strings.Split(tag, ",") {
if strings.HasPrefix(entry, "filter") {
if !strings.HasPrefix(entry, "filter(") || !strings.HasSuffix(entry, ")") {
return "", "", fmt.Errorf("unexpected format for filter %q: missing ()", entry)
}
entry = strings.TrimPrefix(entry, "filter(")
entry = strings.TrimSuffix(entry, ")")
s := strings.Split(entry, ":")
if len(s) != 2 {
return "", "", fmt.Errorf("unexpected format for filter %q: expected single ':'", entry)
}
k = s[0]
v, err = strconv.Unquote(s[1])
if err != nil {
return "", "", fmt.Errorf("unexpected format for filter %q: %s", entry, err.Error())
}
return k, v, nil
}
}
return "", "", nil
}
示例5: ParseColumnNames
// Iterate struct names and return a slice that contains column names.
func ParseColumnNames(val interface{}) ([]string) {
t := reflect.ValueOf(val).Elem()
typeOfT := t.Type()
var structName string = typeOfT.String()
if cache, ok := columnNameCache[structName] ; ok {
return cache
}
var columns []string
for i := 0; i < t.NumField(); i++ {
var tag reflect.StructTag = typeOfT.Field(i).Tag
if tag.Get("field") == "-" {
continue
}
var columnName *string = GetColumnNameFromTag(&tag)
if columnName == nil {
continue
}
columns = append(columns, *columnName)
}
columnNameCache[structName] = columns
return columns
}
示例6: NewParamOptions
// NewParamOptions returns a ParamOptions from a StructTag
func NewParamOptions(tags *reflect.StructTag) *ParamOptions {
output := &ParamOptions{}
// We use the json tag to get the field name
jsonOpts := strings.Split(tags.Get("json"), ",")
if len(jsonOpts) > 0 {
if jsonOpts[0] == "-" {
return &ParamOptions{Ignore: true}
}
output.Name = jsonOpts[0]
}
// We parse the params
opts := strings.Split(tags.Get("params"), ",")
nbOptions := len(opts)
for i := 0; i < nbOptions; i++ {
switch opts[i] {
case "required":
output.Required = true
case "trim":
output.Trim = true
}
}
return output
}
示例7: generateInput
func generateInput(tag reflect.StructTag, valueField string) (fInput string) {
var extras, label string
// Ignore "-" form fields
if tag.Get("form") == "-" || tag.Get("form") == "" {
return
}
fType := "text"
fAttrs := strings.Split(tag.Get("attr"), ";")
for _, element := range fAttrs {
fAttr := strings.Split(element, ":")
ele := strings.ToLower(fAttr[0])
val := ""
if len(fAttr) == 2 {
val = fAttr[1]
}
if valueField != "" && val == "input" {
val = valueField
}
if valueField == "" && val == "input" {
val = ""
}
switch ele {
case "alt":
extras += fmt.Sprintf(" alt=\"%s\"", val)
case "autofocus":
extras += " autofocus"
case "checked":
extras += " checked"
case "class":
extras += fmt.Sprintf(" class=\"%s\"", val)
case "label":
label = fmt.Sprintf("\t<label for=\"%s\">%s</label>\n", tag.Get("form"), val)
case "maxlength":
extras += fmt.Sprintf(" maxlength=\"%s\"", val)
case "min":
extras += fmt.Sprintf(" min=\"%s\"", val)
case "placeholder":
extras += fmt.Sprintf(" placeholder=\"%s\"", val)
case "readonly":
extras += " readonly"
case "required":
extras += " required"
case "type":
fType = val
case "value":
extras += fmt.Sprintf(" value=\"%s\"", val)
}
}
if label != "" {
fInput += label
}
fInput += fmt.Sprintf("\t<input type=\"%s\" name=\"%s\" id=\"%s\"%s>\n", fType, tag.Get("form"), tag.Get("form"), extras)
return
}
示例8: parseTag
// parseTag parses "endpoints" field tag into endpointsTag struct.
//
// type MyMessage struct {
// SomeField int `endpoints:"req,min=0,max=100,desc="Int field"`
// WithDefault string `endpoints:"d=Hello gopher"`
// }
//
// - req, required (boolean)
// - d=val, default value
// - min=val, min value
// - max=val, max value
// - desc=val, description
//
// It is an error to specify both default and required.
func parseTag(t reflect.StructTag) (*endpointsTag, error) {
eTag := &endpointsTag{}
if tag := t.Get("endpoints"); tag != "" {
parts := strings.Split(tag, ",")
for _, k := range parts {
switch k {
case "req":
eTag.required = true
default:
// key=value format
kv := strings.SplitN(k, "=", 2)
if len(kv) < 2 {
continue
}
switch kv[0] {
case "d":
eTag.defaultVal = kv[1]
case "min":
eTag.minVal = kv[1]
case "max":
eTag.maxVal = kv[1]
case "desc":
eTag.desc = kv[1]
}
}
}
if eTag.required && eTag.defaultVal != "" {
return nil, fmt.Errorf(
"Can't have both required and default (%#v)",
eTag.defaultVal)
}
}
return eTag, nil
}
示例9: parse
func parse(r reflect.Value, node *XMLNode, tag reflect.StructTag) error {
rtype := r.Type()
if rtype.Kind() == reflect.Ptr {
rtype = rtype.Elem() // check kind of actual element type
}
t := tag.Get("type")
if t == "" {
switch rtype.Kind() {
case reflect.Struct:
t = "structure"
case reflect.Slice:
t = "list"
case reflect.Map:
t = "map"
}
}
switch t {
case "structure":
if field, ok := rtype.FieldByName("SDKShapeTraits"); ok {
tag = field.Tag
}
return parseStruct(r, node, tag)
case "list":
return parseList(r, node, tag)
case "map":
return parseMap(r, node, tag)
default:
return parseScalar(r, node, tag)
}
}
示例10: buildValue
func (b *xmlBuilder) buildValue(value reflect.Value, current *XMLNode, tag reflect.StructTag) error {
value = elemOf(value)
if !value.IsValid() { // no need to handle zero values
return nil
} else if tag.Get("location") != "" { // don't handle non-body location values
return nil
}
t := tag.Get("type")
if t == "" {
switch value.Kind() {
case reflect.Struct:
t = "structure"
case reflect.Slice:
t = "list"
case reflect.Map:
t = "map"
}
}
switch t {
case "structure":
if field, ok := value.Type().FieldByName("SDKShapeTraits"); ok {
tag = tag + reflect.StructTag(" ") + field.Tag
}
return b.buildStruct(value, current, tag)
case "list":
return b.buildList(value, current, tag)
case "map":
return b.buildMap(value, current, tag)
default:
return b.buildScalar(value, current, tag)
}
}
示例11: parseTag
// parseTag parses the given struct tag attached to the given
// field name into a tag structure.
func parseTag(rtag reflect.StructTag, fieldName string) (tag, error) {
t := tag{
name: fieldName,
}
tagStr := rtag.Get("httprequest")
if tagStr == "" {
return t, nil
}
fields := strings.Split(tagStr, ",")
if fields[0] != "" {
t.name = fields[0]
}
for _, f := range fields[1:] {
switch f {
case "path":
t.source = sourcePath
case "form":
t.source = sourceForm
case "body":
t.source = sourceBody
default:
return tag{}, fmt.Errorf("unknown tag flag %q", f)
}
}
return t, nil
}
示例12: GetConfig
func (source envSource) GetConfig(name string, tag reflect.StructTag) (interface{}, bool) {
var varName = tag.Get("env")
if varName == "" {
return nil, false
}
return os.Getenv(varName) == "1", true
}
示例13: buildStruct
func buildStruct(value reflect.Value, buf *bytes.Buffer, tag reflect.StructTag) error {
if !value.IsValid() {
return nil
}
// unwrap payloads
if payload := tag.Get("payload"); payload != "" {
field, _ := value.Type().FieldByName(payload)
tag = field.Tag
value = elemOf(value.FieldByName(payload))
if !value.IsValid() {
return nil
}
}
buf.WriteString("{")
t, fields := value.Type(), []*reflect.StructField{}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
member := value.FieldByName(field.Name)
if (member.Kind() == reflect.Ptr || member.Kind() == reflect.Slice || member.Kind() == reflect.Map) && member.IsNil() {
continue // ignore unset fields
}
if c := field.Name[0:1]; strings.ToLower(c) == c {
continue // ignore unexported fields
}
if field.Tag.Get("location") != "" {
continue // ignore non-body elements
}
fields = append(fields, &field)
}
for i, field := range fields {
member := value.FieldByName(field.Name)
// figure out what this field is called
name := field.Name
if locName := field.Tag.Get("locationName"); locName != "" {
name = locName
}
buf.WriteString(fmt.Sprintf("%q:", name))
err := buildAny(member, buf, field.Tag)
if err != nil {
return err
}
if i < len(fields)-1 {
buf.WriteString(",")
}
}
buf.WriteString("}")
return nil
}
示例14: writeType
func (w *writing) writeType(e ast.Expr, name string, tag reflect.StructTag) {
switch e := e.(type) {
case *ast.StructType:
w.writeStruct(e, name)
case *ast.SelectorExpr:
pck := e.X.(*ast.Ident).Name
s := e.Sel.Name
w.writeNamed(pck+"."+s, name, tag)
case *ast.StarExpr:
w.writeType(e.X, name, tag)
case *ast.Ident:
w.writeNamed(e.Name, name, tag)
case *ast.ArrayType:
lT := tag.Get("length")
if lT != "remaining" && lT != "" && lT[0] != '@' {
w.writeNamed(lT, fmt.Sprintf("%s(len(%s))", lT, name), "")
}
if i, ok := e.Elt.(*ast.Ident); ok && (i.Name == "byte" || i.Name == "uint8") {
fmt.Fprintf(&w.buf, "if _, err = ww.Write(%s); err != nil { return }\n", name)
} else {
iVar := w.tmp()
fmt.Fprintf(&w.buf, "for %s := range %s {\n", iVar, name)
w.writeType(e.Elt, fmt.Sprintf("%s[%s]", name, iVar), tag)
w.buf.WriteString("}\n")
}
default:
fmt.Fprintf(&w.buf, "// Unhandled %#v\n", e)
}
}
示例15: GetColumnNameFromTag
// Extract column name attribute from struct tag (the first element) of the 'field' tag or
// column name from 'json' tag.
func GetColumnNameFromTag(tag *reflect.StructTag) *string {
var p int
var tagStr string
if tagStr = tag.Get("field"); len(tagStr) != 0 {
// ignore it if it starts with dash
if tagStr[0] == "-"[0] {
return nil
}
if p = IndexOfChar(tagStr, ","); p != -1 {
if p > 1 {
str := tagStr[:p]
return &str
}
} else {
return &tagStr
}
}
if tagStr = tag.Get("json"); len(tagStr) == 0 {
return nil
}
if tagStr[0] == "-"[0] {
return nil
}
if p = IndexOfChar(tagStr, ","); p != -1 {
if p > 1 {
str := tagStr[:p]
return &str
}
return nil
}
return &tagStr
}