Module: Pb
- Defined in:
- lib/pb.rb,
lib/pb/version.rb
Defined Under Namespace
Classes: TypeInfo
Constant Summary collapse
- BUILTIN_PROTO_TYPES =
[ Google::Protobuf::Timestamp, Google::Protobuf::StringValue, Google::Protobuf::Int32Value, Google::Protobuf::Int64Value, Google::Protobuf::UInt32Value, Google::Protobuf::UInt64Value, Google::Protobuf::FloatValue, Google::Protobuf::DoubleValue, Google::Protobuf::BoolValue, Google::Protobuf::BytesValue, ].freeze
- VERSION =
"0.0.2"
Class Method Summary collapse
- .to_boolval(b) ⇒ Google::Protobuf::BoolValue?
-
.to_builtin_proto(klass, v) ⇒ Object?
Protobuf message object.
- .to_bytesval(bytes) ⇒ Google::Protobuf::BytesValue?
- .to_doubleval(num) ⇒ Google::Protobuf::DoubleValue?
- .to_floatval(num) ⇒ Google::Protobuf::FloatValue?
- .to_int32val(num) ⇒ Google::Protobuf::Int32Value?
- .to_int64val(num) ⇒ Google::Protobuf::Int64Value?
- .to_primitive(type, v) ⇒ Object
-
.to_proto(klass, v) ⇒ Object, ...
objects contain parameters of a Protobuf message object.
- .to_strval(str) ⇒ Google::Protobuf::StringValue?
- .to_timestamp(t) ⇒ Google::Protobuf::Timestamp?
- .to_uint32val(num) ⇒ Google::Protobuf::UInt32Value?
- .to_uint64val(num) ⇒ Google::Protobuf::UInt64Value?
Class Method Details
.to_boolval(b) ⇒ Google::Protobuf::BoolValue?
110 111 112 113 114 |
# File 'lib/pb.rb', line 110 def to_boolval(b) return nil if b.nil? return b if b.is_a?(Google::Protobuf::BoolValue) Google::Protobuf::BoolValue.new(value: b) end |
.to_builtin_proto(klass, v) ⇒ Object?
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/pb.rb', line 127 def to_builtin_proto(klass, v) return nil if v.nil? if klass == Google::Protobuf::Timestamp (v) elsif klass == Google::Protobuf::StringValue to_strval(v) elsif klass == Google::Protobuf::Int32Value to_int32val(v) elsif klass == Google::Protobuf::Int64Value to_int64val(v) elsif klass == Google::Protobuf::UInt32Value to_uint32val(v) elsif klass == Google::Protobuf::UInt64Value to_uint64val(v) elsif klass == Google::Protobuf::FloatValue to_floatval(v) elsif klass == Google::Protobuf::DoubleValue to_doubleval(v) elsif klass == Google::Protobuf::BoolValue to_boolval(v) elsif klass == Google::Protobuf::BytesValue to_bytesval(v) else raise "Invalid klass: #{klass} is not included in supported classes (#{BUILTIN_PROTO_TYPES})" end end |
.to_bytesval(bytes) ⇒ Google::Protobuf::BytesValue?
118 119 120 121 122 |
# File 'lib/pb.rb', line 118 def to_bytesval(bytes) return nil if bytes.nil? return bytes if bytes.is_a?(Google::Protobuf::BytesValue) Google::Protobuf::BytesValue.new(value: bytes) end |
.to_doubleval(num) ⇒ Google::Protobuf::DoubleValue?
102 103 104 105 106 |
# File 'lib/pb.rb', line 102 def to_doubleval(num) return nil if num.nil? return num if num.is_a?(Google::Protobuf::DoubleValue) Google::Protobuf::DoubleValue.new(value: num) end |
.to_floatval(num) ⇒ Google::Protobuf::FloatValue?
94 95 96 97 98 |
# File 'lib/pb.rb', line 94 def to_floatval(num) return nil if num.nil? return num if num.is_a?(Google::Protobuf::FloatValue) Google::Protobuf::FloatValue.new(value: num) end |
.to_int32val(num) ⇒ Google::Protobuf::Int32Value?
50 51 52 53 54 |
# File 'lib/pb.rb', line 50 def to_int32val(num) return nil if num.nil? return num if num.is_a?(Google::Protobuf::Int32Value) Google::Protobuf::Int32Value.new(value: num) end |
.to_int64val(num) ⇒ Google::Protobuf::Int64Value?
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pb.rb', line 58 def to_int64val(num) return nil if num.nil? return num if num.is_a?(Google::Protobuf::Int64Value) case num when String n = num.to_i else # Integer n = num end Google::Protobuf::Int64Value.new(value: n) end |
.to_primitive(type, v) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/pb.rb', line 172 def to_primitive(type, v) case v when Array v.map { |e| to_primitive_one(type, e) } else to_primitive_one(type, v) end end |
.to_proto(klass, v) ⇒ Object, ...
objects contain parameters of a Protobuf message object
159 160 161 162 163 164 165 166 |
# File 'lib/pb.rb', line 159 def to_proto(klass, v) case v when Array v.map { |e| to_proto_one(klass, e) } else to_proto_one(klass, v) end end |
.to_strval(str) ⇒ Google::Protobuf::StringValue?
42 43 44 45 46 |
# File 'lib/pb.rb', line 42 def to_strval(str) return nil if str.nil? return str if str.is_a?(Google::Protobuf::StringValue) Google::Protobuf::StringValue.new(value: str) end |
.to_timestamp(t) ⇒ Google::Protobuf::Timestamp?
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pb.rb', line 26 def (t) return nil if t.nil? return t if t.is_a?(Google::Protobuf::Timestamp) case t when DateTime, Date t = t.to_time when String t = Time.parse(t) else # Do nothing end Google::Protobuf::Timestamp.new(seconds: t.to_i, nanos: t.nsec) end |
.to_uint32val(num) ⇒ Google::Protobuf::UInt32Value?
72 73 74 75 76 |
# File 'lib/pb.rb', line 72 def to_uint32val(num) return nil if num.nil? return num if num.is_a?(Google::Protobuf::UInt32Value) Google::Protobuf::UInt32Value.new(value: num) end |
.to_uint64val(num) ⇒ Google::Protobuf::UInt64Value?
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pb.rb', line 80 def to_uint64val(num) return nil if num.nil? return num if num.is_a?(Google::Protobuf::UInt64Value) case num when String n = num.to_i else # Integer n = num end Google::Protobuf::UInt64Value.new(value: n) end |