Mercurial Hosting > luan
changeset 1420:225808b90cee
options handling
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 26 Oct 2019 22:21:09 -0600 |
parents | 59fd2e8b1b9d |
children | 8ab2f0fc3829 |
files | src/luan/LuanTable.java src/luan/modules/BasicLuan.java src/luan/modules/TableLuan.java src/luan/modules/ThreadLuan.java src/luan/modules/Utils.java src/luan/modules/lucene/LuceneIndex.java src/luan/modules/lucene/PostgresBackup.java src/luan/modules/sql/Database.java src/luan/modules/url/LuanUrl.java |
diffstat | 9 files changed, 74 insertions(+), 67 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/LuanTable.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/LuanTable.java Sat Oct 26 22:21:09 2019 -0600 @@ -224,15 +224,16 @@ } } } - if( map==null ) - map = newMap(); if( key instanceof Number && !(key instanceof Double) ) { Number n = (Number)key; key = Double.valueOf(n.doubleValue()); } if( val == null ) { - map.remove(key); + if( map!=null ) + map.remove(key); } else { + if( map==null ) + map = newMap(); map.put(key,val); } } @@ -278,7 +279,7 @@ mapToList(); } - public Object rawRemove(int pos) { + public Object removeFromList(int pos) { check(); return list().remove(pos-1); } @@ -506,7 +507,7 @@ return n; } - public boolean rawIsEmpty() { + public boolean isEmpty() { return (map==null || map.isEmpty()) && (list==null || list.isEmpty()); } @@ -519,6 +520,12 @@ return n; } + public Object remove(Object key) { + Object old = rawGet(key); + rawPut2(key,null); + return old; + } + protected void finalize() throws Throwable { Object h = getHandler("__gc"); if( h != null ) {
--- a/src/luan/modules/BasicLuan.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/BasicLuan.java Sat Oct 26 22:21:09 2019 -0600 @@ -259,17 +259,17 @@ 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"); + options = new LuanTable(options); + Boolean strict = Utils.removeBoolean(options,"strict"); if( strict != null ) lts.strict = strict; - Boolean numberTypes = Utils.removeBoolean(map,"number_types"); + Boolean numberTypes = Utils.removeBoolean(options,"number_types"); if( numberTypes != null ) lts.numberTypes = numberTypes; - Boolean compressed = Utils.removeBoolean(map,"compressed"); + Boolean compressed = Utils.removeBoolean(options,"compressed"); if( compressed != null ) lts.compressed = compressed; - Utils.checkEmpty(map); + Utils.checkEmpty(options); } return lts.toString(obj); } @@ -277,11 +277,11 @@ public static String json_string(Object obj,LuanTable options) throws LuanException { JsonToString jts = new JsonToString(); if( options != null ) { - Map map = options.asMap(); - Boolean compressed = Utils.removeBoolean(map,"compressed"); + options = new LuanTable(options); + Boolean compressed = Utils.removeBoolean(options,"compressed"); if( compressed != null ) jts.compressed = compressed; - Utils.checkEmpty(map); + Utils.checkEmpty(options); } return jts.toString(Luan.toJava(obj)); }
--- a/src/luan/modules/TableLuan.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/TableLuan.java Sat Oct 26 22:21:09 2019 -0600 @@ -39,7 +39,7 @@ public static Object remove(LuanTable list,int pos) throws LuanException { if( list.getMetatable() != null ) throw new LuanException("can't remove from a table with a metatable"); - return list.rawRemove(pos); + return list.removeFromList(pos); } private static interface LessThan { @@ -121,7 +121,7 @@ } public static boolean is_empty(LuanTable tbl) throws LuanException { - return tbl.rawIsEmpty(); + return tbl.isEmpty(); } public static int size(LuanTable tbl) throws LuanException {
--- a/src/luan/modules/ThreadLuan.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/ThreadLuan.java Sat Oct 26 22:21:09 2019 -0600 @@ -57,15 +57,15 @@ public static void schedule(LuanFunction fn,LuanTable options) throws LuanException { - Map map = options.asMap(); - Number delay = Utils.removeNumber(map,"delay"); - Number repeatingDelay = Utils.removeNumber(map,"repeating_delay"); - Number repeatingRate = Utils.removeNumber(map,"repeating_rate"); - String id = Utils.removeString(map,"id"); + options = new LuanTable(options); + Number delay = Utils.removeNumber(options,"delay"); + Number repeatingDelay = Utils.removeNumber(options,"repeating_delay"); + Number repeatingRate = Utils.removeNumber(options,"repeating_rate"); + String id = Utils.removeString(options,"id"); if( repeatingDelay!=null && repeatingRate!=null ) throw new LuanException("can't define both repeating_delay and repeating_rate"); boolean repeating = repeatingDelay!=null || repeatingRate!=null; - Utils.checkEmpty(map); + Utils.checkEmpty(options); if( id != null ) { Reference<ScheduledFuture> ref = scheduleds.remove(id); if( ref != null ) {
--- a/src/luan/modules/Utils.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/Utils.java Sat Oct 26 22:21:09 2019 -0600 @@ -97,29 +97,29 @@ } - public static String removeString(Map map,String key) throws LuanException { - Object val = map.remove(key); + public static String removeString(LuanTable options,String key) throws LuanException { + Object val = options.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 String removeRequiredString(Map map,String key) throws LuanException { - String s = removeString(map,key); + public static String removeRequiredString(LuanTable options,String key) throws LuanException { + String s = removeString(options,key); if( s==null ) throw new LuanException( "parameter '"+key+"' is required" ); return s; } - public static Number removeNumber(Map map,String key) throws LuanException { - Object val = map.remove(key); + public static Number removeNumber(LuanTable options,String key) throws LuanException { + Object val = options.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 removeInteger(Map map,String key) throws LuanException { - Object val = map.remove(key); + public static Integer removeInteger(LuanTable options,String key) throws LuanException { + Object val = options.remove(key); if( val==null ) return null; Integer i = Luan.asInteger(val); @@ -130,37 +130,37 @@ return i; } - public static LuanTable removeTable(Map map,String key) throws LuanException { - Object val = map.remove(key); + public static LuanTable removeTable(LuanTable options,String key) throws LuanException { + Object val = options.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); + public static Boolean removeBoolean(LuanTable options,String key) throws LuanException { + Object val = options.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 LuanFunction removeFunction(Map map,String key) throws LuanException { - Object val = map.remove(key); + public static LuanFunction removeFunction(LuanTable options,String key) throws LuanException { + Object val = options.remove(key); if( val!=null && !(val instanceof LuanFunction) ) throw new LuanException( "parameter '"+key+"' must be a function but is a "+Luan.type(val) ); return (LuanFunction)val; } - public static LuanFunction removeRequiredFunction(Map map,String key) throws LuanException { - LuanFunction fn = removeFunction(map,key); + public static LuanFunction removeRequiredFunction(LuanTable options,String key) throws LuanException { + LuanFunction fn = removeFunction(options,key); if( fn==null ) throw new LuanException( "parameter '"+key+"' is required" ); return fn; } - public static void checkEmpty(Map map) throws LuanException { - if( !map.isEmpty() ) - throw new LuanException( "unrecognized options: "+map ); + public static void checkEmpty(LuanTable options) throws LuanException { + if( !options.isEmpty() ) + throw new LuanException( "unrecognized options: "+options.asMap() ); } }
--- a/src/luan/modules/lucene/LuceneIndex.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/lucene/LuceneIndex.java Sat Oct 26 22:21:09 2019 -0600 @@ -143,14 +143,14 @@ private LuceneIndex(Luan luan,File indexDir,LuanTable options) throws LuanException, IOException, ClassNotFoundException, SQLException { - Map map = options.asMap(); - this.version = map.remove("version"); - FieldParser defaultFieldParser = (FieldParser)map.remove("default_type"); - LuanTable defaultFieldsTbl = Utils.removeTable(map,"default_fields"); + options = new LuanTable(options); + this.version = options.remove("version"); + FieldParser defaultFieldParser = (FieldParser)options.remove("default_type"); + LuanTable defaultFieldsTbl = Utils.removeTable(options,"default_fields"); String[] defaultFields = defaultFieldsTbl==null ? null : (String[])defaultFieldsTbl.asList().toArray(new String[0]); - LuanFunction completer = Utils.removeFunction(map,"completer"); - LuanTable postgresSpec = Utils.removeTable(map,"postgres_spec"); - Utils.checkEmpty(map); + LuanFunction completer = Utils.removeFunction(options,"completer"); + LuanTable postgresSpec = Utils.removeTable(options,"postgres_spec"); + Utils.checkEmpty(options); this.luanLogger = luan.getLogger(LuceneIndex.class); this.defaultFieldParser = defaultFieldParser; @@ -171,8 +171,7 @@ } else { if( completer == null ) throw new LuanException("completer is required for postgres_spec"); - Map spec = postgresSpec.asMap(); - postgresBackup = new PostgresBackup(luan,spec); + postgresBackup = new PostgresBackup(luan,postgresSpec); if( !wasCreated && postgresBackup.wasCreated ) { luanLogger.error("rebuilding postgres backup"); rebuild_postgres_backup(completer);
--- a/src/luan/modules/lucene/PostgresBackup.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/lucene/PostgresBackup.java Sat Oct 26 22:21:09 2019 -0600 @@ -36,9 +36,10 @@ private int trans = 0; private final LuanToString luanToString = new LuanToString(); - PostgresBackup(Luan luan,Map spec) + PostgresBackup(Luan luan,LuanTable spec) throws ClassNotFoundException, SQLException, LuanException { + spec = new LuanTable(spec); this.luanLogger = luan.getLogger(PostgresBackup.class); /* Class.forName("org.postgresql.Driver");
--- a/src/luan/modules/sql/Database.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/sql/Database.java Sat Oct 26 22:21:09 2019 -0600 @@ -28,26 +28,26 @@ this.con = con; } - public Database(LuanTable specTbl) + public Database(LuanTable spec) throws LuanException, ClassNotFoundException, SQLException { - Map<Object,Object> spec = specTbl.asMap(); + spec = new LuanTable(spec); String cls = Utils.removeRequiredString(spec,"class"); Class.forName(cls); String url = Utils.removeRequiredString(spec,"url"); Properties props = new Properties(); - props.putAll(spec); + props.putAll(spec.asMap()); this.con = DriverManager.getConnection(url,props); spec.remove("user"); spec.remove("password"); - set(spec); + set2(spec); } public void set(LuanTable options) throws LuanException, SQLException { - set(options.asMap()); + set2(new LuanTable(options)); } - private void set(Map<Object,Object> options) throws LuanException, SQLException { + private void set2(LuanTable options) throws LuanException, SQLException { Boolean autoCommit = Utils.removeBoolean(options,"auto_commit"); if( autoCommit != null ) con.setAutoCommit(autoCommit);
--- a/src/luan/modules/url/LuanUrl.java Fri Oct 25 22:12:06 2019 -0600 +++ b/src/luan/modules/url/LuanUrl.java Sat Oct 26 22:21:09 2019 -0600 @@ -43,8 +43,8 @@ public LuanUrl(URL url,LuanTable options) throws LuanException { if( options != null ) { - Map map = options.asMap(); - String methodStr = Utils.removeString(map,"method"); + options = new LuanTable(options); + String methodStr = Utils.removeString(options,"method"); if( methodStr != null ) { methodStr = methodStr.toUpperCase(); try { @@ -53,7 +53,7 @@ throw new LuanException( "invalid method: "+methodStr ); } } - Map headerMap = removeMap(map,"headers"); + Map headerMap = removeMap(options,"headers"); if( headerMap != null ) { for( Object hack : headerMap.entrySet() ) { Map.Entry entry = (Map.Entry)hack; @@ -71,7 +71,7 @@ } } } - Map auth = removeMap(map,"authorization"); + LuanTable auth = Utils.removeTable(options,"authorization"); if( auth != null ) { if( headers!=null && headers.containsKey("authorization") ) throw new LuanException( "can't define authorization with header 'authorization' defined" ); @@ -92,9 +92,9 @@ authPassword = password; } } - Map params = removeMap(map,"parameters"); - String enctype = Utils.removeString(map,"enctype"); - Object content = map.remove("content"); + Map params = removeMap(options,"parameters"); + String enctype = Utils.removeString(options,"enctype"); + Object content = options.remove("content"); if( content != null ) { if( this.method != Method.POST ) throw new LuanException( "content can only be used with POST" ); @@ -159,10 +159,10 @@ } } } - Integer timeout = Utils.removeInteger(map,"time_out"); + Integer timeout = Utils.removeInteger(options,"time_out"); if( timeout != null ) this.timeout = timeout; - Utils.checkEmpty(map); + Utils.checkEmpty(options); } this.url = url; } @@ -180,8 +180,8 @@ } } - private static Map removeMap(Map map,String key) throws LuanException { - LuanTable t = Utils.removeTable(map,key); + private static Map removeMap(LuanTable options,String key) throws LuanException { + LuanTable t = Utils.removeTable(options,key); return t==null ? null : t.asMap(); }