aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/magiconair/properties/properties.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/magiconair/properties/properties.go')
-rw-r--r--vendor/github.com/magiconair/properties/properties.go82
1 files changed, 52 insertions, 30 deletions
diff --git a/vendor/github.com/magiconair/properties/properties.go b/vendor/github.com/magiconair/properties/properties.go
index 85bb186..cb3d1a3 100644
--- a/vendor/github.com/magiconair/properties/properties.go
+++ b/vendor/github.com/magiconair/properties/properties.go
@@ -1,4 +1,4 @@
-// Copyright 2017 Frank Schroeder. All rights reserved.
+// Copyright 2018 Frank Schroeder. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
@@ -19,6 +19,8 @@ import (
"unicode/utf8"
)
+const maxExpansionDepth = 64
+
// ErrorHandlerFunc defines the type of function which handles failures
// of the MustXXX() functions. An error handler function must exit
// the application after handling the error.
@@ -81,6 +83,17 @@ func NewProperties() *Properties {
}
}
+// Load reads a buffer into the given Properties struct.
+func (p *Properties) Load(buf []byte, enc Encoding) error {
+ l := &Loader{Encoding: enc, DisableExpansion: p.DisableExpansion}
+ newProperties, err := l.LoadBytes(buf)
+ if err != nil {
+ return err
+ }
+ p.Merge(newProperties)
+ return nil
+}
+
// Get returns the expanded value for the given key if exists.
// Otherwise, ok is false.
func (p *Properties) Get(key string) (value string, ok bool) {
@@ -92,7 +105,7 @@ func (p *Properties) Get(key string) (value string, ok bool) {
return "", false
}
- expanded, err := p.expand(v)
+ expanded, err := p.expand(key, v)
// we guarantee that the expanded value is free of
// circular references and malformed expressions
@@ -525,7 +538,7 @@ func (p *Properties) Set(key, value string) (prev string, ok bool, err error) {
p.m[key] = value
// now check for a circular reference
- _, err = p.expand(value)
+ _, err = p.expand(key, value)
if err != nil {
// revert to the previous state
@@ -696,56 +709,65 @@ outer:
// check expands all values and returns an error if a circular reference or
// a malformed expression was found.
func (p *Properties) check() error {
- for _, value := range p.m {
- if _, err := p.expand(value); err != nil {
+ for key, value := range p.m {
+ if _, err := p.expand(key, value); err != nil {
return err
}
}
return nil
}
-func (p *Properties) expand(input string) (string, error) {
+func (p *Properties) expand(key, input string) (string, error) {
// no pre/postfix -> nothing to expand
if p.Prefix == "" && p.Postfix == "" {
return input, nil
}
- return expand(input, make(map[string]bool), p.Prefix, p.Postfix, p.m)
+ return expand(input, []string{key}, p.Prefix, p.Postfix, p.m)
}
// expand recursively expands expressions of '(prefix)key(postfix)' to their corresponding values.
// The function keeps track of the keys that were already expanded and stops if it
// detects a circular reference or a malformed expression of the form '(prefix)key'.
-func expand(s string, keys map[string]bool, prefix, postfix string, values map[string]string) (string, error) {
- start := strings.Index(s, prefix)
- if start == -1 {
- return s, nil
- }
-
- keyStart := start + len(prefix)
- keyLen := strings.Index(s[keyStart:], postfix)
- if keyLen == -1 {
- return "", fmt.Errorf("malformed expression")
+func expand(s string, keys []string, prefix, postfix string, values map[string]string) (string, error) {
+ if len(keys) > maxExpansionDepth {
+ return "", fmt.Errorf("expansion too deep")
}
- end := keyStart + keyLen + len(postfix) - 1
- key := s[keyStart : keyStart+keyLen]
+ for {
+ start := strings.Index(s, prefix)
+ if start == -1 {
+ return s, nil
+ }
- // fmt.Printf("s:%q pp:%q start:%d end:%d keyStart:%d keyLen:%d key:%q\n", s, prefix + "..." + postfix, start, end, keyStart, keyLen, key)
+ keyStart := start + len(prefix)
+ keyLen := strings.Index(s[keyStart:], postfix)
+ if keyLen == -1 {
+ return "", fmt.Errorf("malformed expression")
+ }
- if _, ok := keys[key]; ok {
- return "", fmt.Errorf("circular reference")
- }
+ end := keyStart + keyLen + len(postfix) - 1
+ key := s[keyStart : keyStart+keyLen]
- val, ok := values[key]
- if !ok {
- val = os.Getenv(key)
- }
+ // fmt.Printf("s:%q pp:%q start:%d end:%d keyStart:%d keyLen:%d key:%q\n", s, prefix + "..." + postfix, start, end, keyStart, keyLen, key)
- // remember that we've seen the key
- keys[key] = true
+ for _, k := range keys {
+ if key == k {
+ return "", fmt.Errorf("circular reference")
+ }
+ }
- return expand(s[:start]+val+s[end+1:], keys, prefix, postfix, values)
+ val, ok := values[key]
+ if !ok {
+ val = os.Getenv(key)
+ }
+ new_val, err := expand(val, append(keys, key), prefix, postfix, values)
+ if err != nil {
+ return "", err
+ }
+ s = s[:start] + new_val + s[end+1:]
+ }
+ return s, nil
}
// encode encodes a UTF-8 string to ISO-8859-1 and escapes some characters.