Replace auto-generated mappers with direct implementation of subset
This commit is contained in:
parent
64ebe30618
commit
047a7f08d8
@ -3,64 +3,224 @@ require "google/protobuf/wrappers_pb"
|
||||
module VagrantPlugins
|
||||
module CommandServe
|
||||
class Mappers
|
||||
Google::Protobuf.constants.grep(/.Value$/).each do |name|
|
||||
value = Google::Protobuf.const_get(name)
|
||||
next if !value.is_a?(Class)
|
||||
if value.instance_methods.include?(:value)
|
||||
type = value.new.value.class
|
||||
elsif value.instance_methods.include?(:values)
|
||||
type = Array
|
||||
elsif value.instance_methods.include?(:fields)
|
||||
type = Hash
|
||||
|
||||
# Boolean mappers
|
||||
|
||||
class BooleanToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Type::Boolean)],
|
||||
output: Google::Protobuf::BoolValue,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
type = value.new.respond_to?(:value) ?
|
||||
value.new.value.class :
|
||||
value.new.values.class
|
||||
n = type.name.to_s.split("::").last
|
||||
|
||||
Class.new(Mapper) do
|
||||
def converter(proto)
|
||||
v = extract(proto)
|
||||
embiggen(v)
|
||||
end
|
||||
def converter(bool)
|
||||
Google::Protobuf::BoolValue.new(value: bool)
|
||||
end
|
||||
end
|
||||
|
||||
def extract(v)
|
||||
if v.respond_to?(:value)
|
||||
v = v.value
|
||||
elsif v.respond_to?(:values)
|
||||
v = v.values.to_a
|
||||
elsif v.respond_to?(:fields)
|
||||
v = v.fields.to_h
|
||||
else
|
||||
v
|
||||
end
|
||||
end
|
||||
class BooleanFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::BoolValue)],
|
||||
output: Type::Boolean,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def embiggen(v)
|
||||
return v if !v.is_a?(Enumerable)
|
||||
v.class[
|
||||
v.map { |nv|
|
||||
embiggen(extract(nv))
|
||||
}
|
||||
]
|
||||
end
|
||||
end.class_eval "
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: #{value.name})],
|
||||
output: #{type.name},
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
def converter(proto)
|
||||
Type::Boolean.new(value: proto.value)
|
||||
end
|
||||
end
|
||||
|
||||
def self.name
|
||||
'#{name}To#{n}'
|
||||
end
|
||||
class TrueToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: TrueClass)],
|
||||
output: Google::Protobuf::BoolValue,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def to_s
|
||||
'<#{name}To#{n}:' + object_id.to_s + '>'
|
||||
end
|
||||
"
|
||||
def converter(v)
|
||||
Google::Protobuf::BoolValue.new(value: v)
|
||||
end
|
||||
end
|
||||
|
||||
class FalseToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: FalseClass)],
|
||||
output: Google::Protobuf::BoolValue,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(v)
|
||||
Google::Protobuf::BoolValue.new(value: v)
|
||||
end
|
||||
end
|
||||
|
||||
# Bytes mappers
|
||||
|
||||
class BytesFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::BytesValue)],
|
||||
output: String,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(proto)
|
||||
proto.value
|
||||
end
|
||||
end
|
||||
|
||||
# Numeric mappers
|
||||
|
||||
class DoubleFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::DoubleValue)],
|
||||
output: Float,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(num)
|
||||
num.value
|
||||
end
|
||||
end
|
||||
|
||||
class FloatFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::FloatValue)],
|
||||
output: Float,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(num)
|
||||
num.value
|
||||
end
|
||||
end
|
||||
|
||||
class FloatToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Float)],
|
||||
output: Google::Protobuf::FloatValue,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(v)
|
||||
Google::Protobuf::FloatValue.new(value: v)
|
||||
end
|
||||
end
|
||||
|
||||
class Int32FromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::Int32Value)],
|
||||
output: Integer,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(v)
|
||||
v.value
|
||||
end
|
||||
end
|
||||
|
||||
class Int64FromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::Int64Value)],
|
||||
output: Integer,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(v)
|
||||
v.value
|
||||
end
|
||||
end
|
||||
|
||||
class UInt32FromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::UInt32Value)],
|
||||
output: Integer,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(v)
|
||||
v.value
|
||||
end
|
||||
end
|
||||
|
||||
class UInt64FromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::UInt64Value)],
|
||||
output: Integer,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(v)
|
||||
v.value
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class IntegerToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Integer)],
|
||||
output: Google::Protobuf::Int64Value,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(num)
|
||||
Google::Protobuf::Int64Value.new(value: num)
|
||||
end
|
||||
end
|
||||
|
||||
# String mappers
|
||||
|
||||
class StringToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: String)],
|
||||
output: Google::Protobuf::StringValue,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(s)
|
||||
Google::Protobuf::StringValue.new(value: s)
|
||||
end
|
||||
end
|
||||
|
||||
class StringFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: Google::Protobuf::StringValue)],
|
||||
output: String,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(proto)
|
||||
proto.value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user