Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor resource #823

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions browser/data-browser/src/routes/NewResource/NewRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function NewResourceSelector() {
(files: string[]) => {
toast.success(`Uploaded ${files.length} files.`);

if (parentSubject) {
navigate(constructOpenURL(parentSubject));
if (calculatedParent) {
navigate(constructOpenURL(calculatedParent));
}
},
[parentSubject, navigate],
Expand All @@ -64,10 +64,10 @@ function NewResourceSelector() {
<StyledForm>
<h1>
Create new resource{' '}
{parentSubject && (
{calculatedParent && (
<>
{`under `}
<ResourceInline subject={parentSubject} />
<ResourceInline subject={calculatedParent} />
</>
)}
</h1>
Expand Down
2 changes: 1 addition & 1 deletion browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"typedoc": "^0.25.3",
"typedoc-plugin-missing-exports": "^2.1.0",
"typescript": "^5.3.2",
"vite": "^5.0.2"
"vite": "^5.0.12"
},
"name": "@tomic/root",
"version": "0.36.0",
Expand Down
56 changes: 36 additions & 20 deletions browser/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading
Loading