Allow renaming of extracted stats via tag.

This commit is contained in:
Kistaro Windrider 2023-04-04 12:14:04 -07:00
parent 99e372a4db
commit ad9e5764f1
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8
2 changed files with 8 additions and 1 deletions

View File

@ -138,6 +138,10 @@ func (s statFunc[T]) Visible() bool {
// start of another word, if it's not at the end). To insert a space between // start of another word, if it's not at the end). To insert a space between
// consecutive capital letters, insert an underscore (`_`). This name inference // consecutive capital letters, insert an underscore (`_`). This name inference
// trims "Stat" and "HiddenStat" off the front of method names. // trims "Stat" and "HiddenStat" off the front of method names.
//
// To override the name extracted from a field name, add `cardsim_name:"name"`
// to the tag, where the name part is the name you want to use. It will not be
// formatted further - use normal spaces, capitalization, etc.
func ExtractStats(x any) []Stat { func ExtractStats(x any) []Stat {
v := reflect.ValueOf(x) v := reflect.ValueOf(x)
for k := v.Kind(); k == reflect.Pointer || k == reflect.Interface; k = v.Kind() { for k := v.Kind(); k == reflect.Pointer || k == reflect.Interface; k = v.Kind() {
@ -248,6 +252,9 @@ func ExtractStats(x any) []Stat {
continue // not identifiably a stat continue // not identifiably a stat
} }
nm := strings.Join(explode(sf.Name), " ") nm := strings.Join(explode(sf.Name), " ")
if t := sf.Tag.Get("cardsim_name"); t != "" {
nm = t
}
if known[nm] { if known[nm] {
continue continue
} }

View File

@ -12,7 +12,7 @@ type SmokeTestCollection struct {
Flavor cardsim.Stored[string] Flavor cardsim.Stored[string]
Things int `cardsim:"stat"` Things int `cardsim:"stat" cardsim_name:"A Renamed Thing"`
MoreThings int `cardsim:"hidden"` MoreThings int `cardsim:"hidden"`
FloatyThings float64 `cardsim:"round1"` FloatyThings float64 `cardsim:"round1"`
Label string `cardsim:"stat"` Label string `cardsim:"stat"`