-
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
base: main
Are you sure you want to change the base?
Conversation
|
also, why you need raw json parser? i understand if it is remote source or smth like this that provides json configuration, but i do not see cases of using file in json over yaml |
Well, in my workplace we use JSON for configs as a standard, so I am kind of used to it. I do not have anything against YAML, however, I find JSON configs to handle better really complex application configs, I find them easier to maintain. |
This reverts commit 2043b7f.
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment Thanks for integrating Codecov - We've got you covered ☂️ |
|
I've added the changes you requested on treating as errors missing file, empty file and malformed file. I've not changed from decode to unmarshall as I still believe that maintaining the expressiveness of the number values in json is important. However, as I said in another comment, if you tell me so, I will change it to use Unmarshall. |
|
i thought how json support should be implemented, here what i think example code to fetch from url with file support package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
func fetchJSON(u *url.URL) ([]byte, error) {
var reader io.ReadCloser
var err error
switch u.Scheme {
case "http", "https":
resp, err := http.Get(u.String())
if err != nil {
return nil, fmt.Errorf("http get failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("http status: %s", resp.Status)
}
reader = resp.Body
case "file":
// Normalize the file path
path := u.Path
if u.Host != "" {
// Handle Windows UNC-style paths like file://host/path
path = "//" + u.Host + u.Path
}
reader, err = os.Open(path)
if err != nil {
return nil, fmt.Errorf("file open failed: %w", err)
}
default:
return nil, fmt.Errorf("unsupported URL scheme: %s", u.Scheme)
}
defer reader.Close()
data, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("read failed: %w", err)
}
return data, nil
}
url:=zfg.URL("name", "file:///a/b/c.json", "path to json config")
but there another option:
|
|
I've added the URL support as you described. I find it a great idea! |
|
add test to z_test.go for url, use struct like type urlVale url.Url |
|
I've modified z_url to use |
Adds a new parser to support loading configuration values from JSON files.
Includes:
jsonpackage with the parser implementation.nullvalues in the coreToStringconversion function.