Skip to content

Commit

Permalink
use index tag first, field name second (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak authored Mar 1, 2022
1 parent 43d0fdb commit d04444b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
8 changes: 4 additions & 4 deletions field/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ var fieldNameTagRe = regexp.MustCompile(`^F.+$`)
func getFieldIndexOrTag(field reflect.StructField) (string, error) {
dataFieldName := field.Name

if len(dataFieldName) > 0 && fieldNameTagRe.MatchString(dataFieldName) {
return dataFieldName[1:], nil
}

if fieldIndex := field.Tag.Get("index"); fieldIndex != "" {
return fieldIndex, nil
}

if len(dataFieldName) > 0 && fieldNameTagRe.MatchString(dataFieldName) {
return dataFieldName[1:], nil
}

return "", nil
}
11 changes: 11 additions & 0 deletions field/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,17 @@ func TestComposite_getFieldIndexOrTag(t *testing.T) {
require.Equal(t, "1", index)
})

t.Run("returns index from field tag instead of field name when both match", func(t *testing.T) {
st := reflect.ValueOf(&struct {
F1 string `index:"AB"`
}{}).Elem()

index, err := getFieldIndexOrTag(st.Type().Field(0))

require.NoError(t, err)
require.Equal(t, "AB", index)
})

t.Run("returns index from field tag", func(t *testing.T) {
st := reflect.ValueOf(&struct {
Name string `index:"abcd"`
Expand Down
6 changes: 3 additions & 3 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,7 @@ var fieldNameIndexRe = regexp.MustCompile(`^F\d+$`)
func getFieldIndex(field reflect.StructField) (int, error) {
dataFieldName := field.Name

if len(dataFieldName) > 0 && fieldNameIndexRe.MatchString(dataFieldName) {
indexStr := dataFieldName[1:]
if indexStr := field.Tag.Get("index"); indexStr != "" {
fieldIndex, err := strconv.Atoi(indexStr)
if err != nil {
return -1, fmt.Errorf("converting field index into int: %w", err)
Expand All @@ -416,7 +415,8 @@ func getFieldIndex(field reflect.StructField) (int, error) {
return fieldIndex, nil
}

if indexStr := field.Tag.Get("index"); indexStr != "" {
if len(dataFieldName) > 0 && fieldNameIndexRe.MatchString(dataFieldName) {
indexStr := dataFieldName[1:]
fieldIndex, err := strconv.Atoi(indexStr)
if err != nil {
return -1, fmt.Errorf("converting field index into int: %w", err)
Expand Down
11 changes: 11 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,17 @@ func Test_getFieldIndex(t *testing.T) {
require.Equal(t, 1, index)
})

t.Run("returns index from field tag instead of field name when both match", func(t *testing.T) {
st := reflect.ValueOf(&struct {
F1 string `index:"2"`
}{}).Elem()

index, err := getFieldIndex(st.Type().Field(0))

require.NoError(t, err)
require.Equal(t, 2, index)
})

t.Run("returns index from field tag", func(t *testing.T) {
st := reflect.ValueOf(&struct {
Name string `index:"abcd"`
Expand Down

0 comments on commit d04444b

Please sign in to comment.