Skip to content

Commit

Permalink
up: modify number enum to const enum
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 19, 2021
1 parent 68addcc commit 4078096
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ const (
sourceStruct
)

// 0: common field
// 1: anonymous field
// 2: nonAnonymous field
const (
fieldAtTopStruct int8 = iota
fieldAtAnonymous
fieldAtSubStruct
)

var timeType = reflect.TypeOf(time.Time{})

// data (Un)marshal func
Expand Down Expand Up @@ -173,7 +182,7 @@ type StructData struct {
// source struct reflect.Type
valueTpy reflect.Type
// field names in the src struct
// 0:common field,1:anonymous field,2:nonAnonymous field
// 0:common field 1:anonymous field 2:nonAnonymous field
fieldNames map[string]int8
// cache field value info
fieldValues map[string]reflect.Value
Expand Down Expand Up @@ -263,9 +272,7 @@ func (d *StructData) parseRulesFromTag(v *Validation) {

vt := d.valueTpy
recursiveFunc = func(vt reflect.Type, preStrName string) {

for i := 0; i < vt.NumField(); i++ {

ft := vt.Field(i).Type
ft = removeTypePtr(ft)

Expand All @@ -285,8 +292,8 @@ func (d *StructData) parseRulesFromTag(v *Validation) {
if preStrName != "" {
name = preStrName + "." + name
} else {
// 0:common field,1:anonymous field,2:nonAnonymous field
d.fieldNames[name] = 0
// 0:common field 1:anonymous field 2:nonAnonymous field
d.fieldNames[name] = fieldAtTopStruct
}

// validate rule
Expand Down Expand Up @@ -412,7 +419,7 @@ func (d *StructData) Get(field string) (interface{}, bool) {
// whether it is an anonymous field
if tft.Anonymous {
fv = d.value.FieldByName(subField)
d.fieldNames[field] = 1
d.fieldNames[field] = fieldAtAnonymous
} else {
// get parent struct
fv = d.value.FieldByName(parentField)
Expand All @@ -421,17 +428,16 @@ func (d *StructData) Get(field string) (interface{}, bool) {
fv = removeValuePtr(fv)
}
fv = fv.FieldByName(subField)
d.fieldNames[field] = 2

d.fieldNames[field] = fieldAtSubStruct
}
fv = removeValuePtr(fv)

fv = removeValuePtr(fv)
} else {

// field at top struct
if d.HasField(field) {
fv = d.value.FieldByName(field)
fv = removeValuePtr(fv)
d.fieldNames[field] = 0
d.fieldNames[field] = fieldAtTopStruct
} else {
// not found field
return nil, false
Expand Down Expand Up @@ -468,11 +474,11 @@ func (d *StructData) Set(field string, val interface{}) (newVal interface{}, err
if !ok {
f := d.fieldNames[field]
switch f {
case 0:
case fieldAtTopStruct:
fv = d.value.FieldByName(field)
case 1:
case fieldAtAnonymous:
fv = d.value.FieldByName(subField)
case 2:
case fieldAtSubStruct:
fv = d.value.FieldByName(parentField)
if fv.Type().Kind() == reflect.Ptr {
fv = removeValuePtr(fv)
Expand Down Expand Up @@ -527,7 +533,7 @@ func (d *StructData) HasField(field string) bool {

// has field, cache it
if _, ok := d.valueTpy.FieldByName(field); ok {
d.fieldNames[field] = 1
d.fieldNames[field] = fieldAtTopStruct
return true
}

Expand Down

0 comments on commit 4078096

Please sign in to comment.