core

package
v0.0.0-...-a3fcc2a Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package core provides the fundamental data structures for XDB, a tuple-based database abstraction.

XDB Data Model:

XDB models data as a tree of Namespaces, Schemas, Records, and Tuples:

┌─────────────────────────────────┐
│            Namespace            │
└────────────────┬────────────────┘
                 ↓
┌─────────────────────────────────┐
│             Schema              │
└────────────────┬────────────────┘
                 ↓
┌─────────────────────────────────┐
│             Record              │
└────────────────┬────────────────┘
                 ↓
┌─────────────────────────────────┐
│             Tuple               │
├─────────────────────────────────┤
│   ID | Attr | Value | Options   │
└─────────────────────────────────┘

Core Types:

Tuple is the fundamental building block in XDB. Each tuple contains:

  • Path: A URI identifying the record (NS + SCHEMA + ID)
  • Attr: An attribute name (e.g., "name", "profile.email")
  • Value: A typed value containing the actual data

Record is a group of tuples sharing the same path. Records are similar to objects, structs, or rows in a database. Records typically represent a single entity or object of domain data.

Schema defines the structure of records and groups them together. Schemas can be "strict" or "flexible" and are uniquely identified by name within a namespace.

Namespace (NS) groups one or more Schemas. Namespaces are typically used to organize schemas by domain, application, or tenant.

Schema is a definition of your domain entities and their relationships. Schemas can be "strict" or "flexible". Strict schemas enforce a predefined structure on the data, while flexible schemas allow for arbitrary data.

URI provides unique references to namespaces, schemas, records, and attributes. The general format is:

xdb:// NS [ / SCHEMA ] [ / ID ] [ #ATTRIBUTE ]

Examples:

Namespace:  xdb://com.example
Schema:     xdb://com.example/posts
Record:     xdb://com.example/posts/123-456-789
Attribute:  xdb://com.example/posts/123-456-789#author.id

NS identifies the namespace. SCHEMA is the schema name. ID is the record identifier ATTRIBUTE is a specific attribute of a record (supports nesting like "profile.email"). Path: NS, SCHEMA, and ID combined uniquely identify a record (URI without xdb://).

Value is a typed container supporting Go's basic types plus arrays and maps. Values provide type-safe casting methods and automatic type inference.

Example usage:

// Create tuples using the builder pattern
title := New().
	NS("com.example").
	Schema("posts").
	ID("123-456-789").
	MustTuple("title", "Hello World")

author := New().
	NS("com.example").
	Schema("posts").
	ID("123-456-789").
	MustTuple("author.id", "user-001")

// Get tuple URI
uri := title.URI() // xdb://com.example/posts/123-456-789#title

// Create records with multiple tuples
record := NewRecord("com.example", "posts", "123-456-789").
	Set("title", "Hello World").
	Set("author", "user-001")

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	TypeUnknown  = newType(TIDUnknown)
	TypeBool     = newType(TIDBoolean)
	TypeInt      = newType(TIDInteger)
	TypeUnsigned = newType(TIDUnsigned)
	TypeFloat    = newType(TIDFloat)
	TypeString   = newType(TIDString)
	TypeBytes    = newType(TIDBytes)
	TypeTime     = newType(TIDTime)
)
View Source
var (
	// ErrUnsupportedValue is returned when a value is not supported.
	ErrUnsupportedValue = errors.New("[xdb/core] unsupported value")
	// ErrTypeMismatch is returned when a value is not of the expected type.
	ErrTypeMismatch = errors.New("[xdb/core] type mismatch")
)
View Source
var ErrCastFailed = errors.New("[xdb/core] cast failed")

ErrCastFailed is returned when a value cannot be converted to the desired type.

View Source
var ErrInvalidAttr = errors.New("[xdb/core] invalid Attr")

ErrInvalidAttr is returned when an invalid Attr is encountered.

View Source
var ErrInvalidID = errors.New("[xdb/core] invalid ID")

ErrInvalidID is returned when an invalid ID is encountered.

View Source
var ErrInvalidNS = errors.New("[xdb/core] invalid NS")

ErrInvalidNS is returned when an invalid NS is encountered.

View Source
var ErrInvalidSchema = errors.New("[xdb/core] invalid Schema")

ErrInvalidSchema is returned when an invalid Schema is encountered.

View Source
var ErrInvalidURI = errors.New("[xdb/core] invalid URI")

ErrInvalidURI is returned when an invalid URI is encountered.

View Source
var ErrUnknownType = errors.New("[xdb/core] unknown type")

ErrUnknownType is returned when an unknown type is encountered.

Functions

This section is empty.

Types

type Attr

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

Attr is a name of an attribute.

func NewAttr

func NewAttr(raw string) *Attr

NewAttr creates a new attribute name. Panics if the attribute name is invalid (contains characters outside a-zA-Z0-9._/-).

func ParseAttr

func ParseAttr(raw string) (*Attr, error)

ParseAttr parses a string into an Attr. Returns ErrInvalidAttr if the Attr is invalid.

func (*Attr) Equals

func (a *Attr) Equals(other *Attr) bool

Equals returns true if this Attr is equal to the other Attr.

func (*Attr) String

func (a *Attr) String() string

String returns the Attr.

type Builder

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

Builder provides a fluent API for constructing URIs, Records, and Tuples. Use New() to create a new Builder, then chain methods to set components.

func New

func New() *Builder

New creates a new URI Builder. The builder starts empty and each component must be set via chaining methods.

func (*Builder) Attr

func (b *Builder) Attr(attr string) *Builder

Attr sets the attribute component for the URI.

func (*Builder) ID

func (b *Builder) ID(id string) *Builder

ID sets the record ID component for the URI.

func (*Builder) MustRecord

func (b *Builder) MustRecord() *Record

MustRecord is like Record but panics if any component is invalid.

func (*Builder) MustTuple

func (b *Builder) MustTuple(attr string, value any) *Tuple

MustTuple is like Tuple but panics if any component is invalid.

func (*Builder) MustURI

func (b *Builder) MustURI() *URI

MustURI is like URI but panics if any component is invalid.

func (*Builder) NS

func (b *Builder) NS(ns string) *Builder

NS sets the namespace component for the URI.

func (*Builder) Record

func (b *Builder) Record() (*Record, error)

Record builds and returns a Record from the configured components. Returns an error if any component is invalid.

func (*Builder) Schema

func (b *Builder) Schema(schema string) *Builder

Schema sets the schema component for the URI.

func (*Builder) Tuple

func (b *Builder) Tuple(attr string, value any) (*Tuple, error)

Tuple builds and returns a Tuple from the configured components and a given attribute/value pair. Returns an error if any component is invalid.

func (*Builder) URI

func (b *Builder) URI() (*URI, error)

URI builds and returns a URI from the configured components. Returns an error if any component is invalid.

type ID

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

ID is an unique identifier for a record.

func NewID

func NewID(raw string) *ID

NewID creates a new ID from a string. Panics if the ID is invalid (contains characters outside a-zA-Z0-9._/-).

func ParseID

func ParseID(raw string) (*ID, error)

ParseID parses a string into an ID. Returns ErrInvalidID if the ID is invalid.

func (*ID) Equals

func (id *ID) Equals(other *ID) bool

Equals returns true if this ID is equal to the other ID.

func (*ID) String

func (id *ID) String() string

String returns the ID.

type NS

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

NS identifies the namespace.

func NewNS

func NewNS(raw string) *NS

NewNS creates a new namespace identifier. Panics if the namespace is invalid (contains characters outside a-zA-Z0-9._/-).

func ParseNS

func ParseNS(raw string) (*NS, error)

ParseNS parses a string into an NS. Returns ErrInvalidNS if the NS is invalid.

func (*NS) Equals

func (n *NS) Equals(other *NS) bool

Equals returns true if this NS is equal to the other NS.

func (*NS) String

func (n *NS) String() string

String returns the NS as a string.

type Record

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

Record is a group of tuples that share the same path (NS + SCHEMA + ID). Records are mutable and thread-safe, similar to database rows.

Example
package main

import (
	"fmt"

	"github.com/xdb-dev/xdb/core"
)

func main() {
	record := core.New().NS("com.example").
		Schema("users").
		ID("123").
		MustRecord()

	record.Set("name", "John Doe").
		Set("age", 25).
		Set("bio.interests", []string{"reading", "traveling", "coding"})

	// Reading attributes
	fmt.Println(record.Get("name").ToString())
	fmt.Println(record.Get("age").ToInt())
	fmt.Println(record.Get("bio.interests").ToStringArray())
	fmt.Println(record.URI())

}
Output:

John Doe
25
[reading traveling coding]
xdb://com.example/users/123

func NewRecord

func NewRecord(ns, schema, id string) *Record

NewRecord creates a new Record.

func (*Record) Get

func (r *Record) Get(attr string) *Tuple

Get retrieves the tuple for the given attribute path. Returns nil if no tuple exists for the specified attribute.

func (*Record) GoString

func (r *Record) GoString() string

GoString returns Go syntax of the Record.

func (*Record) ID

func (r *Record) ID() *ID

ID returns the ID of the record.

func (*Record) IsEmpty

func (r *Record) IsEmpty() bool

IsEmpty returns true if the Record has no tuples.

func (*Record) NS

func (r *Record) NS() *NS

NS returns the namespace of the record.

func (*Record) Schema

func (r *Record) Schema() *Schema

Schema returns the schema of the record.

func (*Record) SchemaURI

func (r *Record) SchemaURI() *URI

SchemaURI returns the schema URI of the record.

func (*Record) Set

func (r *Record) Set(attr string, value any) *Record

Set adds or updates a tuple in the Record with the given attribute and value. If a tuple with the same attribute already exists, it will be replaced.

func (*Record) Tuples

func (r *Record) Tuples() []*Tuple

Tuples returns all tuples contained in this Record. The returned slice is a copy and safe to modify.

func (*Record) URI

func (r *Record) URI() *URI

URI returns a URI that references this Record.

type Schema

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

Schema is a name of a schema.

func NewSchema

func NewSchema(raw string) *Schema

NewSchema creates a new Schema. Panics if the Schema name is invalid (contains characters outside a-zA-Z0-9._/-).

func ParseSchema

func ParseSchema(raw string) (*Schema, error)

ParseSchema parses a string into a Schema. Returns ErrInvalidSchema if the Schema is invalid.

func (*Schema) Equals

func (s *Schema) Equals(other *Schema) bool

Equals returns true if this Schema is equal to the other Schema.

func (*Schema) String

func (s *Schema) String() string

String returns the Schema.

type TID

type TID int

TID represents the type of a value.

const (
	TIDUnknown TID = iota
	TIDBoolean
	TIDInteger
	TIDUnsigned
	TIDFloat
	TIDString
	TIDBytes
	TIDTime
	TIDArray
	TIDMap
)

All supported types.

func ParseType

func ParseType(name string) (TID, error)

ParseType parses a type name into a TID.

func (TID) String

func (t TID) String() string

String returns the name of the type.

type Tuple

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

Tuple is the core data structure of XDB.

Tuple is an immutable data structure containing: - Path: Unique identifier for the record. - Attr: Name of the attribute. - Value: Value of the attribute.

Example
package main

import (
	"fmt"

	"github.com/xdb-dev/xdb/core"
)

func main() {
	tuple := core.New().NS("com.example").
		Schema("users").
		ID("123").
		MustTuple("name", "John Doe")

	fmt.Println(tuple.NS())
	fmt.Println(tuple.Schema())
	fmt.Println(tuple.ID())
	fmt.Println(tuple.Attr())
	fmt.Println(tuple.Value().ToString())
	fmt.Println(tuple.URI())

}
Output:

com.example
users
123
name
John Doe
xdb://com.example/users/123#name

func NewTuple

func NewTuple(path, attr string, value any) *Tuple

NewTuple creates a new Tuple.

func (*Tuple) Attr

func (t *Tuple) Attr() *Attr

Attr returns the attribute of the tuple.

func (*Tuple) GoString

func (t *Tuple) GoString() string

GoString returns Go syntax of the tuple.

func (*Tuple) ID

func (t *Tuple) ID() *ID

ID returns the record ID of the tuple.

func (*Tuple) NS

func (t *Tuple) NS() *NS

NS returns the namespace of the tuple.

func (*Tuple) Path

func (t *Tuple) Path() *URI

Path returns the URI that references the record.

func (*Tuple) Schema

func (t *Tuple) Schema() *Schema

Schema returns the schema of the tuple.

func (*Tuple) SchemaURI

func (t *Tuple) SchemaURI() *URI

SchemaURI returns the schema URI of the tuple.

func (*Tuple) ToBool

func (t *Tuple) ToBool() bool

ToBool returns the tuple's value as a bool.

func (*Tuple) ToBoolArray

func (t *Tuple) ToBoolArray() []bool

ToBoolArray returns the tuple's value as a []bool.

func (*Tuple) ToBytes

func (t *Tuple) ToBytes() []byte

ToBytes returns the tuple's value as a []byte.

func (*Tuple) ToBytesArray

func (t *Tuple) ToBytesArray() [][]byte

ToBytesArray returns the tuple's value as a [][]byte.

func (*Tuple) ToFloat

func (t *Tuple) ToFloat() float64

ToFloat returns the tuple's value as a float64.

func (*Tuple) ToFloatArray

func (t *Tuple) ToFloatArray() []float64

ToFloatArray returns the tuple's value as a []float64.

func (*Tuple) ToInt

func (t *Tuple) ToInt() int64

ToInt returns the tuple's value as an int64.

func (*Tuple) ToIntArray

func (t *Tuple) ToIntArray() []int64

ToIntArray returns the tuple's value as a []int64.

func (*Tuple) ToString

func (t *Tuple) ToString() string

ToString returns the tuple's value as a string.

func (*Tuple) ToStringArray

func (t *Tuple) ToStringArray() []string

ToStringArray returns the tuple's value as a []string.

func (*Tuple) ToTime

func (t *Tuple) ToTime() time.Time

ToTime returns the tuple's value as a time.Time.

func (*Tuple) ToTimeArray

func (t *Tuple) ToTimeArray() []time.Time

ToTimeArray returns the tuple's value as a []time.Time.

func (*Tuple) ToUint

func (t *Tuple) ToUint() uint64

ToUint returns the tuple's value as a uint64.

func (*Tuple) ToUintArray

func (t *Tuple) ToUintArray() []uint64

ToUintArray returns the tuple's value as a []uint64.

func (*Tuple) URI

func (t *Tuple) URI() *URI

URI returns a URI that references the tuple.

func (*Tuple) Value

func (t *Tuple) Value() *Value

Value returns the value of the tuple.

type Type

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

Type represents a value type in XDB, including scalar, array, and map types.

func NewArrayType

func NewArrayType(valueTypeID TID) Type

NewArrayType returns a new array Type with the given value TID.

func NewMapType

func NewMapType(keyTypeID, valueTypeID TID) Type

NewMapType returns a new map Type with the given key and value TIDs.

func (Type) Equals

func (t Type) Equals(other Type) bool

Equals returns true if this Type is equal to the other Type.

func (Type) ID

func (t Type) ID() TID

ID returns the TID of the Type.

func (Type) KeyTypeID

func (t Type) KeyTypeID() TID

KeyTypeID returns the key TID for map types.

func (Type) String

func (t Type) String() string

String returns the name of the Type.

func (Type) ValueTypeID

func (t Type) ValueTypeID() TID

ValueTypeID returns the value TID for array and map types.

type URI

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

URI is a reference to XDB data.

The general format is:

xdb:// NS [ / SCHEMA ] [ / ID ] [ #ATTRIBUTE ]

NS identifies the namespace. SCHEMA is the schema name. ID is the record identifier. ATTRIBUTE is a specific attribute of a record.

func MustParseURI

func MustParseURI(uri string) *URI

MustParseURI is like ParseURI but panics if the URI is invalid.

func ParsePath

func ParsePath(path string) (*URI, error)

ParsePath parses a path string into a URI struct. The path format is: NS[/SCHEMA][/ID][#ATTRIBUTE].

func ParseURI

func ParseURI(uri string) (*URI, error)

ParseURI parses a URI string into a URI struct. The URI format is: xdb://NS[/SCHEMA][/ID][#ATTRIBUTE]

func (*URI) Attr

func (u *URI) Attr() *Attr

Attr returns the attribute part of the URI.

func (*URI) Equals

func (u *URI) Equals(other *URI) bool

Equals returns true if this URI is equal to the other URI.

func (*URI) ID

func (u *URI) ID() *ID

ID returns the ID part of the URI.

func (*URI) MarshalJSON

func (u *URI) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*URI) NS

func (u *URI) NS() *NS

NS returns the namespace part of the URI.

func (*URI) Path

func (u *URI) Path() string

Path returns the URI without the scheme.

func (*URI) Schema

func (u *URI) Schema() *Schema

Schema returns the schema part of the URI.

func (*URI) String

func (u *URI) String() string

String returns the URI as a string.

func (*URI) UnmarshalJSON

func (u *URI) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Value

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

Value represents an attribute value using a tagged union. A zero Value is considered a nil value.

func NewSafeValue

func NewSafeValue(input any) (*Value, error)

NewSafeValue creates a new value. Returns an error if the value is not supported.

func NewValue

func NewValue(input any) *Value

NewValue creates a new value. Panics if the value is not supported.

func (*Value) GoString

func (v *Value) GoString() string

GoString returns Go syntax of the value.

func (*Value) IsNil

func (v *Value) IsNil() bool

IsNil returns true if the value is nil (a zero Value).

func (*Value) String

func (v *Value) String() string

String returns a string representation of the value.

func (*Value) ToBool

func (v *Value) ToBool() bool

ToBool returns the value as a bool.

func (*Value) ToBoolArray

func (v *Value) ToBoolArray() []bool

ToBoolArray returns the value as a []bool.

func (*Value) ToBytes

func (v *Value) ToBytes() []byte

ToBytes returns the value as a []byte.

func (*Value) ToBytesArray

func (v *Value) ToBytesArray() [][]byte

ToBytesArray returns the value as a [][]byte.

func (*Value) ToFloat

func (v *Value) ToFloat() float64

ToFloat returns the value as a float64.

func (*Value) ToFloatArray

func (v *Value) ToFloatArray() []float64

ToFloatArray returns the value as a []float64.

func (*Value) ToInt

func (v *Value) ToInt() int64

ToInt returns the value as an int64.

func (*Value) ToIntArray

func (v *Value) ToIntArray() []int64

ToIntArray returns the value as a []int64.

func (*Value) ToString

func (v *Value) ToString() string

ToString returns the value as a string.

func (*Value) ToStringArray

func (v *Value) ToStringArray() []string

ToStringArray returns the value as a []string.

func (*Value) ToTime

func (v *Value) ToTime() time.Time

ToTime returns the value as a time.Time.

func (*Value) ToTimeArray

func (v *Value) ToTimeArray() []time.Time

ToTimeArray returns the value as a []time.Time.

func (*Value) ToUint

func (v *Value) ToUint() uint64

ToUint returns the value as a uint64.

func (*Value) ToUintArray

func (v *Value) ToUintArray() []uint64

ToUintArray returns the value as a []uint64.

func (*Value) Type

func (v *Value) Type() Type

Type returns the type of the value.

func (*Value) Unwrap

func (v *Value) Unwrap() any

Unwrap returns the raw data of the value.

Jump to

Keyboard shortcuts

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