forked from jinzhu/configor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jinzhu#37 from reuben453/error_on_unmatched_keys_u…
…pstream_pr Add support for using the ErrorOnUnmatchedKeys setting with json files
- Loading branch information
Showing
7 changed files
with
218 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build go1.10 | ||
|
||
package configor | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"strings" | ||
) | ||
|
||
// unmarshalJSON unmarshals the given data into the config interface. | ||
// If the errorOnUnmatchedKeys boolean is true, an error will be returned if there | ||
// are keys in the data that do not match fields in the config interface. | ||
func unmarshalJSON(data []byte, config interface{}, errorOnUnmatchedKeys bool) error { | ||
reader := strings.NewReader(string(data)) | ||
decoder := json.NewDecoder(reader) | ||
|
||
if errorOnUnmatchedKeys { | ||
decoder.DisallowUnknownFields() | ||
} | ||
|
||
err := decoder.Decode(config) | ||
if err != nil && err != io.EOF { | ||
return err | ||
} | ||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// +build go1.10 | ||
|
||
package configor_test | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/jinzhu/configor" | ||
) | ||
|
||
func TestUnmatchedKeyInJsonConfigFile(t *testing.T) { | ||
type configStruct struct { | ||
Name string | ||
} | ||
type configFile struct { | ||
Name string | ||
Test string | ||
} | ||
config := configFile{Name: "test", Test: "ATest"} | ||
|
||
file, err := ioutil.TempFile("/tmp", "configor") | ||
if err != nil { | ||
t.Fatal("Could not create temp file") | ||
} | ||
defer os.Remove(file.Name()) | ||
defer file.Close() | ||
|
||
filename := file.Name() | ||
|
||
if err := json.NewEncoder(file).Encode(config); err == nil { | ||
|
||
var result configStruct | ||
|
||
// Do not return error when there are unmatched keys but ErrorOnUnmatchedKeys is false | ||
if err := configor.New(&configor.Config{}).Load(&result, filename); err != nil { | ||
t.Errorf("Should NOT get error when loading configuration with extra keys. Error: %v", err) | ||
} | ||
|
||
// Return an error when there are unmatched keys and ErrorOnUnmatchedKeys is true | ||
if err := configor.New(&configor.Config{ErrorOnUnmatchedKeys: true}).Load(&result, filename); err == nil || !strings.Contains(err.Error(), "json: unknown field") { | ||
|
||
t.Errorf("Should get unknown field error when loading configuration with extra keys. Instead got error: %v", err) | ||
} | ||
|
||
} else { | ||
t.Errorf("failed to marshal config") | ||
} | ||
|
||
// Add .json to the file name and test | ||
err = os.Rename(file.Name(), file.Name()+".json") | ||
if err != nil { | ||
t.Errorf("Could not add suffix to file") | ||
} | ||
filename = file.Name() + ".json" | ||
defer os.Remove(filename) | ||
|
||
var result configStruct | ||
|
||
// Do not return error when there are unmatched keys but ErrorOnUnmatchedKeys is false | ||
if err := configor.New(&configor.Config{}).Load(&result, filename); err != nil { | ||
t.Errorf("Should NOT get error when loading configuration with extra keys. Error: %v", err) | ||
} | ||
|
||
// Return an error when there are unmatched keys and ErrorOnUnmatchedKeys is true | ||
if err := configor.New(&configor.Config{ErrorOnUnmatchedKeys: true}).Load(&result, filename); err == nil || !strings.Contains(err.Error(), "json: unknown field") { | ||
|
||
t.Errorf("Should get unknown field error when loading configuration with extra keys. Instead got error: %v", err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// +build !go1.10 | ||
|
||
package configor | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// unmarshalJSON unmarshals the given data into the config interface. | ||
// The errorOnUnmatchedKeys boolean is ignored since the json library only has | ||
// support for that feature from go 1.10 onwards. | ||
// If there are any keys in the data that do not match fields in the config | ||
// interface, they will be silently ignored. | ||
func unmarshalJSON(data []byte, config interface{}, errorOnUnmatchedKeys bool) error { | ||
|
||
return json.Unmarshal(data, &config) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters