support normalizing and hashing values
This commit is contained in:
parent
c4b86eeb3b
commit
9929660465
41
spew/dump.go
41
spew/dump.go
|
@ -18,6 +18,7 @@ package spew
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -26,6 +27,8 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"mnk.ee/emails"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -430,7 +433,34 @@ func (d *dumpState) dump(v reflect.Value) {
|
||||||
d.w.Write([]byte(vtf.Name))
|
d.w.Write([]byte(vtf.Name))
|
||||||
d.w.Write(colonSpaceBytes)
|
d.w.Write(colonSpaceBytes)
|
||||||
d.ignoreNextIndent = true
|
d.ignoreNextIndent = true
|
||||||
d.dump(d.unpackValue(v.Field(i)))
|
val := d.unpackValue(v.Field(i))
|
||||||
|
if tag != "" {
|
||||||
|
if val.Type().Kind() != reflect.String || (val.Type().Kind() == reflect.Ptr && val.Type().Elem().Kind() == reflect.String) {
|
||||||
|
var newVal string
|
||||||
|
if val.Type().Kind() == reflect.Ptr {
|
||||||
|
newVal = val.Elem().String()
|
||||||
|
} else {
|
||||||
|
newVal = val.String()
|
||||||
|
}
|
||||||
|
tags := strings.Split(tag, ",")
|
||||||
|
for _, tag := range tags {
|
||||||
|
if tag == "normalize" {
|
||||||
|
newVal = normalize(newVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, tag := range tags {
|
||||||
|
if tag == "hash" {
|
||||||
|
newVal = hash(newVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if val.Type().Kind() == reflect.Ptr {
|
||||||
|
val.Elem().SetString(newVal)
|
||||||
|
} else {
|
||||||
|
val.SetString(newVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d.dump(val)
|
||||||
}
|
}
|
||||||
if writeNewline {
|
if writeNewline {
|
||||||
d.w.Write(newlineBytes)
|
d.w.Write(newlineBytes)
|
||||||
|
@ -458,6 +488,15 @@ func (d *dumpState) dump(v reflect.Value) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalize(str string) string {
|
||||||
|
return emails.Normalize(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func hash(str string) string {
|
||||||
|
hash := md5.Sum([]byte(str))
|
||||||
|
return hex.EncodeToString(hash[:])
|
||||||
|
}
|
||||||
|
|
||||||
// fdump is a helper function to consolidate the logic from the various public
|
// fdump is a helper function to consolidate the logic from the various public
|
||||||
// methods which take varying writers and config states.
|
// methods which take varying writers and config states.
|
||||||
func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
|
func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
|
||||||
|
|
Loading…
Reference in New Issue