Mercurial Hosting > luan
changeset 1389:eb8b35dccd99
cleanup and stringify change
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 03 Sep 2019 22:54:31 -0600 |
parents | 2024d23ddd64 |
children | 179c4882c6b6 |
files | conv.txt src/luan/modules/BasicLuan.java src/luan/modules/ThreadLuan.java src/luan/modules/Utils.java src/luan/modules/url/LuanUrl.java website/src/manual.html |
diffstat | 6 files changed, 83 insertions(+), 58 deletions(-) [+] |
line wrap: on
line diff
--- a/conv.txt Tue Sep 03 22:12:53 2019 -0600 +++ b/conv.txt Tue Sep 03 22:54:31 2019 -0600 @@ -1,3 +1,5 @@ +stringify + (%> ( %> String.concat
--- a/src/luan/modules/BasicLuan.java Tue Sep 03 22:12:53 2019 -0600 +++ b/src/luan/modules/BasicLuan.java Tue Sep 03 22:54:31 2019 -0600 @@ -255,17 +255,18 @@ } } - public static String stringify(Object obj,String strict) throws LuanException { - boolean b; - if( strict==null ) { - b = false; - } else if( strict.equals("strict") ) { - b = true; - } else { - throw new LuanException("strict must be nil or 'strict'"); + public static String stringify(Object obj,LuanTable options) throws LuanException { + LuanToString lts = new LuanToString(); + if( options != null ) { + Map map = options.asMap(); + Boolean strict = Utils.removeBoolean(map,"strict"); + if( strict != null ) + lts.strict = strict; + Boolean numberTypes = Utils.removeBoolean(map,"number_types"); + if( numberTypes != null ) + lts.numberTypes = numberTypes; + Utils.checkEmpty(map); } - LuanToString lts = new LuanToString(); - lts.strict = b; return lts.toString(obj); }
--- a/src/luan/modules/ThreadLuan.java Tue Sep 03 22:12:53 2019 -0600 +++ b/src/luan/modules/ThreadLuan.java Tue Sep 03 22:54:31 2019 -0600 @@ -49,16 +49,15 @@ throws LuanException { Map map = options.asMap(); - Number delay = (Number)map.remove("delay"); - Number repeatingDelay = (Number)map.remove("repeating_delay"); - Number repeatingRate = (Number)map.remove("repeating_rate"); - boolean daemon = Boolean.TRUE.equals(map.remove("daemon")); - final boolean runOnClose = Boolean.TRUE.equals(map.remove("run_on_close")); + Number delay = Utils.removeNumber(map,"delay"); + Number repeatingDelay = Utils.removeNumber(map,"repeating_delay"); + Number repeatingRate = Utils.removeNumber(map,"repeating_rate"); + boolean daemon = Boolean.TRUE.equals(Utils.removeBoolean(map,"daemon")); + final boolean runOnClose = Boolean.TRUE.equals(Utils.removeBoolean(map,"run_on_close")); if( repeatingDelay!=null && repeatingRate!=null ) throw new LuanException("can't define both repeating_delay and repeating_rate"); boolean repeating = repeatingDelay!=null || repeatingRate!=null; - if( !map.isEmpty() ) - throw new LuanException( "unrecognized options: "+map ); + Utils.checkEmpty(map); Luan luan = fn.luan(); LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); final Luan newLuan = (Luan)cloner.clone(luan);
--- a/src/luan/modules/Utils.java Tue Sep 03 22:12:53 2019 -0600 +++ b/src/luan/modules/Utils.java Tue Sep 03 22:54:31 2019 -0600 @@ -9,6 +9,8 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.MalformedURLException; +import java.util.Map; +import luan.Luan; import luan.LuanException; import luan.LuanTable; import luan.LuanFunction; @@ -94,4 +96,50 @@ return out.toByteArray(); } + + public static String removeString(Map map,String key) throws LuanException { + Object val = map.remove(key); + if( val!=null && !(val instanceof String) ) + throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); + return (String)val; + } + + public static Number removeNumber(Map map,String key) throws LuanException { + Object val = map.remove(key); + if( val!=null && !(val instanceof Number) ) + throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); + return (Number)val; + } + + public static Integer removeInt(Map map,String key) throws LuanException { + Object val = map.remove(key); + if( val==null ) + return null; + Integer i = Luan.asInteger(val); + if( i==null ) { + String type = val instanceof Number ? val.getClass().getSimpleName().toLowerCase() : Luan.type(val); + throw new LuanException( "parameter '"+key+"' must be an integer but is a "+type ); + } + return i; + } + + public static LuanTable removeTable(Map map,String key) throws LuanException { + Object val = map.remove(key); + if( val!=null && !(val instanceof LuanTable) ) + throw new LuanException( "parameter '"+key+"' must be a table but is a "+Luan.type(val) ); + return (LuanTable)val; + } + + public static Boolean removeBoolean(Map map,String key) throws LuanException { + Object val = map.remove(key); + if( val!=null && !(val instanceof Boolean) ) + throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); + return (Boolean)val; + } + + public static void checkEmpty(Map map) throws LuanException { + if( !map.isEmpty() ) + throw new LuanException( "unrecognized options: "+map ); + } + }
--- a/src/luan/modules/url/LuanUrl.java Tue Sep 03 22:12:53 2019 -0600 +++ b/src/luan/modules/url/LuanUrl.java Tue Sep 03 22:54:31 2019 -0600 @@ -44,7 +44,7 @@ public LuanUrl(URL url,LuanTable options) throws LuanException { if( options != null ) { Map map = options.asMap(); - String methodStr = getString(map,"method"); + String methodStr = Utils.removeString(map,"method"); if( methodStr != null ) { methodStr = methodStr.toUpperCase(); try { @@ -53,7 +53,7 @@ throw new LuanException( "invalid method: "+methodStr ); } } - Map headerMap = getMap(map,"headers"); + Map headerMap = removeMap(map,"headers"); if( headerMap != null ) { for( Object hack : headerMap.entrySet() ) { Map.Entry entry = (Map.Entry)hack; @@ -71,15 +71,15 @@ } } } - Map auth = getMap(map,"authorization"); + Map auth = removeMap(map,"authorization"); if( auth != null ) { if( headers!=null && headers.containsKey("authorization") ) throw new LuanException( "can't define authorization with header 'authorization' defined" ); - String username = getString(auth,"username"); + String username = Utils.removeString(auth,"username"); if( username==null ) username = ""; - String password = getString(auth,"password"); + String password = Utils.removeString(auth,"password"); if( password==null ) password = ""; - String type = getString(auth,"type"); + String type = Utils.removeString(auth,"type"); if( !auth.isEmpty() ) throw new LuanException( "unrecognized authorization options: "+auth ); if( type != null ) { @@ -92,8 +92,8 @@ authPassword = password; } } - Map params = getMap(map,"parameters"); - String enctype = getString(map,"enctype"); + Map params = removeMap(map,"parameters"); + String enctype = Utils.removeString(map,"enctype"); Object content = map.remove("content"); if( content != null ) { if( this.method != Method.POST ) @@ -159,11 +159,10 @@ } } } - Integer timeout = getInt(map,"time_out"); + Integer timeout = Utils.removeInt(map,"time_out"); if( timeout != null ) this.timeout = timeout; - if( !map.isEmpty() ) - throw new LuanException( "unrecognized options: "+map ); + Utils.checkEmpty(map); } this.url = url; } @@ -181,32 +180,8 @@ } } - private static String getString(Map map,String key) throws LuanException { - Object val = map.remove(key); - if( val!=null && !(val instanceof String) ) - throw new LuanException( "parameter '"+key+"' must be a string" ); - return (String)val; - } - - private static Integer getInt(Map map,String key) throws LuanException { - Object val = map.remove(key); - if( val==null ) - return null; - Integer i = Luan.asInteger(val); - if( i==null ) - throw new LuanException( "parameter '"+key+"' must be an integer" ); - return i; - } - - private static LuanTable getTable(Map map,String key) throws LuanException { - Object val = map.remove(key); - if( val!=null && !(val instanceof LuanTable) ) - throw new LuanException( "parameter '"+key+"' must be a table" ); - return (LuanTable)val; - } - - private static Map getMap(Map map,String key) throws LuanException { - LuanTable t = getTable(map,key); + private static Map removeMap(Map map,String key) throws LuanException { + LuanTable t = Utils.removeTable(map,key); return t==null ? null : t.asMap(); }
--- a/website/src/manual.html Tue Sep 03 22:12:53 2019 -0600 +++ b/website/src/manual.html Tue Sep 03 22:54:31 2019 -0600 @@ -2068,10 +2068,10 @@ raises an error. -<h4 heading><a name="Luan.stringify" href="#Luan.stringify"><code>Luan.stringify (v [,strict])</code></a></h4> - -<p> -Receives a value of any type and converts it to a string that is a Luan expression. <code>strict</code> is a string. If <code>strict</code> is 'strict' then invalid types throw an error. If <code>strict</code> is nil then invalid types are represented but the resulting expression is invalid. <code>strict</code> defaults to <b>nil</b>. +<h4 heading><a name="Luan.stringify" href="#Luan.stringify"><code>Luan.stringify (v [,options])</code></a></h4> + +<p> +Receives a value of any type and converts it to a string that is a Luan expression. <code>options</code> is a table. If <code>options.strict==true</code> then invalid types throw an error. Otherwise invalid types are represented but the resulting expression is invalid. If <code>options.number_types==true</code> then numbers will be wrapped in functions for their type. <h4 heading><a name="Luan.to_string" href="#Luan.to_string"><code>Luan.to_string (v)</code></a></h4>