Skip to content

Commit c4e4c62

Browse files
authored
Merge pull request #580 from ralphocdol/unique-array-custom-api
added unique to filter arrays with unique items in custom-api
2 parents 8f217e6 + 33b54de commit c4e4c62

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Diff for: docs/custom-api.md

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ The following helper functions provided by Glance are available:
397397
- `sortByFloat(key string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a float key in either ascending or descending order.
398398
- `sortByTime(key string, layout string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a time key in either ascending or descending order. The format must be provided in Go's [date format](https://pkg.go.dev/time#pkg-constants).
399399
- `concat(strings ...string) string`: Concatenates multiple strings together.
400+
- `unique(key string, arr []JSON) []JSON`: Returns a unique array of JSON objects based on the given key.
400401

401402
The following helper functions provided by Go's `text/template` are available:
402403

Diff for: internal/glance/cli.go

-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ func cliSensorsPrint() int {
9191
fmt.Printf(" - %v\n", w)
9292
}
9393
}
94-
9594
return 1
9695
}
9796

Diff for: internal/glance/widget-custom-api.go

+12
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,18 @@ var customAPITemplateFuncs = func() template.FuncMap {
547547
"concat": func(items ...string) string {
548548
return strings.Join(items, "")
549549
},
550+
"unique": func(key string, results []decoratedGJSONResult) []decoratedGJSONResult {
551+
seen := make(map[string]struct{})
552+
out := make([]decoratedGJSONResult, 0, len(results))
553+
for _, result := range results {
554+
val := result.String(key)
555+
if _, ok := seen[val]; !ok {
556+
seen[val] = struct{}{}
557+
out = append(out, result)
558+
}
559+
}
560+
return out
561+
},
550562
}
551563

552564
for key, value := range globalTemplateFunctions {

0 commit comments

Comments
 (0)