Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty pointers to complex structs in metadata.Add #221

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions v2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (meta MetaData) AddStruct(tab string, obj interface{}) {
meta[tab] = content
} else {
// Wasn't a struct
meta.Add("Extra data", tab, obj)
meta.Add("Extra data", tab, val)
}

}
Expand Down Expand Up @@ -84,32 +84,38 @@ func (s sanitizer) Sanitize(data interface{}) interface{} {
// Sanitizers are passed by value, so we can modify s and it only affects
// s.Seen for nested calls.
s.Seen = append(s.Seen, data)
t := reflect.TypeOf(data)
v := reflect.ValueOf(data)

if t == nil {
return "<nil>"
}

// Handle nil pointers and interfaces specifically
if t.Kind() == reflect.Interface || t.Kind() == reflect.Ptr {
DariaKunoichi marked this conversation as resolved.
Show resolved Hide resolved
if v.IsNil() {
return "<nil>"
}
}

// Handle certain well known interfaces and types
switch data := data.(type) {
switch dataT := data.(type) {
case error:
return data.Error()
return dataT.Error()

case time.Time:
return data.Format(time.RFC3339Nano)
return dataT.Format(time.RFC3339Nano)

case fmt.Stringer:
// This also covers time.Duration
return data.String()
return dataT.String()

case encoding.TextMarshaler:
if b, err := data.MarshalText(); err == nil {
if b, err := dataT.MarshalText(); err == nil {
return string(b)
}
}

t := reflect.TypeOf(data)
v := reflect.ValueOf(data)

if t == nil {
return "<nil>"
}

switch t.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
Expand All @@ -121,9 +127,6 @@ func (s sanitizer) Sanitize(data interface{}) interface{} {
return data

case reflect.Interface, reflect.Ptr:
if v.IsNil() {
return "<nil>"
}
return s.Sanitize(v.Elem().Interface())

case reflect.Array, reflect.Slice:
Expand Down
23 changes: 23 additions & 0 deletions v2/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func (_textMarshaller) MarshalText() ([]byte, error) {
return []byte("marshalled text"), nil
}

type _testStringer struct{}

func (s _testStringer) String() string {
return "something"
}

var account = _account{}
var notifier = New(Configuration{})

Expand Down Expand Up @@ -77,6 +83,23 @@ func TestMetaDataAdd(t *testing.T) {
}
}

func TestMetadataAddPointer(t *testing.T) {
var pointer *_testStringer
md := MetaData{}
md.AddStruct("emptypointer", pointer)
fullPointer := &_testStringer{}
md.AddStruct("fullpointer", fullPointer)

if !reflect.DeepEqual(md, MetaData{
"Extra data": {
"emptypointer": "<nil>",
"fullpointer": "something",
},
}) {
t.Errorf("metadata.AddStruct didn't work: %#v", md)
}
}

func TestMetaDataUpdate(t *testing.T) {

m := MetaData{
Expand Down
Loading