-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
117 lines (94 loc) · 2.92 KB
/
state.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package replacers
import (
"sync"
)
// ReplaceState is a struct that holds information about the current state of the replace operation.
type ReplaceState struct {
// NamePath is a slice of names of the current element.
// It is used to build a path to the current element.
// For example, "users", "name", "first".
NamePath []string
// ElementIndex is an index of the current element if required structure to generate is an array.
ElementIndex int
// IsHeader is a flag that indicates that the current element we're replacing is a header.
IsHeader bool
// IsPathParam is a flag that indicates that the current element we're replacing is a path parameter.
IsPathParam bool
// ContentType is a content type of the current element.
ContentType string
// IsContentReadOnly is a flag that indicates that the current element we're replacing is a read-only content.
// This value is used only when Schema has ReadOnly set to true.
IsContentReadOnly bool
// IsContentWriteOnly is a flag that indicates that the current element we're replacing is a write-only content.
// This value is used only when Schema has WriteOnly set to true.
IsContentWriteOnly bool
mu sync.Mutex
}
func NewReplaceState(opts ...ReplaceStateOption) *ReplaceState {
return (&ReplaceState{
NamePath: []string{},
}).WithOptions(opts...)
}
func NewReplaceStateWithName(name string) *ReplaceState {
return NewReplaceState(WithName(name))
}
// NewFrom creates a new ReplaceState instance from the given one.
func (s *ReplaceState) NewFrom(src *ReplaceState) *ReplaceState {
return &ReplaceState{
NamePath: src.NamePath,
IsHeader: src.IsHeader,
IsPathParam: src.IsPathParam,
ContentType: src.ContentType,
IsContentReadOnly: src.IsContentReadOnly,
IsContentWriteOnly: src.IsContentWriteOnly,
}
}
type ReplaceStateOption func(*ReplaceState)
func (s *ReplaceState) WithOptions(options ...ReplaceStateOption) *ReplaceState {
s.mu.Lock()
defer s.mu.Unlock()
for _, opt := range options {
opt(s)
}
return s
}
func WithName(name string) ReplaceStateOption {
return func(state *ReplaceState) {
namePath := state.NamePath
if len(namePath) == 0 {
namePath = []string{}
}
namePath = append(namePath, name)
state.NamePath = namePath
}
}
func WithElementIndex(value int) ReplaceStateOption {
return func(state *ReplaceState) {
state.ElementIndex = value
}
}
func WithHeader() ReplaceStateOption {
return func(state *ReplaceState) {
state.IsHeader = true
}
}
func WithPath() ReplaceStateOption {
return func(state *ReplaceState) {
state.IsPathParam = true
}
}
func WithContentType(value string) ReplaceStateOption {
return func(state *ReplaceState) {
state.ContentType = value
}
}
func WithReadOnly() ReplaceStateOption {
return func(state *ReplaceState) {
state.IsContentReadOnly = true
}
}
func WithWriteOnly() ReplaceStateOption {
return func(state *ReplaceState) {
state.IsContentWriteOnly = true
}
}