schema

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2017 License: Apache-2.0 Imports: 14 Imported by: 0

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

This section is empty.

Variables

View Source
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.

View Source
var Optimize = true

Optimize flags controls an optimization step performed before queries.

Functions

func IsNotFound

func IsNotFound(err error) bool

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

func 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 LoadTo

func 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 PathForType

func PathForType(rt reflect.Type) (*path.Path, error)

PathForType builds a path (morphism) for a given Go type.

func RegisterType

func RegisterType(iri quad.IRI, obj interface{})

RegisterType associates an IRI with a given Go type.

All queries and writes will require or add a type triple.

func WriteAsQuads

func WriteAsQuads(w quad.Writer, o interface{}) (quad.Value, error)

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 Class

type Class struct {
	Object
	Properties   []Property `quad:"schema:domainIncludes < *,optional"`
	SupersededBy []quad.IRI `quad:"schema:supersededBy,optional"`
	Extends      []quad.IRI `quad:"rdfs:subClassOf,optional"`
}

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

type ErrTypeConversionFailed struct {
	From reflect.Type
	To   reflect.Type
}

func (ErrTypeConversionFailed) Error

func (e ErrTypeConversionFailed) Error() string

type Object

type Object struct {
	ID quad.IRI `quad:"@id"`

	Label   string `quad:"rdfs:label,optional"`
	Comment string `quad:"rdfs:comment,optional"`

	Name        string `quad:"schema:name,optional"`
	Description string `quad:"schema:description,optional"`
}

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 Property

type Property struct {
	Object
	InverseOf    quad.IRI   `quad:"schema:inverseOf,optional"`
	SupersededBy []quad.IRI `quad:"schema:supersededBy,optional"`
	Expects      []quad.IRI `quad:"schema:rangeIncludes"`
}

type ValueConverter

type ValueConverter interface {
	SetValue(dst reflect.Value, src reflect.Value) error
}
var DefaultConverter ValueConverter

type ValueConverterFunc

type ValueConverterFunc func(dst reflect.Value, src reflect.Value) error

func (ValueConverterFunc) SetValue

func (f ValueConverterFunc) SetValue(dst reflect.Value, src reflect.Value) error

Jump to

Keyboard shortcuts

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