Skip to content

Commit

Permalink
Merge pull request #160 from epage/map
Browse files Browse the repository at this point in the history
 feat(Value): Control key order
  • Loading branch information
epage authored Jan 15, 2018
2 parents 282c78d + ae199fe commit 727d992
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ script:
- rustc -Vv
- cargo -V
- cargo check --verbose
- cargo check --verbose --features "cli"
- cargo check --verbose --features "cli serde_yaml serde_json"
- cargo check --verbose --no-default-features
- cargo check --verbose --features "cli serde_json object_sorted"
- cargo clean
- cargo test --verbose --features "cli serde_yaml serde_json"
- cargo when --channel nightly bench --features "cli serde_yaml serde_json"
- cargo test --verbose --features "cli serde_json"
- cargo when --channel nightly bench --features "cli serde_json"
- cargo when --channel stable doc --no-deps --all-features

after_success:
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ default = ["extra-filters", "serde"]
cli = ["clap", "error-chain", "serde_yaml"]
extra-filters = []
dev = []
# Ensure keys in `Value`s `Object`s to be sorted.
object_sorted = []

[dependencies]
regex = "0.2"
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ test_script:
- rustc -Vv
- cargo -V
- cargo check --verbose
- cargo check --verbose --features "cli"
- cargo check --verbose --features "cli serde serde_yaml serde_json"
- cargo check --verbose --no-default-features
- cargo check --verbose --features "cli serde_json object_sorted"
- cargo clean
- cargo test --verbose --features "cli serde serde_yaml serde_json"
- cargo test --verbose --features "cli serde_json"

cache:
- C:\Users\appveyor\.cargo\registry
Expand Down
10 changes: 6 additions & 4 deletions src/filters/date.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#[cfg(feature = "extra-filters")]
use chrono::FixedOffset;

use value::Value;
use value::Scalar;
use interpreter::{FilterError, FilterResult};
use interpreter::FilterResult;

use super::check_args_len;

#[cfg(feature = "extra-filters")]
use chrono::FixedOffset;
#[cfg(feature = "extra-filters")]
use interpreter::FilterError;

pub fn date(input: &Value, args: &[Value]) -> FilterResult {
check_args_len(args, 1, 0)?;

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
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
15 changes: 13 additions & 2 deletions src/value/values.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
use std::collections::HashMap;
use std::cmp::Ordering;
use std::fmt;
use std::borrow;

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

#[cfg(not(any(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(not(any(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 +32,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 727d992

Please sign in to comment.