|
| 1 | +package defaults |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | +) |
| 6 | + |
| 7 | +type fieldData struct { |
| 8 | + Field reflect.StructField |
| 9 | + Value reflect.Value |
| 10 | +} |
| 11 | + |
| 12 | +type fillerFunc func(field *fieldData, config string) |
| 13 | + |
| 14 | +type Filler struct { |
| 15 | + FuncByName map[string]fillerFunc |
| 16 | + FuncByKind map[reflect.Kind]fillerFunc |
| 17 | + Tag string |
| 18 | +} |
| 19 | + |
| 20 | +func (self *Filler) Fill(variable interface{}) { |
| 21 | + fields := self.getFields(variable) |
| 22 | + self.setDefaultValues(fields) |
| 23 | +} |
| 24 | + |
| 25 | +func (self *Filler) getFields(variable interface{}) []*fieldData { |
| 26 | + valueObject := reflect.ValueOf(variable).Elem() |
| 27 | + |
| 28 | + return self.getFieldsFromValue(valueObject) |
| 29 | +} |
| 30 | + |
| 31 | +func (self *Filler) getFieldsFromValue(valueObject reflect.Value) []*fieldData { |
| 32 | + typeObject := valueObject.Type() |
| 33 | + |
| 34 | + count := valueObject.NumField() |
| 35 | + results := make([]*fieldData, 0) |
| 36 | + for i := 0; i < count; i++ { |
| 37 | + value := valueObject.Field(i) |
| 38 | + field := typeObject.Field(i) |
| 39 | + |
| 40 | + if value.CanSet() { |
| 41 | + results = append(results, &fieldData{ |
| 42 | + Value: value, |
| 43 | + Field: field, |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return results |
| 49 | +} |
| 50 | + |
| 51 | +func (self *Filler) setDefaultValues(fields []*fieldData) { |
| 52 | + for _, field := range fields { |
| 53 | + if self.isEmpty(field) { |
| 54 | + self.setDefaultValue(field) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (self *Filler) isEmpty(field *fieldData) bool { |
| 60 | + switch field.Value.Kind() { |
| 61 | + case reflect.Bool: |
| 62 | + if field.Value.Bool() != false { |
| 63 | + return false |
| 64 | + } |
| 65 | + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 66 | + if field.Value.Int() != 0 { |
| 67 | + return false |
| 68 | + } |
| 69 | + case reflect.Float32, reflect.Float64: |
| 70 | + if field.Value.Float() != .0 { |
| 71 | + return false |
| 72 | + } |
| 73 | + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 74 | + if field.Value.Uint() != 0 { |
| 75 | + return false |
| 76 | + } |
| 77 | + case reflect.Slice: |
| 78 | + if field.Value.Type().Elem().Kind() == reflect.Uint8 { |
| 79 | + if field.Value.Bytes() != nil { |
| 80 | + return false |
| 81 | + } |
| 82 | + } |
| 83 | + case reflect.String: |
| 84 | + if field.Value.String() != "" { |
| 85 | + return false |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return true |
| 90 | +} |
| 91 | + |
| 92 | +func (self *Filler) setDefaultValue(field *fieldData) { |
| 93 | + tagValue := field.Field.Tag.Get(self.Tag) |
| 94 | + |
| 95 | + function := self.getFunctionByKind(field.Field.Type.Kind()) |
| 96 | + if function == nil { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + function(field, tagValue) |
| 101 | +} |
| 102 | + |
| 103 | +func (self *Filler) getFunctionByKind(k reflect.Kind) fillerFunc { |
| 104 | + if f, ok := self.FuncByKind[k]; ok == true { |
| 105 | + return f |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
0 commit comments