From 1094f182fd34e44670b6317b0796eb48371e2a3a Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 9 Aug 2024 22:04:02 -0700 Subject: [PATCH] [framework][simple] Add method to mutate PropertyMap before storing --- .../aptos-token-objects/doc/property_map.md | 28 ++++++++++++++++++- .../sources/property_map.move | 7 ++++- .../ambassador/sources/ambassador.move | 6 +--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/aptos-move/framework/aptos-token-objects/doc/property_map.md b/aptos-move/framework/aptos-token-objects/doc/property_map.md index eec8f62dc627e..048b4de8b9b07 100644 --- a/aptos-move/framework/aptos-token-objects/doc/property_map.md +++ b/aptos-move/framework/aptos-token-objects/doc/property_map.md @@ -39,6 +39,7 @@ represent types and storing values in bcs format. - [Function `add`](#0x4_property_map_add) - [Function `add_typed`](#0x4_property_map_add_typed) - [Function `add_internal`](#0x4_property_map_add_internal) +- [Function `add_typed_to_struct`](#0x4_property_map_add_typed_to_struct) - [Function `update`](#0x4_property_map_update) - [Function `update_typed`](#0x4_property_map_update_typed) - [Function `update_internal`](#0x4_property_map_update_internal) @@ -67,7 +68,7 @@ should keep track of what keys are what types, and parse them accordingly.
#[resource_group_member(#[group = 0x1::object::ObjectGroup])]
-struct PropertyMap has drop, key
+struct PropertyMap has drop, store, key
 
@@ -1129,6 +1130,31 @@ Add a property that isn't already encoded as a + +## Function `add_typed_to_struct` + + + +
public fun add_typed_to_struct<T: drop>(property_map: &mut property_map::PropertyMap, key: string::String, value: T)
+
+ + + +
+Implementation + + +
public fun add_typed_to_struct<T: drop>(property_map: &mut PropertyMap, key: String, value: T) {
+    let type = type_info_to_internal_type<T>();
+    simple_map::add(&mut property_map.inner, key, PropertyValue { type, value: bcs::to_bytes(&value)});
+}
+
+ + +
diff --git a/aptos-move/framework/aptos-token-objects/sources/property_map.move b/aptos-move/framework/aptos-token-objects/sources/property_map.move index 53d983060064e..e71c120fa2456 100644 --- a/aptos-move/framework/aptos-token-objects/sources/property_map.move +++ b/aptos-move/framework/aptos-token-objects/sources/property_map.move @@ -51,7 +51,7 @@ module aptos_token_objects::property_map { #[resource_group_member(group = aptos_framework::object::ObjectGroup)] /// A Map for typed key to value mapping, the contract using it /// should keep track of what keys are what types, and parse them accordingly. - struct PropertyMap has drop, key { + struct PropertyMap has drop, key, store { inner: SimpleMap, } @@ -315,6 +315,11 @@ module aptos_token_objects::property_map { simple_map::add(&mut property_map.inner, key, PropertyValue { type, value }); } + public fun add_typed_to_struct(property_map: &mut PropertyMap, key: String, value: T) { + let type = type_info_to_internal_type(); + simple_map::add(&mut property_map.inner, key, PropertyValue { type, value: bcs::to_bytes(&value)}); + } + /// Updates a property in place already bcs encoded public fun update(ref: &MutatorRef, key: &String, type: String, value: vector) acquires PropertyMap { let new_type = to_internal_type(type); diff --git a/aptos-move/move-examples/token_objects/ambassador/sources/ambassador.move b/aptos-move/move-examples/token_objects/ambassador/sources/ambassador.move index 71cddaf6f6769..159ac3a98cad2 100644 --- a/aptos-move/move-examples/token_objects/ambassador/sources/ambassador.move +++ b/aptos-move/move-examples/token_objects/ambassador/sources/ambassador.move @@ -234,12 +234,8 @@ module ambassador::ambassador { // Initialize the property map and the ambassador rank as Bronze let properties = property_map::prepare_input(vector[], vector[], vector[]); + property_map::add_typed_to_struct(&mut properties, string::utf8(b"Rank"), string::utf8(RANK_BRONZE)); property_map::init(&constructor_ref, properties); - property_map::add_typed( - &property_mutator_ref, - string::utf8(b"Rank"), - string::utf8(RANK_BRONZE) - ); // Publishes the AmbassadorToken resource with the refs. let ambassador_token = AmbassadorToken {