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

fogfish / swarm / 16620741795

30 Jul 2025 11:01AM UTC coverage: 62.481% (+0.07%) from 62.416%
16620741795

push

github

web-flow
(fix) routing of event when meta.Type and Category defined (#135)

0 of 3 new or added lines in 1 file covered. (0.0%)

1672 of 2676 relevant lines covered (62.48%)

0.67 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 (c Typed[T]) Encode(obj T) (swarm.Bag, error) {
×
29
        msg, err := json.Marshal(obj)
×
30
        if err != nil {
×
31
                return swarm.Bag{}, err
×
32
        }
×
33

34
        return swarm.Bag{
×
35
                Category: string(c),
×
36
                Object:   msg,
×
37
        }, nil
×
38
}
39

40
func (Typed[T]) Decode(bag swarm.Bag) (x T, err error) {
×
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

62
func (c Event[M, T]) Encode(obj swarm.Event[M, T]) (swarm.Bag, error) {
×
63
        if obj.Meta == nil {
×
64
                obj.Meta = new(M)
×
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())
×
81
        msg, err := json.Marshal(obj)
×
82
        if err != nil {
×
83
                return swarm.Bag{}, err
×
84
        }
×
85

86
        return swarm.Bag{
×
NEW
87
                // Note: the message category MUST BE always derived from the Type.
×
NEW
88
                //       Metadata type NOT NOT override it.
×
NEW
89
                Category: string(c.cat),
×
90
                Object:   msg,
×
91
        }, nil
×
92
}
93

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

×
98
        return x, err
×
99
}
×
100

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

111
//------------------------------------------------------------------------------
112

113
// Identity encoding for bytes
114
type Bytes string
115

116
func (c Bytes) Category() string { return string(c) }
×
117

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

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

129
// Create bytes identity codec
130
func ForBytes(cat string) Bytes { return Bytes(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