• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fogfish / gold / 17361052740

31 Aug 2025 06:49PM UTC coverage: 52.862%. First build
17361052740

push

github

web-flow
Initial release (#1)

* basic algebra for linked-data definition
* supports url, urn, iri

157 of 297 new or added lines in 7 files covered. (52.86%)

157 of 297 relevant lines covered (52.86%)

1.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/example/content.go
1
//
2
// Copyright (C) 2025 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/fogfish/gold
7
//
8

9
package main
10

11
import (
12
        "encoding/json"
13
        "fmt"
14

15
        "github.com/fogfish/gold"
16
)
17

18
//
19
// Example shows semantic modeling of blogging application
20
//
21
// Consider a simple linked data structure where users can create blogs and comment on them.
22
// Each comment can be anchored to another comment, allowing for threaded discussions.
23
//
24

25
// User Identity is IRI (e.g. user:alice)
26
type UserID = gold.IRI[User]
27

28
// Core domain data for User
29
type User struct {
30
        ID UserID `json:"id"`
31
}
32

33
// Projecting user's core domain into a storage is trivial.
34
// UserID is a "hash key" to be used as-is by the database.
35
type UserDB struct {
36
        HKey UserID `json:"hashkey"`
37
}
38

39
// Blog post Identity is IRI (e.g. blog:1234)
40
type BlogID = gold.IRI[Blog]
41

42
// Core domain for User carries blog ID and contains statement about its creator
43
type Blog struct {
44
        ID      BlogID `json:"id"`
45
        Creator UserID `json:"creator,omitempty"`
46
}
47

48
// Projecting blog's core domain into a storage requires two dimensions:
49
// 1. Hash key - a unique identifier for the user who created the blog
50
// 2. Sort key - a unique identifier for the blog post itself
51
//
52
// Given structure is equally applicable for both SQL and NoSQL databases.
53
type BlogDB struct {
54
        HKey UserID `json:"hashkey"`
55
        SKey BlogID `json:"sortkey"`
56
}
57

58
// Comment Identity is IRI (e.g. comment:1)
59
type CommentID = gold.IRI[Comment]
60

61
// Core domain for comment contains of compound identity. Firstly it has a unique key,
62
// then it is about particular blog post and can be anchored to another comment.
63
type Comment struct {
64
        ID      CommentID `json:"id"`
65
        Creator UserID    `json:"creator,omitempty"`
66
        About   BlogID    `json:"about,omitempty"`
67
        Anchor  CommentID `json:"anchor,omitempty"`
68
}
69

70
// Projecting comment's core domain into a NoSQL storage non-trivial
71
// in comparision with SQL databases. Comment consists of compound key
72
//
73
//        ⟨user, blog, comment⟩
74
//
75
// Imply type allows us to define a hash key as a combination of user and blog.
76
type CommentDB struct {
77
        HKey HashKey   `json:"hashkey"`
78
        SKey CommentID `json:"sortkey"`
79
}
80

81
type HashKey gold.Imply[User, Blog]
82

83
var hashkey = gold.HashKey[HashKey, User, Blog]()
84

NEW
85
func (k HashKey) MarshalJSON() ([]byte, error) {
×
NEW
86
        return gold.EncodeJSON(hashkey.Encode(&k))
×
NEW
87
}
×
88

NEW
89
func main() {
×
NEW
90
        alice := User{ID: UserID("alice").Norm()}
×
NEW
91
        print("==> user signed up", alice)
×
NEW
92

×
NEW
93
        udb := UserDB{HKey: alice.ID}
×
NEW
94
        print("==> user is written to db", udb)
×
NEW
95

×
NEW
96
        blog := Blog{
×
NEW
97
                ID:      BlogID("1234").Norm(),
×
NEW
98
                Creator: alice.ID,
×
NEW
99
        }
×
NEW
100
        print("==> user created a blog", blog)
×
NEW
101

×
NEW
102
        bdb := BlogDB{
×
NEW
103
                HKey: alice.ID,
×
NEW
104
                SKey: blog.ID,
×
NEW
105
        }
×
NEW
106
        print("==> blog is written to db", bdb)
×
NEW
107

×
NEW
108
        bob := User{ID: UserID("bob").Norm()}
×
NEW
109
        comment := Comment{
×
NEW
110
                ID:      CommentID("1").Norm(),
×
NEW
111
                Creator: bob.ID,
×
NEW
112
                About:   blog.ID,
×
NEW
113
        }
×
NEW
114
        print("==> user created a comment", comment)
×
NEW
115

×
NEW
116
        cdb := CommentDB{
×
NEW
117
                HKey: HashKey(gold.ImplyFrom(alice.ID, blog.ID)),
×
NEW
118
                SKey: comment.ID,
×
NEW
119
        }
×
NEW
120
        print("==> comment is written to db", cdb)
×
NEW
121
}
×
122

NEW
123
func print(msg string, obj any) {
×
NEW
124
        b, _ := json.MarshalIndent(obj, "| ", "  ")
×
NEW
125
        fmt.Println(msg)
×
NEW
126
        fmt.Println("| " + string(b))
×
NEW
127
        fmt.Println()
×
NEW
128
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc