Skip to content

Commit

Permalink
Refactor propval methods. closes #822
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Jan 29, 2024
1 parent c11ff74 commit e0cf2d1
Show file tree
Hide file tree
Showing 31 changed files with 183 additions and 199 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ By far most changes relate to `atomic-server`, so if not specified, assume the c
**Changes to JS assets (including the front-end and JS libraries) are not shown here**, but in [`/browser/CHANGELOG`](/browser/CHANGELOG.md).
See [STATUS.md](server/STATUS.md) to learn more about which features will remain stable.

## [v0.36.2] - 2024-01-05
## UNRELEASED

- Use `musl` + `alpine` builds for docker images, way smaller images #620
- Support multi-platform docker builds #731
Expand All @@ -17,6 +17,7 @@ See [STATUS.md](server/STATUS.md) to learn more about which features will remain
- Don't use default agent when fetching with Db #787
- Deterministic serialization JSON AD #794
- Fix HTTPS / TLS setup #768
- Refactor `atomic_lib::Resource` propval methods (e.g. `set_propval` => `set`), make them chainable. #822

## [v0.36.1] - 2023-12-06

Expand Down
4 changes: 2 additions & 2 deletions cli/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn set(context: &Context) -> AtomicResult<()> {
Ok(r) => r,
Err(_) => atomic_lib::Resource::new(subject),
};
resource.set_propval_shortname(&property, &value, &context.store)?;
resource.set_shortname(&property, &value, &context.store)?;
resource.save(&context.store)?;
Ok(())
}
Expand All @@ -34,7 +34,7 @@ pub fn edit(context: &Context) -> AtomicResult<()> {
let edited = edit::edit(current_val)?;
// Remove newline - or else I can's save shortnames or numbers using vim;
let trimmed = edited.trim_end_matches('\n');
resource.set_propval_shortname(&prop, trimmed, &context.store)?;
resource.set_shortname(&prop, trimmed, &context.store)?;
resource.save(&context.store)?;
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions cli/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn prompt_instance(

let mut new_resource: Resource = Resource::new(subject.clone());

new_resource.set_propval(
new_resource.set(
"https://atomicdata.dev/properties/isA".into(),
Value::from(vec![class.subject.clone()]),
&context.store,
Expand All @@ -69,7 +69,7 @@ fn prompt_instance(
for prop_subject in &class.requires {
let field = context.store.get_property(prop_subject)?;
if field.subject == atomic_lib::urls::SHORTNAME && preferred_shortname.clone().is_some() {
new_resource.set_propval_string(
new_resource.set_string(
field.subject.clone(),
&preferred_shortname.clone().unwrap(),
&context.store,
Expand All @@ -86,7 +86,7 @@ fn prompt_instance(
let mut input = prompt_field(&field, false, context)?;
loop {
if let Some(i) = input {
new_resource.set_propval_string(field.subject.clone(), &i, &context.store)?;
new_resource.set_string(field.subject.clone(), &i, &context.store)?;
break;
} else {
println!("Required field, please enter a value.");
Expand All @@ -100,7 +100,7 @@ fn prompt_instance(
println!("{}: {}", field.shortname.bold().blue(), field.description);
let input = prompt_field(&field, true, context)?;
if let Some(i) = input {
new_resource.set_propval_string(field.subject.clone(), &i, &context.store)?;
new_resource.set_string(field.subject.clone(), &i, &context.store)?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn random_atom() -> Atom {

fn random_resource(atom: &Atom) -> Resource {
let mut resource = Resource::new(atom.subject.clone());
resource.set_propval_unsafe(atom.property.clone(), atom.value.clone());
resource.set_unsafe(atom.property.clone(), atom.value.clone());
resource
}

Expand Down
32 changes: 14 additions & 18 deletions lib/examples/basic.rs
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(())
}
8 changes: 4 additions & 4 deletions lib/src/agents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ impl Agent {
resource.set_class(urls::AGENT);
resource.set_subject(self.subject.clone());
if let Some(name) = &self.name {
resource.set_propval_unsafe(crate::urls::NAME.into(), Value::String(name.into()));
resource.set_unsafe(crate::urls::NAME.into(), Value::String(name.into()));
}
resource.set_propval_unsafe(
resource.set_unsafe(
crate::urls::PUBLIC_KEY.into(),
Value::String(self.public_key.clone()),
);
// Agents must be read by anyone when validating their keys
resource.push_propval(crate::urls::READ, urls::PUBLIC_AGENT.into(), true)?;
resource.set_propval_unsafe(
resource.push(crate::urls::READ, urls::PUBLIC_AGENT.into(), true)?;
resource.set_unsafe(
crate::urls::CREATED_AT.into(),
Value::Timestamp(self.created_at),
);
Expand Down
56 changes: 24 additions & 32 deletions lib/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,36 @@ impl CollectionBuilder {
let mut resource = store.get_resource_new(&self.subject);
resource.set_class(urls::COLLECTION);
if let Some(val) = &self.property {
resource.set_propval_string(crate::urls::COLLECTION_PROPERTY.into(), val, store)?;
resource.set_string(crate::urls::COLLECTION_PROPERTY.into(), val, store)?;
}
if let Some(val) = &self.value {
resource.set_propval_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
resource.set_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
}
if let Some(val) = &self.name {
resource.set_propval_string(crate::urls::NAME.into(), val, store)?;
resource.set_string(crate::urls::NAME.into(), val, store)?;
}
if let Some(val) = &self.sort_by {
resource.set_propval_string(crate::urls::COLLECTION_SORT_BY.into(), val, store)?;
resource.set_string(crate::urls::COLLECTION_SORT_BY.into(), val, store)?;
}
if self.include_nested {
resource.set_propval_string(
crate::urls::COLLECTION_INCLUDE_NESTED.into(),
"true",
store,
)?;
resource.set_string(crate::urls::COLLECTION_INCLUDE_NESTED.into(), "true", store)?;
}
if self.include_external {
resource.set_propval_string(
resource.set_string(
crate::urls::COLLECTION_INCLUDE_EXTERNAL.into(),
"true",
store,
)?;
}
if self.sort_desc {
resource.set_propval_string(crate::urls::COLLECTION_SORT_DESC.into(), "true", store)?;
resource.set_string(crate::urls::COLLECTION_SORT_DESC.into(), "true", store)?;
}
resource.set_propval_string(
resource.set_string(
crate::urls::COLLECTION_CURRENT_PAGE.into(),
&self.current_page.to_string(),
store,
)?;
resource.set_propval(
resource.set(
crate::urls::COLLECTION_PAGE_SIZE.into(),
self.page_size.into(),
store,
Expand Down Expand Up @@ -259,7 +255,7 @@ impl Collection {
resource: &mut Resource,
store: &impl Storelike,
) -> AtomicResult<crate::Resource> {
resource.set_propval(
resource.set(
crate::urls::COLLECTION_MEMBERS.into(),
if let Some(nested_members) = &self.members_nested {
nested_members.clone().into()
Expand All @@ -269,46 +265,42 @@ impl Collection {
store,
)?;
if let Some(prop) = &self.property {
resource.set_propval_string(crate::urls::COLLECTION_PROPERTY.into(), prop, store)?;
resource.set_string(crate::urls::COLLECTION_PROPERTY.into(), prop, store)?;
}
if self.include_nested {
resource.set_propval_string(
crate::urls::COLLECTION_INCLUDE_NESTED.into(),
"true",
store,
)?;
resource.set_string(crate::urls::COLLECTION_INCLUDE_NESTED.into(), "true", store)?;
}
if self.include_external {
resource.set_propval_string(
resource.set_string(
crate::urls::COLLECTION_INCLUDE_EXTERNAL.into(),
"true",
store,
)?;
}
if let Some(val) = &self.value {
resource.set_propval_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
resource.set_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
}
if let Some(val) = &self.name {
resource.set_propval_string(crate::urls::NAME.into(), val, store)?;
resource.set_string(crate::urls::NAME.into(), val, store)?;
}
resource.set_propval(
resource.set(
crate::urls::COLLECTION_MEMBER_COUNT.into(),
self.total_items.into(),
store,
)?;
let classes: Vec<String> = vec![crate::urls::COLLECTION.into()];
resource.set_propval(crate::urls::IS_A.into(), classes.into(), store)?;
resource.set_propval(
resource.set(crate::urls::IS_A.into(), classes.into(), store)?;
resource.set(
crate::urls::COLLECTION_TOTAL_PAGES.into(),
self.total_pages.into(),
store,
)?;
resource.set_propval(
resource.set(
crate::urls::COLLECTION_CURRENT_PAGE.into(),
self.current_page.into(),
store,
)?;
resource.set_propval(
resource.set(
crate::urls::COLLECTION_PAGE_SIZE.into(),
self.page_size.into(),
store,
Expand Down Expand Up @@ -429,9 +421,9 @@ pub fn create_collection_resource_for_class(
format!("{}/collections", drive)
};

collection_resource.set_propval_string(urls::PARENT.into(), &parent, store)?;
collection_resource.set_string(urls::PARENT.into(), &parent, store)?;

collection_resource.set_propval_string(urls::NAME.into(), &pluralized, store)?;
collection_resource.set_string(urls::NAME.into(), &pluralized, store)?;

// Should we use save_locally, which creates commits, or add_resource_unsafe, which is faster?
Ok(collection_resource)
Expand Down Expand Up @@ -607,9 +599,9 @@ mod test {
fn sorting_resources() {
let prop = urls::DESCRIPTION.to_string();
let mut a = Resource::new("first".into());
a.set_propval_unsafe(prop.clone(), Value::Markdown("1".into()));
a.set_unsafe(prop.clone(), Value::Markdown("1".into()));
let mut b = Resource::new("second".into());
b.set_propval_unsafe(prop.clone(), Value::Markdown("2".into()));
b.set_unsafe(prop.clone(), Value::Markdown("2".into()));
let c = Resource::new("third_missing_property".into());

let asc = vec![a.clone(), b.clone(), c.clone()];
Expand Down
Loading

0 comments on commit e0cf2d1

Please sign in to comment.