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

fogfish / swarm / 16299950188

15 Jul 2025 05:20PM UTC coverage: 62.628% (+0.09%) from 62.54%
16299950188

Pull #131

github

fogfish
helper function to cast bag to event
Pull Request #131: Unwrap `swarm.Msg[swarm.Event[M, T]]` for usability

185 of 271 new or added lines in 11 files covered. (68.27%)

1 existing line in 1 file now uncovered.

1473 of 2352 relevant lines covered (62.63%)

0.68 hits per line

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

0.0
/kernel/encoding/codec.go
1
//
2
// Copyright (C) 2021 - 2024 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the Apache License Version 2.0. See the LICENSE file for details.
6
// https://github.com/fogfish/swarm
7
//
8

9
package encoding
10

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

15
        "github.com/fogfish/curie/v2"
16
        "github.com/fogfish/golem/optics"
17
        "github.com/fogfish/guid/v2"
18
        "github.com/fogfish/swarm"
19
)
20

21
//------------------------------------------------------------------------------
22

23
// JSON encoding for typed objects
24
type Typed[T any] string
25

26
func (c Typed[T]) Category() string { return string(c) }
×
27

NEW
28
func (c Typed[T]) Encode(obj T) (swarm.Bag, error) {
×
NEW
29
        msg, err := json.Marshal(obj)
×
NEW
30
        if err != nil {
×
NEW
31
                return swarm.Bag{}, err
×
NEW
32
        }
×
33

NEW
34
        return swarm.Bag{
×
NEW
35
                Category: string(c),
×
NEW
36
                Object:   msg,
×
NEW
37
        }, nil
×
38
}
39

NEW
40
func (Typed[T]) Decode(bag swarm.Bag) (x T, err error) {
×
NEW
41
        err = json.Unmarshal(bag.Object, &x)
×
42
        return
×
43
}
×
44

45
// Create JSON codec for typed objects
46
func ForTyped[T any](category ...string) Typed[T] {
×
47
        return Typed[T](swarm.TypeOf[T](category...))
×
48
}
×
49

50
//------------------------------------------------------------------------------
51

52
// JSON encoding for events
53
type Event[M, T any] struct {
54
        realm curie.IRI
55
        agent curie.IRI
56
        cat   curie.IRI
57
        shape optics.Lens5[M, string, curie.IRI, curie.IRI, curie.IRI, time.Time]
58
}
59

60
func (c Event[M, T]) Category() string { return string(c.cat) }
×
61

NEW
62
func (c Event[M, T]) Encode(obj swarm.Event[M, T]) (swarm.Bag, error) {
×
63
        _, cat, rlm, agt, _ := c.shape.Get(obj.Meta)
×
64
        if cat == "" {
×
65
                cat = c.cat
×
66
        }
×
67

68
        if agt == "" {
×
69
                agt = c.agent
×
70
        }
×
71

72
        if rlm == "" {
×
73
                rlm = c.realm
×
74
        }
×
75

76
        c.shape.Put(obj.Meta, guid.G(guid.Clock).String(), cat, rlm, agt, time.Now())
×
NEW
77
        msg, err := json.Marshal(obj)
×
NEW
78
        if err != nil {
×
NEW
79
                return swarm.Bag{}, err
×
NEW
80
        }
×
81

NEW
82
        return swarm.Bag{
×
NEW
83
                Category: string(cat),
×
NEW
84
                Object:   msg,
×
NEW
85
        }, nil
×
86
}
87

NEW
88
func (c Event[M, T]) Decode(bag swarm.Bag) (swarm.Event[M, T], error) {
×
89
        var x swarm.Event[M, T]
×
NEW
90
        err := json.Unmarshal(bag.Object, &x)
×
91

×
92
        return x, err
×
93
}
×
94

95
// Creates JSON codec for events
96
func ForEvent[E swarm.Event[M, T], M, T any](realm, agent string, category ...string) Event[M, T] {
×
97
        return Event[M, T]{
×
98
                realm: curie.IRI(realm),
×
99
                agent: curie.IRI(agent),
×
100
                cat:   curie.IRI(swarm.TypeOf[T](category...)),
×
101
                shape: optics.ForShape5[M, string, curie.IRI, curie.IRI, curie.IRI, time.Time]("ID", "Type", "Realm", "Agent", "Created"),
×
102
        }
×
103
}
×
104

105
//------------------------------------------------------------------------------
106

107
// Identity encoding for bytes
108
type Bytes string
109

NEW
110
func (c Bytes) Category() string { return string(c) }
×
111

NEW
112
func (c Bytes) Encode(x []byte) (swarm.Bag, error) {
×
NEW
113
        return swarm.Bag{
×
NEW
114
                Category: string(c),
×
NEW
115
                Object:   x,
×
NEW
116
        }, nil
×
NEW
117
}
×
118

NEW
119
func (Bytes) Decode(bag swarm.Bag) ([]byte, error) {
×
NEW
120
        return bag.Object, nil
×
NEW
121
}
×
122

123
// Create bytes identity codec
124
func ForBytes(cat string) Bytes { return Bytes(cat) }
×
125

126
//------------------------------------------------------------------------------
127

128
// Base64 encoding for bytes, sent as JSON
129
type BytesJB64 string
130

131
type packet struct {
132
        Octets []byte `json:"p,omitempty"`
133
}
134

135
func (c BytesJB64) Category() string { return string(c) }
×
136

NEW
137
func (c BytesJB64) Encode(obj []byte) (swarm.Bag, error) {
×
NEW
138
        msg, err := json.Marshal(packet{Octets: obj})
×
NEW
139
        if err != nil {
×
NEW
140
                return swarm.Bag{}, err
×
NEW
141
        }
×
142

NEW
143
        return swarm.Bag{
×
NEW
144
                Category: string(c),
×
NEW
145
                Object:   msg,
×
NEW
146
        }, nil
×
147
}
148
func (BytesJB64) Decode(x []byte) ([]byte, error) {
×
149
        var pckt packet
×
150
        err := json.Unmarshal(x, &pckt)
×
151
        return pckt.Octets, err
×
152
}
×
153

154
// Creates bytes codec for Base64 encapsulated into Json "packat"
155
func ForBytesJB64(cat string) BytesJB64 { return BytesJB64(cat) }
×
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

© 2026 Coveralls, Inc