Skip to content

Repository files navigation

go-ipld-polymorph

Treat IPLD refs as values or refs

Godoc Reference Go Report CircleCI FOSSA Status

Up to date docs

To get the most up to date docs:

#!/bin/bash
go get -u github.com/computes/go-ipld-polymorph
godoc -http :8080

Then visit http://localhost:8080/pkg/github.com/computes/go-ipld-polymorph/

ipldpolymorph

-- import "github.com/computes/go-ipld-polymorph"

Usage

var DefaultIPFSURL *url.URL

DefaultIPFSURL can be set to allow Polymorph instantiated to be instantiated without a url

func AssertRef

func AssertRef(raw json.RawMessage) (string, error)

AssertRef verifies that the raw JSON object is a ref. It returns the address if it is, an error if it is not.

func IsRef

func IsRef(raw json.RawMessage) bool

IsRef detects if a rawMessage is an IPLD reference. An IPLD reference MUST be a JSON object with ONLY the key "/". The value pointed to by "/" must be a string. If there are any additional keys, the value is not a string, or the JSON is invalid, then it is not considered an IPLD reference.

func ResolveRef

func ResolveRef(ipfsURL *url.URL, raw json.RawMessage, cache Cache) (json.RawMessage, error)

ResolveRef will resolve the given IPLD reference.

type Cache

type Cache interface {
	// Get returns a cached value for
	// a given HTTP request path. Returns
	// nil if the cache is not present
	Get(path string) json.RawMessage

	// Set sets a cache value.
	Set(path string, value json.RawMessage)
}

Cache is the interface for accessing the http cache.

func NewSimpleCache

func NewSimpleCache() Cache

NewSimpleCache returns an instance of SimpleCache, which can be used as Cache

type Polymorph

type Polymorph struct {
	IPFSURL *url.URL
}

Polymorph an object that treats IPLD references and raw values the same. It is intended to be constructed with New, and to be JSON Unmarshaled into. Polymorph lazy loads all IPLD references and caches the results, so subsequent calls to a path will have nearly no cost.

func FromInterface

func FromInterface(ipfsURL *url.URL, data interface{}) (*Polymorph, error)

FromInterface instantiates a new Polymorph using json.Marshal on the provided interface

func FromRef

func FromRef(ipfsURL *url.URL, ref string) *Polymorph

FromRef instantiates a new Polymorph instance with a ref

func New

func New(ipfsURL *url.URL) *Polymorph

New Constructs a new Polymorph instance

func (*Polymorph) AsBool

func (p *Polymorph) AsBool() (bool, error)

AsBool returns the current value as a bool, resolving the IPLD reference if necessary

func (*Polymorph) AsRawMessage

func (p *Polymorph) AsRawMessage() (json.RawMessage, error)

AsRawMessage returns the current value as a string, resolving the IPLD reference if necessary

func (*Polymorph) AsRef

func (p *Polymorph) AsRef() string

AsRef returns the ref if it is one and an empty string if not

func (*Polymorph) AsString

func (p *Polymorph) AsString() (string, error)

AsString returns the current value as a string, resolving the IPLD reference if necessary

func (*Polymorph) GetBool

func (p *Polymorph) GetBool(path string) (bool, error)

GetBool returns the bool value at path, resolving IPLD references if necessary to get there.

func (*Polymorph) GetPolymorph

func (p *Polymorph) GetPolymorph(path string) (*Polymorph, error)

GetPolymorph returns a Polymorph value at path, resolving IPLD references if necessary to get there.

func (*Polymorph) GetRawMessage

func (p *Polymorph) GetRawMessage(path string) (json.RawMessage, error)

GetRawMessage returns the raw JSON value at path, resolving IPLD references if necessary to get there.

func (*Polymorph) GetString

func (p *Polymorph) GetString(path string) (string, error)

GetString returns the string value at path, resolving IPLD references if necessary to get there.

func (*Polymorph) GetUnresolvedPolymorph

func (p *Polymorph) GetUnresolvedPolymorph(path string) (*Polymorph, error)

GetUnresolvedPolymorph returns a Polymorph value at path, resolving only the necessary IPLD references to get there.

func (*Polymorph) GetUnresolvedRawMessage

func (p *Polymorph) GetUnresolvedRawMessage(path string) (json.RawMessage, error)

GetUnresolvedRawMessage returns the raw JSON value at path, resolving only the necessary IPLD references to get there.

func (*Polymorph) IsRef

func (p *Polymorph) IsRef() bool

IsRef detects if a rawMessage is an IPLD reference. An IPLD reference MUST be a JSON object with ONLY the key "/". The value pointed to by "/" must be a string. If there are any additional keys, the value is not a string, or the JSON is invalid, then it is not considered an IPLD reference.

func (*Polymorph) MarshalJSON

func (p *Polymorph) MarshalJSON() ([]byte, error)

MarshalJSON returns the original JSON used to instantiate this instance of Polymorph. If no JSON was ever Unmarshaled into this Polymorph, then it returns nil

func (*Polymorph) UnmarshalJSON

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

UnmarshalJSON defers parsing json until one of the Get* methods is called. This function will never return an error, it has an error return type to meet the encoding/json interface requirements.

type SimpleCache

type SimpleCache struct {
}

SimpleCache implements Cache in the simplest way possible

func (*SimpleCache) Get

func (s *SimpleCache) Get(path string) json.RawMessage

Get returns a cached value for a given HTTP request path. Returns nil if the cache is not present

func (*SimpleCache) Set

func (s *SimpleCache) Set(path string, value json.RawMessage)

Set sets a cache value.

License

FOSSA Status

About

Treat IPLD refs as values or refs

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages