Class: IcAgent::Candid::RecordClass
Instance Method Summary
collapse
#check_type, #encode_type
Methods inherited from BaseType
_build_type_table_impl, #build_type_table, check_type, covariant, decode_value, encode_type, encode_value
Constructor Details
Returns a new instance of RecordClass.
760
761
762
763
|
# File 'lib/ic_agent/candid.rb', line 760
def initialize(field)
super()
@fields = field.sort_by { |k, _v| IcAgent::Utils.label_hash(k) }.to_h
end
|
Instance Method Details
#_build_type_table_impl(type_table) ⇒ Object
793
794
795
796
797
798
799
800
801
802
803
804
805
|
# File 'lib/ic_agent/candid.rb', line 793
def _build_type_table_impl(type_table)
@fields.values.each do |field_value|
field_value.send(:build_type_table, type_table)
end
op_code = LEB128.encode_signed(TypeIds::Record).string
length = LEB128.encode_signed(@fields.size).string
fields = @fields.map { |k, v|
LEB128.encode_signed(IcAgent::Utils.label_hash(k)).string + v.encode_type(type_table)
}.join.b
type_table.add(self, op_code + length + fields)
end
|
#covariant(x) ⇒ Object
777
778
779
780
781
782
783
784
785
786
|
# File 'lib/ic_agent/candid.rb', line 777
def covariant(x)
raise ArgumentError, 'Expected dict type input.' unless x.is_a?(Hash)
@fields.each do |k, v|
raise ArgumentError, "Record is missing key #{k}" unless x.key?(k)
return false unless v.covariant(x[k])
end
true
end
|
#decode_value(b, t) ⇒ Object
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
|
# File 'lib/ic_agent/candid.rb', line 807
def decode_value(b, t)
record = check_type(t)
raise ArgumentError, 'Not a record type' unless record.is_a?(RecordClass)
x = {}
idx = 0
keys = @fields.keys
@fields.each do |k, v|
if idx >= @fields.length || IcAgent::Utils.label_hash(keys[idx]) != IcAgent::Utils.label_hash(k)
v.decode_value(b, v)
next
end
expect_key = keys[idx]
expected_value = @fields[expect_key]
x[expect_key] = expected_value.decode_value(b, v)
idx += 1
end
raise ArgumentError, "Cannot find field #{keys[idx]}" if idx < @fields.length
x
end
|
#display ⇒ Object
841
842
843
844
845
|
# File 'lib/ic_agent/candid.rb', line 841
def display
d = {}
@fields.each { |k, v| d[v] = v.display }
"record #{d}"
end
|
#encode_value(val) ⇒ Object
788
789
790
791
|
# File 'lib/ic_agent/candid.rb', line 788
def encode_value(val)
bufs = @fields.map { |k, v| v.encode_value(val[k]) }
bufs.join.b
end
|
#id ⇒ Object
837
838
839
|
# File 'lib/ic_agent/candid.rb', line 837
def id
TypeIds::Record
end
|
#name ⇒ Object
832
833
834
835
|
# File 'lib/ic_agent/candid.rb', line 832
def name
fields = @fields.map { |k, v| "#{k}:#{v.name}" }.join(';')
"record {#{fields}}"
end
|
#try_as_tuple ⇒ Object
765
766
767
768
769
770
771
772
773
774
775
|
# File 'lib/ic_agent/candid.rb', line 765
def try_as_tuple
res = []
idx = 0
@fields.each do |k, v|
return nil unless k == "_#{idx}_"
res << v
idx += 1
end
res
end
|