dcode: github.com/go-functional/dcode Index | Files

package dcode

import "github.com/go-functional/dcode"

Index

Package Files

bool.go builder.go decode.go decoder.go exists.go field.go float64.go int.go string.go types.go

func Decode Uses

func Decode(b []byte, d Decoder, i interface{}) error

Decode decodes b into i using d

func Exists Uses

func Exists(decoder Decoder, bytes []byte) bool

Exists checks whether the value that decoder points to (in the JSON bytes) exists and is the expected type

func Map Uses

func Map(
    b []byte,
    pairs ...MapPair,
) error

Map decodes separate JSON fields into separate values, using separate decoders, all from the same JSON.

For example, if you have the following JSON:

json := `{"field1": 123, "field2": "456"}`

And you have the following decoders:

dcode1 := Field("field1", Int())
dcode2 := Field("field2", String())

You can do the following:

stucco := struct{
	field1 int
	field2 string
}

// check the error here!
Map(
	[]byte(json),
	Pair(dcode1, &stucco.field1),
	Pair(dcode2, &stucco.field2),
)

Note: you can decode JSON into structs using this function, but if you're trying to decode tons of fields from JSON to struct fields, you might be better off using encoding/json!

func OneOf Uses

func OneOf(b []byte, dList []Decoder, i interface{}) error

OneOf tries to decode b into i using every decoder in dList.

It returns nil after the first decoder succeeds. If none succeeded, returns an error

type Builder Uses

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

Builder is a convenience struct that lets you write decoders as a chain, rather than in the "pure functional" style. Instead of this:

dcoder := Field("first", Field("second", Field("third", Int())))

You can write this:

dcoder := First("first").Then("second").Then("third").Into(Int())

func First Uses

func First(field string) Builder

First returns a Builder that starts at field in the JSON object

func (Builder) Into Uses

func (b Builder) Into(d Decoder) Decoder

Into returns a Decoder that decodes the value at the current travere level using d. See the documentation in Builder for a complete explanation

func (Builder) Then Uses

func (b Builder) Then(field string) Builder

Then traverses one level down into the JSON object. See the documentation in Builder for a complete explanation

type Decoder Uses

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

Decoder is the core type in dcode. This is basically a pure function that can decode ANY JSON value into a particular type.

You can't implement one of these directly. Instead use one of the built-in ones like Int(), String(), etc... and build them up with Field(...) or First(...).Then(...).Into(...).

Check out the documentation for Field() or Builder for more information

func Bool Uses

func Bool() Decoder

Bool decodes any JSON field into a bool

func Field Uses

func Field(name string, decoder Decoder) Decoder

Field builds up a traversal. Here's an example that starts with "field1", then goes down to "field2", and then into "field3", and decodes that value into an int:

dcoder := Field("field1", Field("field2", Field("field3", Int())))

If you have the following JSON:

{
	"field1": {
		"field2": {
			"field3": 123
		}
	}
}

Then the following code would decode the integer 123 into the variable i

var i int
Decode(dcoder, jsonBytes, &i) // make sure to check the error returned!

func Float64 Uses

func Float64() Decoder

Float64 decodes any JSON field into a float64

func Int Uses

func Int() Decoder

Int decodes any JSON field into an integer

func String Uses

func String() Decoder

String decodes any JSON field into a string

type ErrWrongType Uses

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

func (ErrWrongType) Error Uses

func (e ErrWrongType) Error() string

type MapPair Uses

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

MapPair is a value to pass into the Map function. Create one of these with the Pair function, and see the documentation under the Map function for where this is used

func Pair Uses

func Pair(d Decoder, iface interface{}) MapPair

Pair returns a new MapPair

Package dcode imports 3 packages (graph). Updated 2019-03-13. Refresh now. Tools for package owners.