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

Class Method Details

.to_boolval(b) ⇒ Google::Protobuf::BoolValue?

Parameters:

  • b (bool, nil)

Returns:

  • (Google::Protobuf::BoolValue, nil)


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?

Returns Protobuf message object.

Parameters:

  • klass (Class)

    Protobuf message class

  • v (Object, nil)

Returns:

  • (Object, nil)

    Protobuf message 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
    to_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?

Parameters:

  • bytes (String, nil)

Returns:

  • (Google::Protobuf::BytesValue, nil)


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?

Parameters:

  • num (Float, nil)

Returns:

  • (Google::Protobuf::DoubleValue, nil)


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?

Parameters:

  • num (Float, nil)

Returns:

  • (Google::Protobuf::FloatValue, nil)


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?

Parameters:

  • num (Integer, nil)

Returns:

  • (Google::Protobuf::Int32Value, nil)


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?

Parameters:

  • num (String, Integer, nil)

Returns:

  • (Google::Protobuf::Int64Value, nil)


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

Parameters:

  • type (Symbol)

    The type of protobuf field. e.g. :enum, :int64, :string, :int32, :bool, etc.

  • v (Object)

Returns:

  • (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

Parameters:

  • klass (Class)

    A Protobuf message class

  • v (Hash, Array<Hash>, nil)

    A Hash object or An Array of Hash objects. Hash

Returns:

  • (Object, Array<Object>, nil)

    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?

Parameters:

  • (String, nil)

Returns:

  • (Google::Protobuf::StringValue, nil)


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?

Parameters:

  • t (Time, DateTime, Date, String, nil)

Returns:

  • (Google::Protobuf::Timestamp, nil)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pb.rb', line 26

def to_timestamp(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?

Parameters:

  • num (Integer, nil)

Returns:

  • (Google::Protobuf::UInt32Value, nil)


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?

Parameters:

  • num (String, Integer, nil)

Returns:

  • (Google::Protobuf::UInt64Value, nil)


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