rdf

package module
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: May 12, 2020 License: MIT Imports: 7 Imported by: 10

README

go-rdfjs

Go implementation of the RDF data model

This is a zero-dependency module implementing the RDF data model. It's a faithful and idiomatic adaptation of the RDFJS JSON-based interface and it comes with RDFJS-compatible JSON marshalers and unmarshalers for easy interop with JavaScript libraries like n3.js, jsonld.js, and graphy.js.

Terms

The five term types are the structs NamedNode, BlankNode, Literal, DefaultGraph, and Variable. They all satisfy the Term interface:

type Term interface {
  String() string
  TermType() string
  Value() string
  Equal(term Term) bool
  MarshalJSON() ([]byte, error)
  UnmarshalJSON(data []byte) error
}

Named nodes, blank nodes, and variables all have single string value and can be created with the constructors:

NewNamedNode(value string) *NamedNode
NewBlankNode(value string) *BlankNode
NewVariable(value string) *Variable

It's up to the user to validate that named node values are valid IRIs. Per the RDFJS spec, blank node values should not begin with _:, and variable values should not begin with ?.

Default graphs have no value - the Value() string method always returns the empty string, and the type DefaultGraph is just struct{}. There is a "default default graph" value var Default *DefaultGraph that is recommended for most purposes, although new default graphs can also be created with the constructor:

NewDefaultGraph() *DefaultGraph

Literals have a string value, a string language, and a named node datatype, which may be nil (interpreted as the default datatype of xsd:string). A literal can be created with the constructor:

NewLiteral(value, language string, datatype *NamedNode) *Literal

If the given datatype does not have a value of rdf:langString, then the resulting *Literal will have no langauge, even if one is passed. You can use the exported var RDFLangString *NamedNode value to avoid repeatedly constructing an rdf:langString term.

The term structs do not have internal term type fields - the TermType() string method is a constant function on each struct type. This is done to save memory.

Unmarshal generic terms

Each of the term structs implements MarshalJSON and UnmarshalJSON; however it is often necessary to unmarshal a term without knowing its type in advance:

UnmarshalTerm(data []byte) (Term, error)
Serialize and parse strings

Terms also implement a .String() string method that return their N-Quads term representation (e.g. "example"^^<http://example.com>). The ParseTerm(s: string): Term function parses terms back from this format.

Quads

type Quad [4]Term

Quads are represented interally as 4-tuples of Term interfaces. This was chosen instead of a struct type to support advanced uses like arithmetic or permutations of term positions. Quad terms can be accessed by name with the .Subject(): Term, .Predicate(): Term, .Object(): Term, and .Graph(): Term methods.

Quads also implement MarshalJSON and UnmarshalJSON, which serialize to and from the RDFJS object representation of quads ({"subject": { }, ...}). Internally, Quad.UnmarshalJSON calls the generic UnmarshalTerm for each of its components.

A new quad can be created with the constructor:

NewQuad(subject, predicate, object, graph Term) *Quad

The user is responsible for checking that the terms of a quad are valid for their positions (no literals as subjects, etc). If graph is nil, the "default default graph" var Default *DefaultGraph will be used.

Serialize and parse strings

Quads also have a .String() string method that returns the N-Quads representation of the quad, including a trailing period, but not including a newline. ParseQuad(s: string): *Quad parses a quad back from this format.

You can also parse a slice of quads out of an io.Reader using ReadQuads(input io.Reader) ([]*Quad, error).

Documentation

Index

Constants

View Source
const BlankNodeType = "BlankNode"

BlankNodeType is the TermType blank nodes

View Source
const DefaultGraphType = "DefaultGraph"

DefaultGraphType is the TermType for default graphs

View Source
const LiteralType = "Literal"

LiteralType is the TermType literals

View Source
const NamedNodeType = "NamedNode"

NamedNodeType is the TermType for IRIs

View Source
const VariableType = "Variable"

VariableType is the TermType for variables

Variables

View Source
var Default = &DefaultGraph{}

Default is the default default graph

View Source
var ErrParseTerm = errors.New("Error parsing term")

ErrParseTerm indicates that a string could not parse into a term

View Source
var ErrTermType = errors.New("Mismatching term types")

ErrTermType indicates an unexpected or mismatching term types

View Source
var RDFLangString = &NamedNode{"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"}

RDFLangString is the datatype for language-tagged literals

View Source
var XSDString = &NamedNode{"http://www.w3.org/2001/XMLSchema#string"}

XSDString is the default datatype for literals

Functions

This section is empty.

Types

type BlankNode added in v0.2.0

type BlankNode struct {
	// contains filtered or unexported fields
}

A BlankNode is labelled blank node

func NewBlankNode added in v0.2.0

func NewBlankNode(value string) *BlankNode

NewBlankNode creates a new blank node

func (*BlankNode) Equal added in v0.2.0

func (node *BlankNode) Equal(term Term) bool

Equal checks for functional equivalence of terms

func (*BlankNode) MarshalJSON added in v0.2.0

func (node *BlankNode) MarshalJSON() ([]byte, error)

MarshalJSON marshals the blank node into a byte slice

func (*BlankNode) String added in v0.2.1

func (node *BlankNode) String() string

func (*BlankNode) TermType added in v0.2.0

func (node *BlankNode) TermType() string

TermType of a blank node is "BlankNode"

func (*BlankNode) UnmarshalJSON added in v0.2.0

func (node *BlankNode) UnmarshalJSON(data []byte) error

UnmarshalJSON umarshals a byte slice into the blank node

func (*BlankNode) Value added in v0.2.0

func (node *BlankNode) Value() string

Value of a blank node is its label, which will always begin with "_:"

type DefaultGraph added in v0.2.0

type DefaultGraph struct{}

DefaultGraph is the default graph term

func NewDefaultGraph added in v0.2.0

func NewDefaultGraph() *DefaultGraph

NewDefaultGraph creates a new default graph

func (*DefaultGraph) Equal added in v0.2.0

func (node *DefaultGraph) Equal(term Term) bool

Equal checks for functional equivalence of terms

func (*DefaultGraph) MarshalJSON added in v0.2.0

func (node *DefaultGraph) MarshalJSON() ([]byte, error)

MarshalJSON marshals the literal into a byte slice

func (*DefaultGraph) String added in v0.2.1

func (node *DefaultGraph) String() string

func (*DefaultGraph) TermType added in v0.2.0

func (node *DefaultGraph) TermType() string

TermType of a default graph is "DefaultGraph"

func (*DefaultGraph) UnmarshalJSON added in v0.2.0

func (node *DefaultGraph) UnmarshalJSON(data []byte) error

UnmarshalJSON umarshals a byte slice into the blank node

func (*DefaultGraph) Value added in v0.2.0

func (node *DefaultGraph) Value() string

Value of a DefaultGraph is the empty string

type Literal added in v0.2.0

type Literal struct {
	// contains filtered or unexported fields
}

A Literal is literal term

func NewLiteral added in v0.2.0

func NewLiteral(value string, language string, datatype *NamedNode) *Literal

NewLiteral creates a new literal

func (*Literal) Datatype added in v0.2.0

func (node *Literal) Datatype() Term

Datatype of a literal returns the literal's datatype

func (*Literal) Equal added in v0.2.0

func (node *Literal) Equal(term Term) bool

Equal checks for functional equivalence of terms

func (*Literal) Language added in v0.2.0

func (node *Literal) Language() string

Language of a literal is the literal's language tag

func (*Literal) MarshalJSON added in v0.2.0

func (node *Literal) MarshalJSON() ([]byte, error)

MarshalJSON marshals the literal into a byte slice

func (*Literal) String added in v0.2.1

func (node *Literal) String() string

func (*Literal) TermType added in v0.2.0

func (node *Literal) TermType() string

TermType of a literal is "Literal"

func (*Literal) UnmarshalJSON added in v0.2.0

func (node *Literal) UnmarshalJSON(data []byte) error

UnmarshalJSON umarshals a byte slice into the literal

func (*Literal) Value added in v0.2.0

func (node *Literal) Value() string

Value of a literal is its string value, without the datatype and langauge

type NamedNode added in v0.2.0

type NamedNode struct {
	// contains filtered or unexported fields
}

A NamedNode is an IRI term

func NewNamedNode added in v0.2.0

func NewNamedNode(value string) *NamedNode

NewNamedNode creates a new IRI

func (*NamedNode) Equal added in v0.2.0

func (node *NamedNode) Equal(term Term) bool

Equal checks for functional equivalence of terms

func (*NamedNode) MarshalJSON added in v0.2.0

func (node *NamedNode) MarshalJSON() ([]byte, error)

MarshalJSON marshals the named node into a byte slice

func (*NamedNode) String added in v0.2.1

func (node *NamedNode) String() string

func (*NamedNode) TermType added in v0.2.0

func (node *NamedNode) TermType() string

TermType of an IRI is "NamedNode"

func (*NamedNode) UnmarshalJSON added in v0.2.0

func (node *NamedNode) UnmarshalJSON(data []byte) error

UnmarshalJSON umarshals a byte slice into the named node

func (*NamedNode) Value added in v0.2.0

func (node *NamedNode) Value() string

Value of an IRI is its string value

type Quad

type Quad [4]Term

A Quad is a 4-tuple of terms

func NewQuad added in v0.2.0

func NewQuad(subject, predicate, object, graph Term) *Quad

NewQuad creates a new quad. If graph is nil, Default will be used.

func ParseQuad added in v0.2.1

func ParseQuad(s string) *Quad

ParseQuad parses a Quad out of a string

func ReadQuads added in v0.3.3

func ReadQuads(input io.Reader) ([]*Quad, error)

ReadQuads parses an io.Reader of serialize n-quads into a slice of *Quads

func (*Quad) Graph

func (q *Quad) Graph() Term

Graph returns the fourth term

func (*Quad) MarshalJSON added in v0.2.0

func (q *Quad) MarshalJSON() ([]byte, error)

MarshalJSON marshals a quad into a byte slice

func (*Quad) Object

func (q *Quad) Object() Term

Object returns the third term

func (*Quad) Predicate

func (q *Quad) Predicate() Term

Predicate returns the second term

func (*Quad) String added in v0.2.1

func (q *Quad) String() string

func (*Quad) Subject

func (q *Quad) Subject() Term

Subject returns the first term

func (*Quad) UnmarshalJSON added in v0.2.0

func (q *Quad) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a byte slice into a quad

type Term

type Term interface {
	String() string
	TermType() string
	Value() string
	Equal(term Term) bool
	MarshalJSON() ([]byte, error)
	UnmarshalJSON(data []byte) error
}

Term is the interface that all terms satisfy

func ParseTerm added in v0.2.1

func ParseTerm(t string) (Term, error)

ParseTerm parses a Term value out of an N-Quads string

func UnmarshalTerm

func UnmarshalTerm(data []byte) (Term, error)

UnmarshalTerm unmarshals a byte slice into a Term

func UnmarshalTerms added in v0.3.1

func UnmarshalTerms(data []byte) ([]Term, error)

UnmarshalTerms unmarshals a JSON array of RDFJS terms into a slice of rdf.Terms

type TermLiteral added in v0.2.0

type TermLiteral interface {
	Term
	Language() string
	Datatype() Term
}

TermLiteral is an interface for literal terms. This is only used in the Equal() method of the Literal struct. You shouldn't have to worry about this unless you're going to use external implementations of Term, in which case you need to make sure that literal terms implement this interface.

type Variable added in v0.2.0

type Variable struct {
	// contains filtered or unexported fields
}

A Variable is labelled variable

func NewVariable added in v0.2.0

func NewVariable(value string) *Variable

NewVariable creates a new variable

func (*Variable) Equal added in v0.2.0

func (node *Variable) Equal(term Term) bool

Equal checks for functional equivalence of terms

func (*Variable) MarshalJSON added in v0.2.0

func (node *Variable) MarshalJSON() ([]byte, error)

MarshalJSON marshals the variable into a byte slice

func (*Variable) String added in v0.2.1

func (node *Variable) String() string

func (*Variable) TermType added in v0.2.0

func (node *Variable) TermType() string

TermType of a variable is "Variable"

func (*Variable) UnmarshalJSON added in v0.2.0

func (node *Variable) UnmarshalJSON(data []byte) error

UnmarshalJSON umarshals a byte slice into the variable

func (*Variable) Value added in v0.2.0

func (node *Variable) Value() string

Value of a variable is its label, which will always begin with "_:"

Jump to

Keyboard shortcuts

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