aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/go-logfmt/logfmt/encode.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/go-logfmt/logfmt/encode.go')
-rw-r--r--vendor/github.com/go-logfmt/logfmt/encode.go39
1 files changed, 20 insertions, 19 deletions
diff --git a/vendor/github.com/go-logfmt/logfmt/encode.go b/vendor/github.com/go-logfmt/logfmt/encode.go
index 55f1603..4ea9d23 100644
--- a/vendor/github.com/go-logfmt/logfmt/encode.go
+++ b/vendor/github.com/go-logfmt/logfmt/encode.go
@@ -110,8 +110,8 @@ func (e *MarshalerError) Error() string {
// a nil interface or pointer value.
var ErrNilKey = errors.New("nil key")
-// ErrInvalidKey is returned by Marshal functions and Encoder methods if a key
-// contains an invalid character.
+// ErrInvalidKey is returned by Marshal functions and Encoder methods if, after
+// dropping invalid runes, a key is empty.
var ErrInvalidKey = errors.New("invalid key")
// ErrUnsupportedKeyType is returned by Encoder methods if a key has an
@@ -165,31 +165,32 @@ func writeKey(w io.Writer, key interface{}) error {
}
}
-func invalidKeyRune(r rune) bool {
- return r <= ' ' || r == '=' || r == '"' || r == utf8.RuneError
-}
-
-func invalidKeyString(key string) bool {
- return len(key) == 0 || strings.IndexFunc(key, invalidKeyRune) != -1
-}
-
-func invalidKey(key []byte) bool {
- return len(key) == 0 || bytes.IndexFunc(key, invalidKeyRune) != -1
+// keyRuneFilter returns r for all valid key runes, and -1 for all invalid key
+// runes. When used as the mapping function for strings.Map and bytes.Map
+// functions it causes them to remove invalid key runes from strings or byte
+// slices respectively.
+func keyRuneFilter(r rune) rune {
+ if r <= ' ' || r == '=' || r == '"' || r == utf8.RuneError {
+ return -1
+ }
+ return r
}
func writeStringKey(w io.Writer, key string) error {
- if invalidKeyString(key) {
+ k := strings.Map(keyRuneFilter, key)
+ if k == "" {
return ErrInvalidKey
}
- _, err := io.WriteString(w, key)
+ _, err := io.WriteString(w, k)
return err
}
func writeBytesKey(w io.Writer, key []byte) error {
- if invalidKey(key) {
+ k := bytes.Map(keyRuneFilter, key)
+ if len(k) == 0 {
return ErrInvalidKey
}
- _, err := w.Write(key)
+ _, err := w.Write(k)
return err
}
@@ -278,7 +279,7 @@ func safeError(err error) (s string, ok bool) {
if v := reflect.ValueOf(err); v.Kind() == reflect.Ptr && v.IsNil() {
s, ok = "null", false
} else {
- panic(panicVal)
+ s, ok = fmt.Sprintf("PANIC:%v", panicVal), false
}
}
}()
@@ -292,7 +293,7 @@ func safeString(str fmt.Stringer) (s string, ok bool) {
if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
s, ok = "null", false
} else {
- panic(panicVal)
+ s, ok = fmt.Sprintf("PANIC:%v", panicVal), true
}
}
}()
@@ -306,7 +307,7 @@ func safeMarshal(tm encoding.TextMarshaler) (b []byte, err error) {
if v := reflect.ValueOf(tm); v.Kind() == reflect.Ptr && v.IsNil() {
b, err = nil, nil
} else {
- panic(panicVal)
+ b, err = nil, fmt.Errorf("panic when marshalling: %s", panicVal)
}
}
}()
nihil fit ex nihilo