loams 0.1.0 → 0.1.1

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.
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Loams
6
+ # Conversions between Ruby values and the messages the app modules send and
7
+ # answer.
8
+ module Values
9
+ # A message as plain Ruby: Hashes with the proto's `snake_case` keys, a
10
+ # `Struct` as a Hash, 64-bit integers as Strings (proto3 JSON). nil stays
11
+ # nil.
12
+ def self.to_ruby(message)
13
+ return nil if message.nil?
14
+ return message.map { |m| to_ruby(m) } if message.respond_to?(:to_a) && !message.respond_to?(:to_proto)
15
+ return message unless message.class.respond_to?(:encode_json)
16
+
17
+ JSON.parse(message.class.encode_json(message, preserve_proto_fieldnames: true, emit_defaults: false))
18
+ end
19
+
20
+ # A primary key as a `DocumentId`'s JSON: a non-negative Integer is
21
+ # `uint`, a String is `string`, and `{uuid: …}` (or any Hash naming its
22
+ # arm) passes through.
23
+ def self.document_id(id)
24
+ case id
25
+ when Integer
26
+ raise InvalidArgumentError.new("a document id is a non-negative integer, a string or {uuid: …}", code: :invalid_argument) if id.negative?
27
+
28
+ { "uint" => id.to_s }
29
+ when String, Symbol then { "string" => id.to_s }
30
+ when Hash then id.transform_keys(&:to_s)
31
+ else
32
+ if id.class.respond_to?(:encode_json)
33
+ JSON.parse(id.class.encode_json(id))
34
+ else
35
+ raise InvalidArgumentError.new("a document id is a non-negative integer, a string or {uuid: …}, not #{id.class}", code: :invalid_argument)
36
+ end
37
+ end
38
+ end
39
+
40
+ # A `DocumentId` message (or its JSON) back as a Ruby value: an Integer,
41
+ # a String, or `{"uuid" => …}`.
42
+ def self.from_document_id(id)
43
+ json = id.is_a?(Hash) ? id : to_ruby(id)
44
+ return nil if json.nil? || json.empty?
45
+ return Integer(json["uint"]) if json.key?("uint")
46
+ return json["string"] if json.key?("string")
47
+
48
+ json
49
+ end
50
+
51
+ # A Ruby value as a `loams.graph.v1.Value`'s JSON, for GQL parameters.
52
+ def self.graph_value(value)
53
+ case value
54
+ when nil then { "null" => "NULL_VALUE" }
55
+ when true, false then { "boolean" => value }
56
+ when Integer then value.negative? || value <= 0x7FFF_FFFF_FFFF_FFFF ? { "int64" => value.to_s } : { "uint64" => value.to_s }
57
+ when Float then { "float64" => value }
58
+ when String, Symbol then { "string" => value.to_s }
59
+ when Array then { "list" => { "values" => value.map { |v| graph_value(v) } } }
60
+ when Hash then { "map" => { "entries" => value.to_h { |k, v| [k.to_s, graph_value(v)] } } }
61
+ else raise InvalidArgumentError.new("no graph value for #{value.class}", code: :invalid_argument)
62
+ end
63
+ end
64
+
65
+ # A `loams.graph.v1.Value` (message or JSON) as a Ruby value. Nodes,
66
+ # relationships, paths and temporal values stay as their JSON Hash.
67
+ def self.from_graph_value(value)
68
+ json = value.is_a?(Hash) ? value : to_ruby(value)
69
+ return nil if json.nil? || json.empty?
70
+
71
+ kind, inner = json.first
72
+ case kind
73
+ when "null" then nil
74
+ when "boolean", "float64", "string" then inner
75
+ when "int64", "uint64" then Integer(inner)
76
+ when "list" then Array(inner["values"]).map { |v| from_graph_value(v) }
77
+ when "map" then (inner["entries"] || {}).to_h { |k, v| [k, from_graph_value(v)] }
78
+ else json
79
+ end
80
+ end
81
+ end
82
+
83
+ # `Loams.to_h(message)`: see `Values.to_ruby`.
84
+ def self.to_h(message)
85
+ Values.to_ruby(message)
86
+ end
87
+ end
data/lib/loams/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loams
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/loams.rb CHANGED
@@ -13,10 +13,21 @@ require "loams/transport"
13
13
  require "loams/streams"
14
14
  require "loams/invoker"
15
15
  require "loams/pagination"
16
+ require "loams/consistency"
17
+ require "loams/values"
18
+ require "loams/builders"
19
+ require "loams/batch"
20
+ require "loams/subscribe"
21
+ require "loams/app"
16
22
  require "loams/client"
17
23
 
18
24
  module Loams
19
25
  def self.client(endpoint = "http://127.0.0.1:4400", **options)
20
26
  Client.new(endpoint, **options)
21
27
  end
28
+
29
+ # `Loams.from_env`: see `Client.from_env`.
30
+ def self.from_env(**options)
31
+ Client.from_env(**options)
32
+ end
22
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loams
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ostrium Labs
@@ -41,8 +41,12 @@ files:
41
41
  - README.md
42
42
  - gen/loams-descriptor.binpb
43
43
  - lib/loams.rb
44
+ - lib/loams/app.rb
45
+ - lib/loams/batch.rb
46
+ - lib/loams/builders.rb
44
47
  - lib/loams/client.rb
45
48
  - lib/loams/codec.rb
49
+ - lib/loams/consistency.rb
46
50
  - lib/loams/descriptors.rb
47
51
  - lib/loams/envelopes.rb
48
52
  - lib/loams/errors.rb
@@ -51,8 +55,10 @@ files:
51
55
  - lib/loams/pagination.rb
52
56
  - lib/loams/retry.rb
53
57
  - lib/loams/streams.rb
58
+ - lib/loams/subscribe.rb
54
59
  - lib/loams/token_source.rb
55
60
  - lib/loams/transport.rb
61
+ - lib/loams/values.rb
56
62
  - lib/loams/version.rb
57
63
  - lib/loams/wire.rb
58
64
  homepage: https://loams.dev