def gen_store_code_for_field(f, msg, parent_struct_name): tmpl = "" if util.field_is_message(f) and util.field_is_repeated(f): tmpl = ( " output.{field}.length = input.{field}.length; \n" " for(uint i{i}=0; i{i}<input.{field}.length; i{i}++) {{ \n" " {lib}.store(input.{field}[i{i}], output.{field}[i{i}]); \n" " {map_insert_code}" " }}" ) elif util.field_is_message(f): tmpl = ( " {lib}.store(input.{field}, output.{field}); \n" ) else: return ( " output.{field} = input.{field}; \n" ).format( field = f.name, ) libname = util.gen_struct_codec_lib_name_from_field(f) return tmpl.format( i = f.number, field = f.name, lib = libname, map_insert_code = gen_map_insert_on_store(f, msg, parent_struct_name) )
def gen_value_copy_code(value_field, dst_flagment): if util.field_is_message(value_field): return ("{struct_name}.store(value, {dst}.value);").format( struct_name=util.gen_struct_codec_lib_name_from_field(value_field), dst=dst_flagment) else: return ("{dst}.value = value;").format(dst=dst_flagment)
def gen_store_code_for_field(f, msg, parent_struct_name): tmpl = "" if util.field_is_message(f) and util.field_is_repeated(f): tmpl = sol_constants.STORE_REPEATED elif util.field_is_message(f): tmpl = sol_constants.STORE_MESSAGE else: return (sol_constants.STORE_OTHER).format(field=f.name) libname = util.gen_struct_codec_lib_name_from_field(f) return tmpl.format(i=f.number, field=f.name, lib=libname, map_insert_code=gen_map_insert_on_store( f, msg, parent_struct_name))
def gen_map_helper_codes_for_field(f, nested_type): kf = nested_type.field[0] vf = nested_type.field[1] return (""" //map helpers for {name} function get_{name}(Data storage self, {key_type} key) internal view returns ({value_type} {storage_type}) {{ return {val_name}[{map_name}[key] - 1].value; }} function search_{name}(Data storage self, {key_type} key) internal view returns (bool, {value_type} {storage_type}) {{ if ({map_name}[key] <= 0) {{ return (false, {val_name}[0].value); }} return (true, {val_name}[{map_name}[key] - 1].value); }} function add_{name}(Data storage self, {key_type} key, {value_type} value) internal {{ if ({map_name}[key] != 0) {{ {copy_value_exists} }} else {{ {val_name}.length++; {val_name}[{val_name}.length - 1].key = key; {copy_value_new} {map_name}[key] = {val_name}.length; }} }} function rm_{name}(Data storage self, {key_type} key) internal {{ uint pos = {map_name}[key]; if (pos == 0) {{ return; }} {val_name}[pos - 1] = {val_name}[{val_name}.length - 1]; {val_name}.length--; delete {map_name}[key]; }} """ ).format( name = f.name, val_name="self.{0}".format(f.name), map_name = "self._{0}_map".format(f.name), key_type=util.gen_global_type_name_from_field(kf), value_type=util.gen_global_type_name_from_field(vf), storage_type="storage" if (util.field_is_repeated(vf) or util.field_is_message(vf)) else "", container_type=util.gen_global_type_name_from_field(f), copy_value_exists=gen_value_copy_code(vf, ("self.{0}[self._{0}_map[key] - 1]").format(f.name)), copy_value_new=gen_value_copy_code(vf, ("self.{0}[self.{0}.length - 1]").format(f.name)), );
def gen_struct_decoders(msg, parent_struct_name): return ''.join( list( map((lambda f: gen_struct_decoder(f, msg, parent_struct_name) if util.field_is_message(f) else ""), msg.field)))