writer

package module
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: BSD-3-Clause Imports: 11 Imported by: 0

README

go-writer-config

Go package implementing the whosonfirst/go-writer/v3 interfaces to provide methods for creating a new whosonfirst/go-writer/v3.MultiWriter instance derived from a JSON-encoded config file.

Documentation (godoc)

Go Reference

Documentation

This package implements the whosonfirst/go-writer/v3 interfaces to provide methods for creating a new whosonfirst/go-writer/v3.MultiWriter instance derived from a JSON-encoded config file. These config files are organized (grouped) by named "environments" and "targets". A config file may have multiple environments and an environment may have multiple targets. Each target may contain one or more "configs". For example:

+ environment
  + target
    - config
    - config

This is what the type definition for configs looks like:

// type RuntimevarConfig defines a struct for configuration data used to build a gocloud.dev/runtimevar URI which are
// used to resolve final `whosonfirst/go-writer/v2` URIs.
type RuntimevarConfig struct {
	// The scheme of the gocloud.dev/runtimevar URI to build
	Runtimevar string `json:"runtimevar"`
	// The value of the gocloud.dev/runtimevar URI to build
	Value string `json:"value"`
	// An optional gocloud.dev/runtimevar URI used to replace the string "{credentials}" in `Value`.
	Credentials string `json:"credentials,omitempty"`
	// An optional boolean flag used to flag the config as currently disabled.
	Disabled bool `json:"disabled,omitempty"`
	// An optional string label used to match any "?exclude={LABEL}" parameters in a `whosonfirst/go-writer/v2` URI constuctor; this allows individual configs to be disabled at runtime
	Label string `json:"label,omitempty"`
}

An example config file is described below and included in fixtures/config.json.

Runtimevars

Under the hood this package uses the gocloud.dev/runtimevar package and the sfomuseum/runtimevar.StringVar method to deference Value property of any given config. Please consult the sfomuseum/runtimevar.StringVar documentation for details.

Example

Here is how you might create a writer from a config file located at /fixtures/config.json. This writer will load configs (and create writer.Writer instances) from the dev environment and test target set:

import (
	"context"

	_ "github.com/whosonfirst/go-writer-config/v3"
	
	"github.com/whosonfirst/go-writer/v3"	
)

func  main(){

	ctx := context.Background()
	
	wr_uri := "config://dev/test?config=file:///fixtures/config.json&exclude=stdout"
	wr, _ := writer.NewWriter(ctx, wr_uri)

	r := bytes.NewReader([]byte("testing"))
	wr.Write(ctx, "test", r)

Error handling removed for the sake of brevity.

"Config" writers are instantiated by passing a config:// URI to the writer.NewWriter method. Config writer URIs take the form of:

config://{ENVIRONMENT}/{TARGET}?{QUERY_PARAMETERS}

Where:

  • {ENVIRONMENT} is the name of the top-level environment to load configs for.
  • {TARGET} is the name of the second-level target to load configs for.
  • {QUERY_PARAMETERS} are one or more query parameters:
Name Description Type Required Notes
config A valid gocloud.dev/runtimevar URI used to load the config file to parse string yes As mentioned this package uses the sfomuseum/runtimevar.StringVar wrapper method for resolving runtimevar URIs so consult the documentation for details.
exclude Zero or more query parameters referencing the "label" properties of individual configs to exclude when parsing a config file string no
async A boolean flag indicating whether writers (defined by the config file) should be written to asynchronously boolean no

Config files

Config files are parsed using the tidwall/jsonc package in order to allow for descriptive comments.

Example
{
   "dev": {
        "indexing": [
            {
	    	/* Write data using the whosonfirst/go-whosonfirst-mysql package */
		/* https://github.com/whosonfirst/go-whosonfirst-mysql/blob/main/writer/writer.go */
		
                "label": "mysqln",
                "runtimevar": "constant",
                "value": "mysql://?dsn={credentials}@tcp(localhost:3306)/your_database",
                "credentials": "file:///etc/mysql/credentials"
            },
            {
	    	/* Write data using the whosonfirst/go-whosonfirst-opensearch package */		
		/* https://github.com/whosonfirst/go-whosonfirst-opensearch/blob/main/writer/writer_opensearch2.go */
	    
             	"label": "opensearch",
		"runtimevar": "constant",
		"value": "opensearch://localhost:9200/your_index"
            },
            {
		/* Write data using the whosonfirst/go-writer null writer (basically /dev/null) */
		/* https://github.com/whosonfirst/go-writer/blob/main/null.go */
	    
             	"label": "stdout",
		"runtimevar": "constant",
		"value": "null://",
		"disabled": true
            }
	],
        "test": [
            {
    		/* Write data using the whosonfirst/go-writer STDOUT writer */
		/* https://github.com/whosonfirst/go-writer/blob/main/stdout.go */
		
             	"runtimevar": "constant",
		"value": "stdout://"
            },
            {
		/* Write data using the whosonfirst/go-writer null writer (basically /dev/null) */
		/* https://github.com/whosonfirst/go-writer/blob/main/null.go */
	    
             	"runtimevar": "constant",
		"value": "null://"
            }
	]
    }
}

See also

Documentation

Overview

Package writer implements the `whosonfirst/go-writer/v2` interfaces to provide methods for creating a new `whosonfirst/go-writer/v2.MultiWriter` instance derived from a JSON-encoded config file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConfigWriter

func NewConfigWriter(ctx context.Context, uri string) (wof_writer.Writer, error)

NewConfigWriter return a new `go-writer/v2.Writer` instance derived from 'uri' which is expected to take the form of:

config://{ENVIRONMENT}/{TARGET}?config={VALID_GOCLOUD_DEV_RUNTIMEVAR_URI}

For example:

config://dev/test?config=file:///usr/local/config.json&async=true

func NewConfigWriterFromOptions

func NewConfigWriterFromOptions(ctx context.Context, opts *ConfigWriterOptions) (wof_writer.Writer, error)

NewConfigWriterFromOptions return a new `go-writer/v2.Writer` instance derived from 'opts'.

Types

type ConfigWriterOptions

type ConfigWriterOptions struct {
	// Config is a `WriterConfig` instance containing configuration data for instantiating one or more `go-writer/v2.Writer` instances.
	Config *WriterConfig
	// Exclude is an optional list of string labels to compare against individual `RuntimevarConfig.Label` values; if there is a match that `RuntimevarConfig` instance will be excluded
	Exclude []string
	// Target is the string label mapped to the list of `RuntimevarConfig` instances used to create a new `go-writer/v2.Writer` instance.
	Target string
	// Environment is the string label mapped to the `TargetConfig` instance used to create a new `go-writer/v2.Writer` instance.
	Environment string
	// Async is an optional boolean value to signal that a new asynchronous `go-writer/v2.MultiWriter` instance should be created.
	Async bool
}

ConfigWriterOptions is a struct containing configuration options for create a new `go-writer/v2.MultiWriter` instance derived from a JSON configuration file

type RuntimevarConfig

type RuntimevarConfig struct {
	// The scheme of the gocloud.dev/runtimevar URI to build
	Runtimevar string `json:"runtimevar"`
	// The value of the gocloud.dev/runtimevar URI to build
	Value string `json:"value"`
	// An optional gocloud.dev/runtimevar URI used to replace the string "{credentials}" in `Value`.
	Credentials string `json:"credentials,omitempty"`
	// An optional boolean flag used to flag the config as currently disabled.
	Disabled bool `json:"disabled,omitempty"`
	// An optional string label used to match any "?exclude={LABEL}" parameters in a `whosonfirst/go-writer/v2` URI constuctor; this allows individual configs to be disabled at runtime
	Label string `json:"label,omitempty"`
}

type RuntimevarConfig defines a struct for configuration data used to build a gocloud.dev/runtimevar URI which are used to resolve final `whosonfirst/go-writer/v2` URIs.

type TargetConfig

type TargetConfig map[string][]*RuntimevarConfig

type TargetConfig is a map where the keys are arbitrary labels (string) mapped to a list `RuntimevarConfig` instances.

func (*TargetConfig) RuntimevarConfigs

func (cfg *TargetConfig) RuntimevarConfigs(target string) ([]*RuntimevarConfig, bool)

RuntimevarConfigs returns the list of `RuntimevarConfig` instances associated with the key 'target'.

type WriterConfig

type WriterConfig map[string]*TargetConfig

type WriterConfig is a map where the keys are arbitrary labels (strings) mapped to `TargetConfig` instances.

func (*WriterConfig) Target

func (cfg *WriterConfig) Target(environment string) (*TargetConfig, bool)

Target returns the `TargetConfig` instance associated with the key 'target'.

Jump to

Keyboard shortcuts

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