File: collection.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,280 kB
  • sloc: makefile: 16
file content (104 lines) | stat: -rw-r--r-- 2,501 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
package data

import (
	"github.com/viant/toolbox"
	"strings"
)

//Collection represents a slice of interface{} (generic type)
type Collection []interface{}

//Push appends provided value to the slice
func (s *Collection) Push(value interface{}) {
	(*s) = append(*s, value)
}

//PadWithMap creates missing elements with a map
func (s *Collection) PadWithMap(size int) {
	for i := len(*s); i < size; i++ {
		s.Push(NewMap())
	}
}

//Range iterates over every item in this collection as long as handler returns true. Handler takes  an index and index of the slice element.
func (s *Collection) Range(handler func(item interface{}, index int) (bool, error)) error {
	for i, elem := range *s {
		next, err := handler(elem, i)
		if err != nil {
			return err
		}
		if !next {
			break
		}

	}
	return nil
}

//RangeMap iterates every map item in this collection as long as handler returns true. Handler takes  an index and index of the slice element
func (s *Collection) RangeMap(handler func(item Map, index int) (bool, error)) error {
	var next bool
	var err error
	for i, elem := range *s {
		var aMap, ok = elem.(Map)
		if !ok {
			next, err = handler(nil, i)
		} else {
			next, err = handler(aMap, i)
		}
		if err != nil {
			return err
		}
		if !next {
			break
		}

	}
	return nil
}

//RangeMap iterates every string item in this collection as long as handler returns true. Handler takes  an index and index of the slice element
func (s *Collection) RangeString(handler func(item interface{}, index int) (bool, error)) error {
	for i, elem := range *s {
		next, err := handler(toolbox.AsString(elem), i)
		if err != nil {
			return err
		}
		if !next {
			break
		}

	}
	return nil

}

//RangeMap iterates every int item in this collection as long as handler returns true. Handler takes  an index and index of the slice element
func (s *Collection) RangeInt(handler func(item interface{}, index int) (bool, error)) error {
	for i, elem := range *s {
		next, err := handler(toolbox.AsInt(elem), i)
		if err != nil {
			return err
		}
		if !next {
			break
		}

	}
	return nil
}

//String returns a string representation of this collection
func (s *Collection) String() string {
	var items = make([]string, 0)
	for _, item := range *s {
		items = append(items, toolbox.AsString(item))
	}
	return "[" + strings.Join(items, ",") + "]"
}

//NewCollection creates a new collection and returns a pointer
func NewCollection() *Collection {
	var result Collection = make([]interface{}, 0)
	return &result
}