Fix gometalinter complaints
diff --git a/benchmark_test.go b/benchmark_test.go index b2019e1..62c7cc5 100644 --- a/benchmark_test.go +++ b/benchmark_test.go
@@ -17,6 +17,8 @@ } b.ResetTimer() for i := 0; i < b.N; i++ { - Load([]byte(input), ISO_8859_1) + if _, err := Load([]byte(input), ISO_8859_1); err != nil { + b.Fatal(err) + } } }
diff --git a/decode.go b/decode.go index 1461c0a..0a961bb 100644 --- a/decode.go +++ b/decode.go
@@ -158,16 +158,16 @@ // keydef returns the property key and the default value based on the // name of the struct field and the options in the tag. keydef := func(f reflect.StructField) (string, *string, map[string]string) { - key, opts := parseTag(f.Tag.Get("properties")) + _key, _opts := parseTag(f.Tag.Get("properties")) - var def *string - if d, ok := opts["default"]; ok { - def = &d + var _def *string + if d, ok := _opts["default"]; ok { + _def = &d } - if key != "" { - return key, def, opts + if _key != "" { + return _key, _def, _opts } - return f.Name, def, opts + return f.Name, _def, _opts } switch { @@ -223,7 +223,7 @@ case isMap(t): valT := t.Elem() m := reflect.MakeMap(t) - for postfix, _ := range p.FilterStripPrefix(key + ".").m { + for postfix := range p.FilterStripPrefix(key + ".").m { pp := strings.SplitN(postfix, ".", 2) mk, mv := pp[0], reflect.New(valT) if err := dec(p, key+"."+mk, nil, nil, mv); err != nil { @@ -274,7 +274,6 @@ func isBool(t reflect.Type) bool { return t.Kind() == reflect.Bool } func isDuration(t reflect.Type) bool { return t == reflect.TypeOf(time.Second) } func isMap(t reflect.Type) bool { return t.Kind() == reflect.Map } -func isNumeric(t reflect.Type) bool { return isInt(t) || isUint(t) || isFloat(t) } func isPtr(t reflect.Type) bool { return t.Kind() == reflect.Ptr } func isString(t reflect.Type) bool { return t.Kind() == reflect.String } func isStruct(t reflect.Type) bool { return t.Kind() == reflect.Struct }
diff --git a/integrate_test.go b/integrate_test.go index b8fcf0e..cbee181 100644 --- a/integrate_test.go +++ b/integrate_test.go
@@ -17,8 +17,8 @@ gotI := f.Int("i", -1, "int flag") p := NewProperties() - p.Set("s", "t") - p.Set("i", "9") + p.MustSet("s", "t") + p.MustSet("i", "9") p.MustFlag(f) if want := "t"; *gotS != want { @@ -36,11 +36,13 @@ gotB := f.Int("b", 2, "customized") gotC := f.Int("c", 3, "overridden") - f.Parse([]string{"-c", "4"}) + if err := f.Parse([]string{"-c", "4"}); err != nil { + t.Fatal(err) + } p := NewProperties() - p.Set("b", "5") - p.Set("c", "6") + p.MustSet("b", "5") + p.MustSet("c", "6") p.MustFlag(f) if want := 1; *gotA != want { @@ -63,8 +65,8 @@ fmt.Printf("flagged as x=%d, y=%d\n", *x, *y) p := NewProperties() - p.Set("x", "7") - p.Set("y", "42") // note discard + p.MustSet("x", "7") + p.MustSet("y", "42") // note discard p.MustFlag(flag.CommandLine) fmt.Printf("configured to x=%d, y=%d\n", *x, *y)
diff --git a/lex.go b/lex.go index 6ad94f3..a3cba03 100644 --- a/lex.go +++ b/lex.go
@@ -72,7 +72,7 @@ // next returns the next rune in the input. func (l *lexer) next() rune { - if int(l.pos) >= len(l.input) { + if l.pos >= len(l.input) { l.width = 0 return eof } @@ -96,8 +96,8 @@ // emit passes an item back to the client. func (l *lexer) emit(t itemType) { - item := item{t, l.start, string(l.runes)} - l.items <- item + i := item{t, l.start, string(l.runes)} + l.items <- i l.start = l.pos l.runes = l.runes[:0] } @@ -114,7 +114,7 @@ // accept consumes the next rune if it's from the valid set. func (l *lexer) accept(valid string) bool { - if strings.IndexRune(valid, l.next()) >= 0 { + if strings.ContainsRune(valid, l.next()) { return true } l.backup() @@ -123,7 +123,7 @@ // acceptRun consumes a run of runes from the valid set. func (l *lexer) acceptRun(valid string) { - for strings.IndexRune(valid, l.next()) >= 0 { + for strings.ContainsRune(valid, l.next()) { } l.backup() } @@ -156,9 +156,9 @@ // nextItem returns the next item from the input. func (l *lexer) nextItem() item { - item := <-l.items - l.lastPos = item.pos - return item + i := <-l.items + l.lastPos = i.pos + return i } // lex creates a new scanner for the input string. @@ -279,8 +279,7 @@ for { switch r := l.next(); { case isEscape(r): - r := l.peek() - if isEOL(r) { + if isEOL(l.peek()) { l.next() l.acceptRun(whitespace) } else {
diff --git a/load.go b/load.go index 41f353d..701a86d 100644 --- a/load.go +++ b/load.go
@@ -98,7 +98,7 @@ return must(LoadURL(url)) } -// MustLoadFiles reads the content of multiple URLs in the given order into a +// MustLoadURLs reads the content of multiple URLs in the given order into a // Properties struct and panics on error. If 'ignoreMissing' is true then a 404 // status code will not be reported as error. func MustLoadURLs(urls []string, ignoreMissing bool) *Properties { @@ -172,10 +172,12 @@ return nil, fmt.Errorf("properties: %s returned %d", url, resp.StatusCode) } body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return nil, fmt.Errorf("properties: %s error reading response. %s", url, err) } + if err = resp.Body.Close(); err != nil { + return nil, fmt.Errorf("properties: %s error reading response. %s", url, err) + } ct := resp.Header.Get("Content-Type") var enc Encoding
diff --git a/load_test.go b/load_test.go index 8b3137e..c46339d 100644 --- a/load_test.go +++ b/load_test.go
@@ -66,7 +66,9 @@ tf := make(tempFiles, 0) defer tf.removeAll() - os.Setenv("_VARX", "some-value") + if err := os.Setenv("_VARX", "some-value"); err != nil { + t.Fatal(err) + } filename := tf.makeFilePrefix(os.Getenv("_VARX"), "key=value") filename = strings.Replace(filename, os.Getenv("_VARX"), "${_VARX}", -1) p := MustLoadFile(filename, ISO_8859_1) @@ -189,7 +191,9 @@ return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { send := func(data []byte, contentType string) { w.Header().Set("Content-Type", contentType) - w.Write(data) + if _, err := w.Write(data); err != nil { + panic(err) + } } utf8 := []byte("key=äöü")
diff --git a/properties.go b/properties.go index d41343d..80360c9 100644 --- a/properties.go +++ b/properties.go
@@ -28,8 +28,10 @@ // functions. The default is LogFatalHandler. var ErrorHandler ErrorHandlerFunc = LogFatalHandler +// LogHandlerFunc defines the function prototype for logging errors. type LogHandlerFunc func(fmt string, args ...interface{}) +// LogPrintf defines a log handler which uses log.Printf. var LogPrintf LogHandlerFunc = log.Printf // LogFatalHandler handles the error by logging a fatal error and exiting. @@ -444,6 +446,8 @@ pp := NewProperties() for _, k := range p.k { if re.MatchString(k) { + // TODO(fs): we are ignoring the error which flags a circular reference. + // TODO(fs): since we are just copying a subset of keys this cannot happen (fingers crossed) pp.Set(k, p.m[k]) } } @@ -456,6 +460,8 @@ pp := NewProperties() for _, k := range p.k { if strings.HasPrefix(k, prefix) { + // TODO(fs): we are ignoring the error which flags a circular reference. + // TODO(fs): since we are just copying a subset of keys this cannot happen (fingers crossed) pp.Set(k, p.m[k]) } } @@ -469,6 +475,9 @@ n := len(prefix) for _, k := range p.k { if len(k) > len(prefix) && strings.HasPrefix(k, prefix) { + // TODO(fs): we are ignoring the error which flags a circular reference. + // TODO(fs): since we are modifying keys I am not entirely sure whether we can create a circular reference + // TODO(fs): this function should probably return an error but the signature is fixed pp.Set(k[n:], p.m[k]) } } @@ -483,9 +492,7 @@ // Keys returns all keys in the same order as in the input. func (p *Properties) Keys() []string { keys := make([]string, len(p.k)) - for i, k := range p.k { - keys[i] = k - } + copy(keys, p.k) return keys }
diff --git a/properties_test.go b/properties_test.go index bf5aa0d..a63a560 100644 --- a/properties_test.go +++ b/properties_test.go
@@ -452,8 +452,8 @@ assert.Equal(t, p.MustGet("key2"), "${key}") // with expansion disabled we can introduce circular references - p.Set("keyA", "${keyB}") - p.Set("keyB", "${keyA}") + p.MustSet("keyA", "${keyB}") + p.MustSet("keyB", "${keyA}") assert.Equal(t, p.MustGet("keyA"), "${keyB}") assert.Equal(t, p.MustGet("keyB"), "${keyA}") } @@ -676,6 +676,22 @@ } } +func TestFilterStripPrefix(t *testing.T) { + for _, test := range filterStripPrefixTests { + p := mustParse(t, test.input) + pp := p.FilterPrefix(test.prefix) + assert.Equal(t, pp != nil, true, "want properties") + assert.Equal(t, pp.Len(), len(test.keys)) + for _, key := range test.keys { + v1, ok1 := p.Get(key) + v2, ok2 := pp.Get(key) + assert.Equal(t, ok1, true) + assert.Equal(t, ok2, true) + assert.Equal(t, v1, v2) + } + } +} + func TestKeys(t *testing.T) { for _, test := range keysTests { p := mustParse(t, test.input)