export

package
v1.0.0-alpha.13 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 10 Imported by: 0

README

RDF Export

Serializes []message.Triple to standard RDF formats using the vocabulary registry for IRI resolution.

Supported Formats

Format Extension Description
Turtle .ttl Compact, human-readable with prefix declarations and subject grouping
NTriples .nt Line-based, one triple per line, fully expanded IRIs
JSONLD .jsonld JSON with @context and @graph for web APIs

Usage

import "github.com/c360studio/semstreams/vocabulary/export"

triples := []message.Triple{
    {Subject: "acme.ops.robotics.gcs.drone.001", Predicate: "robotics.battery.level", Object: 85.5},
    {Subject: "acme.ops.robotics.gcs.drone.001", Predicate: "robotics.flight.armed", Object: true},
}

// Write Turtle to a writer
err := export.Serialize(os.Stdout, triples, export.Turtle)

// Get N-Triples as a string
s, err := export.SerializeToString(triples, export.NTriples)

IRI Resolution

The package resolves dotted-notation identifiers to IRIs automatically:

Subjects are converted based on entity ID structure:

  • Valid 6-part entity IDs: acme.ops.robotics.gcs.drone.001 becomes {base}/entities/acme/ops/robotics/gcs/drone/001
  • Other subjects: dots are converted to slashes under {base}/subjects/

Predicates resolve through the vocabulary registry:

  • Registered predicates use their StandardIRI (e.g., robotics.battery.level with a registered IRI maps to that IRI)
  • Unregistered predicates generate {base}/predicates/domain/category/property

Objects are typed by Go type inference:

Go Type RDF Datatype Output
string (entity ID) Resource <iri>
string (other) xsd:string "text"
int, int64, etc. xsd:integer 42
float64 xsd:double "85.5"^^xsd:double
bool xsd:boolean true
time.Time xsd:dateTime "2024-01-15T10:30:00Z"^^xsd:dateTime

Options

WithBaseIRI

Override the default base IRI used for generated subject and predicate URIs:

err := export.Serialize(w, triples, export.Turtle,
    export.WithBaseIRI("https://example.org"))
WithSubjectIRIFunc

Provide a custom function to map entity ID strings to IRIs. When set, this replaces the default subject IRI generation entirely (WithBaseIRI has no effect on subjects):

err := export.Serialize(w, triples, export.JSONLD,
    export.WithSubjectIRIFunc(func(subject string) string {
        return "https://example.org/entities/" + subject
    }))

Prefix Management

Turtle and JSON-LD output automatically compact IRIs using well-known prefixes (OWL, SKOS, Dublin Core, PROV-O, Schema.org, FOAF, SSN/SOSA, XSD). Only prefixes that appear in the output are declared.

API

Function Description
Serialize(w, triples, format, ...opts) Write triples to an io.Writer
SerializeToString(triples, format, ...opts) Return serialized output as a string

Documentation

Overview

Package export serializes []message.Triple to standard RDF formats.

Supported formats:

  • Turtle (.ttl) — compact, human-readable with prefix declarations and subject grouping
  • N-Triples (.nt) — line-based, one triple per line, no abbreviations
  • JSON-LD (.jsonld) — JSON with @context and @graph for web APIs

The package uses the existing vocabulary infrastructure for IRI resolution:

Basic usage:

triples := []message.Triple{
    {Subject: "acme.ops.robotics.gcs.drone.001", Predicate: "robotics.battery.level", Object: 85.5},
    {Subject: "acme.ops.robotics.gcs.drone.001", Predicate: "robotics.flight.armed", Object: true},
}

// Write Turtle to stdout
err := export.Serialize(os.Stdout, triples, export.Turtle)

// Get N-Triples as a string
s, err := export.SerializeToString(triples, export.NTriples)

// Custom base IRI
err = export.Serialize(w, triples, export.JSONLD, export.WithBaseIRI("https://example.org"))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serialize

func Serialize(w io.Writer, triples []message.Triple, format Format, opts ...Option) error

Serialize writes triples in the specified format to the writer. Invalid triples (empty subject or predicate) are silently skipped.

func SerializeToString

func SerializeToString(triples []message.Triple, format Format, opts ...Option) (string, error)

SerializeToString returns the serialized output as a string.

Types

type Format

type Format int

Format identifies an RDF serialization format.

const (
	// Turtle is the Terse RDF Triple Language format (.ttl).
	// Compact and human-readable with prefix declarations and subject grouping.
	Turtle Format = iota

	// NTriples is the N-Triples format (.nt).
	// Line-based, one triple per line, fully expanded IRIs.
	NTriples

	// JSONLD is the JSON-LD format (.jsonld).
	// JSON with @context and @graph for web API consumption.
	JSONLD
)

func (Format) String

func (f Format) String() string

String returns the format name.

type Option

type Option func(*options)

Option configures serialization behavior.

func WithBaseIRI

func WithBaseIRI(base string) Option

WithBaseIRI overrides the default SemStreamsBase IRI used for generated URIs.

func WithSubjectIRIFunc

func WithSubjectIRIFunc(fn func(string) string) Option

WithSubjectIRIFunc provides a custom function to map entity ID strings to IRIs. When set, this replaces the default subject IRI generation logic entirely; WithBaseIRI has no effect on subjects when a custom function is provided.

Jump to

Keyboard shortcuts

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