diff options
Diffstat (limited to 'vendor/github.com/d5/tengo/objects/count_objects.go')
-rw-r--r-- | vendor/github.com/d5/tengo/objects/count_objects.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/vendor/github.com/d5/tengo/objects/count_objects.go b/vendor/github.com/d5/tengo/objects/count_objects.go new file mode 100644 index 00000000..8c482eb3 --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/count_objects.go @@ -0,0 +1,31 @@ +package objects + +// CountObjects returns the number of objects that a given object o contains. +// For scalar value types, it will always be 1. For compound value types, +// this will include its elements and all of their elements recursively. +func CountObjects(o Object) (c int) { + c = 1 + + switch o := o.(type) { + case *Array: + for _, v := range o.Value { + c += CountObjects(v) + } + case *ImmutableArray: + for _, v := range o.Value { + c += CountObjects(v) + } + case *Map: + for _, v := range o.Value { + c += CountObjects(v) + } + case *ImmutableMap: + for _, v := range o.Value { + c += CountObjects(v) + } + case *Error: + c += CountObjects(o.Value) + } + + return +} |