export

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package export writes a graph out in the formats other tools read.

Everything here streams. A Source hands assertions to a Format one at a time and the format writes each one as it arrives, so the graph never has to fit in memory. The formats that name nodes — graphml, gexf, dot, mermaid, cypher — hold the set of nodes they have already declared, which is the graph's nodes and not its edges; the rest hold nothing at all.

Two questions separate the formats. What can one carry? json, ndjson, csv and tsv carry every field of an assertion, the span it was read from and the confidence it was given included; the RDF and graph formats carry the assertion alone, because a triple has nowhere to put the rest. What can one be read back from? The four that carry everything, and nt. Registry.Read parses them into the same Source a writer takes, so converting one to another is one call inside another:

err := export.Default.Write(ctx, w, export.Default.Read(f, "nt"), "ttl")

A term that already carries a scheme is an IRI and is written as it stands. Anything else is a name, and the RDF formats mint it into Base.

A format is named by its file extension, so the name of a document is the name of the format that writes it:

json ndjson jsonld csv tsv nt ttl rdfxml graphml gexf dot mermaid cypher

parquet and yaml resolve too, and report ErrLibrary: both need an encoder outside the standard library, and this module takes no dependencies. See Absent.

Index

Constants

View Source
const Base = "https://semantica.dev/ns#"

Base is the namespace a term without a scheme is minted into. It is Semantica's own vocabulary, so a graph written here and one written by the Python this is ported from name the same things.

Variables

View Source
var (
	// ErrFormat says nothing is registered under that name.
	ErrFormat = errors.New("unknown format")
	// ErrTerm says an assertion is missing a subject, predicate or object.
	// A format that identifies nodes has nothing to call the node, so it
	// stops rather than write a document with a hole in it.
	ErrTerm = errors.New("assertion has an empty term")
	// ErrName says a predicate cannot be written as an XML qualified name,
	// which is the one thing rdfxml needs that the other formats do not.
	ErrName = errors.New("predicate has no XML name")
	// ErrLibrary says the format needs code this module does not carry.
	// Register a writer for it to fill the gap.
	ErrLibrary = errors.New("format needs a library this module does not carry")
	// ErrParse says the format is written here but not read back.
	ErrParse = errors.New("format cannot be parsed")
	// ErrDrained says a source over a reader has already been walked and its
	// bytes are gone.
	ErrDrained = errors.New("source is already read")
)

What this package returns when it cannot write what it was given.

View Source
var Default = &Registry{by: map[string]Format{
	"json":    JSON{},
	"ndjson":  NDJSON{},
	"jsonld":  JSONLD{},
	"csv":     CSV{},
	"tsv":     CSV{Sep: '\t'},
	"nt":      NT{},
	"ttl":     Turtle{},
	"rdfxml":  RDFXML{},
	"graphml": GraphML{},
	"gexf":    GEXF{},
	"dot":     DOT{},
	"mermaid": Mermaid{},
	"cypher":  Cypher{},
	"parquet": Absent{"parquet"},
	"yaml":    Absent{"yaml"},
}}

Default resolves the formats this package implements.

Functions

This section is empty.

Types

type Absent

type Absent struct{ Name string }

Absent stands for a format this module knows of and cannot write, so asking for it says what is missing rather than that the name is unknown. parquet and yaml are registered this way: a columnar writer and a YAML encoder are both outside the standard library, and this module takes no dependencies.

Registering a real writer under the name fills the gap:

export.Default.Set("parquet", myParquetWriter)

Until then csv and ndjson carry every field either of them would.

func (Absent) Write

func (a Absent) Write(context.Context, io.Writer, Source) error

Write reports that the format is not built in.

type CSV

type CSV struct {
	Sep  rune
	Bare bool
}

CSV writes the graph as a table, a row to an assertion, carrying every field. Sep is the column separator, a comma when unset; the registry has it under csv with a comma and under tsv with a tab.

Bare writes the rows without the header line, for appending to a table that already has one. Reading rows written that way takes the columns in the order above, since there is no header to say otherwise.

func (CSV) Read

func (c CSV) Read(r io.Reader) Source

Read parses delimited text back into assertions. The header names the columns, so a table whose columns were reordered or trimmed still reads; with Bare set the columns are taken in their written order.

func (CSV) Write

func (c CSV) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as delimited text.

type Cypher

type Cypher struct{ Label string }

Cypher writes CREATE statements for Neo4j, Memgraph and the graph databases that speak the same language. A node is created the first time an assertion names it, and each edge is a MATCH on the two ids it joins followed by a CREATE of the relation between them.

Label is the label every node is created with, Node when unset; the graph carries no type of its own to use in its place. A relation keeps the predicate it was asserted with, back-quoted, rather than being upper-cased into the house style: case is part of a name, and losing it cannot be undone.

func (Cypher) Write

func (c Cypher) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as Cypher statements.

type DOT

type DOT struct{ Name string }

DOT writes the Graphviz language. Name is the graph's name, G when unset.

func (DOT) Write

func (d DOT) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as DOT.

type Format

type Format interface {
	Write(ctx context.Context, w io.Writer, src Source) error
}

Format writes a stream of assertions. A format is a value with no state of its own, so one may be shared and used concurrently.

type GEXF

type GEXF struct{}

GEXF writes the XML Gephi reads. Its grammar puts every node before every edge, so this is the one format here that walks the source twice: once for the nodes and once for the edges. The alternative is holding the edges, and a graph has more of those than nodes.

func (GEXF) Write

func (GEXF) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as GEXF, walking src twice.

type GraphML

type GraphML struct{}

GraphML writes the XML that Cytoscape, yEd and Gephi read. A node is declared the first time an assertion names it, immediately before the edge that needs it, so the document is written in one pass and every edge follows the nodes it joins.

func (GraphML) Write

func (GraphML) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as GraphML.

type JSON

type JSON struct{ Indent string }

JSON writes the graph as one array of assertions, one to a line so the output diffs, and carries every field.

Indent pretty-prints each assertion by that much; unset, an assertion is one line. The array brackets are written as the walk starts and ends, so the document streams whichever way it is set.

func (JSON) Read

func (j JSON) Read(r io.Reader) Source

Read parses a JSON array of assertions. It decodes one element at a time, so a file larger than memory reads as well as it writes.

func (JSON) Write

func (j JSON) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as a JSON array.

type JSONLD

type JSONLD struct{ Base string }

JSONLD writes the graph as JSON-LD: a context naming the namespace and a graph of nodes, one per assertion.

One node per assertion rather than one per subject, because grouping by subject means holding the graph. A JSON-LD reader merges the nodes back by @id, so the document says the same thing either way. Being RDF, it carries the assertion and not the confidence or the span it came from.

func (JSONLD) Write

func (j JSONLD) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as a JSON-LD document.

type Mermaid

type Mermaid struct{ Dir string }

Mermaid writes a flowchart, for a diagram in a document that renders one. Dir is the layout direction — LR, TD, RL, BT — LR when unset.

Mermaid names a node with an identifier rather than a string, so each term gets one, n0 upward in the order the terms are first seen, with the term itself as the node's text.

func (Mermaid) Write

func (m Mermaid) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as a Mermaid flowchart.

type NDJSON

type NDJSON struct{}

NDJSON writes one assertion per line, each a complete JSON object. It is the format to reach for when the graph is large or the reader is a pipe: nothing has to be held, and a line is a whole record.

func (NDJSON) Read

func (NDJSON) Read(r io.Reader) Source

Read parses one assertion per line. A blank line is skipped, so a file that ends in a newline reads as the records it holds.

func (NDJSON) Write

func (NDJSON) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w, one JSON object per line.

type NT

type NT struct{ Base string }

NT writes N-Triples: one <subject> <predicate> <object> . to a line. It is the format every RDF tool reads, the cheapest to write, and the only RDF format here that reads back, since a line is a whole statement.

A triple has nowhere to put a confidence or the span a claim came from, so those are not written. A graph that has to keep them goes out as ndjson.

func (NT) Read

func (n NT) Read(r io.Reader) Source

Read parses N-Triples whose terms are all IRI references, which is what NT.Write produces. A blank line and a comment are skipped; a literal object is reported rather than guessed at, since a literal and a name are different claims and this package writes only the second.

func (NT) Write

func (n NT) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as N-Triples.

type RDFXML

type RDFXML struct{ Base string }

RDFXML writes RDF/XML: an rdf:Description per assertion, carrying the predicate as a property element.

One description per assertion rather than one per subject, for the reason JSON-LD writes one node per assertion: grouping means holding the graph. The namespace of each predicate is declared on the property element itself, which is what lets the document be written in one pass.

RDF/XML is the one format here that cannot write every predicate: a property element needs an XML name, so a predicate whose IRI does not end in one is reported as ErrName rather than dropped.

func (RDFXML) Write

func (x RDFXML) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as RDF/XML.

type Reader

type Reader interface {
	Read(r io.Reader) Source
}

Reader is the other half of a format that can parse its own output. json, ndjson, csv, tsv and nt implement it; the rest are written and not read.

type Registry

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

Registry resolves a name to the format written under it, and is the seam for formats this package does not implement: register a writer and it is reached the same way as the rest.

export.Default.Set("parquet", myParquetWriter)
err := export.Default.Write(ctx, f, src, "parquet")

The zero Registry is empty and usable; Default holds the formats here.

func (*Registry) Drop

func (r *Registry) Drop(name string)

Drop removes the format registered under name.

func (*Registry) Formats

func (r *Registry) Formats() []string

Formats lists what is registered, in order.

func (*Registry) Get

func (r *Registry) Get(name string) (Format, bool)

Get returns the format registered under name.

func (*Registry) Read

func (r *Registry) Read(rd io.Reader, format string) Source

Read is the source that parses rd as the named format. A format that does not carry every field of an assertion cannot be read back, and the walk reports ErrParse.

The reader is spent by the first walk; a second returns ErrDrained rather than an empty graph, so a format that needs two walks fails loudly instead of writing a graph with no edges.

func (*Registry) Set

func (r *Registry) Set(name string, f Format)

Set registers f under name, replacing whatever was there.

func (*Registry) Write

func (r *Registry) Write(ctx context.Context, w io.Writer, src Source, format string) error

Write serializes src to w in the named format.

type Source

type Source func(ctx context.Context, yield func(semantic.Triple) error) error

Source walks a graph, handing every assertion to yield in order. It returns yield's error unchanged, so a format that cannot write stops the walk instead of being handed the rest of a graph it has no use for.

A source may be walked more than once: Triples, Match and Walk all replay, which is what GEXF needs, since its grammar puts every node before every edge. A source over an io.Reader is the exception and says so.

func Match

func Match(st store.Triple, s, p, o string) Source

Match is the source of what a triple store holds for a pattern. An empty field matches anything, so Match(st, "", "", "") is the whole store.

A store answers with the assertion and not with what was known about it, so the triples this yields carry no confidence and no source span.

func Triples

func Triples(ts []semantic.Triple) Source

Triples is the source of a slice already in memory.

func Walk

func Walk(g store.Graph, label string, roots ...string) Source

Walk is the source of the edges reachable from roots in a graph store, breadth first from each root in turn, every edge yielded once.

A graph store reports a node's neighbours without saying which relation leads to them, so the walk cannot recover the predicate and records label in its place. Where the predicates matter, export from the triple store.

type Turtle

type Turtle struct{ Base string }

Turtle writes the same statements as NT with a prefix declared for the namespace, so a name minted here is written sem:alice rather than in full. It is the format to hand a person; nt is the one to hand a program.

func (Turtle) Write

func (u Turtle) Write(ctx context.Context, w io.Writer, src Source) error

Write serializes src to w as Turtle.

Jump to

Keyboard shortcuts

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