Documentation ¶
Overview ¶
Package schema contains helpers to map Go objects to quads and vise-versa.
This package is not a full schema library. It will not save or force any RDF schema constrains, it only provides a mapping.
Index ¶
- Constants
- Variables
- func IsNotFound(err error) bool
- func LoadIteratorTo(ctx context.Context, qs graph.QuadStore, dst reflect.Value, ...) errordeprecated
- func LoadIteratorToDepth(ctx context.Context, qs graph.QuadStore, dst reflect.Value, depth int, ...) errordeprecated
- func LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) errordeprecated
- func LoadPathTo(ctx context.Context, qs graph.QuadStore, dst interface{}, p *path.Path) errordeprecated
- func LoadTo(ctx context.Context, qs graph.QuadStore, dst interface{}, ids ...quad.Value) errordeprecated
- func LoadToDepth(ctx context.Context, qs graph.QuadStore, dst interface{}, depth int, ...) errordeprecated
- func PathForType(rt reflect.Type) (*path.Path, error)deprecated
- func RegisterType(iri quad.IRI, obj interface{})
- func WriteAsQuads(w quad.Writer, o interface{}) (quad.Value, error)deprecated
- func WriteNamespaces(w quad.Writer, n *voc.Namespaces) errordeprecated
- type Class
- type ClassesByIRI
- type Config
- func (c *Config) LoadIteratorTo(ctx context.Context, qs graph.QuadStore, dst reflect.Value, ...) error
- func (c *Config) LoadIteratorToDepth(ctx context.Context, qs graph.QuadStore, dst reflect.Value, depth int, ...) error
- func (c *Config) LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) error
- func (c *Config) LoadPathTo(ctx context.Context, qs graph.QuadStore, dst interface{}, p *path.Path) error
- func (c *Config) LoadTo(ctx context.Context, qs graph.QuadStore, dst interface{}, ids ...quad.Value) error
- func (c *Config) LoadToDepth(ctx context.Context, qs graph.QuadStore, dst interface{}, depth int, ...) error
- func (c *Config) PathForType(rt reflect.Type) (*path.Path, error)
- func (c *Config) WriteAsQuads(w quad.Writer, o interface{}) (quad.Value, error)
- func (c *Config) WriteNamespaces(w quad.Writer, n *voc.Namespaces) error
- type ErrReqFieldNotSet
- type ErrTypeConversionFailed
- type IRIMode
- type Object
- type PropertiesByIRI
- type Property
- type ValueConverter
- type ValueConverterFunc
Constants ¶
const ( // IRINative applies no transformation to IRIs. IRINative = IRIMode(iota) // IRIShort will compact all IRIs with known namespaces. IRIShort // IRIFull will expand all IRIs with known namespaces. IRIFull )
Variables ¶
var GenerateID = func(_ interface{}) quad.Value { return quad.RandomBlankNode() }
GenerateID is called when any object without an ID field is being saved.
Deprecated: see Config.GenerateID
var Optimize = true
Optimize flags controls an optimization step performed before queries.
Functions ¶
func IsNotFound ¶
IsNotFound check if error is related to a missing object (either because of wrong ID or because of type constrains).
func LoadNamespaces
deprecated
func LoadTo
deprecated
func RegisterType ¶
RegisterType associates an IRI with a given Go type.
All queries and writes will require or add a type triple.
func WriteNamespaces
deprecated
func WriteNamespaces(w quad.Writer, n *voc.Namespaces) error
WriteNamespaces will writes namespaces list into graph.
Deprecated: see Config.WriteNamespaces
Types ¶
type ClassesByIRI ¶
type ClassesByIRI []Class
func (ClassesByIRI) Len ¶
func (a ClassesByIRI) Len() int
func (ClassesByIRI) Less ¶
func (a ClassesByIRI) Less(i, j int) bool
func (ClassesByIRI) Swap ¶
func (a ClassesByIRI) Swap(i, j int)
type Config ¶
type Config struct { // IRIs set a conversion mode for all IRIs. IRIs IRIMode // GenerateID is called when any object without an ID field is being saved. GenerateID func(_ interface{}) quad.Value // Label will be added to all quads written. Does not affect queries. Label quad.Value // contains filtered or unexported fields }
Config controls behavior of schema package.
func (*Config) LoadIteratorTo ¶
func (c *Config) LoadIteratorTo(ctx context.Context, qs graph.QuadStore, dst reflect.Value, list graph.Iterator) error
LoadIteratorTo is a lower level version of LoadTo.
It expects an iterator of nodes to be passed explicitly and destination value to be obtained via reflect package manually.
Nodes iterator can be nil, All iterator will be used in this case.
func (*Config) LoadIteratorToDepth ¶
func (c *Config) LoadIteratorToDepth(ctx context.Context, qs graph.QuadStore, dst reflect.Value, depth int, list graph.Iterator) error
LoadIteratorToDepth is the same as LoadIteratorTo, but stops at a specified depth. Negative value means unlimited depth, and zero means top level only.
func (*Config) LoadNamespaces ¶
func (c *Config) LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) error
LoadNamespaces will load namespaces stored in graph to a specified list. If destination list is empty, global namespace registry will be used.
func (*Config) LoadPathTo ¶
func (c *Config) LoadPathTo(ctx context.Context, qs graph.QuadStore, dst interface{}, p *path.Path) error
LoadPathTo is the same as LoadTo, but starts loading objects from a given path.
func (*Config) LoadTo ¶
func (c *Config) LoadTo(ctx context.Context, qs graph.QuadStore, dst interface{}, ids ...quad.Value) error
LoadTo will load a sub-graph of objects starting from ids (or from any nodes, if empty) to a destination Go object. Destination can be a struct, slice or channel.
Mapping to quads is done via Go struct tag "quad" or "json" as a fallback.
A simplest mapping is an "@id" tag which saves node ID (subject of a quad) into tagged field.
type Node struct{ ID quad.IRI `json:"@id"` // or `quad:"@id"` }
Field with an "@id" tag is omitted, but in case of Go->quads mapping new ID will be generated using GenerateID callback, which can be changed to provide a custom mappings.
All other tags are interpreted as a predicate name for a specific field:
type Person struct{ ID quad.IRI `json:"@id"` Name string `json:"name"` } p := Person{"bob","Bob"} // is equivalent to triple: // <bob> <name> "Bob"
Predicate IRIs in RDF can have a long namespaces, but they can be written in short form. They will be expanded automatically if namespace prefix is registered within QuadStore or globally via "voc" package. There is also a special predicate name "@type" which is mapped to "rdf:type" IRI.
voc.RegisterPrefix("ex:", "http://example.org/") type Person struct{ ID quad.IRI `json:"@id"` Type quad.IRI `json:"@type"` Name string `json:"ex:name"` // will be expanded to http://example.org/name } p := Person{"bob",quad.IRI("Person"),"Bob"} // is equivalent to triples: // <bob> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <Person> // <bob> <http://example.org/name> "Bob"
Predicate link direction can be reversed with a special tag syntax (not available for "json" tag):
type Person struct{ ID quad.IRI `json:"@id"` Name string `json:"name"` // same as `quad:"name"` or `quad:"name > *"` Parents []quad.IRI `quad:"isParentOf < *"` } p := Person{"bob","Bob",[]quad.IRI{"alice","fred"}} // is equivalent to triples: // <bob> <name> "Bob" // <alice> <isParentOf> <bob> // <fred> <isParentOf> <bob>
All fields in structs are interpreted as required (except slices), thus struct will not be loaded if one of fields is missing. An "optional" tag can be specified to relax this requirement. Also, "required" can be specified for slices to alter default value.
type Person struct{ ID quad.IRI `json:"@id"` Name string `json:"name"` // required field ThirdName string `quad:"thirdName,optional"` // can be empty FollowedBy []quad.IRI `quad:"follows"` }
func (*Config) LoadToDepth ¶
func (c *Config) LoadToDepth(ctx context.Context, qs graph.QuadStore, dst interface{}, depth int, ids ...quad.Value) error
LoadToDepth is the same as LoadTo, but stops at a specified depth. Negative value means unlimited depth, and zero means top level only.
func (*Config) PathForType ¶
PathForType builds a path (morphism) for a given Go type.
func (*Config) WriteAsQuads ¶
WriteAsQuads writes a single value in form of quads into specified quad writer.
It returns an identifier of the object in the output sub-graph. If an object has an annotated ID field, it's value will be converted to quad.Value and returned. Otherwise, a new BNode will be generated using GenerateID function.
See LoadTo for a list of quads mapping rules.
func (*Config) WriteNamespaces ¶
WriteNamespaces will writes namespaces list into graph.
type ErrReqFieldNotSet ¶
type ErrReqFieldNotSet struct {
Field string
}
func (ErrReqFieldNotSet) Error ¶
func (e ErrReqFieldNotSet) Error() string
type ErrTypeConversionFailed ¶
func (ErrTypeConversionFailed) Error ¶
func (e ErrTypeConversionFailed) Error() string
type PropertiesByIRI ¶
type PropertiesByIRI []Property
func (PropertiesByIRI) Len ¶
func (a PropertiesByIRI) Len() int
func (PropertiesByIRI) Less ¶
func (a PropertiesByIRI) Less(i, j int) bool
func (PropertiesByIRI) Swap ¶
func (a PropertiesByIRI) Swap(i, j int)
type ValueConverter ¶
var DefaultConverter ValueConverter