-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.go
52 lines (40 loc) · 863 Bytes
/
object.go
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
package gometawebhooks
import (
"encoding/json"
"errors"
"fmt"
)
type Object string
const (
Instagram Object = "instagram"
)
var (
ErrObjectRequired = errors.New("object required")
ErrObjectNotSupported = errors.New("object not supported")
supportedObjects = map[string]Object{
"instagram": Instagram,
}
)
func (t Object) String() string {
return string(t)
}
func (t *Object) FromString(status string) Object {
return supportedObjects[status]
}
func (t Object) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func (t *Object) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if s == "" {
return ErrObjectRequired
}
if _, ok := supportedObjects[s]; !ok {
return fmt.Errorf("'%s': %w", s, ErrObjectNotSupported)
}
*t = t.FromString(s)
return nil
}