path

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 7 Imported by: 2

README

path

Go package for path handling. Currently specialized for, but not restricted to, JSON paths.

Motivation

This library was written as a way to handle parsing and expressing paths pointing at components in JSON values. Unlike more comprehensive libraries such as JsonPath, this library is only intended to handle a single pointer towards one component of a JSON value, rather than being a full query language. On the other hand, it allows for serialization as slices rather than strings, which helps to avoid a class of errors around improperly (de-)escaping path components.

Each component of a path should either be a string or an integer. In the context of JSON paths, a string represents a value in a JSON object, and an integer represents a value in a JSON array.

JS Path syntax and examples

When using JS path (de-)serialization, each component of the serialized string should be one of three types:

  1. Dot-based strings: These are separated from the previous element by a single . (e.g. a.Test_2009). They can only consist of alphanumeric characters and the underscore chracter. Note that for the first dot-based string in a serialized path, the dot should be omitted.
  2. Square-bracket-and-quote-based strings: These should be contained in square brackets and quotes [""] (e.g. a["Test.ing"]). These can contain any arbitrary characters, though quotes and backslashes should be backslash-escaped (e.g. a["\""]).
  3. Integers: These should be contained in square brackets [] (e.g. a[0]).
package main

import (
  "log"

  "github.com/airplane/path"
)

func main() {
  p, err := path.FromJS(`foo[0].bar["baz"]`)

  // Here, p.Components() should be ["foo", 0, "bar", "baz"].
  log.Println(p.Components()) 

  s := p.ToJS()

  // Here, s should be "foo[0].bar.baz". Note that though this is different
  // from the original parsed string, it still will parse back to the same path components.
  log.Println(s) 
}

More examples (particularly unusual edge cases) can be found in (javascript_test.go)[https://github.com/airplanedev/path/blob/main/javascript_test.go].

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type P

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

P is a slice-based representation of a JSON path. Each slice element represents a single path component: either an object key (if a string) or an array index (if an integer).

Unlike JSONPath (https://goessner.net/articles/JsonPath/) and JSON pointers (https://datatracker.ietf.org/doc/html/rfc6901), paths are serialized as slices rather than strings. This helps avoid a class of errors around improperly (de-)escaping path components.

Unlike []interface{}, paths are type-safe via the Str/Int/Path methods.

The zero value is safe to use.

func FromJS

func FromJS(s string) (P, error)

FromJS parses a JSONPath-style path string and returns an error if it is unable to parse the string.

For example:

"foo[0].bar" -> ["foo", 0, "bar"]

func FromJSPartial

func FromJSPartial(s string) (p P, idx int, err error)

FromJSPartial parses a JSONPath-style path string from the start greedily, and returns the index of how far it managed to parse.

For example:

"foo[0].bar asdf" -> ["foo", 0, "bar"], index for " asdf"

func Int

func Int(i ...int) P

Int is a helper for constructing a path from one or more ints.

func Path

func Path(paths ...P) P

Path is a helper for constructing a path from one or more paths.

func Str

func Str(s ...string) P

Str is a helper for constructing a path from one or more strings.

func (P) At

func (p P) At(i int) interface{}

func (P) Components

func (p P) Components() []interface{}

func (P) Int

func (p P) Int(i ...int) P

Int appends one or more ints onto the provided path.

It has the same semantics as the built-in append: the returned path may re-allocate the underlying array as needed.

func (P) Len

func (p P) Len() int

func (P) MarshalJSON

func (p P) MarshalJSON() ([]byte, error)

func (P) Path

func (p P) Path(paths ...P) P

Path appends one or more paths onto the provided path.

It has the same semantics as the built-in append: the returned path may re-allocate the underlying array as needed.

func (P) Str

func (p P) Str(s ...string) P

Str appends one or more strings onto the provided path.

It has the same semantics as the built-in append: the returned path may re-allocate the underlying array as needed.

func (P) String

func (p P) String() string

func (P) Sub

func (p P) Sub(idxs ...int) P

Sub(start, end) returns a subpath from [start, end).

If not provided, end defaults to len(p). If not provided, start defaults to 0.

For example, ["foo", "bar"].Sub(1) would return ["bar"].

func (P) ToJS

func (p P) ToJS() string

ToJS converts a path to a JSONPath-style string representation.

For example:

["foo", 0, "bar"] -> "foo[0].bar"

The produced value can be parsed by FromJS and will produce an identical path.

func (*P) UnmarshalJSON

func (p *P) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳