Skip to content

Commit

Permalink
Merge pull request #14 from MattEstHaut/feature/resp-types
Browse files Browse the repository at this point in the history
Ajoute des types RESP
  • Loading branch information
MattEstHaut authored Jun 3, 2024
2 parents 4f57229 + 372bb4a commit 770f76a
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/Shared/Resp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ static public Item Decode(StreamReader reader)
'_' => Null.Decode(reader),
'*' => ItemArray.Decode(reader),
'$' => BulkString.Decode(reader),
':' => Integer.Decode(reader),
'#' => Boolean.Decode(reader),
'%' => Map.Decode(reader),
_ => throw new Exception("Invalid item type")
};
}
Expand Down Expand Up @@ -157,4 +160,110 @@ public override string Encode()

return new ItemArray(items.ToArray());
}
}

public class Integer : Item
{
public long Value { get; }

public Integer(long value) { Value = value; }

public override string Encode() => $":{Value}\r\n";

public override string ToString() => Value.ToString();

public new static Integer Decode(StreamReader reader)
{
if ((char)reader.Read() != ':')
throw new Exception("Invalid integer header");

if (!long.TryParse(reader.ReadLine(), out long value))
throw new Exception("Unable to parse integer");

return new Integer(value);
}
}

public class Boolean : Item
{
public bool Value { get; }

public Boolean(bool value) { Value = value; }

public override string Encode() => $"#{(Value ? 't' : 'f')}\r\n";

public override string ToString() => Value.ToString();

public new static Boolean Decode(StreamReader reader)
{
if ((char)reader.Read() != '#')
throw new Exception("Invalid boolean header");

char value = (char)reader.Read();
if (value != 't' && value != 'f')
throw new Exception("Invalid boolean value");

_ = reader.ReadLine();
return new Boolean(value == 't');
}
}

public class Map : Item
{
public Dictionary<Item, Item> Items { get; } = new Dictionary<Item, Item>();

public Map() { }

public Map(Dictionary<Item, Item> items)
{
Items = items.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}

public override string ToString()
{
var sb = new StringBuilder();
sb.Append("{");

foreach (var (key, value) in Items)
sb.Append($"{key}: {value}, ");

if (Items.Count > 0)
sb.Length -= 2;

sb.Append("}");
return sb.ToString();
}

public override string Encode()
{
var sb = new StringBuilder();
sb.Append($"%{Items.Count}\r\n");

foreach (var (key, value) in Items)
{
sb.Append(key.Encode());
sb.Append(value.Encode());
}

return sb.ToString();
}

public new static Map Decode(StreamReader reader)
{
if ((char)reader.Read() != '%')
throw new Exception("Invalid map header");

int size = Size(reader);
if (size < 0) throw new Exception("Size less than zero");
var items = new Dictionary<Item, Item>();

for (int i = 0; i < size; i++)
{
var key = Item.Decode(reader);
var value = Item.Decode(reader);
items.Add(key, value);
}

return new Map(items);
}
}

0 comments on commit 770f76a

Please sign in to comment.