Skip to content

Commit

Permalink
Merge pull request #30 from martin-olivier/feat/queue-interface-wait
Browse files Browse the repository at this point in the history
feat: Wait() virtual pure on IResponseInputQueue
  • Loading branch information
Romain-1 authored Feb 20, 2022
2 parents 4c880b3 + 7e07110 commit d42d926
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
43 changes: 37 additions & 6 deletions docs/guides/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ You could represent it using a `ziapi::config::Node` like so

```cpp
using Node = ziapi::config::Node;
using Dict = ziapi::config::Dict;
using Array = ziapi::config::Array;
Node obj(
{
Expand Down Expand Up @@ -75,7 +77,31 @@ auto obj = Node::MakeDict({
})}
})}
})
```
### More informations about Dict and Array helper functions
```cpp
// You can also give the helper methods a vector representing your array or dictionnary
// The helper functions will automatically transform your contained Nodes into shared_ptr
std::unordered_map<std::string, Node> dictionnary_vector;
// ... Fill the dictionnary vector
Node dictionnary_node = Node::MakeDict(dictionnary_vector)
std::vector<Node> array_vector;
// ... Fill the array vector
Node array_node = Node::MakeArray(array_vector)
// You can of course build them from the Node base types
// In this case, you're responsible of giving the values as shared_ptr and converting the final variable as a Node
Dict dictionnary;
dictionnary["modules_count"] = std::make_shared<Node>(10);
Node dictionnary_node(dictionnary);
Array arr;
arr.emplace_back(std::make_shared("/bin/ls"));
Node array_node(arr);
```

## Data Types
Expand Down Expand Up @@ -170,17 +196,20 @@ The `Array` data type represents an array of values. So the following JSON:
Would translate to

```cpp
ziapi::config::Node({
ziapi::config::Array arr({
std::make_shared<ziapi::config::Node>("Hello"),
std::make_shared<ziapi::config::Node>("World"),
std::make_shared<ziapi::config::Node>("!")
});
ziapi::config::Node arr_node(arr);
```

Which is equivalent to

```c++
ziapi::config::Node::MakeArray({ "Hello", "World", "!" });
auto array_node = ziapi::config::Node::MakeArray({ "Hello", "World", "!" });
// vector<Node> can also be passed as a parameter
```

### `Dict`
Expand All @@ -199,21 +228,23 @@ The `Dict` data type represents a dictionary of values. So the following JSON:
Would translate to

```cpp
ziapi::config::Node({
ziapi::config::Dict dict({
{"age", std::make_shared<ziapi::config::Node>(19)},
{"first_name", std::make_shared<ziapi::config::Node>("Charlie")},
{"last_name", std::make_shared<ziapi::config::Node>("Chou")},
{"is_sexy", std::make_shared<ziapi::config::Node>(true)}
})
ziapi::config::Node node_dict(dict);
```

Which is equivalent to

```c++
ziapi::config::Node::MakeDict({
auto node_dict = ziapi::config::Node::MakeDict({
{"age", 19},
{"first_name", "Charlie"},
{"last_name", "Chou"},
{"is_sexy", true},
})
```
}) // vector<std::string, Node> can also be passed as a parameter
```
26 changes: 24 additions & 2 deletions include/ziapi/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct Node : public NodeVariant {
public:
using NodeVariant::NodeVariant;

/// Simpler way to construct a node from a Array, it instantiate std::shared_ptr by itself
/// Simpler way to construct an Array node, it instantiates an std::shared_ptr for each value
static Node MakeArray(const std::initializer_list<Node> &values)
{
Array arr;
Expand All @@ -51,7 +51,18 @@ struct Node : public NodeVariant {
return arr;
}

/// Simpler way to construct a node from a Dict, it instantiate std::shared_ptr by itself
/// Constructs an Array node from a vector, it instantiates an std::shared_ptr for each value
static Node MakeArray(const std::vector<Node> &values)
{
Array arr;

for (auto &value : values) {
arr.push_back(std::make_shared<Node>(value));
}
return arr;
}

/// Simpler way to construct a Dict node, it instantiates an std::shared_ptr for each value
static Node MakeDict(const std::initializer_list<std::pair<std::string, Node>> &values)
{
Dict dict;
Expand All @@ -62,6 +73,17 @@ struct Node : public NodeVariant {
return dict;
}

/// Constructs a Dict node from a vector, it instantiates an std::shared_ptr for each value
static Node MakeDict(const std::unordered_map<std::string, Node> &values)
{
Dict dict;

for (auto &[key, value] : values) {
dict[key] = std::make_shared<Node>(value);
}
return dict;
}

/// Used to construct a Node from a string
Node(const char *str) : NodeVariant(std::string(str)){};

Expand Down
2 changes: 2 additions & 0 deletions include/ziapi/Http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class IResponseInputQueue {
[[nodiscard]] virtual std::optional<std::pair<Response, Context>> Pop() = 0;

[[nodiscard]] virtual std::size_t Size() const noexcept = 0;

virtual void Wait() noexcept = 0;
};

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,24 @@ TEST(Config, OperatorBool)
ASSERT_EQ(null.operator bool(), false);
ASSERT_EQ(string.operator bool(), true);
}

TEST(Config, VectorConstructArray)
{
std::vector<Node> vec{10, "value", 1.f};
Node n = Node::MakeArray(vec);

ASSERT_EQ(n[(std::size_t)0].AsInt(), 10);
ASSERT_EQ(n[1].AsString(), "value");
ASSERT_EQ(n[2].AsDouble(), 1.f);
}

TEST(Config, VectorConstructDict)
{
std::unordered_map<std::string, Node> vec{{"ten", 10}, {"string", "value"}, {"float", 1.f}, {"null", nullptr}};
Node n = Node::MakeDict(vec);

ASSERT_EQ(n["ten"].AsInt(), 10);
ASSERT_EQ(n["string"].AsString(), "value");
ASSERT_EQ(n["float"].AsDouble(), 1.f);
ASSERT_EQ(n["null"].IsNull(), true);
}

0 comments on commit d42d926

Please sign in to comment.