Treat IPLD refs as values or refs
To get the most up to date docs:
#!/bin/bash
go get -u github.com/computes/go-ipld-polymorph
godoc -http :8080Then visit http://localhost:8080/pkg/github.com/computes/go-ipld-polymorph/
-- import "github.com/computes/go-ipld-polymorph"
var DefaultIPFSURL *url.URLDefaultIPFSURL can be set to allow Polymorph instantiated to be instantiated without a url
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(raw json.RawMessage) boolIsRef 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(ipfsURL *url.URL, raw json.RawMessage, cache Cache) (json.RawMessage, error)ResolveRef will resolve the given IPLD reference.
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() CacheNewSimpleCache returns an instance of SimpleCache, which can be used as Cache
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(ipfsURL *url.URL, data interface{}) (*Polymorph, error)FromInterface instantiates a new Polymorph using json.Marshal on the provided interface
func FromRef(ipfsURL *url.URL, ref string) *PolymorphFromRef instantiates a new Polymorph instance with a ref
func New(ipfsURL *url.URL) *PolymorphNew Constructs a new Polymorph instance
func (p *Polymorph) AsBool() (bool, error)AsBool returns the current value as a bool, resolving the IPLD reference if necessary
func (p *Polymorph) AsRawMessage() (json.RawMessage, error)AsRawMessage returns the current value as a string, resolving the IPLD reference if necessary
func (p *Polymorph) AsRef() stringAsRef returns the ref if it is one and an empty string if not
func (p *Polymorph) AsString() (string, error)AsString returns the current value as a string, resolving the IPLD reference if necessary
func (p *Polymorph) GetBool(path string) (bool, error)GetBool returns the bool value at path, resolving IPLD references if necessary to get there.
func (p *Polymorph) GetPolymorph(path string) (*Polymorph, error)GetPolymorph returns a Polymorph value at path, resolving IPLD references if necessary to get there.
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 (p *Polymorph) GetString(path string) (string, error)GetString returns the string value at path, resolving IPLD references if necessary to get there.
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 (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 (p *Polymorph) IsRef() boolIsRef 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 (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 (p *Polymorph) UnmarshalJSON(b []byte) errorUnmarshalJSON 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 struct {
}SimpleCache implements Cache in the simplest way possible
func (s *SimpleCache) Get(path string) json.RawMessageGet returns a cached value for a given HTTP request path. Returns nil if the cache is not present
func (s *SimpleCache) Set(path string, value json.RawMessage)Set sets a cache value.