protobug_well_known_protos 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,185 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Code generated by protoc-gen-protobug. DO NOT EDIT.
4
+
5
+ # source: google/protobuf/struct.proto
6
+ # syntax: proto3
7
+ # package: google.protobuf
8
+ # options:
9
+ # java_package: "com.google.protobuf"
10
+ # java_outer_classname: "StructProto"
11
+ # java_multiple_files: true
12
+ # go_package: "google.golang.org/protobuf/types/known/structpb"
13
+ # cc_enable_arenas: true
14
+ # objc_class_prefix: "GPB"
15
+ # csharp_namespace: "Google.Protobuf.WellKnownTypes"
16
+
17
+ # Protocol Buffers - Google's data interchange format
18
+ # Copyright 2008 Google Inc. All rights reserved.
19
+ # https://developers.google.com/protocol-buffers/
20
+ #
21
+ # Redistribution and use in source and binary forms, with or without
22
+ # modification, are permitted provided that the following conditions are
23
+ # met:
24
+ #
25
+ # * Redistributions of source code must retain the above copyright
26
+ # notice, this list of conditions and the following disclaimer.
27
+ # * Redistributions in binary form must reproduce the above
28
+ # copyright notice, this list of conditions and the following disclaimer
29
+ # in the documentation and/or other materials provided with the
30
+ # distribution.
31
+ # * Neither the name of Google Inc. nor the names of its
32
+ # contributors may be used to endorse or promote products derived from
33
+ # this software without specific prior written permission.
34
+ #
35
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
+
47
+ require "protobug"
48
+
49
+ module Google
50
+ module Protobuf
51
+ # `Struct` represents a structured data value, consisting of fields
52
+ # which map to dynamically typed values. In some languages, `Struct`
53
+ # might be supported by a native representation. For example, in
54
+ # scripting languages like JS a struct is represented as an
55
+ # object. The details of that representation are described together
56
+ # with the proto support for the language.
57
+ #
58
+ # The JSON representation for `Struct` is JSON object.
59
+ class Struct
60
+ extend Protobug::Message
61
+
62
+ self.full_name = "google.protobuf.Struct"
63
+
64
+ # Unordered map of dynamically typed values.
65
+ map(
66
+ 1,
67
+ "fields",
68
+ key_type: :string,
69
+ value_type: :message,
70
+ message_type: "google.protobuf.Value"
71
+ )
72
+ end
73
+
74
+ # `Value` represents a dynamically typed value which can be either
75
+ # null, a number, a string, a boolean, a recursive struct value, or a
76
+ # list of values. A producer of value is expected to set one of these
77
+ # variants. Absence of any variant indicates an error.
78
+ #
79
+ # The JSON representation for `Value` is JSON value.
80
+ class Value
81
+ extend Protobug::Message
82
+
83
+ self.full_name = "google.protobuf.Value"
84
+
85
+ # The kind of value.
86
+
87
+ # Represents a null value.
88
+ optional(
89
+ 1,
90
+ "null_value",
91
+ type: :enum,
92
+ enum_type: "google.protobuf.NullValue",
93
+ json_name: "nullValue",
94
+ oneof: :kind,
95
+ proto3_optional: false
96
+ )
97
+ # Represents a double value.
98
+ optional(
99
+ 2,
100
+ "number_value",
101
+ type: :double,
102
+ json_name: "numberValue",
103
+ oneof: :kind,
104
+ proto3_optional: false
105
+ )
106
+ # Represents a string value.
107
+ optional(
108
+ 3,
109
+ "string_value",
110
+ type: :string,
111
+ json_name: "stringValue",
112
+ oneof: :kind,
113
+ proto3_optional: false
114
+ )
115
+ # Represents a boolean value.
116
+ optional(
117
+ 4,
118
+ "bool_value",
119
+ type: :bool,
120
+ json_name: "boolValue",
121
+ oneof: :kind,
122
+ proto3_optional: false
123
+ )
124
+ # Represents a structured value.
125
+ optional(
126
+ 5,
127
+ "struct_value",
128
+ type: :message,
129
+ message_type: "google.protobuf.Struct",
130
+ json_name: "structValue",
131
+ oneof: :kind,
132
+ proto3_optional: false
133
+ )
134
+ # Represents a repeated `Value`.
135
+ optional(
136
+ 6,
137
+ "list_value",
138
+ type: :message,
139
+ message_type: "google.protobuf.ListValue",
140
+ json_name: "listValue",
141
+ oneof: :kind,
142
+ proto3_optional: false
143
+ )
144
+ end
145
+
146
+ # `NullValue` is a singleton enumeration to represent the null value for the
147
+ # `Value` type union.
148
+ #
149
+ # The JSON representation for `NullValue` is JSON `null`.
150
+ class NullValue
151
+ extend Protobug::Enum
152
+
153
+ self.full_name = "google.protobuf.NullValue"
154
+
155
+ # Null value.
156
+ NULL_VALUE = new("NULL_VALUE", 0).freeze
157
+ end
158
+
159
+ # `ListValue` is a wrapper around a repeated field of values.
160
+ #
161
+ # The JSON representation for `ListValue` is JSON array.
162
+ class ListValue
163
+ extend Protobug::Message
164
+
165
+ self.full_name = "google.protobuf.ListValue"
166
+
167
+ # Repeated field of dynamically typed values.
168
+ repeated(
169
+ 1,
170
+ "values",
171
+ type: :message,
172
+ message_type: "google.protobuf.Value"
173
+ )
174
+ end
175
+
176
+ def self.register_struct_protos(registry)
177
+ registry.register(Google::Protobuf::Struct)
178
+ registry.register(Google::Protobuf::Value)
179
+ registry.register(Google::Protobuf::NullValue)
180
+ registry.register(Google::Protobuf::ListValue)
181
+ end
182
+ end
183
+ end
184
+
185
+ require_relative "struct_well_known"
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ Google::Protobuf::Value.class_eval do
4
+ class self::UnexpectedStructType < Protobug::InvalidValueError # rubocop:disable Lint/ConstantDefinitionInBlock,Style/ClassAndModuleChildren
5
+ end
6
+
7
+ def to_ruby(recursive: false)
8
+ case kind
9
+ when :struct_value
10
+ if recursive
11
+ struct_value.to_h
12
+ else
13
+ struct_value
14
+ end
15
+ when :list_value
16
+ if recursive
17
+ list_value.to_a
18
+ else
19
+ list_value
20
+ end
21
+ when :null_value
22
+ nil
23
+ when :number_value
24
+ number_value
25
+ when :string_value
26
+ string_value
27
+ when :bool_value
28
+ bool_value
29
+ else
30
+ raise UnexpectedStructType
31
+ end
32
+ end
33
+
34
+ def self.from_ruby(value)
35
+ new.from_ruby(value)
36
+ end
37
+
38
+ def from_ruby(value)
39
+ case value
40
+ when NilClass
41
+ self.null_value = Google::Protobuf::NullValue::NULL_VALUE
42
+ when Numeric
43
+ self.number_value = value
44
+ when String
45
+ self.string_value = value
46
+ when TrueClass
47
+ self.bool_value = true
48
+ when FalseClass
49
+ self.bool_value = false
50
+ when Struct
51
+ self.struct_value = value
52
+ when Hash
53
+ self.struct_value = Struct.from_hash(value)
54
+ when ListValue
55
+ self.list_value = value
56
+ when Array
57
+ self.list_value = ListValue.from_a(value)
58
+ else
59
+ raise UnexpectedStructType
60
+ end
61
+
62
+ self
63
+ end
64
+
65
+ def self.decode_json_hash(json, registry:, ignore_unknown_fields: false)
66
+ case json
67
+ when NilClass
68
+ json = { "nullValue" => "NULL_VALUE" }
69
+ when Float, Integer
70
+ json = { "numberValue" => json.to_f }
71
+ when String
72
+ json = { "stringValue" => json }
73
+ when TrueClass, FalseClass
74
+ json = { "boolValue" => json }
75
+ when Hash
76
+ json = { "structValue" => json }
77
+ when Array
78
+ json = { "listValue" => json }
79
+ end
80
+
81
+ super
82
+ end
83
+
84
+ def as_json(print_unknown_fields: false)
85
+ case kind
86
+ when :null_value
87
+ nil
88
+ when :number_value
89
+ number_value
90
+ when :string_value
91
+ string_value
92
+ when :bool_value
93
+ bool_value
94
+ when :struct_value
95
+ struct_value.as_json(print_unknown_fields: print_unknown_fields)
96
+ when :list_value
97
+ list_value.values.map! { _1.as_json(print_unknown_fields: print_unknown_fields) }
98
+ else
99
+ raise Protobug::EncodeError, "unknown kind: #{kind.inspect}"
100
+ end
101
+ end
102
+ end
103
+
104
+ Google::Protobuf::Struct.class_eval do
105
+ def [](key)
106
+ fields[key]&.to_ruby
107
+ end
108
+
109
+ def []=(key, value)
110
+ raise ArgumentError, "Struct keys must be strings." unless key.is_a?(String)
111
+
112
+ fields[key] ||= Google::Protobuf::Value.new
113
+ fields[key].from_ruby(value)
114
+ end
115
+
116
+ def to_h
117
+ fields.transform_values { |val| val.to_ruby(true) }
118
+ end
119
+
120
+ def self.from_hash(hash)
121
+ hash.each_with_object(new) { |(key, val), ret| ret[key] = val }
122
+ end
123
+
124
+ def key?(key)
125
+ fields.key?(key)
126
+ end
127
+
128
+ def self.decode_json_hash(json, registry:, ignore_unknown_fields: false)
129
+ json = { "fields" => json }
130
+ super
131
+ end
132
+
133
+ def as_json(print_unknown_fields: false)
134
+ fields.transform_values { _1&.as_json(print_unknown_fields: print_unknown_fields) }
135
+ end
136
+ end
137
+
138
+ Google::Protobuf::ListValue.class_eval do
139
+ include Enumerable
140
+
141
+ def length
142
+ values.length
143
+ end
144
+
145
+ def [](index)
146
+ values[index].to_ruby
147
+ end
148
+
149
+ def []=(index, value)
150
+ values[index].from_ruby(value)
151
+ end
152
+
153
+ def <<(value)
154
+ values << Google::Protobuf::Value.from_ruby(value)
155
+ end
156
+
157
+ def each
158
+ return enum_for(__method__) unless block_given?
159
+
160
+ values.each { |x| yield(x.to_ruby) }
161
+ end
162
+
163
+ def to_a
164
+ values.map { |x| x.to_ruby(true) }
165
+ end
166
+
167
+ def self.from_a(arr)
168
+ arr.each_with_object(new) { |val, ret| ret << val }
169
+ end
170
+
171
+ def self.decode_json_hash(json, registry:, ignore_unknown_fields: false)
172
+ json = { "values" => json }
173
+ super
174
+ end
175
+
176
+ def as_json(print_unknown_fields: false)
177
+ values.map { _1.as_json(print_unknown_fields: print_unknown_fields) }
178
+ end
179
+ end
180
+
181
+ Google::Protobuf::NullValue.class_eval do
182
+ def self.decode_json_hash(json, registry:, ignore_unknown_fields: false)
183
+ return values.fetch(0) if json.nil?
184
+
185
+ super
186
+ end
187
+
188
+ def as_json(print_unknown_fields: false)
189
+ return if value.zero?
190
+
191
+ super
192
+ end
193
+ end
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Code generated by protoc-gen-protobug. DO NOT EDIT.
4
+
5
+ # source: google/protobuf/timestamp.proto
6
+ # syntax: proto3
7
+ # package: google.protobuf
8
+ # options:
9
+ # java_package: "com.google.protobuf"
10
+ # java_outer_classname: "TimestampProto"
11
+ # java_multiple_files: true
12
+ # go_package: "google.golang.org/protobuf/types/known/timestamppb"
13
+ # cc_enable_arenas: true
14
+ # objc_class_prefix: "GPB"
15
+ # csharp_namespace: "Google.Protobuf.WellKnownTypes"
16
+
17
+ # Protocol Buffers - Google's data interchange format
18
+ # Copyright 2008 Google Inc. All rights reserved.
19
+ # https://developers.google.com/protocol-buffers/
20
+ #
21
+ # Redistribution and use in source and binary forms, with or without
22
+ # modification, are permitted provided that the following conditions are
23
+ # met:
24
+ #
25
+ # * Redistributions of source code must retain the above copyright
26
+ # notice, this list of conditions and the following disclaimer.
27
+ # * Redistributions in binary form must reproduce the above
28
+ # copyright notice, this list of conditions and the following disclaimer
29
+ # in the documentation and/or other materials provided with the
30
+ # distribution.
31
+ # * Neither the name of Google Inc. nor the names of its
32
+ # contributors may be used to endorse or promote products derived from
33
+ # this software without specific prior written permission.
34
+ #
35
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
+
47
+ require "protobug"
48
+
49
+ module Google
50
+ module Protobuf
51
+ # A Timestamp represents a point in time independent of any time zone or local
52
+ # calendar, encoded as a count of seconds and fractions of seconds at
53
+ # nanosecond resolution. The count is relative to an epoch at UTC midnight on
54
+ # January 1, 1970, in the proleptic Gregorian calendar which extends the
55
+ # Gregorian calendar backwards to year one.
56
+ #
57
+ # All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap
58
+ # second table is needed for interpretation, using a [24-hour linear
59
+ # smear](https://developers.google.com/time/smear).
60
+ #
61
+ # The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By
62
+ # restricting to that range, we ensure that we can convert to and from [RFC
63
+ # 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings.
64
+ #
65
+ # # Examples
66
+ #
67
+ # Example 1: Compute Timestamp from POSIX `time()`.
68
+ #
69
+ # Timestamp timestamp;
70
+ # timestamp.set_seconds(time(NULL));
71
+ # timestamp.set_nanos(0);
72
+ #
73
+ # Example 2: Compute Timestamp from POSIX `gettimeofday()`.
74
+ #
75
+ # struct timeval tv;
76
+ # gettimeofday(&tv, NULL);
77
+ #
78
+ # Timestamp timestamp;
79
+ # timestamp.set_seconds(tv.tv_sec);
80
+ # timestamp.set_nanos(tv.tv_usec * 1000);
81
+ #
82
+ # Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`.
83
+ #
84
+ # FILETIME ft;
85
+ # GetSystemTimeAsFileTime(&ft);
86
+ # UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
87
+ #
88
+ # // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z
89
+ # // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z.
90
+ # Timestamp timestamp;
91
+ # timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL));
92
+ # timestamp.set_nanos((INT32) ((ticks % 10000000) * 100));
93
+ #
94
+ # Example 4: Compute Timestamp from Java `System.currentTimeMillis()`.
95
+ #
96
+ # long millis = System.currentTimeMillis();
97
+ #
98
+ # Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000)
99
+ # .setNanos((int) ((millis % 1000) * 1000000)).build();
100
+ #
101
+ # Example 5: Compute Timestamp from Java `Instant.now()`.
102
+ #
103
+ # Instant now = Instant.now();
104
+ #
105
+ # Timestamp timestamp =
106
+ # Timestamp.newBuilder().setSeconds(now.getEpochSecond())
107
+ # .setNanos(now.getNano()).build();
108
+ #
109
+ # Example 6: Compute Timestamp from current time in Python.
110
+ #
111
+ # timestamp = Timestamp()
112
+ # timestamp.GetCurrentTime()
113
+ #
114
+ # # JSON Mapping
115
+ #
116
+ # In JSON format, the Timestamp type is encoded as a string in the
117
+ # [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the
118
+ # format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z"
119
+ # where {year} is always expressed using four digits while {month}, {day},
120
+ # {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional
121
+ # seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution),
122
+ # are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone
123
+ # is required. A proto3 JSON serializer should always use UTC (as indicated by
124
+ # "Z") when printing the Timestamp type and a proto3 JSON parser should be
125
+ # able to accept both UTC and other timezones (as indicated by an offset).
126
+ #
127
+ # For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past
128
+ # 01:30 UTC on January 15, 2017.
129
+ #
130
+ # In JavaScript, one can convert a Date object to this format using the
131
+ # standard
132
+ # [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString)
133
+ # method. In Python, a standard `datetime.datetime` object can be converted
134
+ # to this format using
135
+ # [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with
136
+ # the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use
137
+ # the Joda Time's [`ISODateTimeFormat.dateTime()`](
138
+ # http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime()
139
+ # ) to obtain a formatter capable of generating timestamps in this format.
140
+ class Timestamp
141
+ extend Protobug::Message
142
+
143
+ self.full_name = "google.protobuf.Timestamp"
144
+
145
+ # Represents seconds of UTC time since Unix epoch
146
+ # 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
147
+ # 9999-12-31T23:59:59Z inclusive.
148
+ optional(1, "seconds", type: :int64, proto3_optional: false)
149
+ # Non-negative fractions of a second at nanosecond resolution. Negative
150
+ # second values with fractions must still have non-negative nanos values
151
+ # that count forward in time. Must be from 0 to 999,999,999
152
+ # inclusive.
153
+ optional(2, "nanos", type: :int32, proto3_optional: false)
154
+ end
155
+
156
+ def self.register_timestamp_protos(registry)
157
+ registry.register(Google::Protobuf::Timestamp)
158
+ end
159
+ end
160
+ end
161
+
162
+ require_relative "timestamp_well_known"
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ Google::Protobuf::Timestamp.class_eval do
4
+ def to_time
5
+ Time.at(seconds, nanos, :nanosecond, in: 0)
6
+ end
7
+
8
+ def self.from_time(time)
9
+ new.from_time(time)
10
+ end
11
+
12
+ def from_time(time)
13
+ self.seconds = time.to_i
14
+ self.nanos = time.nsec
15
+ self
16
+ end
17
+
18
+ def to_i
19
+ seconds
20
+ end
21
+
22
+ def to_f
23
+ seconds + nanos.quo(1_000_000_000)
24
+ end
25
+
26
+ def self.decode_json_hash(json, registry:, ignore_unknown_fields: false)
27
+ return Protobug::UNSET if json.nil?
28
+ raise Protobug::DecodeError, "expected string for #{full_name}, got #{json.inspect}" unless json.is_a? String
29
+
30
+ if /t|z/.match?(json)
31
+ raise Protobug::DecodeError, "json timestamp string has incorrect capitalization: #{json.inspect}"
32
+ elsif !json.include?("T")
33
+ raise Protobug::DecodeError, "json timestamp string missing T: #{json.inspect}"
34
+ end
35
+
36
+ begin
37
+ time = DateTime.rfc3339(json, Date::GREGORIAN).to_time
38
+ rescue Date::Error => e
39
+ raise Protobug::DecodeError, "json timestamp string invalid: #{json.inspect} (#{e})"
40
+ end
41
+ validate_json_range(Protobug::DecodeError, time)
42
+ json = {
43
+ "seconds" => time.to_i,
44
+ "nanos" => time.nsec
45
+ }
46
+ super
47
+ end
48
+
49
+ def as_json(print_unknown_fields: false) # rubocop:disable Lint/UnusedMethodArgument
50
+ time = Time.at(seconds, nanos, :nanosecond, in: 0)
51
+ self.class.validate_json_range(Protobug::EncodeError, time)
52
+
53
+ nanosecs = time.nsec
54
+
55
+ if nanosecs.positive?
56
+ nanosecs = nanosecs.to_s.rjust(9, "0")
57
+ nil while nanosecs.delete_suffix!("000")
58
+ digits = nanosecs.size
59
+ format = "%FT%H:%M:%S.%#{digits}NZ"
60
+ else
61
+ format = "%FT%H:%M:%SZ"
62
+ end
63
+
64
+ time.strftime(format)
65
+ end
66
+
67
+ def self.validate_json_range(err, time)
68
+ raise err, "time value too large #{time.inspect}" if time.year > 9999
69
+ raise err, "time value too small #{time.inspect}" if time.year <= 0
70
+ end
71
+ end