diff src/luan/modules/Utils.java @ 1389:eb8b35dccd99

cleanup and stringify change
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 03 Sep 2019 22:54:31 -0600
parents 836e00bf7ce2
children 002152af497a
line wrap: on
line diff
--- 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 );
+	}
+
 }