gotext: github.com/tideland/gotext/gjp Index | Files

package gjp

import "github.com/tideland/gotext/gjp"

Package gjp of the Tideland GoText package provides the generic parsing and processing of JSON documents by paths. The returned values are typed, also a default has to be provided. The path separator for accessing can be defined when parsing a document.

doc, err := gjp.Parse(myDoc, "/")
if err != nil {
    ...
}
name := doc.ValueAt("name").AsString("")
street := doc.ValueAt("address/street").AsString("unknown")

The value passed to AsString() and the others are default values if there's none at the path. Another way is to create an empty document with

doc := gjp.NewDocument("::")

Here and at parsed documents values can be set with

err := doc.SetValueAt("a/b/3/c", 4711)

Additionally values of the document can be processed using

err := doc.Process(func(path string, value gjp.Value) error {
    ...
})

Sometimes one is more interested in the differences between two documents. Here

diff, err := gjp.Compare(firstDoc, secondDoc, "/")

privides a gjp.Diff instance which helps to compare individual paths of the two document.

Index

Package Files

diff.go doc.go errors.go gjp.go processing.go value.go

Constants

const (
    ErrUnmarshalling      = "E001"
    ErrInvalidDocument    = "E002"
    ErrCorruptingDocument = "E003"
    ErrInvalidPart        = "E004"
    ErrInvalidPath        = "E005"
    ErrPathTooLong        = "E006"
    ErrProcessing         = "E007"
)

Error codes of the etc package.

type Diff Uses

type Diff interface {
    // FirstDocument returns the first document passed to Diff().
    FirstDocument() Document

    // SecondDocument returns the second document passed to Diff().
    SecondDocument() Document

    // Differences returns a list of paths where the documents
    // have different content.
    Differences() []string

    // DifferenceAt returns the differences at the given path by
    // returning the first and the second value.
    DifferenceAt(path string) (Value, Value)
}

Diff manages the two parsed documents and their differences.

func Compare Uses

func Compare(first, second []byte, separator string) (Diff, error)

Compare parses and compares the documents and returns their differences.

func CompareDocuments Uses

func CompareDocuments(first, second Document, separator string) (Diff, error)

CompareDocuments compares the documents and returns their differences.

type Document Uses

type Document interface {
    json.Marshaler

    // Length returns the number of elements for the given path.
    Length(path string) int

    // SetValueAt sets the value at the given path.
    SetValueAt(path string, value interface{}) error

    // ValueAt returns the addressed value.
    ValueAt(path string) Value

    // Clear removes the so far build document data.
    Clear()

    // Query allows to find pathes matching a given pattern.
    Query(pattern string) (PathValues, error)

    // Process iterates over a document and processes its values.
    // There's no order, so nesting into an embedded document or
    // list may come earlier than higher level paths.
    Process(processor ValueProcessor) error
}

Document represents one JSON document.

func NewDocument Uses

func NewDocument(separator string) Document

NewDocument creates a new empty document.

func Parse Uses

func Parse(data []byte, separator string) (Document, error)

Parse reads a raw document and returns it as accessible document.

type PathValue Uses

type PathValue struct {
    Path  string
    Value Value
}

PathValue is the combination of path and value.

type PathValues Uses

type PathValues []PathValue

PathValues contains a number of path/value combinations.

type Value Uses

type Value interface {
    fmt.Stringer

    // IsUndefined returns true if this value is undefined.
    IsUndefined() bool

    // AsString returns the value as string.
    AsString(dv string) string

    // AsInt returns the value as int.
    AsInt(dv int) int

    // AsFloat64 returns the value as float64.
    AsFloat64(dv float64) float64

    // AsBool returns the value as bool.
    AsBool(dv bool) bool

    // Equals compares a value with the passed one.
    Equals(to Value) bool
}

Value contains one JSON value.

type ValueProcessor Uses

type ValueProcessor func(path string, value Value) error

ValueProcessor describes a function for the processing of values while iterating over a document.

Package gjp imports 7 packages (graph). Updated 2018-10-15. Refresh now. Tools for package owners.