From a0e08cb2d6d820b9c8aec768fb6ed5e8fca563f5 Mon Sep 17 00:00:00 2001 From: Kristopolous Date: Mon, 12 Dec 2011 02:59:31 -0800 Subject: [PATCH] (2) Fixes #10: Variables with hyphens in them do not work. Fixes #8: Needs example with curl --- .gitignore | 1 + test.sh => example.sh | 0 tests/data.json | 19 +++++++++++++++++++ tests/runtime.sh | 9 +++++++++ ticktick.sh | 17 ++++++++++++++--- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 .gitignore rename test.sh => example.sh (100%) create mode 100644 tests/data.json create mode 100755 tests/runtime.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/test.sh b/example.sh similarity index 100% rename from test.sh rename to example.sh diff --git a/tests/data.json b/tests/data.json new file mode 100644 index 0000000..27c8e65 --- /dev/null +++ b/tests/data.json @@ -0,0 +1,19 @@ +{ + "query": {}, + "pathname": "/echo/request.json", + "href": "http://foobar3000.com/echo/request.json", + "httpVersion": "1.1", + "method": "GET", + "headers": { + "user-agent": "curl/7.21.6 (i686-pc-linux-gnu) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.3.4 libidn/1.22 librtmp/2.3", + "host": "foobar3000.com", + "accept": "*/*" + }, + "trailers": {}, + "remoteAddress": "99.59.97.101", + "remotePort": 39099, + "protocol": "http:", + "host": "foobar3000.com", + "hostname": "foobar3000.com", + "session": "NjMzMjgxODMxNjAw" +} diff --git a/tests/runtime.sh b/tests/runtime.sh new file mode 100755 index 0000000..0c6535e --- /dev/null +++ b/tests/runtime.sh @@ -0,0 +1,9 @@ +#!/bin/bash +. ../ticktick.sh + +DATA=`cat data.json` + +tickParse "$DATA" + +echo ``pathname`` +echo ``headers["user-agent"]`` diff --git a/ticktick.sh b/ticktick.sh index a345d9d..1cd17d8 100644 --- a/ticktick.sh +++ b/ticktick.sh @@ -76,6 +76,7 @@ __tick_json_parse_value() { local prej=${jpath//\"/} [ "$prej" ] && prej="_$prej" + [ "$prej" ] && prej=${prej/-/__hyphen__} case "$Token" in '{') __tick_json_parse_object "$jpath" ;; @@ -83,7 +84,9 @@ __tick_json_parse_value() { *) Value=$Token - echo __tick_data_$Prefix$prej=$Value + Path="$Prefix$prej" + Path=${Path/#_/} + echo __tick_data_$Path=$Value ;; esac } @@ -146,6 +149,7 @@ __tick_fun_parse_expression() { if [ "$suffix" ]; then echo "$suffix" | __tick_json_tokenize | __tick_json_parse else + Prefix=${Prefix/-/__hyphen__} echo '${__tick_data_'$Prefix'}' fi } @@ -196,6 +200,7 @@ __tick_fun_tokenize() { exit } +## Runtime { __tick_runtime_length() { echo $#; } __tick_runtime_first() { echo ${!1}; } __tick_runtime_last() { eval 'echo $'${!#}; } @@ -215,14 +220,20 @@ __tick_runtime_shift() { eval unset $left } __tick_runtime_push() { - local value="${1/\"/\\\"}" + local value="${1/\'/\\\'}" local base=$2 local lastarg=${!#} let nextval=${lastarg/$base/}+1 nextval=`printf "%012d" $nextval` - eval $base$nextval=\"$value\" + eval $base$nextval=\'$value\' } +tickParse() { + eval `echo "$1" | __tick_json_tokenize | __tick_json_parse | tr '\n' ';'` +} +## } End of Runtime + + [ $__tick_var_tokenized ] || __tick_fun_tokenize