Skip to content

Commit

Permalink
[framework][simple] Add method to mutate PropertyMap before storing
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-aptos committed Dec 12, 2024
1 parent 3013155 commit 80291e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
28 changes: 27 additions & 1 deletion aptos-move/framework/aptos-token-objects/doc/property_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -67,7 +68,7 @@ should keep track of what keys are what types, and parse them accordingly.


<pre><code>#[resource_group_member(#[group = <a href="../../aptos-framework/doc/object.md#0x1_object_ObjectGroup">0x1::object::ObjectGroup</a>])]
<b>struct</b> <a href="property_map.md#0x4_property_map_PropertyMap">PropertyMap</a> <b>has</b> drop, key
<b>struct</b> <a href="property_map.md#0x4_property_map_PropertyMap">PropertyMap</a> <b>has</b> drop, store, key
</code></pre>


Expand Down Expand Up @@ -1129,6 +1130,31 @@ Add a property that isn't already encoded as a <code><a href="../../aptos-framew



</details>

<a id="0x4_property_map_add_typed_to_struct"></a>

## Function `add_typed_to_struct`



<pre><code><b>public</b> <b>fun</b> <a href="property_map.md#0x4_property_map_add_typed_to_struct">add_typed_to_struct</a>&lt;T: drop&gt;(<a href="property_map.md#0x4_property_map">property_map</a>: &<b>mut</b> <a href="property_map.md#0x4_property_map_PropertyMap">property_map::PropertyMap</a>, key: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, value: T)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="property_map.md#0x4_property_map_add_typed_to_struct">add_typed_to_struct</a>&lt;T: drop&gt;(<a href="property_map.md#0x4_property_map">property_map</a>: &<b>mut</b> <a href="property_map.md#0x4_property_map_PropertyMap">PropertyMap</a>, key: String, value: T) {
<b>let</b> type = <a href="property_map.md#0x4_property_map_type_info_to_internal_type">type_info_to_internal_type</a>&lt;T&gt;();
<a href="../../aptos-framework/../aptos-stdlib/doc/simple_map.md#0x1_simple_map_add">simple_map::add</a>(&<b>mut</b> <a href="property_map.md#0x4_property_map">property_map</a>.inner, key, <a href="property_map.md#0x4_property_map_PropertyValue">PropertyValue</a> { type, value: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(&value)});
}
</code></pre>



</details>

<a id="0x4_property_map_update"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, PropertyValue>,
}

Expand Down Expand Up @@ -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<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)});
}

/// Updates a property in place already bcs encoded
public fun update(ref: &MutatorRef, key: &String, type: String, value: vector<u8>) acquires PropertyMap {
let new_type = to_internal_type(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 80291e3

Please sign in to comment.