tnetstring.nim
changeset 4 800be124db98
parent 1 87a0365b799b
child 5 d3804875d828
equal deleted inserted replaced
3:ef9f89362aac 4:800be124db98
   182     new( result )
   182     new( result )
   183     result.kind = TNetstringArray
   183     result.kind = TNetstringArray
   184     result.elems = @[]
   184     result.elems = @[]
   185 
   185 
   186 
   186 
       
   187 proc getStr*( node: TNetstringNode, default: string = "" ): string =
       
   188     ## Retrieves the string value of a `TNetstringString TNetstringNodee`.
       
   189     ## Returns ``default`` if ``node`` is not a ``TNetstringString``.
       
   190     if node.kind != TNetstringString: return default
       
   191     return node.str
       
   192 
       
   193 
       
   194 proc getInt*( node: TNetstringNode, default: BiggestInt = 0 ): BiggestInt =
       
   195     ## Retrieves the int value of a `TNetstringInt TNetstringNode`.
       
   196     ## Returns ``default`` if ``node`` is not a ``TNetstringInt``.
       
   197     if node.kind != TNetstringInt: return default
       
   198     return node.num
       
   199 
       
   200 
       
   201 proc getFloat*( node: TNetstringNode, default: float = 0.0 ): float =
       
   202     ## Retrieves the float value of a `TNetstringFloat TNetstringNode`.
       
   203     ## Returns ``default`` if ``node`` is not a ``TNetstringFloat``.
       
   204     if node.kind != TNetstringFloat: return default
       
   205     return node.fnum
       
   206 
       
   207 
       
   208 proc getBool*( node: TNetstringNode, default: bool = false ): bool =
       
   209     ## Retrieves the bool value of a `TNetstringBool TNetstringNode`.
       
   210     ## Returns ``default`` if ``node`` is not a ``TNetstringBool``.
       
   211     if node.kind != TNetstringBool: return default
       
   212     return node.bval
       
   213 
       
   214 
       
   215 proc getFields*( node: TNetstringNode,
       
   216     default: seq[tuple[key: string, val: TNetstringNode]] = @[] ):
       
   217         seq[tuple[key: string, val: TNetstringNode]] =
       
   218     ## Retrieves the key, value pairs of a `TNetstringObject TNetstringNode`.
       
   219     ## Returns ``default`` if ``node`` is not a ``TNetstringObject``.
       
   220     if node.kind != TNetstringObject: return default
       
   221     return node.fields
       
   222 
       
   223 
       
   224 proc getElems*( node: TNetstringNode, default: seq[TNetstringNode] = @[] ): seq[TNetstringNode] =
       
   225     ## Retrieves the values of a `TNetstringArray TNetstringNode`.
       
   226     ## Returns ``default`` if ``node`` is not a ``TNetstringArray``.
       
   227     if node.kind != TNetstringArray: return default
       
   228     return node.elems
       
   229 
       
   230 
   187 proc parse_tnetstring*( data: string ): TNetstringNode =
   231 proc parse_tnetstring*( data: string ): TNetstringNode =
   188     ## Given an encoded tnetstring, parse and return a TNetstringNode.
   232     ## Given an encoded tnetstring, parse and return a TNetstringNode.
   189     var
   233     var
   190         length:  int
   234         length:  int
   191         kind:    char
   235         kind:    char
   628         "5:false!7:galhide,0:~}13:homeDirectory,14:/home/m/mahlon,3:uid,6:mahlon,9:yubi" &
   672         "5:false!7:galhide,0:~}13:homeDirectory,14:/home/m/mahlon,3:uid,6:mahlon,9:yubi" &
   629         "KeyId,12:vvidhghkhehj,5:gecos,12:Mahlon Smith,2:sn,5:Smith,14:employeeNumber,5:12921#}"
   673         "KeyId,12:vvidhghkhehj,5:gecos,12:Mahlon Smith,2:sn,5:Smith,14:employeeNumber,5:12921#}"
   630     tnet_obj = parse_tnetstring( tstr )
   674     tnet_obj = parse_tnetstring( tstr )
   631     doAssert( tstr == tnet_obj.dump_tnetstring )
   675     doAssert( tstr == tnet_obj.dump_tnetstring )
   632 
   676 
       
   677     # Value fetching methods
       
   678     #
       
   679     var tnet_null = newTNetstringNull()
       
   680     tnet_obj = newTNetstringString( "Hello." )
       
   681     doAssert( tnet_obj.getStr == "Hello." )
       
   682     doAssert( tnet_null.getStr("nope") == "nope" )
       
   683     doAssert( tnet_null.getStr == "" )
       
   684     tnet_obj = newTNetstringInt( 42 )
       
   685     doAssert( tnet_obj.getInt == 42 )
       
   686     doAssert( tnet_null.getInt == 0 )
       
   687     doAssert( tnet_null.getInt(1) == 1 )
       
   688     tnet_obj = newTNetstringFloat( 1.0 )
       
   689     doAssert( tnet_obj.getFloat == 1.0 )
       
   690     doAssert( tnet_null.getFloat == 0 )
       
   691     doAssert( tnet_null.getFloat(0.1) == 0.1 )
       
   692     tnet_obj = newTNetstringObject()
       
   693     tnet_obj[ "yay" ] = newTNetstringInt( 1 )
       
   694     doAssert( tnet_obj.getFields[0].val == newTNetstringInt(1) )
       
   695     doAssert( tnet_null.getFields.len == 0 )
       
   696     tnet_obj = newTNetstringArray()
       
   697     tnet_obj.add( newTNetstringInt(1) )
       
   698     doAssert( tnet_obj.getElems[0] == newTNetstringInt(1) )
       
   699     doAssert( tnet_null.getElems.len == 0 )
       
   700 
       
   701 
   633     echo "* Tests passed!"
   702     echo "* Tests passed!"
   634 
       
   635 
   703 
   636     while true and defined( testing ):
   704     while true and defined( testing ):
   637         for line in readline( stdin ).split_lines:
   705         for line in readline( stdin ).split_lines:
   638             let input = line.strip
   706             let input = line.strip
   639             try:
   707             try: