File: slice.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (211 lines) | stat: -rw-r--r-- 4,170 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) 2020, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package flat

import (
	"reflect"

	"github.com/maxatome/go-testdeep/helpers/tdutil"
)

var sliceType = reflect.TypeOf(Slice{})

// Slice allows to flatten any slice, array or map.
type Slice struct {
	Slice any
}

// isFlat returns true if no flat.Slice items can be contained in
// f.Slice, so this Slice is already flattened.
func (f Slice) isFlat() bool {
	t := reflect.TypeOf(f.Slice).Elem()
	return t != sliceType && t.Kind() != reflect.Interface
}

func subLen(v reflect.Value) int {
	if v.Kind() == reflect.Interface {
		v = v.Elem()
	}
	if s, ok := v.Interface().(Slice); ok {
		return s.len()
	}
	return 1
}

func (f Slice) len() int {
	fv := reflect.ValueOf(f.Slice)
	l := 0

	if fv.Kind() == reflect.Map {
		if f.isFlat() {
			return fv.Len() * 2
		}
		tdutil.MapEach(fv, func(k, v reflect.Value) bool {
			l += 1 + subLen(v)
			return true
		})
		return l
	}

	fvLen := fv.Len()
	if f.isFlat() {
		return fvLen
	}
	for i := 0; i < fvLen; i++ {
		l += subLen(fv.Index(i))
	}
	return l
}

func subAppendValuesTo(sv []reflect.Value, v reflect.Value) []reflect.Value {
	if v.Kind() == reflect.Interface {
		v = v.Elem()
	}
	if s, ok := v.Interface().(Slice); ok {
		return s.appendValuesTo(sv)
	}
	return append(sv, v)
}

func (f Slice) appendValuesTo(sv []reflect.Value) []reflect.Value {
	fv := reflect.ValueOf(f.Slice)

	if fv.Kind() == reflect.Map {
		if f.isFlat() {
			tdutil.MapEach(fv, func(k, v reflect.Value) bool {
				sv = append(sv, k, v)
				return true
			})
			return sv
		}

		tdutil.MapEach(fv, func(k, v reflect.Value) bool {
			sv = append(sv, k)
			sv = subAppendValuesTo(sv, v)
			return true
		})
		return sv
	}

	fvLen := fv.Len()

	if f.isFlat() {
		for i := 0; i < fvLen; i++ {
			sv = append(sv, fv.Index(i))
		}
		return sv
	}

	for i := 0; i < fvLen; i++ {
		sv = subAppendValuesTo(sv, fv.Index(i))
	}
	return sv
}

func subAppendTo(si []any, v reflect.Value) []any {
	if v.Kind() == reflect.Interface {
		v = v.Elem()
	}
	i := v.Interface()
	if s, ok := i.(Slice); ok {
		return s.appendTo(si)
	}
	return append(si, i)
}

func (f Slice) appendTo(si []any) []any {
	fv := reflect.ValueOf(f.Slice)

	if fv.Kind() == reflect.Map {
		if f.isFlat() {
			tdutil.MapEach(fv, func(k, v reflect.Value) bool {
				si = append(si, k.Interface(), v.Interface())
				return true
			})
			return si
		}

		tdutil.MapEach(fv, func(k, v reflect.Value) bool {
			si = append(si, k.Interface())
			si = subAppendTo(si, v)
			return true
		})
		return si
	}

	fvLen := fv.Len()

	if f.isFlat() {
		for i := 0; i < fvLen; i++ {
			si = append(si, fv.Index(i).Interface())
		}
		return si
	}

	for i := 0; i < fvLen; i++ {
		si = subAppendTo(si, fv.Index(i))
	}
	return si
}

// Len returns the number of items contained in items. Nested Slice
// items are counted as if they are flattened. It returns true if at
// least one [Slice] item is found, false otherwise.
func Len(items []any) (int, bool) {
	l := len(items)
	flattened := true

	for _, item := range items {
		if subf, ok := item.(Slice); ok {
			l += subf.len() - 1
			flattened = false
		}
	}
	return l, flattened
}

// Values returns the items values as a slice of
// [reflect.Value]. Nested [Slice] items are flattened.
func Values(items []any) []reflect.Value {
	l, flattened := Len(items)
	if flattened {
		sv := make([]reflect.Value, l)
		for i, item := range items {
			sv[i] = reflect.ValueOf(item)
		}
		return sv
	}

	sv := make([]reflect.Value, 0, l)
	for _, item := range items {
		if f, ok := item.(Slice); ok {
			sv = f.appendValuesTo(sv)
		} else {
			sv = append(sv, reflect.ValueOf(item))
		}
	}
	return sv
}

// Interfaces returns the items values as a slice of
// any. Nested [Slice] items are flattened.
func Interfaces(items ...any) []any {
	l, flattened := Len(items)
	if flattened {
		return items
	}

	si := make([]any, 0, l)
	for _, item := range items {
		if f, ok := item.(Slice); ok {
			si = f.appendTo(si)
		} else {
			si = append(si, item)
		}
	}
	return si
}