ref: master
./struct.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
package dirty import ( "fmt" "reflect" "unicode" ) func getStructPair(e Element) Array { if e.getType() != ElemArray && len(e.(Array)) != 2 { // todo error: not a pair fmt.Println("not a pair") return nil } return e.(Array) } func getStructFieldName(pair Array) string { k := reflect.TypeOf(pair[0]).Kind() if k != reflect.String { // todo error: name not string fmt.Println("name not string") return "" } fieldName := string(pair[0].(String)) runes := []rune(fieldName) runes[0] = unicode.ToUpper(runes[0]) fieldName = string(runes) return fieldName } func getStructMapKey(pair Array, mk reflect.Kind) reflect.Value { sk := reflect.ValueOf(pair[0]).Type().String() if (sk == "dirty.String" && mk != reflect.String) || (sk == "dirty.Int" && mk != reflect.Int && mk != reflect.Int8 && mk != reflect.Int16 && mk != reflect.Int32 && mk != reflect.Int64 && mk != reflect.Uint && mk != reflect.Uint8 && mk != reflect.Uint16 && mk != reflect.Uint32 && mk != reflect.Uint64) || (sk == "dirty.Const" && mk != reflect.Bool) /*test sk ==dirty.Const but not dirty::Bool */ { // todo error fmt.Printf("pair[0] (%v) not map key (%v)\n", sk, mk) return reflect.ValueOf(nil) } switch mk { case reflect.String: return reflect.ValueOf(string(pair[0].(String))) case reflect.Bool: return reflect.ValueOf(pair[0].(Const).Bool()) case reflect.Uint: return reflect.ValueOf(uint(pair[0].(Int))) case reflect.Uint8: return reflect.ValueOf(uint8(pair[0].(Int))) case reflect.Uint16: return reflect.ValueOf(uint16(pair[0].(Int))) case reflect.Uint32: return reflect.ValueOf(uint32(pair[0].(Int))) case reflect.Uint64: return reflect.ValueOf(uint64(pair[0].(Int))) case reflect.Int: return reflect.ValueOf(int(pair[0].(Int))) case reflect.Int8: return reflect.ValueOf(int8(pair[0].(Int))) case reflect.Int16: return reflect.ValueOf(int16(pair[0].(Int))) case reflect.Int32: return reflect.ValueOf(int32(pair[0].(Int))) case reflect.Int64: return reflect.ValueOf(int64(pair[0].(Int))) default: return reflect.ValueOf(nil) } } func setStructValue(value Element, f reflect.Value) error { switch value.getType() { case ElemArray: err := convertStruct(value.(Array), f) if err != nil { return err } case ElemConst: if value == NULL && f.Kind() == reflect.Ptr { // do nothing; default value is nil } else if (value == TRUE || value == FALSE) && f.Kind() == reflect.Bool { f.SetBool(value.(Const).Bool()) } else { // todo error: type mismatch fmt.Println("type mismatch") return nil } case ElemFloat: if f.Kind() == reflect.Float64 || f.Kind() == reflect.Float32 { f.SetFloat(float64(value.(Float))) } else { // todo error: type mismatch fmt.Println("type mismatch") return nil } case ElemInt: if f.Kind() == reflect.Int || f.Kind() == reflect.Int8 || f.Kind() == reflect.Int16 || f.Kind() == reflect.Int32 || f.Kind() == reflect.Int64 { f.SetInt(int64(value.(Int))) } else { // todo error: type mismatch fmt.Println("type mismatch") return nil } case ElemString: if f.Kind() == reflect.String { f.SetString(string(value.(String))) } else { // todo error: type mismatch fmt.Println("type mismatch") return nil } default: // todo error: unknown type fmt.Println("unknown type") return nil } return nil } func convertStruct(array Array, s reflect.Value) error { kind := s.Kind() if kind == reflect.Struct { for _, e := range array { pair := getStructPair(e) fieldName := getStructFieldName(pair) f := s.FieldByName(fieldName) if !f.IsValid() { // todo error no such field fmt.Println("no such field", fieldName) return nil } if f.Kind() == reflect.Ptr && pair[1] != NULL { f.Set(reflect.New(f.Type().Elem())) f = f.Elem() } setStructValue(pair[1], f) } //fmt.Printf("%+v\n", s) return nil } if kind == reflect.Slice { elemType := s.Type().Elem() if s.Len() != 0 { // todo error slice not empty fmt.Println("slice len != 0") return nil } capacity := s.Cap() s.SetLen(capacity) s2 := s for i, e := range array { if i < capacity { f := s2.Index(i) setStructValue(e, f) } else { f := reflect.New(elemType).Elem() setStructValue(e, f) s2 = reflect.Append(s2, f) } } s.Set(s2) //fmt.Printf("%+v\n", s) return nil } if kind == reflect.Array { if s.Len() != len(array) { // todo error array len not Array len fmt.Println("array len not length of Array") return nil } for i, e := range array { f := s.Index(i) setStructValue(e, f) } //fmt.Printf("%+v\n", s) return nil } if kind == reflect.Map { keyType := s.Type().Key() keyKind := keyType.Kind() valueType := s.Type().Elem() for _, e := range array { pair := getStructPair(e) k := getStructMapKey(pair, keyKind) v := reflect.New(valueType).Elem() setStructValue(pair[1], v) if s.IsNil() { var mapType = reflect.MapOf(keyType, valueType) s.Set(reflect.MakeMapWithSize(mapType, 0)) } s.SetMapIndex(k, v) } //fmt.Printf("%+v\n", s) return nil } //fmt.Println(kind) return nil } |