-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor propval methods. closes #822
- Loading branch information
Showing
31 changed files
with
183 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,47 @@ | ||
// Should be the same as code in `lib.rs` | ||
|
||
fn main() { | ||
use atomic_lib::errors::AtomicResult; | ||
|
||
fn main() -> AtomicResult<()> { | ||
// Import the `Storelike` trait to get access to most functions | ||
use atomic_lib::Storelike; | ||
// Start with initializing the in-memory store | ||
let store = atomic_lib::Store::init().unwrap(); | ||
let store = atomic_lib::Store::init()?; | ||
// Pre-load the default Atomic Data Atoms (from atomicdata.dev), | ||
// this is not necessary, but will probably make your project a bit faster | ||
store.populate().unwrap(); | ||
store.populate()?; | ||
// We can create a new Resource, linked to the store. | ||
// Note that since this store only exists in memory, it's data cannot be accessed from the internet. | ||
// Let's make a new Property instance! Let's create "age". | ||
let mut new_property = | ||
atomic_lib::Resource::new_instance("https://atomicdata.dev/classes/Property", &store) | ||
.unwrap(); | ||
atomic_lib::Resource::new_instance("https://atomicdata.dev/classes/Property", &store)?; | ||
// And add a description for that Property | ||
new_property | ||
.set_propval_shortname("description", "the age of a person", &store) | ||
.unwrap(); | ||
new_property.set_shortname("description", "the age of a person", &store)?; | ||
// A subject URL for the new resource has been created automatically. | ||
let subject = new_property.get_subject().clone(); | ||
// Now we need to make sure these changes are also applied to the store. | ||
// In order to change things in the store, we should use Commits, | ||
// which are signed pieces of data that contain state changes. | ||
// Because these are signed, we need an Agent, which has a private key to sign Commits. | ||
// If you want to use an _existing_ agent, use atomic_lib::Agent::from_secret | ||
let agent = store.create_agent(Some("my_agent")).unwrap(); | ||
let agent = store.create_agent(Some("my_agent"))?; | ||
store.set_default_agent(agent); | ||
// Saving locally means it is _not_ send to the server. Use `.save` otherwise. | ||
let _fails = new_property.save_locally(&store); | ||
// But.. when we commit, we get an error! | ||
// Because we haven't set all the properties required for the Property class. | ||
// We still need to set `shortname` and `datatype`. | ||
new_property | ||
.set_propval_shortname("shortname", "age", &store) | ||
.unwrap(); | ||
new_property | ||
.set_propval_shortname("datatype", atomic_lib::urls::INTEGER, &store) | ||
.unwrap(); | ||
new_property.save_locally(&store).unwrap(); | ||
.set_shortname("shortname", "age", &store)? | ||
.set_shortname("datatype", atomic_lib::urls::INTEGER, &store)? | ||
.save_locally(&store)?; | ||
// Now the changes to the resource applied to the in-memory store, and we can fetch the newly created resource! | ||
let fetched_new_resource = store.get_resource(&subject).unwrap(); | ||
let fetched_new_resource = store.get_resource(&subject)?; | ||
assert!( | ||
fetched_new_resource | ||
.get_shortname("description", &store) | ||
.unwrap() | ||
.get_shortname("description", &store)? | ||
.to_string() | ||
== "the age of a person" | ||
); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.