accessor

package module
v0.0.0-...-02ea372 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2017 License: MIT Imports: 7 Imported by: 1

README

accessor

Build Status GoDoc Go Report Card

accessor provides accessor for map/slice objects like json, yaml, toml etc.

Install

go get github.com/morikuni/accessor

Example

package main

import (
	"encoding/json"
	"log"
	"os"

	"github.com/morikuni/accessor"
)

func main() {
	text := `
	{
		"name": "morikuni",
		"friends": [
			{ "name": "hello" },
			{ "name": "world" }
		]
	}`

	var obj interface{}
	err := json.Unmarshal([]byte(text), &obj)
	if err != nil {
		log.Fatal(err)
	}

	acc, err := accessor.NewAccessor(obj)
	if err != nil {
		log.Fatal(err)
	}

	path, err := accessor.ParsePath("/friends")
	if err != nil {
		log.Fatal(err)
	}

	friends, err := acc.Get(path)
	if err != nil {
		log.Fatal(err)
	}

	err = friends.Foreach(func(path accessor.Path, value interface{}) error {
		return friends.Set(path, value.(string)+"_accessor")
	})
	if err != nil {
		log.Fatal(err)
	}

	err = json.NewEncoder(os.Stdout).Encode(acc.Unwrap())
	if err != nil {
		log.Fatal(err)
	}
}

will output

$ go run main.go | jq .
{
  "friends": [
    {
      "name": "hello_accessor"
    },
    {
      "name": "world_accessor"
    }
  ],
  "name": "morikuni"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(i interface{}, path string) (interface{}, error)

Get finds a value from the object by the path.

func NewInvalidKeyError

func NewInvalidKeyError(v interface{}) error

NewInvalidKeyError createa InvalidKeyError.

func NewInvalidPathError

func NewInvalidPathError(message string) error

NewInvalidPathError creates a InvalidKeyError.

func Update

func Update(i interface{}, path string, value interface{}) (interface{}, error)

Update updates a value in the object by the path and returns updated object.

Types

type Accessor

type Accessor interface {
	// Get finds a object at specific path.
	// NoSuchPathError is returned when no object was found in the path.
	Get(path Path) (Accessor, error)

	// Set set a object into specific path.
	// NoSuchPathError is returned when the path is invalid.
	Set(path Path, value interface{}) error

	// Unwrap unwraps the object and returns actual value.
	Unwrap() interface{}

	// Foreach enumerates all value in the object.
	Foreach(f func(path Path, value interface{}) error) error
}

Accessor provides getter/setter to the object.

func NewAccessor

func NewAccessor(acc interface{}) (Accessor, error)

NewAccessor creates a new Accessor from a object. The object is a map[string]interface{} or []interface{}.

type DummyAccessor

type DummyAccessor struct {
	ID int
}

DummyAccessor is the Accessor for testing.

func (DummyAccessor) Foreach

func (a DummyAccessor) Foreach(f func(path Path, value interface{}) error) error

Foreach implements Accessor.

func (DummyAccessor) Get

func (a DummyAccessor) Get(path Path) (Accessor, error)

Get implements Accessor.

func (DummyAccessor) Set

func (a DummyAccessor) Set(path Path, _ interface{}) error

Set implements Accessor.

func (DummyAccessor) Unwrap

func (a DummyAccessor) Unwrap() interface{}

Unwrap implements Accessor.

type InvalidKeyError

type InvalidKeyError struct {
	Value interface{}
}

InvalidKeyError is returned when a key type of the map was not a string.

func (*InvalidKeyError) Error

func (e *InvalidKeyError) Error() string

type InvalidPathError

type InvalidPathError struct {
	Message string
}

InvalidPathError is returned when failing to create a Path.

func (*InvalidPathError) Error

func (e *InvalidPathError) Error() string

type MapAccessor

type MapAccessor map[string]Accessor

MapAccessor is the Accessor for a map.

func (MapAccessor) Foreach

func (a MapAccessor) Foreach(f func(path Path, value interface{}) error) error

Foreach implements Accessor.

func (MapAccessor) Get

func (a MapAccessor) Get(path Path) (Accessor, error)

Get implements Accessor.

func (MapAccessor) Set

func (a MapAccessor) Set(path Path, value interface{}) error

Set implements Accessor.

func (MapAccessor) Unwrap

func (a MapAccessor) Unwrap() interface{}

Unwrap implements Accessor.

type NoSuchPathError

type NoSuchPathError struct {
	Message string
	Key     string
	Path    Path
}

NoSuchPathError is returned when no key was found in the object.

func NewNoSuchPathError

func NewNoSuchPathError(message string, key string, keys ...string) *NoSuchPathError

NewNoSuchPathError creates a NoSuchPathError.

func (*NoSuchPathError) Error

func (e *NoSuchPathError) Error() string

func (*NoSuchPathError) PushKey

func (e *NoSuchPathError) PushKey(key string)

PushKey push key to the head of the stack trace.

type Path

type Path interface {
	fmt.Stringer

	// Key returns a head key of the sequence.
	Key() string

	// SubPath returns a path excluding the beginning.
	SubPath() (Path, bool)

	// PushKey add a key to the head of the sequence.
	PushKey(key string) Path
}

Path is a sequence of object keys.

func NewPath

func NewPath(keys []string) (Path, error)

NewPath creates a Path from keys.

func ParsePath

func ParsePath(path string) (Path, error)

ParsePath creates a Path from a slash(/)-separeted-keys.

type SliceAccessor

type SliceAccessor []Accessor

SliceAccessor is the Accessor for a slice.

func (SliceAccessor) Foreach

func (a SliceAccessor) Foreach(f func(path Path, value interface{}) error) error

Foreach implements Accessor.

func (SliceAccessor) Get

func (a SliceAccessor) Get(path Path) (Accessor, error)

Get implements Accessor.

func (SliceAccessor) Set

func (a SliceAccessor) Set(path Path, value interface{}) error

Set implements Accessor.

func (SliceAccessor) Unwrap

func (a SliceAccessor) Unwrap() interface{}

Unwrap implements Accessor.

type ValueAccessor

type ValueAccessor struct {
	Value interface{}
}

ValueAccessor is the Accessor for non-object types.

func (*ValueAccessor) Foreach

func (a *ValueAccessor) Foreach(f func(path Path, value interface{}) error) error

Foreach implements Accessor.

func (*ValueAccessor) Get

func (a *ValueAccessor) Get(path Path) (Accessor, error)

Get implements Accessor.

func (*ValueAccessor) MarshalJSON

func (a *ValueAccessor) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler.

func (*ValueAccessor) MarshalText

func (a *ValueAccessor) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (ValueAccessor) MarshalYAML

func (a ValueAccessor) MarshalYAML() (interface{}, error)

MarshalYAML implements github.com/go-yaml/yaml.Marshaler.

func (*ValueAccessor) Set

func (a *ValueAccessor) Set(path Path, value interface{}) error

Set implements Accessor.

func (*ValueAccessor) Unwrap

func (a *ValueAccessor) Unwrap() interface{}

Unwrap implements Accessor.

Jump to

Keyboard shortcuts

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