Skip to content

Commit

Permalink
feat(Value): Control key order
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 15, 2018
1 parent 8c43de8 commit 7ff0fcd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ default = ["extra-filters", "serde"]
cli = ["clap", "error-chain", "serde_yaml"]
extra-filters = []
dev = []
# Ensure keys in `Value`s `Object`s to be sorted.
# Mutually exclusive with `object_order_preserved`
object_sorted = []
# Ensure the order that keys in `Value`s `Object`s are inserted is preserved
# Mutually exclusive with `object_sorted`
object_order_preserved = ["linked-hash-map"]

[dependencies]
regex = "0.2"
Expand All @@ -44,6 +50,8 @@ error-chain = { version = "0.11.0", optional = true }
serde_yaml = { version = "0.7", optional = true }
serde_json = { version = "1.0", optional = true }

linked-hash-map = { version = "0.5", optional = true }

[build-dependencies]
skeptic = "0.13.2"
serde = { version = "1.0", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ pub fn pluralize(input: &Value, args: &[Value]) -> FilterResult {
#[cfg(test)]
mod tests {

use std::collections::HashMap;
use value::Object;
use super::*;

macro_rules! unit {
Expand Down Expand Up @@ -1371,7 +1371,7 @@ mod tests {
&[Value::scalar(1_f32)]),
Value::Array(vec![tos!("")]));
assert_eq!(unit!(default,
Value::Object(HashMap::new()),
Value::Object(Object::new()),
&[Value::scalar(1_f32)]),
Value::scalar(1_f32));
assert_eq!(unit!(default, Value::scalar(false), &[Value::scalar(1_f32)]),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ extern crate lazy_static;
extern crate serde;
#[cfg(test)]
extern crate serde_yaml;
#[cfg(feature = "object_order_preserved")]
extern crate linked_hash_map;

mod error;
mod filters;
Expand Down
5 changes: 2 additions & 3 deletions src/tags/for_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::slice::Iter;

use error::{Error, Result};
Expand All @@ -11,7 +10,7 @@ use compiler::Element;
use compiler::LiquidOptions;
use compiler::Token;
use compiler::{parse, expect, split_block};
use value::Value;
use value::{Value, Object};

#[derive(Clone, Debug)]
enum Range {
Expand Down Expand Up @@ -95,7 +94,7 @@ impl Renderable for For {
range_len => {
let mut ret = String::default();
context.run_in_scope(|mut scope| {
let mut helper_vars: HashMap<String, Value> = HashMap::new();
let mut helper_vars = Object::new();
helper_vars.insert("length".to_owned(), Value::scalar(range_len as i32));

for (i, v) in slice.iter().enumerate() {
Expand Down
6 changes: 3 additions & 3 deletions src/tags/if_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ pub fn if_block(_tag_name: &str,

#[cfg(test)]
mod test {
use std::collections::HashMap;
use super::*;
use value::Value;
use value::Object;
use compiler;
use interpreter;

Expand Down Expand Up @@ -418,7 +418,7 @@ mod test {
.unwrap();

let mut context = Context::new();
let mut obj = HashMap::new();
let mut obj = Object::new();
obj.insert("Star Wars".to_owned(), Value::scalar("1977"));
context.set_global_val("movies", Value::Object(obj));
let output = template.render(&mut context).unwrap();
Expand All @@ -434,7 +434,7 @@ mod test {
.unwrap();

let mut context = Context::new();
let obj = HashMap::new();
let obj = Object::new();
context.set_global_val("movies", Value::Object(obj));
let output = template.render(&mut context).unwrap();
assert_eq!(output, Some("if false".to_owned()));
Expand Down
21 changes: 19 additions & 2 deletions src/value/values.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
use std::collections::HashMap;
use std::cmp::Ordering;
use std::fmt;
use std::borrow;

#[cfg(feature = "object_sorted")]
use std::collections::BTreeMap;

#[cfg(feature = "object_order_preserved")]
use linked_hash_map::LinkedHashMap;

#[cfg(not(any(feature = "object_order_preserved", feature = "object_sorted")))]
use std::collections::HashMap;

use super::Index;
use super::Scalar;

#[cfg(feature = "object_sorted")]
type MapImpl<K, V> = BTreeMap<K, V>;

#[cfg(feature = "object_order_preserved")]
type MapImpl<K, V> = LinkedHashMap<K, V>;

#[cfg(not(any(feature = "object_order_preserved", feature = "object_sorted")))]
type MapImpl<K, V> = HashMap<K, V>;

/// An enum to represent different value types
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand All @@ -21,7 +38,7 @@ pub enum Value {
pub type Array = Vec<Value>;

/// Type representing a Liquid object, payload of the `Value::Object` variant
pub type Object = HashMap<String, Value>;
pub type Object = MapImpl<String, Value>;

impl Value {
pub fn scalar<T: Into<Scalar>>(value: T) -> Self {
Expand Down

0 comments on commit 7ff0fcd

Please sign in to comment.