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

fogfish / swarm / 16289476399

15 Jul 2025 09:25AM UTC coverage: 61.581% (-1.0%) from 62.54%
16289476399

Pull #131

github

fogfish
improve encoding obj -> bag, giving fine grain control to application about dynamic routing
Pull Request #131: Unwrap `swarm.Msg[swarm.Event[M, T]]` for usability

12 of 49 new or added lines in 4 files covered. (24.49%)

1 existing line in 1 file now uncovered.

1340 of 2176 relevant lines covered (61.58%)

0.66 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

40
func (Typed[T]) Decode(b []byte) (x T, err error) {
×
41
        err = json.Unmarshal(b, &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

88
func (c Event[M, T]) Decode(b []byte) (swarm.Event[M, T], error) {
×
89
        var x swarm.Event[M, T]
×
90
        err := json.Unmarshal(b, &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

UNCOV
119
func (Bytes) Decode(x []byte) ([]byte, error) { return x, nil }
×
120

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

124
//------------------------------------------------------------------------------
125

126
// Base64 encoding for bytes, sent as JSON
127
type BytesJB64 string
128

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

133
func (c BytesJB64) Category() string { return string(c) }
×
134

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

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

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