schema

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2018 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

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

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

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 deprecated

func LoadIteratorTo(ctx context.Context, qs graph.QuadStore, dst reflect.Value, list graph.Iterator) error

LoadIteratorTo is a lower level version of LoadTo.

Deprecated: see Config.LoadIteratorTo

func LoadIteratorToDepth deprecated

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

Deprecated: see Config.LoadIteratorToDepth

func LoadNamespaces deprecated

func LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) error

LoadNamespaces will load namespaces stored in graph to a specified list.

Deprecated: see Config.LoadNamespaces

func LoadPathTo deprecated

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

Deprecated: see Config.LoadPathTo

func LoadTo deprecated

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.

Deprecated: see Config.LoadTo

func LoadToDepth deprecated

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

Deprecated: see Config.LoadToDepth

func PathForType deprecated

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

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

Deprecated: see Config.PathForType

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 deprecated

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

WriteAsQuads writes a single value in form of quads into specified quad writer.

Deprecated: see Config.WriteAsQuads

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 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 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 Global

func Global() *Config

Global returns a default global schema config.

func NewConfig

func NewConfig() *Config

NewConfig creates a new schema config.

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

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

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

func (*Config) WriteAsQuads

func (c *Config) 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 (*Config) WriteNamespaces

func (c *Config) WriteNamespaces(w quad.Writer, n *voc.Namespaces) error

WriteNamespaces will writes namespaces list into graph.

type ErrReqFieldNotSet

type ErrReqFieldNotSet struct {
	Field string
}

func (ErrReqFieldNotSet) Error

func (e ErrReqFieldNotSet) Error() string

type ErrTypeConversionFailed

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

func (ErrTypeConversionFailed) Error

func (e ErrTypeConversionFailed) Error() string

type IRIMode

type IRIMode int

IRIMode controls how IRIs are processed.

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