jsptr

package module
v0.0.0-...-ece4951 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: MIT Imports: 7 Imported by: 1

README

github.com/lestrrat-go/jsptr

github.com/lestrrat-go/jsptr implements JSON pointers. Currently it only supports retrievals.

package jsptr_test

import (
  "fmt"

  "github.com/lestrrat-go/blackmagic"
  "github.com/lestrrat-go/jsptr"
)

type Root struct {
  Foo Foo `json:"foo"`
}

type Foo struct {
  Bar Bar `json:"bar"`
}

type Bar struct {
  Baz string `json:"baz"`
}

type Custom struct{}

func (c *Custom) RetrieveJSONPointer(dst any, ptr string) error {
  if ptr == "/foo/bar/baz" {
    return blackmagic.AssignIfCompatible(dst, "hello world")
  }
  return fmt.Errorf("not found")
}

func Example() {
  const message = "hello world"

  // Retrieve from a map: Useful if you unmarshal a JSON into a map[string]any
  m := map[string]any{
    "foo": map[string]any{
      "bar": map[string]any{
        "baz": message,
      },
    },
  }

  // Retrieve from a struct: Useful if you unmarshal a JSON into a struct
  s := &Root{
    Foo: Foo{
      Bar: Bar{
        Baz: message,
      },
    },
  }

  // You could even use a custom target that implements the RetrieveJSONPointer method
  custom := &Custom{}

  // Or slices
  slice := []string{"foo", "bar", "baz", message}

  testcases := []struct {
    Ptr    string
    Target any
  }{
    {
      Target: m,
      Ptr:    "/foo/bar/baz",
    },
    {
      Target: s,
      Ptr:    "/foo/bar/baz",
    },
    {
      Target: custom,
      Ptr:    "/foo/bar/baz",
    },
    {
      Target: slice,
      Ptr:    "/3",
    },
  }
  for _, tc := range testcases {
    // Obviously, in real likfe you could (and should) reuse the same pointer if you are
    // going to be evaluating the same pointer multiple times.
    p, err := jsptr.New(tc.Ptr)
    if err != nil {
      fmt.Printf("Error creating pointer: %v\n", err)
      return
    }

    var dst string
    if err := p.Retrieve(&dst, tc.Target); err != nil {
      fmt.Printf("Error retrieving value: %v\n", err)
      return
    }
    if dst != message {
      fmt.Printf("Expected 'hello world', got '%s'\n", dst)
      return
    }
  }

  // OUTPUT:
}

source: ./jsptr_example_test.go

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/lestrrat-go/blackmagic"
	"github.com/lestrrat-go/jsptr"
)

type Root struct {
	Foo Foo `json:"foo"`
}

type Foo struct {
	Bar Bar `json:"bar"`
}

type Bar struct {
	Baz string `json:"baz"`
}

type Custom struct{}

func (c *Custom) RetrieveJSONPointer(dst any, ptr string) error {
	if ptr == "/foo/bar/baz" {
		return blackmagic.AssignIfCompatible(dst, "hello world")
	}
	return fmt.Errorf("not found")
}

func main() {
	const message = "hello world"

	// Retrieve from a map: Useful if you unmarshal a JSON into a map[string]any
	m := map[string]any{
		"foo": map[string]any{
			"bar": map[string]any{
				"baz": message,
			},
		},
	}

	// Retrieve from a struct: Useful if you unmarshal a JSON into a struct
	s := &Root{
		Foo: Foo{
			Bar: Bar{
				Baz: message,
			},
		},
	}

	// You could even use a custom target that implements the RetrieveJSONPointer method
	custom := &Custom{}

	// Or slices
	slice := []string{"foo", "bar", "baz", message}

	testcases := []struct {
		Ptr    string
		Target any
	}{
		{
			Target: m,
			Ptr:    "/foo/bar/baz",
		},
		{
			Target: s,
			Ptr:    "/foo/bar/baz",
		},
		{
			Target: custom,
			Ptr:    "/foo/bar/baz",
		},
		{
			Target: slice,
			Ptr:    "/3",
		},
	}
	for _, tc := range testcases {
		// Obviously, in real likfe you could (and should) reuse the same pointer if you are
		// going to be evaluating the same pointer multiple times.
		p, err := jsptr.New(tc.Ptr)
		if err != nil {
			fmt.Printf("Error creating pointer: %v\n", err)
			return
		}

		var dst string
		if err := p.Retrieve(&dst, tc.Target); err != nil {
			fmt.Printf("Error retrieving value: %v\n", err)
			return
		}
		if dst != message {
			fmt.Printf("Expected 'hello world', got '%s'\n", dst)
			return
		}
	}

}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pointer

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

Pointer represents a compiled JSON pointer

func New

func New(pathspec string) (*Pointer, error)

New creates a new JSON pointer from a path specification

func (*Pointer) Pattern

func (p *Pointer) Pattern() string

Pattern returns the original path specification

func (*Pointer) Retrieve

func (p *Pointer) Retrieve(dst any, target any) error

Retrieve retrieves the value at the JSON pointer location

type Source

type Source interface {
	RetrieveJSONPointer(dst any, ptrspec string) error
}

Source is an interface for abstracting different data sources

Jump to

Keyboard shortcuts

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