-
Notifications
You must be signed in to change notification settings - Fork 8
/
upBuilder.go
141 lines (118 loc) · 4.3 KB
/
upBuilder.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package go_up
import (
"github.com/ufoscout/go-up/reader"
"github.com/ufoscout/go-up/reader/decorator"
)
// HighestPriority the highest priority
const HighestPriority int = 0
// DefaultPriority the default priority
const DefaultPriority int = 100
// DefaultStartDelimiter the default placeholder start delimiter
const DefaultStartDelimiter string = "${"
// DefaultEndDelimiter the default placeholder end delimiter
const DefaultEndDelimiter string = "}"
// DefaultValueSeparator the default separator for a placeholder default value
const DefaultValueSeparator string = ":"
// DefaultListSeparator default list separator
const DefaultListSeparator string = ","
// GoUpBuilder the GoUp builder
type GoUpBuilder interface {
Add(key string, value string) GoUpBuilder
AddReader(newReader reader.Reader) GoUpBuilder
AddReaderWithPriority(newReader reader.Reader, priority int) GoUpBuilder
AddFile(filename string, ignoreNotFound bool) GoUpBuilder
AddFileWithPriority(filename string, ignoreNotFound bool, priority int) GoUpBuilder
Delimiters(startDelimiter string, endDelimiter string) GoUpBuilder
DefaultValueSeparator(defaultValueSeparator string) GoUpBuilder
IgnoreUnresolvablePlaceholders(ignoreUnresolvablePlaceholders bool) GoUpBuilder
Build() (GoUp, error)
}
// NewGoUp creates a new GoUpBuilder
func NewGoUp() GoUpBuilder {
return &goUpBuilderImpl{decorator.NewPriorityQueueDecoratorReader(),
DefaultStartDelimiter,
DefaultEndDelimiter,
DefaultValueSeparator,
false}
}
type goUpBuilderImpl struct {
reader *decorator.PriorityQueueDecoratorReader
startDelimiter string
endDelimiter string
defaultValueSeparator string
ignoreUnresolvablePlaceholders bool
}
func (up *goUpBuilderImpl) Add(key string, value string) GoUpBuilder {
return up.AddReaderWithPriority(reader.NewProgrammaticReader().Add(key, value), DefaultPriority)
}
/**
* Add a new property Reader with the default priority.
* If two or more Readers have the same priority, the last added has the highest priority among them.
*
*/
func (up *goUpBuilderImpl) AddReader(newReader reader.Reader) GoUpBuilder {
return up.AddReaderWithPriority(newReader, DefaultPriority)
}
/**
* Add a new property Reader.
* If two or more Readers have the same priority, the last added has the highest priority among them.
*
*/
func (up *goUpBuilderImpl) AddReaderWithPriority(newReader reader.Reader, priority int) GoUpBuilder {
up.reader.Add(newReader, priority)
return up
}
/**
* Add a new properties file with the default priority.
* If two or more Readers have the same priority, the last added has the highest priority among them.
*/
func (up *goUpBuilderImpl) AddFile(filename string, ignoreNotFound bool) GoUpBuilder {
return up.AddFileWithPriority(filename, ignoreNotFound, DefaultPriority)
}
/**
* Add a new properties file with the specified priority.
* If two or more Readers have the same priority, the last added has the highest priority among them.
*/
func (up *goUpBuilderImpl) AddFileWithPriority(filename string, ignoreNotFound bool, priority int) GoUpBuilder {
return up.AddReaderWithPriority(&reader.FileReader{filename, ignoreNotFound}, priority)
}
/**
*
* Set the start and end placeholder delimiters.
* Default are {@value Default#START_DELIMITER} and {@value Default#END_DELIMITER}
*/
func (up *goUpBuilderImpl) Delimiters(startDelimiter string, endDelimiter string) GoUpBuilder {
up.startDelimiter = startDelimiter
up.endDelimiter = endDelimiter
return up
}
/**
*
* Set the separator for the default value.
* Default is ':'
*/
func (up *goUpBuilderImpl) DefaultValueSeparator(defaultValueSeparator string) GoUpBuilder {
up.defaultValueSeparator = defaultValueSeparator
return up
}
/**
* Whether to ignore not resolvable placeholders.
* Default is false.
*/
func (up *goUpBuilderImpl) IgnoreUnresolvablePlaceholders(ignoreUnresolvablePlaceholders bool) GoUpBuilder {
up.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders
return up
}
func (up *goUpBuilderImpl) Build() (GoUp, error) {
replacer := decorator.PlaceholderReplacerDecoratorReader{
up.reader,
up.startDelimiter,
up.endDelimiter,
up.defaultValueSeparator,
up.ignoreUnresolvablePlaceholders}
properties, err := replacer.Read()
if err != nil {
return nil, err
}
return &goUpImpl{properties}, nil
}