Skip to content

Commit

Permalink
Merge pull request #17 from m-mizutani/enhance/prevent-circular
Browse files Browse the repository at this point in the history
Prevent circular reference
  • Loading branch information
m-mizutani authored Mar 6, 2024
2 parents 89175da + 166b539 commit 52aa50f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
31 changes: 24 additions & 7 deletions clone.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package masq

import (
"context"
"reflect"
"unsafe"
)

func (x *masq) clone(fieldName string, src reflect.Value, tag string) reflect.Value {
type ctxKeyDepth struct{}

const (
maxDepth = 32
)

func (x *masq) clone(ctx context.Context, fieldName string, src reflect.Value, tag string) reflect.Value {
if v, ok := ctx.Value(ctxKeyDepth{}).(int); !ok {
ctx = context.WithValue(ctx, ctxKeyDepth{}, 0)
} else {
if v >= maxDepth {
return src
}
ctx = context.WithValue(ctx, ctxKeyDepth{}, v+1)

}

if _, ok := x.allowedTypes[src.Type()]; ok {
return src
}
Expand Down Expand Up @@ -68,7 +85,7 @@ func (x *masq) clone(fieldName string, src reflect.Value, tag string) reflect.Va
}

tagValue := f.Tag.Get("masq")
copied := x.clone(f.Name, srcValue, tagValue)
copied := x.clone(ctx, f.Name, srcValue, tagValue)
dstValue.Set(copied)
}
return dst.Elem()
Expand All @@ -78,14 +95,14 @@ func (x *masq) clone(fieldName string, src reflect.Value, tag string) reflect.Va
keys := src.MapKeys()
for i := 0; i < src.Len(); i++ {
mValue := src.MapIndex(keys[i])
dst.SetMapIndex(keys[i], x.clone(keys[i].String(), mValue, ""))
dst.SetMapIndex(keys[i], x.clone(ctx, keys[i].String(), mValue, ""))
}
return dst

case reflect.Slice:
dst := reflect.MakeSlice(src.Type(), src.Len(), src.Cap())
for i := 0; i < src.Len(); i++ {
dst.Index(i).Set(x.clone(fieldName, src.Index(i), ""))
dst.Index(i).Set(x.clone(ctx, fieldName, src.Index(i), ""))
}
return dst

Expand All @@ -96,21 +113,21 @@ func (x *masq) clone(fieldName string, src reflect.Value, tag string) reflect.Va

dst := reflect.New(src.Type()).Elem()
for i := 0; i < src.Len(); i++ {
dst.Index(i).Set(x.clone(fieldName, src.Index(i), ""))
dst.Index(i).Set(x.clone(ctx, fieldName, src.Index(i), ""))
}
return dst

case reflect.Ptr:
dst := reflect.New(src.Elem().Type())
copied := x.clone(fieldName, src.Elem(), tag)
copied := x.clone(ctx, fieldName, src.Elem(), tag)
dst.Elem().Set(copied)
return dst

case reflect.Interface:
if src.IsNil() {
return src
}
return x.clone(fieldName, src.Elem(), tag)
return x.clone(ctx, fieldName, src.Elem(), tag)

default:
dst := reflect.New(src.Type())
Expand Down
15 changes: 15 additions & 0 deletions clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,18 @@ func TestNilInterface(t *testing.T) {
logger.Info("hello", slog.Any("test", myStruct{}))
gt.S(t, buf.String()).Contains("null")
}

func TestCircularReference(t *testing.T) {
type myStruct struct {
Child *myStruct
Str string
}
data := &myStruct{
Str: "blue",
}
data.Child = data

c := masq.NewMasq(masq.WithContain("blue"))
newData := c.Redact(data).(*myStruct)
gt.V(t, newData.Child.Child.Str).Equal("[REDACTED]")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ go 1.22

require github.com/m-mizutani/gt v0.0.7

require github.com/google/go-cmp v0.5.9 // indirect
require github.com/google/go-cmp v0.6.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/m-mizutani/gt v0.0.7 h1:wKESp5LWdKpKMySX4Rf05iXYUGtoQ3aI7xOO9VVHAoY=
github.com/m-mizutani/gt v0.0.7/go.mod h1:0MPYSfGBLmYjTduzADVmIqD58ELQ5IfBFiK/f0FmB3k=
5 changes: 4 additions & 1 deletion masq.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package masq

import (
"context"
"reflect"

"log/slog"
Expand Down Expand Up @@ -49,7 +50,9 @@ func (x *masq) redact(k string, v any) any {
if v == nil {
return nil
}
copied := x.clone(k, reflect.ValueOf(v), "")

ctx := context.Background()
copied := x.clone(ctx, k, reflect.ValueOf(v), "")
return copied.Interface()
}

Expand Down

0 comments on commit 52aa50f

Please sign in to comment.