Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @:forward(keys)
- abstract YamlMap (StringMap<Either<String, YamlMap>>)
- {
- public inline function new()
- {
- this = new StringMap<Either<String, YamlMap>>();
- }
- public function getEither(aKey:String):Either<String, YamlMap>
- {
- return this.get(aKey);
- }
- public function getDynamic(aKey:String):Dynamic
- {
- switch (this.get(aKey))
- {
- case Left(s): return s;
- case Right(m): return m;
- case null: return null;
- }
- }
- public function isMap(aKey:String):Bool
- {
- switch(this.get(aKey))
- {
- case Left(str): return false;
- case Right(map): return true;
- }
- }
- public function isString(aKey:String):Bool
- {
- return !isMap(aKey);
- }
- @:arrayAccess
- public function getString(aKey:String):String
- {
- switch (this.get(aKey))
- {
- case Left(str): return str;
- case Right(map): new WrongYamlTypeError('$aKey is not a string but a map'); return null;
- default: new NullYamlValueError('$aKey does not exist'); return null;
- }
- }
- @:arrayAccess
- public function getMap(aKey:String):YamlMap
- {
- switch (this.get(aKey))
- {
- case Right(map): return map;
- case Left(str): new WrongYamlTypeError('$aKey is not a map but a string'); return null;
- default: new NullYamlValueError('$aKey does not exist'); return null;
- }
- }
- public function setString(aKey:String, aString:String):Either<String, YamlMap>
- {
- var e = Either.Left(aString);
- this.set(aKey, e);
- return e;
- }
- public function setMap(aKey:String, aMap:YamlMap):Either<String, YamlMap>
- {
- var e = Either.Right(aMap);
- this.set(aKey, e);
- return e;
- }
- public function iterator():Iterator<Either<String, YamlMap>>
- {
- return this.iterator();
- }
- public function toString(?aTab:Int = 0):String
- {
- var s:String = '';
- if (aTab == 0) s = '\n';
- for (i in this.keys())
- {
- var line:String = ''.lpad('\t', aTab);
- if (isString(i))
- {
- line += i + '(s):' + getString(i) + '\n';
- }
- else
- {
- line += i + '(m):\n' + getMap(i).toString(aTab+1);
- }
- s += line;
- }
- return s;
- }
- }
- class WrongYamlTypeError extends Error {}
- class NullYamlValueError extends Error {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement