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 ¶
- Variables
- func IsNotFound(err error) bool
- func LoadIteratorTo(ctx context.Context, qs graph.QuadStore, dst reflect.Value, ...) error
- func LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) error
- func LoadTo(ctx context.Context, qs graph.QuadStore, dst interface{}, ids ...quad.Value) error
- func PathForType(rt reflect.Type) (*path.Path, error)
- func RegisterType(iri quad.IRI, obj interface{})
- func WriteAsQuads(w quad.Writer, o interface{}) (quad.Value, error)
- func WriteNamespaces(w quad.Writer, n *voc.Namespaces) error
- type Class
- type ClassesByIRI
- type ErrTypeConversionFailed
- type Object
- type PropertiesByIRI
- type Property
- type ValueConverter
- type ValueConverterFunc
Constants ¶
This section is empty.
Variables ¶
var GenerateID func(interface{}) quad.Value = func(_ interface{}) quad.Value { return quad.RandomBlankNode() }
GenerateID is called when any object without an ID field is being saved.
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 LoadIteratorTo ¶
func 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 LoadNamespaces ¶
LoadNamespaces will load namespaces stored in graph to a specified list. If destination list is empty, global namespace registry will be used.
func LoadTo ¶
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 PathForType ¶
PathForType builds a path (morphism) for a given Go type.
func RegisterType ¶
RegisterType associates an IRI with a given Go type.
All queries and writes will require or add a type triple.
func 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 WriteNamespaces ¶
func WriteNamespaces(w quad.Writer, n *voc.Namespaces) error
WriteNamespaces will writes namespaces list into graph.
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 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