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

fogfish / swarm / 14548602918

19 Apr 2025 11:16AM UTC coverage: 56.92%. Remained the same
14548602918

push

github

web-flow
simplify event definition for sync api (#122)

4 of 5 new or added lines in 4 files covered. (80.0%)

987 of 1734 relevant lines covered (56.92%)

0.62 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
        source string
47
        cat    string
48
        shape  optics.Lens4[M, string, curie.IRI, curie.IRI, time.Time]
49
}
50

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

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

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

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

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

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[E swarm.Event[M, T], M, T any](source string, category ...string) Event[M, T] {
×
77
        return Event[M, T]{
×
78
                source: source,
×
79
                cat:    swarm.TypeOf[T](category...),
×
80
                shape:  optics.ForShape4[M, string, curie.IRI, curie.IRI, time.Time]("ID", "Type", "Agent", "Created"),
×
81
        }
×
82
}
×
83

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

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

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

93
// Create bytes identity codec
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

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

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

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