-
Notifications
You must be signed in to change notification settings - Fork 0
/
member_type.go
54 lines (49 loc) · 1.25 KB
/
member_type.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
53
54
package jsonapi
import (
"fmt"
"reflect"
"strings"
)
// memberType is a JSONP:API member type.
type memberType string
const (
memberTypeAttribute memberType = "attribute"
memberTypeLinks memberType = "links"
memberTypeMeta memberType = "meta"
memberTypePrimary memberType = "primary"
memberTypeRelationship memberType = "relationship"
)
func newMemberType(s string) (memberType, error) {
switch s {
case "attribute":
return memberTypeAttribute, nil
case "links":
return memberTypeLinks, nil
case "meta":
return memberTypeMeta, nil
case "primary":
return memberTypePrimary, nil
case "relationship":
return memberTypeRelationship, nil
default:
return "", fmt.Errorf("member type '%s' not found", s)
}
}
func getMember(field reflect.StructField) (memberType, string, error) {
tag, ok := field.Tag.Lookup(tagKey)
if !ok {
return "", "", fmt.Errorf("tag: %s, not specified", tagKey)
}
if tag == "" {
return "", "", fmt.Errorf("tag: %s, was empty", tagKey)
}
tagParts := strings.Split(tag, ",")
if len(tagParts) != 2 {
return "", "", fmt.Errorf("tag: %s, was not formatted properly", tagKey)
}
memberType, err := newMemberType(tagParts[0])
if err != nil {
return "", "", err
}
return memberType, tagParts[1], nil
}