jam

package module
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2019 License: MIT Imports: 17 Imported by: 0

README

jam

Build Status Coverage Status

if structured data is scones and you are clotted cream, this is jam

about

Jam is a structured data manipulation tool.

  • Decode from yaml, json or toml.
  • Merge and diff multiple sources.
  • Apply filters and jmespath queries.
  • Execute go text templates.
  • Encode yaml, json, toml, go or struct.

Interacting with structured data should be more pleasant for shell and go programmers.

get

go get -tags pretty github.com/tr-d/jam/cmd/jam

why pretty

Because 🌈! But the code that makes the pretty colors weighs approximately ten billion tons. If you don't need them the binary is a fair bit smaller.

use

# usage
jam -h

# detailed help
jam -H

# examples
jam -X
json to yaml
jam '{"blep":7,"mlem":9}'

# implied
jam -m '{"blep":7,"mlem":9}' -e yaml -o -

# output
blep: 7
mlem: 9
toml to yaml
jam 'cute = "blep"'

# implied
jam -m 'cute = "blep"' -e yaml -o -

# output
cute: blep
merge
jam -m '{"blep":2,"mlem":6}' -m '{"blep":4}' -e json

# implied
jam -m '{"blep":2,"mlem":6}' -m '{"blep":4}' -e json -o -

# output
{"blep":4,"mlem":6}
diff
jam -m '{"blep":2,"mlem":6}' -d '{"blep":4,"mlem":6}' -e json

# implied
jam -m '{"blep":2,"mlem":6}' -d '{"blep":4,"mlem":6}' -e json -o -

# output
{"blep":4}
filter
jam -m '{"cute":{"blep":3,"mlem":5}}' -f cute.blep

# implied
jam -m '{"cute":{"blep":3,"mlem":5}}' -f cute.blep -e yaml -o -

# output
cute:
  blep: 3
template
jam -m '["blep","mlem"]' -x '{{range .}}kitty gon {{println .}}{{end}}'

# implied
jam -m '["blep","mlem"]' -x '{{range .}}kitty gon {{println .}}{{end}}' -o -

# output
kitty gon blep
kitty gon mlem
struct
curl https://api.github.com/user | jam -e struct

# implied
jam -m - -e struct -o -

# output
type T struct {
        DocumentationUrl string `json:"documentation_url"`
        Message          string `json:"message"`
}
script use

List releases for tr-d repositories.

curl -s https://api.github.com/orgs/tr-d/repos \
| jam -x '{{range .}}{{println .name}}{{end}}' \
| while read -r repo; do
        echo "# releases for $repo"
        curl -s "https://api.github.com/repos/tr-d/$repo/releases" \
        | jam -x '{{range .}}{{println .name}}{{else}}none (μ_μ){{println}}{{end}}'
done

package

import "github.com/tr-d/jam"

Decoder decodes from yaml, json, or toml. The format is detected automatically.

err := jam.NewDecoder(reader).Decode(&v)

Go struct tags labelled jam are evaluated with jmespath.

v := struct {
	X     string   `jam:"foo.x"`
	Y     string   `jam:"foo.y"`
	Names []string `jam:"p[].name"`
}{}

err := jam.NewDecoder(reader).Decode(&v)

Jam struct tags work on the decode side only. They are built to play nice with json struct tags. You can use a combination of either or both. In the case of both, the jam struct tag is used by the decoder.

Note: actually, under the hood, the value being decoded is converted to be json decodable, so both json and jam tags are in play.

Encoder encodes to yaml, json, toml, go syntax or struct.

e := jam.NewEncoder(writer)

// the default is yaml
err := e.Encode(v)

err := e.AsGo().Encode(v)
err := e.AsJson().Encode(v)
err := e.AsToml().Encode(v)
err := e.AsStruct().Encode(v)
err := e.AsYaml().Encode(v)

Core functions

func Diff(a, b interface{}) interface{}
func Merge(a, b interface{}) interface{}

func Filter(v interface{}, path string) interface{}
func FilterI(v interface{}, path string) interface{}
func FilterR(v interface{}, path string) interface{}
func FilterIR(v interface{}, path string) interface{}
func Query(v interface{}, s string) interface{}

Documentation

Overview

Package jam. Decode yaml, json or toml. Encode yaml, json, toml, go syntax and go struct defs. Merge, Diff, Filter, Query things. Struct tags.

If structured data is scones and you are clotted cream, this is jam.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff(a, b interface{}) interface{}

Diff ouputs c that satisfies Merge(a, c) == b. Transpose of Merge.

func Filter

func Filter(v interface{}, path string) interface{}

Filter filters a structure according to the path. Elements of v that do not match are removed. The path must match from the root of v.

func FilterI

func FilterI(v interface{}, path string) interface{}

FilterI filters a structure according to the path. Inverted. Elements of v that match are removed. The path must match from the root of v.

func FilterIR

func FilterIR(v interface{}, path string) interface{}

FilterIR filters a structure according to the path. Inverted and Recursive. Elements of v that match are removed. The path may match at any depth in v.

func FilterR

func FilterR(v interface{}, path string) interface{}

FilterR filters a structure according to the path. Recursive. Elements of v that do not match are removed. The path may match at any depth in v.

func Merge

func Merge(a, b interface{}) interface{}

Merge outputs the union of a and b with preference to b on matching keys.

func Query

func Query(v interface{}, s string) interface{}

Query applies a jmespath search to v.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder reads yaml, json, or toml from a reader, "jam" struct tags are evaluated as jmespath expressions.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder creates a Decoder for r.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Decode json, yaml, or toml from the reader and store the result in the value pointed to by v. Struct tags labelled "jam" are evaluated as jmespath expressions.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder writes yaml, json, toml, go syntax, or go struct definition to a writer. The behaviour depends on the underlying function, which may be set using the AsYaml, AsJson, AsToml, AsGo, and AsStruct methods. The default is yaml.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates an Encoder set to encode as yaml.

func (*Encoder) AsGo

func (e *Encoder) AsGo() *Encoder

AsGo creates a copy of this Encoder set to create go syntax.

func (*Encoder) AsJson

func (e *Encoder) AsJson() *Encoder

AsJson creates a copy of this Encoder set to encode as json.

func (*Encoder) AsStruct

func (e *Encoder) AsStruct() *Encoder

AsStruct creates a copy of this Encoder set to create go struct definitions.

func (*Encoder) AsToml

func (e *Encoder) AsToml() *Encoder

AsToml creates a copy of this Encoder set to encode as toml.

func (*Encoder) AsYaml

func (e *Encoder) AsYaml() *Encoder

AsYaml creates a copy of this Encoder set to encode as yaml.

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

Encode writes to the underlying writer. The behaviour depends on the underlying function, which may be set using the AsYaml, AsJson, AsToml, AsGo, AsStruct methods. The default is yaml.

type Jam

type Jam struct {
	// contains filtered or unexported fields
}

Jam accumulates operations on a data tree.

func NewJam

func NewJam(v interface{}) *Jam

NewJam creates a Jam.

func (*Jam) Diff

func (j *Jam) Diff(r interface{})

func (*Jam) Exec

func (j *Jam) Exec(dst io.Writer, src io.Reader) error

func (*Jam) Filter

func (j *Jam) Filter(q string)

Filter applies the Filter function to the Jam's value.

func (*Jam) FilterI

func (j *Jam) FilterI(q string)

FilterI applies the FilterI function to the Jam's value.

func (*Jam) FilterIR

func (j *Jam) FilterIR(q string)

FilterIR applies the FilterIR function to the Jam's value.

func (*Jam) FilterR

func (j *Jam) FilterR(q string)

FilterR applies the FilterR function to the Jam's value.

func (*Jam) Merge

func (j *Jam) Merge(r interface{})

func (*Jam) Query

func (j *Jam) Query(q string)

Query applies the Query function to the Jam's value.

func (*Jam) Value

func (j *Jam) Value() interface{}

Value returns the Jam's value.

Directories

Path Synopsis
cmd
jam

Jump to

Keyboard shortcuts

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