Documentation
¶
Overview ¶
Package gold provides a typesafe Linked Data support for Golang. It defines algebra for declaring identity and hierarchical relationships between data types.
Index ¶
- func DecodeJSON(b []byte) (string, error)
- func EncodeJSON(s string, err error) ([]byte, error)
- func Register[T any](schema string) string
- func Schema[T any]() string
- func Schemas() map[string]string
- type Codec
- type Decoder
- type Encoder
- type IRI
- func (iri IRI[T]) IsEmpty() bool
- func (iri *IRI[T]) IsValid() bool
- func (iri IRI[T]) MarshalJSON() ([]byte, error)
- func (iri IRI[T]) MarshalText() ([]byte, error)
- func (iri IRI[T]) Norm() IRI[T]
- func (iri IRI[T]) Reference() string
- func (iri *IRI[T]) UnmarshalJSON(b []byte) (err error)
- func (iri *IRI[T]) UnmarshalText(b []byte) error
- func (iri *IRI[T]) Validate() (err error)
- type Imply
- type URN
- func (urn *URN[N, T]) FromIRI(iri IRI[T])
- func (urn URN[N, T]) IsEmpty() bool
- func (urn *URN[N, T]) IsValid() bool
- func (urn URN[N, T]) Join(other URN[N, T]) URN[N, T]
- func (urn URN[N, T]) MarshalJSON() ([]byte, error)
- func (urn URN[N, T]) MarshalText() ([]byte, error)
- func (urn URN[N, T]) Norm() URN[N, T]
- func (urn URN[N, T]) Reference() string
- func (urn URN[N, T]) Split() (URN[N, T], URN[N, T])
- func (urn URN[N, T]) ToIRI() IRI[T]
- func (urn *URN[N, T]) UnmarshalJSON(b []byte) (err error)
- func (urn *URN[N, T]) UnmarshalText(b []byte) error
- func (urn *URN[N, T]) Validate() (err error)
- type Url1
- type Url2
- type Url3
- type Url4
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSON ¶
Types ¶
type IRI ¶
Compact IRI (CURIE) is universal identifier for a resource, defined as
{schema}:{id}
where schema is a namespace prefix and id is a local identifier. The schema is always derived from the type of the resource T, making it safe at compiletime.
It is recommended to derive schema from the class itself:
type MyClass struct {
ID gold.IRI[MyClass] `json:"id"`
}
IRIs are not hierarchical in the linked-data sense. They are flat, typed identifiers structured as {schema}:{id}.
func Any ¶ added in v0.0.3
Any returns the schema-only IRI "{schema}:", used as a begins_with prefix to select every identity of class T.
gold.Any[User]() // "user:"
func AsIRI ¶
IRI parser, converts a string into a compact IRI type. Input string is expected to be a compact IRI of the form {schema}:{id}. It returns an error if the schema prefix is different than one associated with type T.
func ToIRI ¶
IRI type constructor, creates a new IRI from the string. It returns a valid compact IRI annotated with a schema.
If input string is compact IRI of other kind, it is still annotated with a schema.
func (IRI[T]) MarshalJSON ¶
Encodes IRI into JSON as a CURIE string.
func (IRI[T]) MarshalText ¶ added in v0.0.3
Encodes IRI as text. Empty IRI is encoded as an empty string, otherwise the value must carry the schema prefix of the type T. It is strict on write, a forgotten Norm() fails instead of silently persisting an invalid identity.
func (IRI[T]) Norm ¶
Normalize IRI to a compact IRI type. Use it as constructor for IRI types aliases.
type MyID = gold.IRI[MyClass]
var id = MyID("foo").Norm()
func (IRI[T]) Reference ¶
Reference returns a local identifier of the IRI, without schema prefix. It returns an empty string for the schema-only IRI (see Any).
func (*IRI[T]) UnmarshalJSON ¶
Decodes IRI from JSON as a CURIE string.
func (*IRI[T]) UnmarshalText ¶ added in v0.0.3
Decodes IRI from text. An empty string is decoded into an empty IRI, otherwise the value is validated against the schema of the type T.
type Imply ¶
IRIs are not hierarchical in the linked-data sense. They are flat, typed identifiers structured as {schema}:{id}. IRI assumes hierarchical identifiers but they are not hierarchical in this context {id} is a "flat" identity of the class. Conveniently, it is possible to use IRIs as hierarchical identifiers but it is not reflected in the type system and should be managed by the application itself.
Relations between concepts are not implied by IRI structure itself. Instead, They are modeled explicitly as product of IRIs or using predicate statements:
image:Selfie schema:creator user:Alice
Application models predicate statements as attributes of the IRI type
type Image struct {
ID gold.IRI[Image] `json:"id"`
Creator gold.IRI[User] `json:"creator"`
}
Statements are not flexible enough to model convolution of statements into composite keys, which are essential for persisting linked-data.
Generic Imply type is used to explicitly model relationships between two types A and B as composite structure.
user:Alice/schema:creator/image:Selfie type Creator = gold.Imply[Image, User]
Imply is a text type, it is serialised as a string "{Domain}|{Range}" (e.g. "user:alice|image:selfie"), including JSON and DynamoDB attributevalue codecs. Use a type alias to keep the methods. A defined type drops them and requires explicit forwarding of MarshalText and UnmarshalText.
func (Imply[A, B]) MarshalText ¶ added in v0.0.3
Encodes Imply as text "{Domain}|{Range}".
empty, empty → ""
set, empty → "{Domain}|"
set, set → "{Domain}|{Range}"
Empty domain with non-empty range, domain with "|" and IRIs without the schema prefix are errors.
func (Imply[A, B]) String ¶ added in v0.0.3
String returns text form of Imply, an invalid value yields an empty string.
func (*Imply[A, B]) UnmarshalText ¶ added in v0.0.3
Decodes Imply from text "{Domain}|{Range}", splitting on the first separator.
type URN ¶
Uniform Resource Name (URN) is a type-safe identifier for hierarchical identity, it is defined as
urn:{namespace}:{schema}:{id⁰}:{id¹}:...:{idⁿ}
where namespace is a unique identifier for the application, schema is a type-safe identifier for the resource, and id is a local identifier.
URNs supports convenient hierarchical identity. It is not reflected in the type system and should be managed by the application itself.
func AsURN ¶
URN parser, converts a string into a URN type. Input string is expected to be a compact URN of the form urn:{namespace}:{schema}. It returns an error if the schema prefix is different than one associated with type T.
func ToURN ¶
URN type constructor, creates a new URN from the string. It returns a valid compact URN annotated with a urn:{namespace}:{schema}.
If input segment is compact URN of other kind, it panic.
func (URN[N, T]) Join ¶
Join combines two URNs into one, where the second URN is appended to the first.
func (URN[N, T]) MarshalJSON ¶
Encodes IRI into JSON as a CURIE string.
func (URN[N, T]) MarshalText ¶ added in v0.0.3
Encodes URN as text.
func (URN[N, T]) Norm ¶
Normalize URN to a URN type. Use it as constructor for URN types aliases.
type MyID = gold.URN[MyNamespace, MyClass]
var id = MyID("foo").Norm()
func (URN[N, T]) Reference ¶
Reference returns a local identifier of the URN, without schema prefix.
func (URN[N, T]) Split ¶
Split splits URN into two parts: base and reference.
urn:{namespace}:{schema}:{id⁰}:{id¹}:...:{idⁿ} ⇒
urn:{namespace}:{schema}:{id⁰}:{id¹}:...:{idⁿ⁻¹}
urn:{namespace}:{schema}:{idⁿ}
func (*URN[N, T]) UnmarshalJSON ¶
Decodes IRI from JSON as a CURIE string.
func (*URN[N, T]) UnmarshalText ¶ added in v0.0.3
Decodes URN from text. An empty string is decoded into the zero value, otherwise the value is validated against the schema of the type N, T.
type Url1 ¶
Url1 is a type-safe URL template with one parameter. T is the target string type, A is the type of the parameter.
Example:
type UserUrl = gold.Url1[string, UserID]
const UserProfile = UserUrl("/users/%s")
url := UserProfile.Build(userID)
type Url2 ¶
Url2 is a type-safe URL template with two parameters. T is the target string type, A and B are the types of the parameters.
Example:
type StoryUrl = gold.Url2[string, AccountID, StoryID]
const CatalogStoryUrl = StoryUrl("/catalog/authors/%s/stories/%s")
url := CatalogStoryUrl.Build(accountID, storyID)
type Url3 ¶
Url3 is a type-safe URL template with three parameters. T is the target string type, A, B, and C are the types of the parameters.
Example:
type CommentUrl = gold.Url3[string, AccountID, StoryID, CommentID]
const StoryCommentUrl = CommentUrl("/catalog/authors/%s/stories/%s/comments/%s")
url := StoryCommentUrl.Build(accountID, storyID, commentID)
type Url4 ¶
Url4 is a type-safe URL template with four parameters. T is the target string type, A, B, C, and D are the types of the parameters.
Example:
type ThreadUrl = gold.Url4[string, AccountID, StoryID, CommentID, ThreadID]
const CommentThreadUrl = ThreadUrl("/catalog/authors/%s/stories/%s/comments/%s/threads/%s")
url := CommentThreadUrl.Build(accountID, storyID, commentID, threadID)
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
testa
Package testa declares a type that shares its package-independent name with a type in a sibling package.
|
Package testa declares a type that shares its package-independent name with a type in a sibling package. |
|
testb
Package testb declares a type that shares its package-independent name with a type in a sibling package.
|
Package testb declares a type that shares its package-independent name with a type in a sibling package. |