Phase Two: Data Structure
Overview
Before move into higher logic like encryption, let's refine our Note
struct. Since we currently have no version control, this upgrade will break the previous Note
implementation.
We'll start by recreating a Note
that can accept different types of data. To support this, we'll create a data
module.
Creating the Data Module
In the sources
folder, create a file named data.move
.
Define the module name and import useful modules:
// Define the module name `data`
module suinotes::data;
// Import the `String` struct from the `string` module in the `std` package
use std::string::String;
Now, let's define the Data
struct:
public struct Data has store, copy, drop {
data_type: u8,
value: String,
}
This struct has three out of four abilities:
store
: Can be stored in another object.copy
: The struct can be duplicated.drop
: The struct can be ignored after creation.
You can read more about abilities here: link_to_resources
Standardizing the data_type
Attribute
We will standardize the data_type
attribute by creating constants:
const TEXT: u8 = 0;
const LONG_TEXT: u8 = 1;
const ENCRYPTED_TEXT: u8 = 2;
Constants cannot be accessed directly outside their module. To expose them, we create public functions:
public fun long_text(): u8 {
LONG_TEXT
}
public fun text(): u8 {
TEXT
}
public fun encrypted_text(): u8 {
ENCRYPTED_TEXT
}
Creating and Editing Data
Now, let's add create
and edit
functions:
public fun create(data_type: u8, value: String): Data {
let data = Data { data_type, value };
data
}
public fun edit(data: &mut Data, value: String) {
data.value = value;
}
That's all for this module.
Next Up: Modify the Note
struct and add encryption logic.