nested

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2019 License: MIT Imports: 7 Imported by: 0

README

Build Status GoDoc Go Report Card GolangCI Codecov GitHub tag (latest SemVer)

nested

  • PubSub: Access nested values lock-free at runtime.
  • Follow: Access nested values via object path.

This package uses the empty interface{}. For type-safe implementations see:

  • text: Nested union-Type for Text(string), map[string]Text, []Text, map[string]map[string]Text, ...
  • union: Nested union-Type for Text, Number(float64), Boolean(bool), map[string]Text, map[string]Number, ...
  • protobuf-example: Nested union-Type for oneof Object, List, Text, Number, Boolean
  • genny: Generate this package for your own types.
Usage
Usage: Follow

playground

An object path refers to nested values ​​with keys separated by periods. Typically, integers refer to values ​​in a list and all other keys to values ​​in a map (Object).

o := nested.Object(map[string]nested.TypeI{
    "key": nested.List(
        nested.New("Hello"),
        nested.Object(map[string]nested.TypeI{
            "key": nested.New("World"),
        }),
    ),
})

v, ok := o.Follow("key.1.key")
fmt.Println(v, ok)
// World true
Usage: PubSub

playground

With PubSub you can publish values ​​along an object path and all subscribers who refer to the same or a child's path will be notified with the appropriate value.

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
o2, err := nested.PubSub(ctx, map[string]nested.Job{
    "key": {
        Work: func(subs map[string]nested.TypeI) nested.TypeI {
            return nested.List(nested.New("Hello"), nested.New(100), nested.New("World"))
        },
    },
    "": {
        SubPaths: []string{"key.0", "key.2", "key"},
        Work: func(subs map[string]nested.TypeI) nested.TypeI {
            return nested.Object(map[string]nested.TypeI{
                "key":   subs["key"],
                "greet": nested.New(fmt.Sprintf("%v %v", subs["key.0"], subs["key.2"])),
            })
        },
    },
})
fmt.Println(o2, err)
// map[greet:Hello World key:[Hello 100 World]] <nil>
Genny
go get github.com/cheekybits/genny
Genny: For own nested (union) types
wget -q -O - "https://github.com/wroge/nested/raw/master/nested.go" | genny gen "Type=MyType" >> nested_gen.go
  • MyType should implement this interface:
type MyTypeI interface {
	Follow(path string) (n MyTypeI, ok bool)
	Value() MyType
}
Genny: For build-in or own union types (see union)
wget -q -O - "https://github.com/wroge/nested/raw/master/nested.go" | genny gen "Type=float64" >> nested_gen.go
wget -q -O - "https://github.com/wroge/nested/raw/master/common.go" | genny gen "Type=float64" >> common_gen.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Common

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

Common contains a not nested Type.

func New

func New(v Type) Common

New initialize a Common not nested Type.

func (Common) Follow

func (c Common) Follow(path string) (v TypeI, ok bool)

Follow a Common type returns the contained type if path is a default string.

func (Common) String

func (c Common) String() string

String implements the Stringer interface.

func (Common) Value

func (c Common) Value() Type

Value is a helper method that allows you to use nested union values and Common types. It returns always the contained Type.

type CommonList

type CommonList []TypeI

CommonList is a nested Type list.

func List

func List(l ...TypeI) CommonList

List initialize a CommonList.

func (CommonList) Follow

func (l CommonList) Follow(path string) (v TypeI, ok bool)

Follow a CommonList type returns the nested Type object if the path is a default string. In all others cases it traverses the object path. It returns the default Type and false it the path is wrong. The first element of the path has to be an integer.

func (CommonList) Value

func (l CommonList) Value() (v Type)

Value is a helper method that allows you to use nested union values and Common types. It returns always the default Type.

type CommonObject

type CommonObject map[string]TypeI

CommonObject is a nested Type object.

func Object

func Object(o map[string]TypeI) CommonObject

Object initialize a CommonObject.

func (CommonObject) Follow

func (o CommonObject) Follow(path string) (v TypeI, ok bool)

Follow a CommonObject type returns the nested Type object if the path is a default string. In all others cases it traverses the object path. It returns the default Type and false it the path is wrong.

func (CommonObject) Value

func (o CommonObject) Value() (v Type)

Value is a helper method that allows you to use nested union values and Common types. It returns always the default Type.

type Job

type Job struct {
	SubPaths []string
	Work     Work
}

A Job defines the Work that has to be done when all SubPaths are solved.

type Type

type Type generic.Type

Type is a generic Type which is interface{} by default. It is possible to generate a type-safe package by using cheekybits/genny.

type TypeI

type TypeI interface {
	Follow(path string) (n TypeI, ok bool)
	Value() Type
}

TypeI is the interface that all nested values have to implement.

func PubSub

func PubSub(ctx context.Context, jobs map[string]Job) (v TypeI, err error)

PubSub returns the value of the last running Work. Each subscriber path of a Job has to be equal or a child of a publisher path.

type Work

type Work func(subs map[string]TypeI) TypeI

Work is a function that is triggered when all SubPaths of a Job are solved.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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