Documentation
¶
Index ¶
Constants ¶
const ( // EnvVarConfigJSON is the environment variable that can be used to provide the JSON configuration for the Read // function. EnvVarConfigJSON = "CONFIG_JSON" // EnvVarConfigPath is the environment variable that can be used to provide the path to the JSON configuration file // for the Read function. EnvVarConfigPath = "CONFIG_PATH" )
Variables ¶
var ErrDefaultsAndValidate = errors.New("jsontype: failed to apply defaults and validate configuration")
ErrDefaultsAndValidate is the error returned by the DefaultsAndValidate function when it fails to apply defaults and validate the configuration.
Functions ¶
func Read ¶ added in v0.1.0
Read is a convenience function to read JSON configuration. It will first check the environment variable in the EnvVarConfigJSON for raw JSON, then it will check the environment variable in the EnvVarConfigPath for the path to a JSON file. If neither are set, it will attempt to read "config.json" in the current working directory. If that file does not exist, it will return an os.ErrNotExist error.
Types ¶
type Defaulter ¶ added in v0.6.0
type Defaulter[T any] interface { // DefaultsAndValidate applies default values and validates the data structure. If this function has an error, it // returns an error that can be checked with errors.Is to match ErrDefaultsAndValidate. // // For example, if a zero value is left for a *jsontype.JSONType[time.Duration], the default value can be set here. DefaultsAndValidate() (T, error) }
Defaulter is any data structure that can unmarshalled from JSON that has defaults and can be validated.
type JSONType ¶
type JSONType[T J] struct { // contains filtered or unexported fields }
JSONType holds a generic J value. It can be used to marshal and unmarshal its value to and from JSON.
func NewWithOptions ¶
NewWithOptions creates a new JSONType with options.
func (*JSONType[T]) MarshalJSON ¶
MarshalJSON helps implement the json.Marshaler interface.
func (*JSONType[T]) UnmarshalJSON ¶
UnmarshalJSON helps implement the json.Unmarshaler interface.