Skip to content

Conversation

@FumingPower3925
Copy link

Adds a new parser to support loading configuration values from JSON files.

Includes:

  • json package with the parser implementation.
  • Comprehensive unit tests covering various JSON structures and edge cases.
  • Fix for handling null values in the core ToString conversion function.

@chaindead chaindead added enhancement New feature or request good first issue Good for newcomers labels May 5, 2025
@chaindead
Copy link
Owner

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

@FumingPower3925
Copy link
Author

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.

@codecov-commenter
Copy link

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 ☂️

@FumingPower3925
Copy link
Author

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.

@chaindead
Copy link
Owner

chaindead commented May 7, 2025

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
}
  • add URL configuration option support like

url:=zfg.URL("name", "file:///a/b/c.json", "path to json config")
and json provider api like this
json.New(url)

  • add json provider description to readme section

but there another option:

  • add community providers section to readme
  • add your provider with link to your repo

@FumingPower3925
Copy link
Author

I've added the URL support as you described. I find it a great idea!

@chaindead
Copy link
Owner

add test to z_test.go for url, use struct like

type urlVale url.Url

@FumingPower3925
Copy link
Author

I've modified z_url to use type urlVale url.Url.
Added the tests in z_test.go for url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants