Skip to content

Commit e0cf2d1

Browse files
committed
Refactor propval methods. closes #822
1 parent c11ff74 commit e0cf2d1

31 files changed

+183
-199
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ By far most changes relate to `atomic-server`, so if not specified, assume the c
55
**Changes to JS assets (including the front-end and JS libraries) are not shown here**, but in [`/browser/CHANGELOG`](/browser/CHANGELOG.md).
66
See [STATUS.md](server/STATUS.md) to learn more about which features will remain stable.
77

8-
## [v0.36.2] - 2024-01-05
8+
## UNRELEASED
99

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

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

cli/src/commit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn set(context: &Context) -> AtomicResult<()> {
1111
Ok(r) => r,
1212
Err(_) => atomic_lib::Resource::new(subject),
1313
};
14-
resource.set_propval_shortname(&property, &value, &context.store)?;
14+
resource.set_shortname(&property, &value, &context.store)?;
1515
resource.save(&context.store)?;
1616
Ok(())
1717
}
@@ -34,7 +34,7 @@ pub fn edit(context: &Context) -> AtomicResult<()> {
3434
let edited = edit::edit(current_val)?;
3535
// Remove newline - or else I can's save shortnames or numbers using vim;
3636
let trimmed = edited.trim_end_matches('\n');
37-
resource.set_propval_shortname(&prop, trimmed, &context.store)?;
37+
resource.set_shortname(&prop, trimmed, &context.store)?;
3838
resource.save(&context.store)?;
3939
Ok(())
4040
}

cli/src/new.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn prompt_instance(
6060

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

63-
new_resource.set_propval(
63+
new_resource.set(
6464
"https://atomicdata.dev/properties/isA".into(),
6565
Value::from(vec![class.subject.clone()]),
6666
&context.store,
@@ -69,7 +69,7 @@ fn prompt_instance(
6969
for prop_subject in &class.requires {
7070
let field = context.store.get_property(prop_subject)?;
7171
if field.subject == atomic_lib::urls::SHORTNAME && preferred_shortname.clone().is_some() {
72-
new_resource.set_propval_string(
72+
new_resource.set_string(
7373
field.subject.clone(),
7474
&preferred_shortname.clone().unwrap(),
7575
&context.store,
@@ -86,7 +86,7 @@ fn prompt_instance(
8686
let mut input = prompt_field(&field, false, context)?;
8787
loop {
8888
if let Some(i) = input {
89-
new_resource.set_propval_string(field.subject.clone(), &i, &context.store)?;
89+
new_resource.set_string(field.subject.clone(), &i, &context.store)?;
9090
break;
9191
} else {
9292
println!("Required field, please enter a value.");
@@ -100,7 +100,7 @@ fn prompt_instance(
100100
println!("{}: {}", field.shortname.bold().blue(), field.description);
101101
let input = prompt_field(&field, true, context)?;
102102
if let Some(i) = input {
103-
new_resource.set_propval_string(field.subject.clone(), &i, &context.store)?;
103+
new_resource.set_string(field.subject.clone(), &i, &context.store)?;
104104
}
105105
}
106106

lib/benches/benchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn random_atom() -> Atom {
1616

1717
fn random_resource(atom: &Atom) -> Resource {
1818
let mut resource = Resource::new(atom.subject.clone());
19-
resource.set_propval_unsafe(atom.property.clone(), atom.value.clone());
19+
resource.set_unsafe(atom.property.clone(), atom.value.clone());
2020
resource
2121
}
2222

lib/examples/basic.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
11
// Should be the same as code in `lib.rs`
22

3-
fn main() {
3+
use atomic_lib::errors::AtomicResult;
4+
5+
fn main() -> AtomicResult<()> {
46
// Import the `Storelike` trait to get access to most functions
57
use atomic_lib::Storelike;
68
// Start with initializing the in-memory store
7-
let store = atomic_lib::Store::init().unwrap();
9+
let store = atomic_lib::Store::init()?;
810
// Pre-load the default Atomic Data Atoms (from atomicdata.dev),
911
// this is not necessary, but will probably make your project a bit faster
10-
store.populate().unwrap();
12+
store.populate()?;
1113
// We can create a new Resource, linked to the store.
1214
// Note that since this store only exists in memory, it's data cannot be accessed from the internet.
1315
// Let's make a new Property instance! Let's create "age".
1416
let mut new_property =
15-
atomic_lib::Resource::new_instance("https://atomicdata.dev/classes/Property", &store)
16-
.unwrap();
17+
atomic_lib::Resource::new_instance("https://atomicdata.dev/classes/Property", &store)?;
1718
// And add a description for that Property
18-
new_property
19-
.set_propval_shortname("description", "the age of a person", &store)
20-
.unwrap();
19+
new_property.set_shortname("description", "the age of a person", &store)?;
2120
// A subject URL for the new resource has been created automatically.
2221
let subject = new_property.get_subject().clone();
2322
// Now we need to make sure these changes are also applied to the store.
2423
// In order to change things in the store, we should use Commits,
2524
// which are signed pieces of data that contain state changes.
2625
// Because these are signed, we need an Agent, which has a private key to sign Commits.
2726
// If you want to use an _existing_ agent, use atomic_lib::Agent::from_secret
28-
let agent = store.create_agent(Some("my_agent")).unwrap();
27+
let agent = store.create_agent(Some("my_agent"))?;
2928
store.set_default_agent(agent);
3029
// Saving locally means it is _not_ send to the server. Use `.save` otherwise.
3130
let _fails = new_property.save_locally(&store);
3231
// But.. when we commit, we get an error!
3332
// Because we haven't set all the properties required for the Property class.
3433
// We still need to set `shortname` and `datatype`.
3534
new_property
36-
.set_propval_shortname("shortname", "age", &store)
37-
.unwrap();
38-
new_property
39-
.set_propval_shortname("datatype", atomic_lib::urls::INTEGER, &store)
40-
.unwrap();
41-
new_property.save_locally(&store).unwrap();
35+
.set_shortname("shortname", "age", &store)?
36+
.set_shortname("datatype", atomic_lib::urls::INTEGER, &store)?
37+
.save_locally(&store)?;
4238
// Now the changes to the resource applied to the in-memory store, and we can fetch the newly created resource!
43-
let fetched_new_resource = store.get_resource(&subject).unwrap();
39+
let fetched_new_resource = store.get_resource(&subject)?;
4440
assert!(
4541
fetched_new_resource
46-
.get_shortname("description", &store)
47-
.unwrap()
42+
.get_shortname("description", &store)?
4843
.to_string()
4944
== "the age of a person"
5045
);
46+
Ok(())
5147
}

lib/src/agents.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ impl Agent {
6666
resource.set_class(urls::AGENT);
6767
resource.set_subject(self.subject.clone());
6868
if let Some(name) = &self.name {
69-
resource.set_propval_unsafe(crate::urls::NAME.into(), Value::String(name.into()));
69+
resource.set_unsafe(crate::urls::NAME.into(), Value::String(name.into()));
7070
}
71-
resource.set_propval_unsafe(
71+
resource.set_unsafe(
7272
crate::urls::PUBLIC_KEY.into(),
7373
Value::String(self.public_key.clone()),
7474
);
7575
// Agents must be read by anyone when validating their keys
76-
resource.push_propval(crate::urls::READ, urls::PUBLIC_AGENT.into(), true)?;
77-
resource.set_propval_unsafe(
76+
resource.push(crate::urls::READ, urls::PUBLIC_AGENT.into(), true)?;
77+
resource.set_unsafe(
7878
crate::urls::CREATED_AT.into(),
7979
Value::Timestamp(self.created_at),
8080
);

lib/src/collections.rs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,36 @@ impl CollectionBuilder {
4343
let mut resource = store.get_resource_new(&self.subject);
4444
resource.set_class(urls::COLLECTION);
4545
if let Some(val) = &self.property {
46-
resource.set_propval_string(crate::urls::COLLECTION_PROPERTY.into(), val, store)?;
46+
resource.set_string(crate::urls::COLLECTION_PROPERTY.into(), val, store)?;
4747
}
4848
if let Some(val) = &self.value {
49-
resource.set_propval_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
49+
resource.set_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
5050
}
5151
if let Some(val) = &self.name {
52-
resource.set_propval_string(crate::urls::NAME.into(), val, store)?;
52+
resource.set_string(crate::urls::NAME.into(), val, store)?;
5353
}
5454
if let Some(val) = &self.sort_by {
55-
resource.set_propval_string(crate::urls::COLLECTION_SORT_BY.into(), val, store)?;
55+
resource.set_string(crate::urls::COLLECTION_SORT_BY.into(), val, store)?;
5656
}
5757
if self.include_nested {
58-
resource.set_propval_string(
59-
crate::urls::COLLECTION_INCLUDE_NESTED.into(),
60-
"true",
61-
store,
62-
)?;
58+
resource.set_string(crate::urls::COLLECTION_INCLUDE_NESTED.into(), "true", store)?;
6359
}
6460
if self.include_external {
65-
resource.set_propval_string(
61+
resource.set_string(
6662
crate::urls::COLLECTION_INCLUDE_EXTERNAL.into(),
6763
"true",
6864
store,
6965
)?;
7066
}
7167
if self.sort_desc {
72-
resource.set_propval_string(crate::urls::COLLECTION_SORT_DESC.into(), "true", store)?;
68+
resource.set_string(crate::urls::COLLECTION_SORT_DESC.into(), "true", store)?;
7369
}
74-
resource.set_propval_string(
70+
resource.set_string(
7571
crate::urls::COLLECTION_CURRENT_PAGE.into(),
7672
&self.current_page.to_string(),
7773
store,
7874
)?;
79-
resource.set_propval(
75+
resource.set(
8076
crate::urls::COLLECTION_PAGE_SIZE.into(),
8177
self.page_size.into(),
8278
store,
@@ -259,7 +255,7 @@ impl Collection {
259255
resource: &mut Resource,
260256
store: &impl Storelike,
261257
) -> AtomicResult<crate::Resource> {
262-
resource.set_propval(
258+
resource.set(
263259
crate::urls::COLLECTION_MEMBERS.into(),
264260
if let Some(nested_members) = &self.members_nested {
265261
nested_members.clone().into()
@@ -269,46 +265,42 @@ impl Collection {
269265
store,
270266
)?;
271267
if let Some(prop) = &self.property {
272-
resource.set_propval_string(crate::urls::COLLECTION_PROPERTY.into(), prop, store)?;
268+
resource.set_string(crate::urls::COLLECTION_PROPERTY.into(), prop, store)?;
273269
}
274270
if self.include_nested {
275-
resource.set_propval_string(
276-
crate::urls::COLLECTION_INCLUDE_NESTED.into(),
277-
"true",
278-
store,
279-
)?;
271+
resource.set_string(crate::urls::COLLECTION_INCLUDE_NESTED.into(), "true", store)?;
280272
}
281273
if self.include_external {
282-
resource.set_propval_string(
274+
resource.set_string(
283275
crate::urls::COLLECTION_INCLUDE_EXTERNAL.into(),
284276
"true",
285277
store,
286278
)?;
287279
}
288280
if let Some(val) = &self.value {
289-
resource.set_propval_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
281+
resource.set_string(crate::urls::COLLECTION_VALUE.into(), val, store)?;
290282
}
291283
if let Some(val) = &self.name {
292-
resource.set_propval_string(crate::urls::NAME.into(), val, store)?;
284+
resource.set_string(crate::urls::NAME.into(), val, store)?;
293285
}
294-
resource.set_propval(
286+
resource.set(
295287
crate::urls::COLLECTION_MEMBER_COUNT.into(),
296288
self.total_items.into(),
297289
store,
298290
)?;
299291
let classes: Vec<String> = vec![crate::urls::COLLECTION.into()];
300-
resource.set_propval(crate::urls::IS_A.into(), classes.into(), store)?;
301-
resource.set_propval(
292+
resource.set(crate::urls::IS_A.into(), classes.into(), store)?;
293+
resource.set(
302294
crate::urls::COLLECTION_TOTAL_PAGES.into(),
303295
self.total_pages.into(),
304296
store,
305297
)?;
306-
resource.set_propval(
298+
resource.set(
307299
crate::urls::COLLECTION_CURRENT_PAGE.into(),
308300
self.current_page.into(),
309301
store,
310302
)?;
311-
resource.set_propval(
303+
resource.set(
312304
crate::urls::COLLECTION_PAGE_SIZE.into(),
313305
self.page_size.into(),
314306
store,
@@ -429,9 +421,9 @@ pub fn create_collection_resource_for_class(
429421
format!("{}/collections", drive)
430422
};
431423

432-
collection_resource.set_propval_string(urls::PARENT.into(), &parent, store)?;
424+
collection_resource.set_string(urls::PARENT.into(), &parent, store)?;
433425

434-
collection_resource.set_propval_string(urls::NAME.into(), &pluralized, store)?;
426+
collection_resource.set_string(urls::NAME.into(), &pluralized, store)?;
435427

436428
// Should we use save_locally, which creates commits, or add_resource_unsafe, which is faster?
437429
Ok(collection_resource)
@@ -607,9 +599,9 @@ mod test {
607599
fn sorting_resources() {
608600
let prop = urls::DESCRIPTION.to_string();
609601
let mut a = Resource::new("first".into());
610-
a.set_propval_unsafe(prop.clone(), Value::Markdown("1".into()));
602+
a.set_unsafe(prop.clone(), Value::Markdown("1".into()));
611603
let mut b = Resource::new("second".into());
612-
b.set_propval_unsafe(prop.clone(), Value::Markdown("2".into()));
604+
b.set_unsafe(prop.clone(), Value::Markdown("2".into()));
613605
let c = Resource::new("third_missing_property".into());
614606

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

0 commit comments

Comments
 (0)