Advertisement
Guest User

abstract YamlMap

a guest
Aug 29th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.16 KB | None | 0 0
  1. @:forward(keys)
  2. abstract YamlMap (StringMap<Either<String, YamlMap>>)
  3. {
  4.     public inline function new()
  5.     {
  6.         this = new StringMap<Either<String, YamlMap>>();
  7.     }
  8.    
  9.     public function getEither(aKey:String):Either<String, YamlMap>
  10.     {
  11.         return this.get(aKey);
  12.     }
  13.    
  14.    
  15.     public function getDynamic(aKey:String):Dynamic
  16.     {
  17.         switch (this.get(aKey))
  18.         {
  19.             case Left(s): return s;
  20.             case Right(m): return m;
  21.             case null: return null;
  22.         }
  23.     }
  24.    
  25.     public function isMap(aKey:String):Bool
  26.     {
  27.         switch(this.get(aKey))
  28.         {
  29.             case Left(str): return false;
  30.             case Right(map): return true;
  31.         }
  32.     }
  33.    
  34.     public function isString(aKey:String):Bool
  35.     {
  36.         return !isMap(aKey);
  37.     }
  38.    
  39.     @:arrayAccess
  40.     public function getString(aKey:String):String
  41.     {
  42.         switch (this.get(aKey))
  43.         {
  44.             case Left(str): return str;
  45.             case Right(map): new WrongYamlTypeError('$aKey is not a string but a map'); return null;
  46.             default: new NullYamlValueError('$aKey does not exist'); return null;
  47.         }
  48.     }
  49.    
  50.     @:arrayAccess
  51.     public function getMap(aKey:String):YamlMap
  52.     {
  53.         switch (this.get(aKey))
  54.         {
  55.             case Right(map): return map;
  56.             case Left(str): new WrongYamlTypeError('$aKey is not a map but a string'); return null;
  57.             default: new NullYamlValueError('$aKey does not exist'); return null;      
  58.         }
  59.     }
  60.    
  61.    
  62.    
  63.    
  64.     public function setString(aKey:String, aString:String):Either<String, YamlMap>
  65.     {
  66.         var e = Either.Left(aString);
  67.         this.set(aKey, e);
  68.         return e;
  69.     }
  70.    
  71.     public function setMap(aKey:String, aMap:YamlMap):Either<String, YamlMap>
  72.     {
  73.         var e = Either.Right(aMap);
  74.         this.set(aKey, e);
  75.         return e;
  76.     }
  77.    
  78.     public function iterator():Iterator<Either<String, YamlMap>>
  79.     {
  80.         return this.iterator();
  81.     }
  82.    
  83.     public function toString(?aTab:Int = 0):String
  84.     {
  85.         var s:String = '';
  86.         if (aTab == 0) s = '\n';
  87.         for (i in this.keys())
  88.         {
  89.             var line:String = ''.lpad('\t', aTab);
  90.             if (isString(i))
  91.             {
  92.                 line += i + '(s):' + getString(i) + '\n';
  93.             }
  94.             else
  95.             {
  96.                 line += i + '(m):\n' + getMap(i).toString(aTab+1);
  97.             }
  98.             s += line;
  99.         }
  100.        
  101.         return s;
  102.     }
  103.    
  104. }
  105.  
  106. class WrongYamlTypeError extends Error {}
  107. class NullYamlValueError extends Error {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement