-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.go
More file actions
55 lines (46 loc) · 1.5 KB
/
collection.go
File metadata and controls
55 lines (46 loc) · 1.5 KB
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
// Copyright (c) 2025 Emin Salih Açıkgöz
// SPDX-License-Identifier: MIT
package hal
import (
"context"
"reflect"
)
// CollectionPage represents a standard HAL collection response.
// It includes navigation links, embedded items, and pagination metadata.
type CollectionPage struct {
Links map[string]any `json:"_links"`
Embedded map[string]any `json:"_embedded"`
Count int `json:"count"`
Total int `json:"total,omitempty"`
}
// Collection creates a CollectionPage using the DefaultInstance.
func Collection[T any](ctx context.Context, items []*T, total int, selfLink Link) *CollectionPage {
return DefaultInstance.Collection(ctx, items, total, selfLink)
}
// Collection wraps a slice of items into a HAL CollectionPage.
// It iterates over the items, wraps each one using the registered generators,
// and constructs the embedded items list.
//
// This method panics if items is not a slice.
func (i *Instance) Collection(ctx context.Context, items any, total int, selfLink Link) *CollectionPage {
val := reflect.ValueOf(items)
if val.Kind() != reflect.Slice {
panic("hal: Collection items must be a slice")
}
count := val.Len()
embeddedItems := make([]*Envelope, count)
for idx := 0; idx < count; idx++ {
item := val.Index(idx).Interface()
embeddedItems[idx] = i.Wrap(ctx, item)
}
links := make(map[string]any)
links["self"] = selfLink
return &CollectionPage{
Links: links,
Embedded: map[string]any{
"items": embeddedItems,
},
Count: count,
Total: total,
}
}