Mercurial Hosting > luan
comparison src/luan/modules/Utils.java @ 1420:225808b90cee
options handling
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 26 Oct 2019 22:21:09 -0600 |
parents | 002152af497a |
children | 471ef3e6a84e |
comparison
equal
deleted
inserted
replaced
1419:59fd2e8b1b9d | 1420:225808b90cee |
---|---|
95 copyAll(in,out); | 95 copyAll(in,out); |
96 return out.toByteArray(); | 96 return out.toByteArray(); |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 public static String removeString(Map map,String key) throws LuanException { | 100 public static String removeString(LuanTable options,String key) throws LuanException { |
101 Object val = map.remove(key); | 101 Object val = options.remove(key); |
102 if( val!=null && !(val instanceof String) ) | 102 if( val!=null && !(val instanceof String) ) |
103 throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); | 103 throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); |
104 return (String)val; | 104 return (String)val; |
105 } | 105 } |
106 | 106 |
107 public static String removeRequiredString(Map map,String key) throws LuanException { | 107 public static String removeRequiredString(LuanTable options,String key) throws LuanException { |
108 String s = removeString(map,key); | 108 String s = removeString(options,key); |
109 if( s==null ) | 109 if( s==null ) |
110 throw new LuanException( "parameter '"+key+"' is required" ); | 110 throw new LuanException( "parameter '"+key+"' is required" ); |
111 return s; | 111 return s; |
112 } | 112 } |
113 | 113 |
114 public static Number removeNumber(Map map,String key) throws LuanException { | 114 public static Number removeNumber(LuanTable options,String key) throws LuanException { |
115 Object val = map.remove(key); | 115 Object val = options.remove(key); |
116 if( val!=null && !(val instanceof Number) ) | 116 if( val!=null && !(val instanceof Number) ) |
117 throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); | 117 throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); |
118 return (Number)val; | 118 return (Number)val; |
119 } | 119 } |
120 | 120 |
121 public static Integer removeInteger(Map map,String key) throws LuanException { | 121 public static Integer removeInteger(LuanTable options,String key) throws LuanException { |
122 Object val = map.remove(key); | 122 Object val = options.remove(key); |
123 if( val==null ) | 123 if( val==null ) |
124 return null; | 124 return null; |
125 Integer i = Luan.asInteger(val); | 125 Integer i = Luan.asInteger(val); |
126 if( i==null ) { | 126 if( i==null ) { |
127 String type = val instanceof Number ? val.getClass().getSimpleName().toLowerCase() : Luan.type(val); | 127 String type = val instanceof Number ? val.getClass().getSimpleName().toLowerCase() : Luan.type(val); |
128 throw new LuanException( "parameter '"+key+"' must be an integer but is a "+type ); | 128 throw new LuanException( "parameter '"+key+"' must be an integer but is a "+type ); |
129 } | 129 } |
130 return i; | 130 return i; |
131 } | 131 } |
132 | 132 |
133 public static LuanTable removeTable(Map map,String key) throws LuanException { | 133 public static LuanTable removeTable(LuanTable options,String key) throws LuanException { |
134 Object val = map.remove(key); | 134 Object val = options.remove(key); |
135 if( val!=null && !(val instanceof LuanTable) ) | 135 if( val!=null && !(val instanceof LuanTable) ) |
136 throw new LuanException( "parameter '"+key+"' must be a table but is a "+Luan.type(val) ); | 136 throw new LuanException( "parameter '"+key+"' must be a table but is a "+Luan.type(val) ); |
137 return (LuanTable)val; | 137 return (LuanTable)val; |
138 } | 138 } |
139 | 139 |
140 public static Boolean removeBoolean(Map map,String key) throws LuanException { | 140 public static Boolean removeBoolean(LuanTable options,String key) throws LuanException { |
141 Object val = map.remove(key); | 141 Object val = options.remove(key); |
142 if( val!=null && !(val instanceof Boolean) ) | 142 if( val!=null && !(val instanceof Boolean) ) |
143 throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); | 143 throw new LuanException( "parameter '"+key+"' must be a string but is a "+Luan.type(val) ); |
144 return (Boolean)val; | 144 return (Boolean)val; |
145 } | 145 } |
146 | 146 |
147 public static LuanFunction removeFunction(Map map,String key) throws LuanException { | 147 public static LuanFunction removeFunction(LuanTable options,String key) throws LuanException { |
148 Object val = map.remove(key); | 148 Object val = options.remove(key); |
149 if( val!=null && !(val instanceof LuanFunction) ) | 149 if( val!=null && !(val instanceof LuanFunction) ) |
150 throw new LuanException( "parameter '"+key+"' must be a function but is a "+Luan.type(val) ); | 150 throw new LuanException( "parameter '"+key+"' must be a function but is a "+Luan.type(val) ); |
151 return (LuanFunction)val; | 151 return (LuanFunction)val; |
152 } | 152 } |
153 | 153 |
154 public static LuanFunction removeRequiredFunction(Map map,String key) throws LuanException { | 154 public static LuanFunction removeRequiredFunction(LuanTable options,String key) throws LuanException { |
155 LuanFunction fn = removeFunction(map,key); | 155 LuanFunction fn = removeFunction(options,key); |
156 if( fn==null ) | 156 if( fn==null ) |
157 throw new LuanException( "parameter '"+key+"' is required" ); | 157 throw new LuanException( "parameter '"+key+"' is required" ); |
158 return fn; | 158 return fn; |
159 } | 159 } |
160 | 160 |
161 public static void checkEmpty(Map map) throws LuanException { | 161 public static void checkEmpty(LuanTable options) throws LuanException { |
162 if( !map.isEmpty() ) | 162 if( !options.isEmpty() ) |
163 throw new LuanException( "unrecognized options: "+map ); | 163 throw new LuanException( "unrecognized options: "+options.asMap() ); |
164 } | 164 } |
165 | 165 |
166 } | 166 } |