-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmeta.go
More file actions
66 lines (56 loc) · 1.62 KB
/
meta.go
File metadata and controls
66 lines (56 loc) · 1.62 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
56
57
58
59
60
61
62
63
64
65
66
package jsonapi
import (
"fmt"
"time"
)
// Meta holds meta information.
type Meta map[string]any
// Has reports whether the Meta map contains or not the given key.
func (m Meta) Has(key string) bool {
_, ok := m[key]
return ok
}
// GetString returns the string associated with the given key.
//
// An empty string is returned if the key could not be found or the type is not
// compatible.
func (m Meta) GetString(key string) string {
return fmt.Sprint(m[key])
}
// GetInt returns the int associated with the given key.
//
// 0 is returned if the key could not be found or the type is not compatible.
func (m Meta) GetInt(key string) int {
v, _ := m[key].(int)
return v
}
// GetBool returns the bool associated with the given key.
//
// False is returned if the key could not be found or the type is not
// compatible. The "true" JSON keyword is the only value that will make this
// method return true.
func (m Meta) GetBool(key string) bool {
b, _ := m[key].(bool)
return b
}
// GetTime returns the time.Time associated with the given key.
//
// time.Time{} is returned is the value associated with the key could not be
// found or could not be parsed with time.RFC3339Nano.
func (m Meta) GetTime(key string) time.Time {
t := time.Time{}
if s, ok := m[key].(string); ok {
t, _ = time.Parse(time.RFC3339Nano, s)
}
return t
}
// A MetaHolder can hold and return meta values.
//
// It is useful for a struct that represents a resource type to implement this
// interface to have a meta property as part of its JSON output.
//
// Implementations don't have to deeply copy the maps.
type MetaHolder interface {
Meta() Meta
SetMeta(Meta)
}