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

fogfish / swarm / 16241430889

12 Jul 2025 07:43PM UTC coverage: 59.549% (+1.2%) from 58.352%
16241430889

push

github

web-flow
swarm kernel v0.23.0 (#126)

* swarm kernel v0.23.0
* preemptive emitter loop without loss of emission
* improve retry and timeout handling in kernel
* re:enable configurable backoff strategy
* refactor kernel api for better developer experience
* update unit tests with centralised mock
* broadcaster utility

182 of 239 new or added lines in 11 files covered. (76.15%)

2 existing lines in 1 file now uncovered.

1163 of 1953 relevant lines covered (59.55%)

0.65 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

28
func (Typed[T]) Encode(x T) ([]byte, error) {
×
29
        return json.Marshal(x)
×
30
}
×
31

32
func (Typed[T]) Decode(b []byte) (x T, err error) {
×
33
        err = json.Unmarshal(b, &x)
×
34
        return
×
35
}
×
36

37
// Create JSON codec for typed objects
38
func ForTyped[T any](category ...string) Typed[T] {
×
39
        return Typed[T](swarm.TypeOf[T](category...))
×
40
}
×
41

42
//------------------------------------------------------------------------------
43

44
// JSON encoding for events
45
type Event[M, T any] struct {
46
        realm curie.IRI
47
        agent curie.IRI
48
        cat   curie.IRI
49
        shape optics.Lens5[M, string, curie.IRI, curie.IRI, curie.IRI, time.Time]
50
}
51

NEW
52
func (c Event[M, T]) Category() string { return string(c.cat) }
×
53

54
func (c Event[M, T]) Encode(obj swarm.Event[M, T]) ([]byte, error) {
×
NEW
55
        _, cat, rlm, agt, _ := c.shape.Get(obj.Meta)
×
NEW
56
        if cat == "" {
×
NEW
57
                cat = c.cat
×
UNCOV
58
        }
×
59

NEW
60
        if agt == "" {
×
NEW
61
                agt = c.agent
×
UNCOV
62
        }
×
63

NEW
64
        if rlm == "" {
×
NEW
65
                rlm = c.realm
×
NEW
66
        }
×
67

NEW
68
        c.shape.Put(obj.Meta, guid.G(guid.Clock).String(), cat, rlm, agt, time.Now())
×
69

×
70
        return json.Marshal(obj)
×
71
}
72

73
func (c Event[M, T]) Decode(b []byte) (swarm.Event[M, T], error) {
×
74
        var x swarm.Event[M, T]
×
75
        err := json.Unmarshal(b, &x)
×
76

×
77
        return x, err
×
78
}
×
79

80
// Creates JSON codec for events
NEW
81
func ForEvent[E swarm.Event[M, T], M, T any](realm, agent string, category ...string) Event[M, T] {
×
82
        return Event[M, T]{
×
NEW
83
                realm: curie.IRI(realm),
×
NEW
84
                agent: curie.IRI(agent),
×
NEW
85
                cat:   curie.IRI(swarm.TypeOf[T](category...)),
×
NEW
86
                shape: optics.ForShape5[M, string, curie.IRI, curie.IRI, curie.IRI, time.Time]("ID", "Type", "Realm", "Agent", "Created"),
×
87
        }
×
88
}
×
89

90
//------------------------------------------------------------------------------
91

92
// Identity encoding for bytes
93
type Bytes string
94

95
func (c Bytes) Category() string              { return string(c) }
×
96
func (Bytes) Encode(x []byte) ([]byte, error) { return x, nil }
×
97
func (Bytes) Decode(x []byte) ([]byte, error) { return x, nil }
×
98

99
// Create bytes identity codec
100
func ForBytes(cat string) Bytes { return Bytes(cat) }
×
101

102
//------------------------------------------------------------------------------
103

104
// Base64 encoding for bytes, sent as JSON
105
type BytesJB64 string
106

107
type packet struct {
108
        Octets []byte `json:"p,omitempty"`
109
}
110

111
func (c BytesJB64) Category() string { return string(c) }
×
112

113
func (BytesJB64) Encode(x []byte) ([]byte, error) {
×
114
        b, err := json.Marshal(packet{Octets: x})
×
115
        return b, err
×
116
}
×
117
func (BytesJB64) Decode(x []byte) ([]byte, error) {
×
118
        var pckt packet
×
119
        err := json.Unmarshal(x, &pckt)
×
120
        return pckt.Octets, err
×
121
}
×
122

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

© 2025 Coveralls, Inc