Created
April 7, 2019 08:12
-
-
Save leshido/7de7869ae3003806bcd28730ec8004f2 to your computer and use it in GitHub Desktop.
Get param from Map as different supported types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
class Main { | |
public static function main() { | |
Params.fromQueryString("?foo=bar&thing=true&num=12"); | |
Params.get("foo", "cool"); // 'bar' ('foo' is defined) | |
Params.get("foo2", "cool"); // 'cool' ('foo2' is undefined, default value returned) | |
Params.get("thing", false); // true ('thing' is defined) | |
Params.get("thing", ""); // 'true' (get 'thing' value as String) | |
Params.get("num", 10) + 2; // 14 ('num' is defined = 12) | |
Params.get("num", 10.0) + 2; // 14.0 ('num' is defined and returned as Float) | |
Params.get("num", "10") + 2; // '122' ('num' is defined and returned as String) | |
Params.get("array", [1,2,3]); // ERROR: Unsupported parameter type | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import haxe.macro.Context; | |
class Params { | |
private static var _params:Map<String, String>; | |
#if display | |
/** | |
Get the value of query string parameter `param`, or `defaultValue` if it's null or not set. | |
@param param The param to return. | |
@param defaultValue The value to return if `param` is missing. | |
**/ | |
public static function get<T>(param:String, defaultValue:T):T { | |
return defaultValue; | |
} | |
#else | |
public static macro function get(param:String, defaultValue) { | |
var type = Context.typeof(defaultValue); | |
if (Context.unify(type, Context.typeof(macro ""))) { | |
return macro Params.getParamAsString($v{param}, ${defaultValue}); | |
} else if (Context.unify(type, Context.typeof(macro 1))) { | |
return macro Params.getParamAsInt($v{param}, ${defaultValue}); | |
} else if (Context.unify(type, Context.typeof(macro 1.1))) { | |
return macro Params.getParamAsFloat($v{param}, ${defaultValue}); | |
} else if (Context.unify(type, Context.typeof(macro true))) { | |
return macro Params.getParamAsBool($v{param}, ${defaultValue}); | |
} | |
Context.error("Unsupported parameter type: " + type, Context.currentPos()); | |
return macro null; | |
} | |
public static function getParamAsString(param:String, defaultValue:String):String { | |
if (_params == null || !_params.exists(param) || _params[param] == null) return defaultValue; | |
return _params[param]; | |
} | |
public static function getParamAsInt(param:String, defaultValue:Int):Int { | |
if (_params == null || !_params.exists(param) || _params[param] == null) return defaultValue; | |
return Std.parseInt(_params[param]); | |
} | |
public static function getParamAsFloat(param:String, defaultValue:Float):Float { | |
if (_params == null || !_params.exists(param) || _params[param] == null) return defaultValue; | |
return Std.parseFloat(_params[param]); | |
} | |
public static function getParamAsBool(param:String, defaultValue:Bool):Bool { | |
if (_params == null || !_params.exists(param) || _params[param] == null) return defaultValue; | |
return _params[param] == "true"; | |
} | |
#end | |
@:noCompletion public static function fromQueryString(qs:String) { | |
if (_params == null) _params = new Map<String, String>(); | |
qs = qs.substring(1); | |
var pairs = qs.split("&"); | |
for (pair in pairs) { | |
var p = pair.split("="); | |
if (p.length > 1) { | |
_params.set( StringTools.urlDecode(p.shift()), StringTools.urlDecode(p.join("=")) ); | |
} else { | |
_params.set( StringTools.urlDecode(p[0]), null ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment