Skip to content

Commit

Permalink
Support implicit values in JSON models (#154)
Browse files Browse the repository at this point in the history
* Support implicit values in JSON models

* Update Changelog
  • Loading branch information
LPGhatguy committed Apr 5, 2019
1 parent 83a0ae6 commit f290e7b
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Rojo Changelog

## [Unreleased]
* Added support for implicit property values in JSON model files ([#154](https://github.com/LPGhatguy/rojo/pull/154))

## [0.5.0 Alpha 9](https://github.com/LPGhatguy/rojo/releases/tag/v0.5.0-alpha.9) (April 4, 2019)
* Changed `rojo build` to use buffered I/O, which can make it up to 2x faster in some cases.
Expand Down
30 changes: 19 additions & 11 deletions server/src/rbx_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rlua::Lua;
use failure::Fail;
use log::info;
use maplit::hashmap;
use rbx_dom_weak::{RbxTree, RbxValue, RbxInstanceProperties};
use rbx_dom_weak::{RbxTree, RbxValue, RbxInstanceProperties, UnresolvedRbxValue};
use serde_derive::{Serialize, Deserialize};
use rbx_reflection::{try_resolve_value, ValueResolveError};

Expand Down Expand Up @@ -602,7 +602,7 @@ fn snapshot_json_model_file<'source>(
path: file.path.to_owned(),
})?;

let mut snapshot = json_instance.into_snapshot();
let mut snapshot = json_instance.into_snapshot()?;
snapshot.metadata.source_path = Some(file.path.to_owned());

Ok(Some(snapshot))
Expand All @@ -618,23 +618,31 @@ struct JsonModelInstance {
children: Vec<JsonModelInstance>,

#[serde(default = "HashMap::new", skip_serializing_if = "HashMap::is_empty")]
properties: HashMap<String, RbxValue>,
properties: HashMap<String, UnresolvedRbxValue>,
}

impl JsonModelInstance {
fn into_snapshot(mut self) -> RbxSnapshotInstance<'static> {
let children = self.children
.drain(..)
.map(JsonModelInstance::into_snapshot)
.collect();
fn into_snapshot(self) -> Result<RbxSnapshotInstance<'static>, SnapshotError> {
let mut children = Vec::with_capacity(self.children.len());

RbxSnapshotInstance {
for child in self.children {
children.push(child.into_snapshot()?);
}

let mut properties = HashMap::with_capacity(self.properties.len());

for (key, value) in self.properties {
let resolved_value = try_resolve_value(&self.class_name, &key, &value)?;
properties.insert(key, resolved_value);
}

Ok(RbxSnapshotInstance {
name: Cow::Owned(self.name),
class_name: Cow::Owned(self.class_name),
properties: self.properties,
properties,
children,
metadata: Default::default(),
}
})
}
}

Expand Down
1 change: 1 addition & 0 deletions server/tests/snapshot_snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ macro_rules! generate_snapshot_tests {

generate_snapshot_tests!(
empty,
json_model,
localization,
multi_partition_game,
nested_partitions,
Expand Down
6 changes: 6 additions & 0 deletions test-projects/json_model/default.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "json_model",
"tree": {
"$path": "src"
}
}
76 changes: 76 additions & 0 deletions test-projects/json_model/expected-snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "json_model",
"class_name": "Folder",
"properties": {},
"children": [
{
"name": "children",
"class_name": "Folder",
"properties": {},
"children": [
{
"name": "The Child",
"class_name": "StringValue",
"properties": {},
"children": [],
"metadata": {
"ignore_unknown_instances": false,
"source_path": null,
"project_definition": null
}
}
],
"metadata": {
"ignore_unknown_instances": false,
"source_path": "src/children.model.json",
"project_definition": null
}
},
{
"name": "explicit",
"class_name": "StringValue",
"properties": {
"Value": {
"Type": "String",
"Value": "Hello, world!"
}
},
"children": [],
"metadata": {
"ignore_unknown_instances": false,
"source_path": "src/explicit.model.json",
"project_definition": null
}
},
{
"name": "implicit",
"class_name": "StringValue",
"properties": {
"Value": {
"Type": "String",
"Value": "What's happenin', Earth?"
}
},
"children": [],
"metadata": {
"ignore_unknown_instances": false,
"source_path": "src/implicit.model.json",
"project_definition": null
}
}
],
"metadata": {
"ignore_unknown_instances": false,
"source_path": "src",
"project_definition": [
"json_model",
{
"class_name": null,
"children": {},
"properties": {},
"ignore_unknown_instances": null,
"path": "src"
}
]
}
}
10 changes: 10 additions & 0 deletions test-projects/json_model/src/children.model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Name": "children",
"ClassName": "Folder",
"Children": [
{
"Name": "The Child",
"ClassName": "StringValue"
}
]
}
11 changes: 11 additions & 0 deletions test-projects/json_model/src/explicit.model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Name": "explicit",
"ClassName": "StringValue",
"Properties": {
"Value": {
"Type": "String",
"Value": "Hello, world!"
}
},
"Children": []
}
7 changes: 7 additions & 0 deletions test-projects/json_model/src/implicit.model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Name": "implicit",
"ClassName": "StringValue",
"Properties": {
"Value": "What's happenin', Earth?"
}
}

0 comments on commit f290e7b

Please sign in to comment.