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

fogfish / swarm / 16342700096

17 Jul 2025 10:28AM UTC coverage: 62.696% (+0.2%) from 62.54%
16342700096

Pull #131

github

fogfish
fix panic if metadata is undefined
Pull Request #131: Unwrap `swarm.Msg[swarm.Event[M, T]]` for usability

188 of 278 new or added lines in 11 files covered. (67.63%)

1 existing line in 1 file now uncovered.

1479 of 2359 relevant lines covered (62.7%)

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) {
×
NEW
63
        if obj.Meta == nil {
×
NEW
64
                obj.Meta = new(M)
×
NEW
65
        }
×
66

67
        _, cat, rlm, agt, _ := c.shape.Get(obj.Meta)
×
68
        if cat == "" {
×
69
                cat = c.cat
×
70
        }
×
71

72
        if agt == "" {
×
73
                agt = c.agent
×
74
        }
×
75

76
        if rlm == "" {
×
77
                rlm = c.realm
×
78
        }
×
79

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

NEW
86
        return swarm.Bag{
×
NEW
87
                Category: string(cat),
×
NEW
88
                Object:   msg,
×
NEW
89
        }, nil
×
90
}
91

NEW
92
func (c Event[M, T]) Decode(bag swarm.Bag) (swarm.Event[M, T], error) {
×
93
        var x swarm.Event[M, T]
×
NEW
94
        err := json.Unmarshal(bag.Object, &x)
×
95

×
96
        return x, err
×
97
}
×
98

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

109
//------------------------------------------------------------------------------
110

111
// Identity encoding for bytes
112
type Bytes string
113

NEW
114
func (c Bytes) Category() string { return string(c) }
×
115

NEW
116
func (c Bytes) Encode(x []byte) (swarm.Bag, error) {
×
NEW
117
        return swarm.Bag{
×
NEW
118
                Category: string(c),
×
NEW
119
                Object:   x,
×
NEW
120
        }, nil
×
NEW
121
}
×
122

NEW
123
func (Bytes) Decode(bag swarm.Bag) ([]byte, error) {
×
NEW
124
        return bag.Object, nil
×
NEW
125
}
×
126

127
// Create bytes identity codec
128
func ForBytes(cat string) Bytes { return Bytes(cat) }
×
129

130
//------------------------------------------------------------------------------
131

132
// Base64 encoding for bytes, sent as JSON
133
type BytesJB64 string
134

135
type packet struct {
136
        Octets []byte `json:"p,omitempty"`
137
}
138

139
func (c BytesJB64) Category() string { return string(c) }
×
140

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

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

158
// Creates bytes codec for Base64 encapsulated into Json "packat"
159
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