sourced-message 0.5.3 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/sourced/message.rb +45 -3
- metadata +2 -3
- data/CHANGELOG.md +0 -74
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9982289a156032997798dfdedaf95aaccf04872655e03b589a261db2a4032ebd
|
|
4
|
+
data.tar.gz: 13ca751ea5bcb1d9f76c2e72485a8aaee4fc3148f2727fd00bce1574bcdd51dd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 457cce26fd22fdbc94001cc7ebefc51ef063ca7250e8da3c9ab6844efa9369844c6b4ac979ae68e0395e3d12cb1c0e9c17db3404d21e48843e46ef20859786a6
|
|
7
|
+
data.tar.gz: ed69eab1c6ee0b9f1c7bdb047cac6835e280cbc3e40da0a1fab8851c912cb2364e5cfe9b8a98250400ccde10db0e4869585f6f4524179fd6883d13901e90530d
|
data/lib/sourced/message.rb
CHANGED
|
@@ -22,7 +22,7 @@ module Sourced
|
|
|
22
22
|
# +Sourced::Message.registry[type]+ resolves a type registered under any
|
|
23
23
|
# subclass. Resolve from this root to see the whole tree.
|
|
24
24
|
class Message < Plumb::Types::Data
|
|
25
|
-
VERSION = '0.5.
|
|
25
|
+
VERSION = '0.5.4'
|
|
26
26
|
|
|
27
27
|
EMPTY_ARRAY = [].freeze
|
|
28
28
|
|
|
@@ -58,6 +58,38 @@ module Sourced
|
|
|
58
58
|
DuplicateTypeError = Class.new(ArgumentError)
|
|
59
59
|
LaxTime = Types::Time | Types::String.build(::Time, :parse).policy(:rescue, ::ArgumentError)
|
|
60
60
|
|
|
61
|
+
# Messages are immutable all the way down: a message's attribute values,
|
|
62
|
+
# and the strings, arrays, hashes and sets in them, are frozen. Unfrozen
|
|
63
|
+
# values are copied, not frozen in place, so a caller's own array passed
|
|
64
|
+
# in a payload stays theirs. Structs (+Plumb::Attributes+) are left as
|
|
65
|
+
# they are: payload structs ({Payload} and nested +attribute :x do+
|
|
66
|
+
# blocks) freeze their own values.
|
|
67
|
+
module DeepFreeze
|
|
68
|
+
# @param value [Object]
|
|
69
|
+
# @return [Object] +value+, or a frozen copy of it
|
|
70
|
+
def self.call(value)
|
|
71
|
+
case value
|
|
72
|
+
when ::String then value.frozen? ? value : -value
|
|
73
|
+
when ::Array then same_or(value, value.map { |v| call(v) })
|
|
74
|
+
when ::Hash then hash(value)
|
|
75
|
+
when ::Set then value.frozen? && value.all? { |v| call(v).equal?(v) } ? value : ::Set.new(value.map { |v| call(v) }).freeze
|
|
76
|
+
else value
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# @param value [Hash]
|
|
81
|
+
# @return [Hash] frozen, with frozen values
|
|
82
|
+
def self.hash(value)
|
|
83
|
+
copy = value.transform_values { |v| call(v) }
|
|
84
|
+
value.frozen? && copy.all? { |k, v| v.equal?(value[k]) } ? value : copy.freeze
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.same_or(value, copy)
|
|
88
|
+
value.frozen? && copy.each_with_index.all? { |v, i| v.equal?(value[i]) } ? value : copy.freeze
|
|
89
|
+
end
|
|
90
|
+
private_class_method :same_or
|
|
91
|
+
end
|
|
92
|
+
|
|
61
93
|
attribute :id, Types::AutoUUID
|
|
62
94
|
attribute :type, Types::String.present
|
|
63
95
|
attribute? :causation_id, Types::UUID::V4
|
|
@@ -173,13 +205,22 @@ module Sourced
|
|
|
173
205
|
end
|
|
174
206
|
|
|
175
207
|
# Base class for typed message payloads.
|
|
208
|
+
#
|
|
209
|
+
# Frozen all the way down (see {DeepFreeze}), as are the structs its nested
|
|
210
|
+
# +attribute :x do+ blocks define, which subclass it.
|
|
176
211
|
class Payload < Plumb::Types::Data
|
|
212
|
+
def self.__plumb_struct_class__ = Payload
|
|
213
|
+
|
|
177
214
|
# @param key [Symbol] attribute name
|
|
178
215
|
# @return [Object] attribute value
|
|
179
216
|
def [](key) = attributes[key]
|
|
180
217
|
|
|
181
218
|
# @see Hash#fetch
|
|
182
219
|
def fetch(...) = to_h.fetch(...)
|
|
220
|
+
|
|
221
|
+
private
|
|
222
|
+
|
|
223
|
+
def prepare_attributes(attrs) = DeepFreeze.hash(attrs)
|
|
183
224
|
end
|
|
184
225
|
|
|
185
226
|
# Define a new message type. Registers it in the {.registry} and
|
|
@@ -369,11 +410,12 @@ module Sourced
|
|
|
369
410
|
private
|
|
370
411
|
|
|
371
412
|
# Hook called by Plumb after schema parsing, when +:id+ has been resolved.
|
|
372
|
-
# Defaults +causation_id+ and +correlation_id+ to the message's own +id
|
|
413
|
+
# Defaults +causation_id+ and +correlation_id+ to the message's own +id+,
|
|
414
|
+
# and freezes the attributes (see {DeepFreeze}).
|
|
373
415
|
def prepare_attributes(attrs)
|
|
374
416
|
attrs[:correlation_id] = attrs[:id] unless attrs[:correlation_id]
|
|
375
417
|
attrs[:causation_id] = attrs[:id] unless attrs[:causation_id]
|
|
376
|
-
attrs
|
|
418
|
+
DeepFreeze.hash(attrs)
|
|
377
419
|
end
|
|
378
420
|
|
|
379
421
|
# A duration string's seconds, up to weeks.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sourced-message
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.5.
|
|
4
|
+
version: 0.5.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ismael Celis
|
|
@@ -46,7 +46,6 @@ executables: []
|
|
|
46
46
|
extensions: []
|
|
47
47
|
extra_rdoc_files: []
|
|
48
48
|
files:
|
|
49
|
-
- CHANGELOG.md
|
|
50
49
|
- LICENSE.txt
|
|
51
50
|
- README.md
|
|
52
51
|
- Rakefile
|
|
@@ -74,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
74
73
|
- !ruby/object:Gem::Version
|
|
75
74
|
version: '0'
|
|
76
75
|
requirements: []
|
|
77
|
-
rubygems_version: 4.0.
|
|
76
|
+
rubygems_version: 4.0.20
|
|
78
77
|
specification_version: 4
|
|
79
78
|
summary: Canonical typed Message class and registry shared by Sourced and Sidereal.
|
|
80
79
|
test_files: []
|
data/CHANGELOG.md
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
## [Unreleased]
|
|
2
|
-
|
|
3
|
-
- `Message#created_at` is kept to the microsecond, the precision `Plumb::Codec::JSON`
|
|
4
|
-
encodes, so a message equals itself read back from JSON. On Linux, `Time.now` has
|
|
5
|
-
nanoseconds, so a message and its stored copy differed in `created_at`. Every way of
|
|
6
|
-
setting it is truncated: the default, a `created_at:` passed in, `#with` and `#at`.
|
|
7
|
-
- `Message#at` with a duration string (`'1h30m'`) raised `ArgumentError` ("Cannot
|
|
8
|
-
determine timezone from \"BST\"") in the hour the clocks go back. The duration's
|
|
9
|
-
seconds are now added to `Time.now`, exactly. Durations in months or years, which
|
|
10
|
-
aren't a fixed number of seconds, raise `ArgumentError`.
|
|
11
|
-
- `Message::Registry` raises `DuplicateTypeError` when two classes with different names
|
|
12
|
-
claim one type string, instead of silently replacing the registered class. Registering
|
|
13
|
-
a type again is still allowed when the names are equal (a code reloader redefining a
|
|
14
|
-
constant) or either class is anonymous (throwaway classes in specs; a constant in an
|
|
15
|
-
anonymous module counts as anonymous until the module is named). `.define`
|
|
16
|
-
registers a class before it's assigned to its constant, so the comparison waits until
|
|
17
|
-
the new class has a name: the next registration of the type, its lookup (`[]`,
|
|
18
|
-
`Message.from`, codec `decode`) or `#all` (codec `compile!`) raises.
|
|
19
|
-
|
|
20
|
-
## [0.5.0]
|
|
21
|
-
|
|
22
|
-
- `Message#tags`: an envelope attribute (`Types::Tags`, Symbol keys and String values,
|
|
23
|
-
empty by default) holding key/value pairs a store can index alongside the payload's,
|
|
24
|
-
without the message class declaring them. Codecs encode and decode it with the rest
|
|
25
|
-
of the envelope.
|
|
26
|
-
- `#with_tags(**tags)` returns a copy with tags merged in, values stringified. It raises
|
|
27
|
-
`TagConflictError` when a tag names a payload attribute with a different value.
|
|
28
|
-
- `#correlate` leaves the target's tags alone: tags never pass on to messages produced
|
|
29
|
-
in reaction, unlike metadata.
|
|
30
|
-
- `Sourced::Message::VERSION` is now `0.5.0`.
|
|
31
|
-
|
|
32
|
-
## [0.4.0]
|
|
33
|
-
|
|
34
|
-
- Requires plumb 0.4, whose codecs rewrite Hash keys like values: `#encode` now emits
|
|
35
|
-
String keys (`{'type' => ..., 'payload' => {'name' => ...}}`), and decoders accept
|
|
36
|
-
String or Symbol keys. `JSON.dump` output is unchanged.
|
|
37
|
-
- `Codec#decode` reads the envelope's `type` and `id` under either key form, so
|
|
38
|
-
`decode(encode(message))` works without a JSON round trip.
|
|
39
|
-
- `metadata` is typed as `Types::Metadata`: Symbol keys and `Types::JSONValue` values
|
|
40
|
-
(String, Numeric, boolean, nil, and Arrays/Symbol-keyed Hashes of those, at any depth).
|
|
41
|
-
Codecs now restore nested metadata keys as Symbols. A non-JSON value (a Time, a
|
|
42
|
-
Symbol) makes the message invalid and fails `#encode`, where `JSON.dump` used to
|
|
43
|
-
stringify it silently.
|
|
44
|
-
- `Sourced::Message::VERSION` is now `0.4.0`.
|
|
45
|
-
|
|
46
|
-
## [0.3.0]
|
|
47
|
-
|
|
48
|
-
- Add `Sourced::Message#correlation_type`, the type counterpart of `correlation_id`:
|
|
49
|
-
the type of the message at the root of a causal chain. `#correlate` now records the
|
|
50
|
-
source's `correlation_type` under `metadata[:correlation_type]` on the target, so
|
|
51
|
-
every consequence of a command carries the command's type. A message that was never
|
|
52
|
-
correlated answers with its own `type`. A target whose metadata already holds a
|
|
53
|
-
`correlation_type` keeps it, which starts a new chain.
|
|
54
|
-
- `Sourced::Message::VERSION` is now `0.3.0`.
|
|
55
|
-
|
|
56
|
-
## [0.2.0]
|
|
57
|
-
|
|
58
|
-
- Add `Sourced::Message::Codec`, the abstract serializer: it compiles a
|
|
59
|
-
`[decoder, encoder]` pair per registered message type over a `Plumb::Codec` format and
|
|
60
|
-
encodes/decodes whole messages. A subclass binds the format by answering
|
|
61
|
-
`.default_format`; each gets its own `.default` and pair cache.
|
|
62
|
-
- Add `Sourced::Message::JSONCodec` (`Plumb::Codec::JSON`) and
|
|
63
|
-
`Sourced::Message::FormsCodec` (`Plumb::Codec::Forms`). The Forms one types string
|
|
64
|
-
params from a browser using the message's own schema.
|
|
65
|
-
- Sourced's store and Sidereal's file store and socket pubsub carried near-identical
|
|
66
|
-
copies of this machinery; both now build on it. Three private seams —
|
|
67
|
-
`#compiled_type`, `#encode_subject`, `#build` — let a subclass change what is
|
|
68
|
-
compiled and encoded, which is how `Sourced::Store::MessageCodec` encodes payloads
|
|
69
|
-
alone while keeping its envelope in columns.
|
|
70
|
-
- `Sourced::Message::VERSION` is now `0.2.0`.
|
|
71
|
-
|
|
72
|
-
## [0.1.0] - 2026-06-06
|
|
73
|
-
|
|
74
|
-
- Initial release
|