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

fogfish / swarm / 13873166240

15 Mar 2025 01:08PM UTC coverage: 59.373% (-1.9%) from 61.277%
13873166240

push

github

web-flow
update kernel api - unified support for events and codecs (#117)

* update kernel api - unified support for events and codecs
* go version 1.24

66 of 155 new or added lines in 8 files covered. (42.58%)

56 existing lines in 5 files now uncovered.

1023 of 1723 relevant lines covered (59.37%)

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

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

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

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

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

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

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

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

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

UNCOV
59
        if src == "" {
×
UNCOV
60
                src = curie.IRI(c.source)
×
UNCOV
61
        }
×
62

UNCOV
63
        c.shape.Put(obj.Meta, guid.G(guid.Clock).String(), knd, src, time.Now())
×
UNCOV
64

×
UNCOV
65
        return json.Marshal(obj)
×
66
}
67

NEW
68
func (c Event[M, T]) Decode(b []byte) (swarm.Event[M, T], error) {
×
69
        var x swarm.Event[M, T]
×
70
        err := json.Unmarshal(b, &x)
×
71

×
72
        return x, err
×
73
}
×
74

75
// Creates JSON codec for events
NEW
76
func ForEvent[M, T any](source string, category ...string) Event[M, T] {
×
NEW
77
        return Event[M, T]{
×
UNCOV
78
                source: source,
×
NEW
79
                cat:    swarm.TypeOf[T](category...),
×
UNCOV
80
                shape:  optics.ForShape4[M, string, curie.IRI, curie.IRI, time.Time]("ID", "Type", "Agent", "Created"),
×
UNCOV
81
        }
×
UNCOV
82
}
×
83

84
//------------------------------------------------------------------------------
85

86
// Identity encoding for bytes
87
type Bytes string
88

NEW
89
func (c Bytes) Category() string              { return string(c) }
×
NEW
90
func (Bytes) Encode(x []byte) ([]byte, error) { return x, nil }
×
NEW
91
func (Bytes) Decode(x []byte) ([]byte, error) { return x, nil }
×
92

93
// Create bytes identity codec
NEW
94
func ForBytes(cat string) Bytes { return Bytes(cat) }
×
95

96
//------------------------------------------------------------------------------
97

98
// Base64 encoding for bytes, sent as JSON
99
type BytesJB64 string
100

101
type packet struct {
102
        Octets []byte `json:"p,omitempty"`
103
}
104

NEW
105
func (c BytesJB64) Category() string { return string(c) }
×
106

NEW
107
func (BytesJB64) Encode(x []byte) ([]byte, error) {
×
NEW
108
        b, err := json.Marshal(packet{Octets: x})
×
NEW
109
        return b, err
×
NEW
110
}
×
NEW
111
func (BytesJB64) Decode(x []byte) ([]byte, error) {
×
NEW
112
        var pckt packet
×
NEW
113
        err := json.Unmarshal(x, &pckt)
×
NEW
114
        return pckt.Octets, err
×
NEW
115
}
×
116

117
// Creates bytes codec for Base64 encapsulated into Json "packat"
NEW
118
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