-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support new tag * code refactoring * remove getFieldIndexOrTag * append test for keepzero tag * code refactoring, fix error when using keepzero index * code refactoring * move test codes into index_tag_test file * fix error, update test cases * changed test naming * move tests from exp_test file to message_test * skip fields without tags * add more tags for index tag * rename Id to ID in index tag * skip fields without index --------- Co-authored-by: Pavel Gabriel <alovak@gmail.com>
- Loading branch information
1 parent
add4df1
commit 8f4cb18
Showing
7 changed files
with
866 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package field | ||
|
||
import ( | ||
"reflect" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var fieldNameIndexRe = regexp.MustCompile(`^F.+$`) | ||
|
||
type IndexTag struct { | ||
ID int // is -1 if index is not a number | ||
|
||
Tag string | ||
// KeepZero tells the marshaler to use zero value and set bitmap bit to | ||
// 1 for this field. Default behavior is to omit the field from the | ||
// message if it's zero value. | ||
KeepZero bool | ||
} | ||
|
||
func NewIndexTag(field reflect.StructField) IndexTag { | ||
// value of the key "index" in the tag | ||
var value string | ||
|
||
// keep the order of tags for now, when index tag is deprecated we can | ||
// change the order | ||
for _, tag := range []string{"index", "iso8583"} { | ||
if value = field.Tag.Get(tag); value != "" { | ||
break | ||
} | ||
} | ||
|
||
// format of the value is "id[,keep_zero_value]" | ||
// id is the id of the field | ||
// let's parse it | ||
if value != "" { | ||
tag, opts := parseTag(value) | ||
|
||
id, err := strconv.Atoi(tag) | ||
if err != nil { | ||
id = -1 | ||
} | ||
|
||
return IndexTag{ | ||
ID: id, | ||
Tag: tag, | ||
KeepZero: opts.Contains("keepzero"), | ||
} | ||
} | ||
|
||
dataFieldName := field.Name | ||
if len(dataFieldName) > 0 && fieldNameIndexRe.MatchString(dataFieldName) { | ||
indexStr := dataFieldName[1:] | ||
fieldIndex, err := strconv.Atoi(indexStr) | ||
if err != nil { | ||
return IndexTag{ | ||
ID: -1, | ||
Tag: indexStr, | ||
} | ||
} | ||
|
||
return IndexTag{ | ||
ID: fieldIndex, | ||
Tag: indexStr, | ||
} | ||
} | ||
|
||
return IndexTag{ | ||
ID: -1, | ||
} | ||
} | ||
|
||
type tagOptions string | ||
|
||
// parseTag splits a struct field's index tag into its id and | ||
// comma-separated options. | ||
func parseTag(tag string) (string, tagOptions) { | ||
tag, opt, _ := strings.Cut(tag, ",") | ||
return tag, tagOptions(opt) | ||
} | ||
|
||
// Contains reports whether a comma-separated list of options | ||
// contains a particular substr flag. substr must be surrounded by a | ||
// string boundary or commas. | ||
func (o tagOptions) Contains(optionName string) bool { | ||
if len(o) == 0 { | ||
return false | ||
} | ||
s := string(o) | ||
for s != "" { | ||
var name string | ||
name, s, _ = strings.Cut(s, ",") | ||
if name == optionName { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.