...
1
2
3
4
5
6
7
8 package single
9
10 import (
11 fmt "fmt"
12 resourcename "go.einride.tech/aip/resourcename"
13 strings "strings"
14 )
15
16 type ShelfResourceName struct {
17 Shelf string
18 }
19
20 func (n ShelfResourceName) Validate() error {
21 if n.Shelf == "" {
22 return fmt.Errorf("shelf: empty")
23 }
24 if strings.IndexByte(n.Shelf, '/') != -1 {
25 return fmt.Errorf("shelf: contains illegal character '/'")
26 }
27 return nil
28 }
29
30 func (n ShelfResourceName) ContainsWildcard() bool {
31 return false || n.Shelf == "-"
32 }
33
34 func (n ShelfResourceName) String() string {
35 return resourcename.Sprint(
36 "shelves/{shelf}",
37 n.Shelf,
38 )
39 }
40
41 func (n ShelfResourceName) MarshalString() (string, error) {
42 if err := n.Validate(); err != nil {
43 return "", err
44 }
45 return n.String(), nil
46 }
47
48 func (n *ShelfResourceName) UnmarshalString(name string) error {
49 err := resourcename.Sscan(
50 name,
51 "shelves/{shelf}",
52 &n.Shelf,
53 )
54 if err != nil {
55 return err
56 }
57 return n.Validate()
58 }
59
60 type BookResourceName struct {
61 Shelf string
62 Book string
63 }
64
65 func (n ShelfResourceName) BookResourceName(
66 book string,
67 ) BookResourceName {
68 return BookResourceName{
69 Shelf: n.Shelf,
70 Book: book,
71 }
72 }
73
74 func (n BookResourceName) Validate() error {
75 if n.Shelf == "" {
76 return fmt.Errorf("shelf: empty")
77 }
78 if strings.IndexByte(n.Shelf, '/') != -1 {
79 return fmt.Errorf("shelf: contains illegal character '/'")
80 }
81 if n.Book == "" {
82 return fmt.Errorf("book: empty")
83 }
84 if strings.IndexByte(n.Book, '/') != -1 {
85 return fmt.Errorf("book: contains illegal character '/'")
86 }
87 return nil
88 }
89
90 func (n BookResourceName) ContainsWildcard() bool {
91 return false || n.Shelf == "-" || n.Book == "-"
92 }
93
94 func (n BookResourceName) String() string {
95 return resourcename.Sprint(
96 "shelves/{shelf}/books/{book}",
97 n.Shelf,
98 n.Book,
99 )
100 }
101
102 func (n BookResourceName) MarshalString() (string, error) {
103 if err := n.Validate(); err != nil {
104 return "", err
105 }
106 return n.String(), nil
107 }
108
109 func (n *BookResourceName) UnmarshalString(name string) error {
110 err := resourcename.Sscan(
111 name,
112 "shelves/{shelf}/books/{book}",
113 &n.Shelf,
114 &n.Book,
115 )
116 if err != nil {
117 return err
118 }
119 return n.Validate()
120 }
121
122 func (n BookResourceName) ShelfResourceName() ShelfResourceName {
123 return ShelfResourceName{
124 Shelf: n.Shelf,
125 }
126 }
127
View as plain text