From 2a23efb6f931bd7a430e5d50f7cb6486852e7873 Mon Sep 17 00:00:00 2001 From: zeroflag Date: Tue, 29 Jun 2021 15:43:15 +0200 Subject: [PATCH] http json request --- src/main/java/com/vectron/fcl/Fcl.java | 16 ++++++++++- .../com/vectron/fcl/interop/MethodSpec.java | 4 +++ src/main/java/com/vectron/fcl/types/Bool.java | 5 ++++ src/main/java/com/vectron/fcl/types/Dic.java | 8 ++++++ .../java/com/vectron/fcl/types/JvmObj.java | 5 ++++ src/main/java/com/vectron/fcl/types/Lst.java | 8 ++++++ src/main/java/com/vectron/fcl/types/Nil.java | 5 ++++ src/main/java/com/vectron/fcl/types/Num.java | 5 ++++ src/main/java/com/vectron/fcl/types/Obj.java | 1 + .../java/com/vectron/fcl/types/Primitive.java | 5 ++++ src/main/java/com/vectron/fcl/types/Quot.java | 7 ++++- .../java/com/vectron/fcl/types/Range.java | 5 ++++ src/main/java/com/vectron/fcl/types/Str.java | 5 ++++ src/main/res/raw/http.forth | 27 +++++++++++++++---- src/main/res/raw/misc.forth | 4 ++- src/test/java/com/vectron/fcl/FclTest.java | 10 +++++++ 16 files changed, 112 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/vectron/fcl/Fcl.java b/src/main/java/com/vectron/fcl/Fcl.java index 028ffb4..09dbc4e 100644 --- a/src/main/java/com/vectron/fcl/Fcl.java +++ b/src/main/java/com/vectron/fcl/Fcl.java @@ -111,6 +111,11 @@ public class Fcl { return address; } + @Override + public Object unwrap() { + return value(); + } + @Override public int compareTo(Obj other) { return other instanceof ColonDef @@ -191,6 +196,11 @@ public class Fcl { return address; } + @Override + public Object unwrap() { + return value(); + } + @Override public int compareTo(Obj other) { return other instanceof Var @@ -270,6 +280,11 @@ public class Fcl { return value; } + @Override + public Object unwrap() { + return value(); + } + @Override public int compareTo(Obj other) { return other instanceof Val @@ -278,7 +293,6 @@ public class Fcl { } } - public Fcl(FclStack stack, int heapSize, Transcript transcript) { this.stack = stack; this.heap = new Object[heapSize]; diff --git a/src/main/java/com/vectron/fcl/interop/MethodSpec.java b/src/main/java/com/vectron/fcl/interop/MethodSpec.java index b83d878..f47645b 100644 --- a/src/main/java/com/vectron/fcl/interop/MethodSpec.java +++ b/src/main/java/com/vectron/fcl/interop/MethodSpec.java @@ -3,6 +3,7 @@ package com.vectron.fcl.interop; import com.vectron.fcl.FclStack; import com.vectron.fcl.exceptions.InterOpFailed; import com.vectron.fcl.types.Bool; +import com.vectron.fcl.types.Dic; import com.vectron.fcl.types.JvmObj; import com.vectron.fcl.types.Nil; import com.vectron.fcl.types.Num; @@ -114,6 +115,8 @@ class MethodSpec { params.add(value.doubleValue()); else if (clazz == String.class) params.add((String)value.value()); + else if (clazz == Dic.class) + params.add((Dic)value); else if (clazz == Map.class) params.add((Map)value.value()); else if (clazz == List.class) @@ -133,6 +136,7 @@ class MethodSpec { case 'm': return Map.class; case 't': return List.class; case 'O': return Obj.class; + case 'M': return Dic.class; default: throw new InterOpFailed("Invalid type spec: " + type); } diff --git a/src/main/java/com/vectron/fcl/types/Bool.java b/src/main/java/com/vectron/fcl/types/Bool.java index 83dc4c9..e123760 100644 --- a/src/main/java/com/vectron/fcl/types/Bool.java +++ b/src/main/java/com/vectron/fcl/types/Bool.java @@ -43,6 +43,11 @@ public class Bool implements Obj, LogicOperand { return value; } + @Override + public Object unwrap() { + return value(); + } + @Override public long longValue() { if (STRICT) throw new TypeMismatched(this, "long"); diff --git a/src/main/java/com/vectron/fcl/types/Dic.java b/src/main/java/com/vectron/fcl/types/Dic.java index 2b0eabf..aa1a06e 100644 --- a/src/main/java/com/vectron/fcl/types/Dic.java +++ b/src/main/java/com/vectron/fcl/types/Dic.java @@ -52,6 +52,14 @@ public class Dic implements Obj { return value; } + @Override + public Map unwrap() { + Map result = new LinkedHashMap<>(); + for (Map.Entry each : value.entrySet()) + result.put(each.getKey().unwrap(), each.getValue().unwrap()); + return result; + } + @Override public int compareTo(Obj o) { return -1; diff --git a/src/main/java/com/vectron/fcl/types/JvmObj.java b/src/main/java/com/vectron/fcl/types/JvmObj.java index d0c3d3a..aeafe8b 100644 --- a/src/main/java/com/vectron/fcl/types/JvmObj.java +++ b/src/main/java/com/vectron/fcl/types/JvmObj.java @@ -57,6 +57,11 @@ public class JvmObj implements Obj { return object; } + @Override + public Object unwrap() { + return value(); + } + @Override public int compareTo(Obj o) { return -1; diff --git a/src/main/java/com/vectron/fcl/types/Lst.java b/src/main/java/com/vectron/fcl/types/Lst.java index 3b7d015..ede569b 100644 --- a/src/main/java/com/vectron/fcl/types/Lst.java +++ b/src/main/java/com/vectron/fcl/types/Lst.java @@ -46,6 +46,14 @@ public class Lst implements Obj, ArithmeticOperand { return value; } + @Override + public Object unwrap() { + List result = new ArrayList<>(); + for (Obj each : value) + result.add(each.unwrap()); + return result; + } + @Override public int compareTo(Obj o) { return -1; diff --git a/src/main/java/com/vectron/fcl/types/Nil.java b/src/main/java/com/vectron/fcl/types/Nil.java index 373b0db..92626e8 100644 --- a/src/main/java/com/vectron/fcl/types/Nil.java +++ b/src/main/java/com/vectron/fcl/types/Nil.java @@ -49,6 +49,11 @@ public class Nil implements Obj { return null; } + @Override + public Object unwrap() { + return value(); + } + @Override public int compareTo(Obj o) { return -1; diff --git a/src/main/java/com/vectron/fcl/types/Num.java b/src/main/java/com/vectron/fcl/types/Num.java index 89e2c52..a734e49 100644 --- a/src/main/java/com/vectron/fcl/types/Num.java +++ b/src/main/java/com/vectron/fcl/types/Num.java @@ -251,6 +251,11 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand { return value; } + @Override + public Object unwrap() { + return value(); + } + @Override public int compareTo(Obj other) { return other instanceof Num diff --git a/src/main/java/com/vectron/fcl/types/Obj.java b/src/main/java/com/vectron/fcl/types/Obj.java index a483ef8..75d7691 100644 --- a/src/main/java/com/vectron/fcl/types/Obj.java +++ b/src/main/java/com/vectron/fcl/types/Obj.java @@ -8,4 +8,5 @@ public interface Obj extends Comparable { Num asNum(); Str asStr(); Object value(); + Object unwrap(); } \ No newline at end of file diff --git a/src/main/java/com/vectron/fcl/types/Primitive.java b/src/main/java/com/vectron/fcl/types/Primitive.java index 3fac79d..357cdbb 100644 --- a/src/main/java/com/vectron/fcl/types/Primitive.java +++ b/src/main/java/com/vectron/fcl/types/Primitive.java @@ -70,6 +70,11 @@ public class Primitive implements Word { return code; } + @Override + public Object unwrap() { + return value(); + } + @Override public String toString() { return "xt_" + name; diff --git a/src/main/java/com/vectron/fcl/types/Quot.java b/src/main/java/com/vectron/fcl/types/Quot.java index 3b9b5f6..0ad452b 100644 --- a/src/main/java/com/vectron/fcl/types/Quot.java +++ b/src/main/java/com/vectron/fcl/types/Quot.java @@ -48,7 +48,12 @@ public class Quot implements Obj { @Override public Object value() { throw new TypeMismatched(this, "value"); - } + } + + @Override + public Object unwrap() { + return value(); + } @Override public Num asNum() { diff --git a/src/main/java/com/vectron/fcl/types/Range.java b/src/main/java/com/vectron/fcl/types/Range.java index 0c888d7..9ff4138 100644 --- a/src/main/java/com/vectron/fcl/types/Range.java +++ b/src/main/java/com/vectron/fcl/types/Range.java @@ -76,6 +76,11 @@ public class Range implements Obj { return iterator; } + @Override + public Object unwrap() { + return value(); + } + @Override public int compareTo(Obj o) { return -1; diff --git a/src/main/java/com/vectron/fcl/types/Str.java b/src/main/java/com/vectron/fcl/types/Str.java index e9bae85..db054d4 100644 --- a/src/main/java/com/vectron/fcl/types/Str.java +++ b/src/main/java/com/vectron/fcl/types/Str.java @@ -46,6 +46,11 @@ public class Str implements Obj, ArithmeticOperand { return Num.parse(value); } + @Override + public Object unwrap() { + return value(); + } + @Override public Str asStr() { return this; diff --git a/src/main/res/raw/http.forth b/src/main/res/raw/http.forth index 0b217a6..d51f53c 100644 --- a/src/main/res/raw/http.forth +++ b/src/main/res/raw/http.forth @@ -1,12 +1,29 @@ +'Content-Type' val: CONTENT-TYPE +'application/json' val: APPLICATION/JSON + : http-get ( u -- n b ) 'com.vectron.forthcalc.support.HttpClient/get/s' jvm-call-static -> response response 'body' jvm-call-method - response 'code' jvm-call-method - ; + response 'code' jvm-call-method ; : http-post ( d u -- n b ) -> url -> request - request jvmValue url 'com.vectron.forthcalc.support.HttpClient/post/sm' jvm-call-static -> response + request url 'com.vectron.forthcalc.support.HttpClient/post/sM' jvm-call-static -> response response 'body' jvm-call-method - response 'code' jvm-call-method - ; \ No newline at end of file + response 'code' jvm-call-method ; + +: http-put ( d u -- n b ) + -> url -> request + request url 'com.vectron.forthcalc.support.HttpClient/put/sM' jvm-call-static -> response + response 'body' jvm-call-method + response 'code' jvm-call-method ; + +: +json-type ( m -- m ) -> request + request 'headers' at -> headers + headers nil = if + #[ 'content' request + 'headers' #[ CONTENT-TYPE APPLICATION/JSON ]# ]# + else + headers CONTENT-TYPE APPLICATION/JSON put + request + then ; \ No newline at end of file diff --git a/src/main/res/raw/misc.forth b/src/main/res/raw/misc.forth index 6b70f86..661b6a9 100644 --- a/src/main/res/raw/misc.forth +++ b/src/main/res/raw/misc.forth @@ -11,4 +11,6 @@ tbl elem count 1+ put then } each - tbl ; \ No newline at end of file + tbl ; + +: ms ( n -- ) 'java.lang.Thread/sleep/l' jvm-call-static ; \ No newline at end of file diff --git a/src/test/java/com/vectron/fcl/FclTest.java b/src/test/java/com/vectron/fcl/FclTest.java index 352910c..3e7462c 100644 --- a/src/test/java/com/vectron/fcl/FclTest.java +++ b/src/test/java/com/vectron/fcl/FclTest.java @@ -1139,6 +1139,16 @@ public class FclTest { assertEquals(42, evalPop(" ': tst 42 ; tst' eval").intValue()); } + @Test + public void testHttpHeaders() { + assertEquals( + "#[ 'headers' #[ 'Content-Type' 'application/json' ]# 'content' #[ 'a' 1 ]# ]#", + evalPop("#[ 'a' 1 ]# +json-type").toString()); + assertEquals( + "#[ 'content' #[ 'a' 1 ]# 'headers' #[ 'Content-Type' 'application/json' ]# ]#", + evalPop("#[ 'headers' #[ 'Content-Type' 'text/plain' ]# 'content' #[ 'a' 1 ]# ]# +json-type").toString()); + } + private String transcript() { return transcript.content(); }