-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add JSON configuration parser #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FumingPower3925
wants to merge
12
commits into
chaindead:main
Choose a base branch
from
FumingPower3925:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2043b7f
fix: prevent reflect on null
FumingPower3925 536d2eb
feat: added json parser
FumingPower3925 3eca132
test: added tests for json parser
FumingPower3925 acb9cee
Revert "fix: prevent reflect on null"
FumingPower3925 da8d706
fix: applied comments to correct the code
FumingPower3925 571de87
fix: simplified code and standarized naming
FumingPower3925 e269b8d
fix: treat missing file, empty file, malformed file, as errors
FumingPower3925 3061df1
feat: add url type
FumingPower3925 dbe3529
feat: modified json parsers to work with proposed url implementation
FumingPower3925 36e33a0
docs: added json source in the README.md
FumingPower3925 b89a7a1
test: add z_url tests
FumingPower3925 2de7eda
fix: fixed implementation to align with required type
FumingPower3925 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,106 @@ | ||
| package json | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| type Parser struct { | ||
| path *string | ||
|
|
||
| conv func(any) string | ||
| awaited map[string]bool | ||
| } | ||
|
|
||
| func New(path *string) *Parser { | ||
| return &Parser{path: path} | ||
| } | ||
|
|
||
| func (p *Parser) Type() string { | ||
| if p.path == nil || *p.path == "" { | ||
| return "json" | ||
| } | ||
| return fmt.Sprintf("json[%s]", *p.path) | ||
| } | ||
|
|
||
| func (p *Parser) Parse(keys map[string]bool, conv func(any) string) (found, unknown map[string]string, err error) { | ||
| found = make(map[string]string) | ||
| unknown = make(map[string]string) | ||
|
|
||
| if p.path == nil || *p.path == "" { | ||
| return found, unknown, nil | ||
| } | ||
|
|
||
| data, err := os.ReadFile(*p.path) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return found, unknown, nil | ||
| } | ||
| return nil, nil, fmt.Errorf("read json file %q: %w", *p.path, err) | ||
| } | ||
|
|
||
| if len(data) == 0 { | ||
| return found, unknown, nil | ||
| } | ||
|
|
||
| p.conv = conv | ||
| p.awaited = keys | ||
|
|
||
| return p.parse(data) | ||
| } | ||
|
|
||
| func (p *Parser) parse(data []byte) (found, unknown map[string]string, err error) { | ||
| var settings any | ||
|
|
||
| decoder := json.NewDecoder(bytes.NewReader(data)) | ||
| decoder.UseNumber() | ||
| if err = decoder.Decode(&settings); err != nil { | ||
FumingPower3925 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err.Error() == "EOF" && len(data) == 0 { | ||
FumingPower3925 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return make(map[string]string), make(map[string]string), nil | ||
| } | ||
| return nil, nil, fmt.Errorf("unmarshal json: %w", err) | ||
| } | ||
|
|
||
| settingsMap, ok := settings.(map[string]any) | ||
| if !ok { | ||
| return make(map[string]string), make(map[string]string), nil | ||
| } | ||
|
|
||
| found, unknown = p.flatten(settingsMap) | ||
|
|
||
| return found, unknown, nil | ||
| } | ||
|
|
||
| func (p *Parser) flatten(settings map[string]any) (found, unknown map[string]string) { | ||
| found, unknown = make(map[string]string), make(map[string]string) | ||
| p.flattenDFS(settings, "", found, unknown) | ||
| return found, unknown | ||
| } | ||
|
|
||
| func (p *Parser) flattenDFS(m map[string]any, prefix string, found, unknown map[string]string) { | ||
chaindead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for k, v := range m { | ||
|
|
||
| if v == nil { | ||
| continue | ||
| } | ||
|
|
||
| newKey := k | ||
| if prefix != "" { | ||
| newKey = prefix + "." + k | ||
| } | ||
|
|
||
| if p.awaited[newKey] { | ||
| found[newKey] = p.conv(v) | ||
| continue | ||
| } | ||
|
|
||
| if subMap, ok := v.(map[string]any); ok { | ||
| p.flattenDFS(subMap, newKey, found, unknown) | ||
| continue | ||
| } | ||
|
|
||
| unknown[newKey] = p.conv(v) | ||
| } | ||
| } | ||
This file contains hidden or 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,163 @@ | ||
| package json_test | ||
FumingPower3925 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| zfg "github.com/chaindead/zerocfg" | ||
| "github.com/chaindead/zerocfg/json" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestParse(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| awaited map[string]bool | ||
| found map[string]string | ||
| unknown map[string]string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "simple key-value", | ||
| input: `{"str": "name", "int": 1}`, | ||
| awaited: map[string]bool{ | ||
| "str": true, | ||
| }, | ||
| found: map[string]string{ | ||
| "str": `name`, | ||
| }, | ||
| unknown: map[string]string{ | ||
| "int": `1`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "nested", | ||
| input: `{ | ||
| "host": "localhost", | ||
| "database": { | ||
| "port": 5432, | ||
| "credentials": { | ||
| "username": "admin" | ||
| } | ||
| } | ||
| }`, | ||
| awaited: map[string]bool{ | ||
| "database.credentials.username": true, | ||
| }, | ||
| found: map[string]string{ | ||
| "database.credentials.username": `admin`, | ||
| }, | ||
| unknown: map[string]string{ | ||
| "host": `localhost`, | ||
| "database.port": `5432`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "array", | ||
| input: `{"tags": ["a", "b"]}`, | ||
| awaited: map[string]bool{ | ||
| "tags": true, | ||
| }, | ||
| found: map[string]string{ | ||
| "tags": `["a","b"]`, | ||
| }, | ||
| unknown: map[string]string{}, | ||
| }, | ||
| { | ||
| name: "map", | ||
| input: `{"options": {"k1": 1, "k2": "v2"}}`, | ||
| awaited: map[string]bool{ | ||
| "options": true, | ||
| }, | ||
| found: map[string]string{ | ||
| "options": `{"k1":1,"k2":"v2"}`, | ||
| }, | ||
| unknown: map[string]string{}, | ||
| }, | ||
| { | ||
| name: "null value is skipped", | ||
| input: `{"key1": "value1", "key2": null, "key3": 123}`, | ||
| awaited: map[string]bool{ | ||
| "key1": true, | ||
| "key2": true, | ||
| "key3": true, | ||
| }, | ||
| found: map[string]string{ | ||
| "key1": `value1`, | ||
| "key3": `123`, | ||
| }, | ||
| unknown: map[string]string{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if tt.found == nil { | ||
| tt.found = map[string]string{} | ||
| } | ||
| if tt.unknown == nil { | ||
| tt.unknown = map[string]string{} | ||
| } | ||
|
|
||
| path := tempFile(t, tt.input) | ||
| p := json.New(&path) | ||
|
|
||
| found, unknown, err := p.Parse(tt.awaited, zfg.ToString) | ||
|
|
||
| if tt.wantErr { | ||
| assert.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.found, found, "Found map mismatch") | ||
| assert.Equal(t, tt.unknown, unknown, "Unknown map mismatch") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestParse_Error(t *testing.T) { | ||
| path := tempFile(t, `{"invalid": "json",}`) | ||
| p := json.New(&path) | ||
|
|
||
| _, _, err := p.Parse(map[string]bool{}, zfg.ToString) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "unmarshal json") | ||
| } | ||
|
|
||
| func TestParse_EmptyInputNoError(t *testing.T) { | ||
| path := tempFile(t, ``) | ||
| p := json.New(&path) | ||
|
|
||
| found, unknown, err := p.Parse(map[string]bool{}, zfg.ToString) | ||
| assert.NoError(t, err) | ||
| assert.Empty(t, found) | ||
| assert.Empty(t, unknown) | ||
| } | ||
|
|
||
| func TestParse_FileNotExist(t *testing.T) { | ||
| nonExistentPath := "no_such_file.json" | ||
| p := json.New(&nonExistentPath) | ||
|
|
||
| found, unknown, err := p.Parse(map[string]bool{"some.key": true}, zfg.ToString) | ||
| require.NoError(t, err) | ||
FumingPower3925 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.Empty(t, found) | ||
| assert.Empty(t, unknown) | ||
| } | ||
|
|
||
| func tempFile(t *testing.T, data string) string { | ||
| f, err := os.CreateTemp("", "tmpjson-") | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { | ||
| f.Close() | ||
| os.Remove(f.Name()) | ||
| }) | ||
|
|
||
| _, err = f.WriteString(data) | ||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, f.Close()) | ||
|
|
||
| return f.Name() | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.