jsonconfig

package
v0.0.1-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Jsonconfig implements configuration, reading json into an ice Module.

To use:

1) Create the Schema. List your configurable Implementations. Each Implementations can be backed by several named Implementations.

  1. Options.Parse parses bytes and creates a Configuration. a) for each Implementations, pick which Implementation. b) json.Unmarshal the json into that Implementation c) Implementation can now be used as a Module or json.Marshal'ed to print its configuration
  2. Configuration is an ice Module that installs each Implementation

Example: 1) Create the Schema

schema := jsonconfig.Schema(map[string]jsonconfig.Implementations{
 "Storage": {
  "foo": StorageFooConfig{},
  "baz": StorageBazConfig{},
  "": StorageFooConfig{Type: "foo", Param: 17},
 },
 "Cluster": {
  "bar": ClusterBarConfig{},
  "quuz": ClusterQuuxConfig{},
 }
}

2) Parse

mod, _ := schema.Parse([]byte(`{
 "Storage": {
  "Type": "FooStorage",
  "FooArg": "Val"
 },
 "Cluster": {
  "Type": "BarCluster",
  "BarArg": 17
 }
}`)

3) Install the Confniguration

bag.InstallModule(mod)

Notes

Right now, that's an oddity that config's should be unset except the default, which should be. It would be better if we solved this.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfigText

func GetConfigText(configFlag string, asset func(string) ([]byte, error)) ([]byte, error)

GetConfigText finds the right text for a configFlag. If configFlag looks like a filename (of the form foo.bar where foo and bar are just alphanumeric), read it as an asset. This filename can optionally have appended json (foo.bar.<json>) to override the file. Otherwise, assume it's the literal json text.

Types

type Configuration

type Configuration map[string]ice.Module

func (Configuration) Install

func (c Configuration) Install(bag *ice.MagicBag)

Configuration is itself a Module, that installs each Impl as a Module (in Design Patterns terminology, it's a Composite)

type Implementation

type Implementation interface {
	// The Implementation needs to do 3 things:
	// 1) parse the JSON config
	// 2) add the relevant providers to the ice MagicBag
	// 3) print its configuration
	// 1 & 3 are handled implicitly (ugh) by json.(Un)marshal
	// 2 is handled by being an ice Module
	ice.Module
}

type Implementations

type Implementations map[string]Implementation

Implementations maps the the names of implementations to the Implementation As a special case, "" maps to a default implementation that will not be unmarshal'ed, and so the Implementation will be used as-is.

type Schema

type Schema map[string]Implementations

Schema holds the different Implementations's the client wants to configure

func EmptySchema

func EmptySchema() Schema

EmptySchema returns an empty Schema, needed if you don't allow configuration

func (Schema) Parse

func (schema Schema) Parse(text []byte) (Configuration, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL