| 50 | } |
| 51 | |
| 52 | func forEachStructField(t reflect.Type, index []int, do func(structField)) { |
| 53 | for i := range t.NumField() { |
| 54 | f := t.Field(i) |
| 55 | |
| 56 | if f.PkgPath != "" && !f.Anonymous { // unexported |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | fieldIndex := append(index, i) |
| 61 | fieldIndex = fieldIndex[:len(fieldIndex):len(fieldIndex)] |
| 62 | |
| 63 | if f.Anonymous { |
| 64 | fieldType := f.Type |
| 65 | |
| 66 | for fieldType.Kind() == reflect.Ptr { |
| 67 | fieldType = fieldType.Elem() |
| 68 | } |
| 69 | |
| 70 | if fieldType.Kind() == reflect.Struct { |
| 71 | forEachStructField(fieldType, fieldIndex, do) |
| 72 | continue |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | tag := f.Tag.Get("thrift") |
| 77 | if tag == "" { |
| 78 | continue |
| 79 | } |
| 80 | tags := strings.Split(tag, ",") |
| 81 | flags := flags(0) |
| 82 | |
| 83 | for _, opt := range tags[1:] { |
| 84 | switch opt { |
| 85 | case "enum": |
| 86 | flags = flags.with(enum) |
| 87 | case "union": |
| 88 | flags = flags.with(union) |
| 89 | case "required": |
| 90 | flags = flags.with(required) |
| 91 | case "optional": |
| 92 | flags = flags.with(optional) |
| 93 | default: |
| 94 | panic(fmt.Errorf("thrift struct field contains an unknown tag option %q in `thrift:\"%s\"`", opt, tag)) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if flags.have(optional | required) { |
| 99 | panic(fmt.Errorf("thrift struct field cannot be both optional and required in `thrift:\"%s\"`", tag)) |
| 100 | } |
| 101 | |
| 102 | if flags.have(union) { |
| 103 | if f.Type.Kind() != reflect.Interface { |
| 104 | panic(fmt.Errorf("thrift union tag found on a field which is not an interface type `thrift:\"%s\"`", tag)) |
| 105 | } |
| 106 | |
| 107 | if tags[0] != "" { |
| 108 | panic(fmt.Errorf("invalid thrift field id on union field `thrift:\"%s\"`", tag)) |
| 109 | } |