-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move request_test so coverage is accurate * go mod tidy * update Makefile test definition * getTagNames refactor * getTagNames unit tests * rename files * code defensively for empty <tag_name> * add CHANGELOG entry
- Loading branch information
Showing
11 changed files
with
354 additions
and
50 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
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,52 @@ | ||
package runtime | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func getTagName(tag reflect.StructTag) (name string) { | ||
|
||
name, protoOK := getProtoTagName(tag) | ||
if !protoOK { | ||
name, _ = getJsonTagName(tag) | ||
} | ||
|
||
return | ||
} | ||
|
||
func getProtoTagName(tag reflect.StructTag) (name string, ok bool) { | ||
protoTag, protoOK := tag.Lookup("protobuf") | ||
if !protoOK { | ||
return | ||
} | ||
|
||
namePrefix := "name=" | ||
jsonPrefix := "json=" | ||
|
||
options := strings.Split(protoTag, ",") | ||
for _, option := range options { | ||
if strings.HasPrefix(option, jsonPrefix) { | ||
name = option[len(jsonPrefix):] | ||
if ok = len(name) > 0; ok { | ||
return // found the first valid `jsonPrefix` so we are able to return | ||
} | ||
} else if !ok && strings.HasPrefix(option, namePrefix) { | ||
name = option[len(namePrefix):] | ||
ok = len(name) > 0 | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func getJsonTagName(tag reflect.StructTag) (name string, ok bool) { | ||
jsonTag, jsonOK := tag.Lookup("json") | ||
if !jsonOK { | ||
return | ||
} | ||
|
||
name = strings.Split(jsonTag, ",")[0] | ||
ok = len(name) > 0 | ||
return | ||
} |
Oops, something went wrong.