egdm

package module
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 5 Imported by: 7

README

Entity Graph Data Model

Data structures, parser and utilities for the entity graph data model

To use the module import the following:

github.com/mimiro-io/entity-graph-data-model

Core Entity Graph Data Model structures

The following example shows how to create an Entity, add a property, reference and serialise it to JSON.

package main

import ( 
    "fmt"
    egdm "github.com/mimiro-io/entity-graph-data-model"
    "encoding/json"
)

func main() {

    entity := egdm.NewEntity()
    entity.Properties["http://data.mimiro.io/schema/name"] = "homer"
    entity.References["http://data.mimiro.io/schema/worksFor"] = "http://data.mimiro.io/people/mrburns"

    entityJson, _ := json.Marshall(entity)
    fmt.Print(entityJson)
}

Parsing Entity Graph Data Model JSON

The module can be used to parse entity graph data model JSON data.

package main

import ( 
    "fmt"
    "github.com/mimiro-io/entity-graph-data-model"
)

func main() {
    byteReader := bytes.NewReader([]byte(`
		[
			{
				"id" : "@context", 
				"namespaces": {
					"ex": "http://example.com/",
					"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
				}
			},
			{
				"id" : "ex:1",
				"props": {
					"http://example.com/address": {
						"props": {
							"http://example.com/street": "123 Main Street"
						}
					}
				}
			}
		]`))

	nsManager := NewNamespaceContext()
	parser := NewEntityParser(nsManager).WithExpandURIs()
	entityCollection, err := parser.LoadEntityCollection(byteReader)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	ID         string            `json:"id"`
	Namespaces map[string]string `json:"namespaces"`
}

func NewContext

func NewContext() *Context

type Continuation

type Continuation struct {
	ID    string `json:"id"`
	Token string `json:"token"`
}

func NewContinuation

func NewContinuation() *Continuation

type Entity

type Entity struct {
	ID         string         `json:"id,omitempty"`
	InternalID uint64         `json:"internalId,omitempty"`
	Recorded   uint64         `json:"recorded,omitempty"`
	IsDeleted  bool           `json:"deleted,omitempty"`
	References map[string]any `json:"refs"`
	Properties map[string]any `json:"props"`
}

func NewEntity

func NewEntity() *Entity

func (*Entity) GetBooleanPropertyValues

func (anEntity *Entity) GetBooleanPropertyValues(typeURI string) ([]bool, error)

func (*Entity) GetFirstBooleanPropertyValue

func (anEntity *Entity) GetFirstBooleanPropertyValue(typeURI string) (bool, error)

func (*Entity) GetFirstFloatPropertyValue

func (anEntity *Entity) GetFirstFloatPropertyValue(typeURI string) (float64, error)

func (*Entity) GetFirstIntPropertyValue

func (anEntity *Entity) GetFirstIntPropertyValue(typeURI string) (int, error)

func (*Entity) GetFirstReferenceValue

func (anEntity *Entity) GetFirstReferenceValue(typeURI string) (string, error)

func (*Entity) GetFirstStringPropertyValue

func (anEntity *Entity) GetFirstStringPropertyValue(typeURI string) (string, error)

func (*Entity) GetFloatPropertyValues

func (anEntity *Entity) GetFloatPropertyValues(typeURI string) ([]float64, error)

func (*Entity) GetIntPropertyValues

func (anEntity *Entity) GetIntPropertyValues(typeURI string) ([]int, error)

func (*Entity) GetReferenceValues

func (anEntity *Entity) GetReferenceValues(typeURI string) ([]string, error)

func (*Entity) GetStringPropertyValues

func (anEntity *Entity) GetStringPropertyValues(typeURI string) ([]string, error)

func (*Entity) SetID added in v0.7.1

func (anEntity *Entity) SetID(id string) *Entity

func (*Entity) SetProperty added in v0.7.1

func (anEntity *Entity) SetProperty(property string, value any) *Entity

func (*Entity) SetReference added in v0.7.1

func (anEntity *Entity) SetReference(reference string, value any) *Entity

type EntityCollection

type EntityCollection struct {
	Entities           []*Entity
	Continuation       *Continuation
	NamespaceManager   NamespaceManager
	OmitContextOnWrite bool
}

EntityCollection is a utility structure for collecting together a set of entities, namespace mappings and a continuation token

func NewEntityCollection

func NewEntityCollection(nsManager NamespaceManager) *EntityCollection

func (*EntityCollection) AddEntity

func (ec *EntityCollection) AddEntity(entity *Entity) error

AddEntity adds the given entity to the collection

func (*EntityCollection) AddEntityFromMap added in v0.7.5

func (ec *EntityCollection) AddEntityFromMap(data map[string]any) error

AddEntityFromMap adds an entity to the collection from a map The map should have the following structure (the keys are case sensitive):

{
  "id": "ns0:entity1",
  "deleted": false,
  "recorded": 1234567890,
  "props": {
    "ns0:property1": "value1"
  },
  "refs": {
    "ns0:reference1": "ns0:entity2"
  }

func (*EntityCollection) ExpandNamespacePrefixes added in v0.7.4

func (ec *EntityCollection) ExpandNamespacePrefixes() error

func (*EntityCollection) GetContinuationToken

func (ec *EntityCollection) GetContinuationToken() *Continuation

func (*EntityCollection) GetEntities

func (ec *EntityCollection) GetEntities() []*Entity

func (*EntityCollection) GetNamespaceManager

func (ec *EntityCollection) GetNamespaceManager() NamespaceManager

func (*EntityCollection) GetNamespaceMappings

func (ec *EntityCollection) GetNamespaceMappings() map[string]string

func (*EntityCollection) SetContinuationToken

func (ec *EntityCollection) SetContinuationToken(continuation *Continuation)

SetContinuationToken sets the continuation token on the EntityCollection

func (*EntityCollection) SetOmitContextOnWrite added in v0.7.6

func (ec *EntityCollection) SetOmitContextOnWrite(isOmitted bool)

SetOmitContextOnWrite sets the OmitContextOnWrite flag on the EntityCollection such that when writing the collection to Entity Graph JSON the context is omitted

func (*EntityCollection) WriteEntityGraphJSON

func (ec *EntityCollection) WriteEntityGraphJSON(writer io.Writer) error

func (*EntityCollection) WriteJSON_LD

func (ec *EntityCollection) WriteJSON_LD(writer io.Writer) error

type EntityParser

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

func NewEntityParser

func NewEntityParser(nsmanager NamespaceManager) *EntityParser

func (*EntityParser) GetIdentityValue

func (esp *EntityParser) GetIdentityValue(value string) (string, error)

func (*EntityParser) GetNamespaceManager

func (esp *EntityParser) GetNamespaceManager() NamespaceManager

func (*EntityParser) LoadEntityCollection

func (esp *EntityParser) LoadEntityCollection(reader io.Reader) (*EntityCollection, error)

func (*EntityParser) Parse

func (esp *EntityParser) Parse(reader io.Reader, emitEntity func(*Entity) error, emitContinuation func(*Continuation)) error

func (*EntityParser) WithCompressURIs

func (esp *EntityParser) WithCompressURIs() *EntityParser

func (*EntityParser) WithExpandURIs

func (esp *EntityParser) WithExpandURIs() *EntityParser

func (*EntityParser) WithLenientNamespaceChecks added in v0.7.3

func (esp *EntityParser) WithLenientNamespaceChecks() *EntityParser

func (*EntityParser) WithNoContext

func (esp *EntityParser) WithNoContext() *EntityParser

func (*EntityParser) WithParsedContextCallback added in v0.7.1

func (esp *EntityParser) WithParsedContextCallback(callback func(context *Context)) *EntityParser

type JsonLDWriter

type JsonLDWriter struct {
}

func (*JsonLDWriter) Write

func (jsonLDWriter *JsonLDWriter) Write(ec *EntityCollection, writer io.Writer) error

type JsonLdRef

type JsonLdRef struct {
	ID string `json:"@id"`
}

type NamespaceContext

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

func NewNamespaceContext

func NewNamespaceContext() *NamespaceContext

func (*NamespaceContext) AsContext

func (aContext *NamespaceContext) AsContext() *Context

func (*NamespaceContext) AssertPrefixedIdentifierFromURI added in v0.7.1

func (aContext *NamespaceContext) AssertPrefixedIdentifierFromURI(URI string) (string, error)

func (*NamespaceContext) GetFullURI

func (aContext *NamespaceContext) GetFullURI(value string) (string, error)

func (*NamespaceContext) GetNamespaceExpansionForPrefix

func (aContext *NamespaceContext) GetNamespaceExpansionForPrefix(prefix string) (string, error)

func (*NamespaceContext) GetNamespaceMappings

func (aContext *NamespaceContext) GetNamespaceMappings() map[string]string

implement get namespace mappings

func (*NamespaceContext) GetPrefixForExpansion

func (aContext *NamespaceContext) GetPrefixForExpansion(expansion string) (string, error)

func (*NamespaceContext) GetPrefixedIdentifier

func (aContext *NamespaceContext) GetPrefixedIdentifier(value string) (string, error)

implement get prefixed identifier

func (*NamespaceContext) IsFullUri

func (aContext *NamespaceContext) IsFullUri(value string) bool

func (*NamespaceContext) StorePrefixExpansionMapping

func (aContext *NamespaceContext) StorePrefixExpansionMapping(prefix string, expansion string)

type NamespaceManager

type NamespaceManager interface {
	GetNamespaceExpansionForPrefix(prefix string) (string, error)
	GetPrefixForExpansion(expansion string) (string, error)
	StorePrefixExpansionMapping(prefix string, expansion string)
	IsFullUri(value string) bool
	GetFullURI(value string) (string, error)
	GetPrefixedIdentifier(value string) (string, error)
	GetNamespaceMappings() map[string]string
	AssertPrefixedIdentifierFromURI(URI string) (string, error)
	AsContext() *Context
}

type Parser

type Parser interface {
	Parse(data io.Reader, entity func(*Entity) error, continuation func(*Continuation)) error
	LoadEntityCollection(reader io.Reader) (*EntityCollection, error)
	GetNamespaceManager() NamespaceManager
}

Jump to

Keyboard shortcuts

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