Skip to content

Commit

Permalink
feat uservice-dynconf: add codegen for SQL queries and JSONs (#59)
Browse files Browse the repository at this point in the history
* Move request and response schemas into components in api.yaml
* Add json schema codegen to cmake
* Use codegen types in handlers
* Add codegen for SQL-queries
* Fix style in includes
  • Loading branch information
Dangerio authored Nov 22, 2024
1 parent 7b00e6d commit d6c6a06
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 310 deletions.
28 changes: 27 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(USERVER_FEATURE_POSTGRESQL ON CACHE BOOL "" FORCE)
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)

# Adding userver dependency
find_package(userver COMPONENTS core postgresql QUIET)
find_package(userver COMPONENTS core postgresql chaotic QUIET)
if(NOT userver_FOUND) # Fallback to subdirectory usage
# Enable userver libraries that are needed in this project
set(USERVER_FEATURE_POSTGRESQL ON CACHE BOOL "" FORCE)
Expand Down Expand Up @@ -37,6 +37,32 @@ add_executable("${PROJECT_NAME}" ${SOURCES})
target_include_directories("${PROJECT_NAME}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries("${PROJECT_NAME}" userver::core userver::postgresql)

# Adding JSON schema codegen
file(GLOB_RECURSE SCHEMAS ${CMAKE_CURRENT_SOURCE_DIR}/docs/*.yaml)
userver_target_generate_chaotic(${PROJECT_NAME}-chgen
ARGS
-n "/components/schemas/([^/]*)/=uservice_dynconf::handlers::{0}"
-f "(.*)={0}"
--clang-format=
--generate-serializers
OUTPUT_DIR
${CMAKE_CURRENT_BINARY_DIR}/src
SCHEMAS
${SCHEMAS}
RELATIVE_TO
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries("${PROJECT_NAME}" ${PROJECT_NAME}-chgen)

# Adding SQL-queries codegen
userver_add_sql_library(
${PROJECT_NAME}-sql
NAMESPACE uservice_dynconf
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}
SQL_FILES src/sql/*.sql
)
target_link_libraries("${PROJECT_NAME}" ${PROJECT_NAME}-sql)

# Functional Tests
userver_testsuite_add_simple()

Expand Down
214 changes: 112 additions & 102 deletions docs/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,14 @@ paths:
content:
application/json:
schema:
additionalProperties: false
type: object
properties:
ids:
type: array
description: list of config ids
items:
type: string
description: config id
update_since:
type: string
description: the date from which to watch config updates
service:
type: string
description: The name of the service to search for configs for
default: __default__
$ref: '#/components/schemas/ConfigsValuesRequestBody'
responses:
200:
description: OK
content:
application/json:
schema:
type: object
additionalProperties: false
required:
- configs
properties:
configs:
type: object
additionalProperties: true
description: Map of received configs
kill_switches_enabled:
type: array
description: Enabled Kill Switches
items:
type: string
description: Config id
kill_switches_disabled:
type: array
description: Disabled Kill Switches
items:
type: string
description: Config id
updated_at:
type: string
description: Time of last update
not_found:
description: list of configs not found
type: array
items:
type: string
description: id конфига
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ConfigsValuesResponseBody'

/admin/v1/configs:
post:
Expand All @@ -90,39 +46,8 @@ paths:
content:
application/json:
schema:
additionalProperties: false
type: object
required:
- configs
properties:
configs:
type: object
additionalProperties: true
description: |
Map key-values, config name and its value,
you can have multiple configs at the same time
kill_switches_enabled:
type: array
description: |
Enabled Kill Switches,
must consist of ids from configs,
must not overlap with kill_switches_disabled
items:
type: string
description: Config id
kill_switches_disabled:
type: array
description: |
Disabled Kill Switches,
must consist of ids from configs,
must not overlap with kill_switches_enabled
items:
type: string
description: Config id
service:
type: string
description: The name of the service to create/update for configs for
default: __default__
$ref: '#/components/schemas/AdminConfigsRequestBody'

responses:
204:
description: OK
Expand All @@ -143,23 +68,8 @@ paths:
content:
application/json:
schema:
additionalProperties: false
type: object
required:
- ids
- service
properties:
ids:
type: array
description: |
list of config ids to delete
items:
type: string
description: id конфига
service:
type: string
description: The name of the service to delete for configs for
default: __default__
$ref: '#/components/schemas/AdminConfigsDeleteRequestBody'

responses:
204:
description: OK
Expand All @@ -182,4 +92,104 @@ components:
message:
type: string
description: message error

ConfigsValuesRequestBody:
description: The body of the config/values request
type: object
additionalProperties: false
properties:
ids:
type: array
description: list of config ids
items:
type: string
description: config id
update_since:
type: string
format: date-time
description: the date from which to watch config updates
service:
type: string
description: The name of the service to search for configs for
default: __default__
ConfigsValuesResponseBody:
description: Response to the config/values request (with code 200)
type: object
additionalProperties: false
required:
- configs
properties:
configs:
type: object
additionalProperties: true
description: Map of received configs
kill_switches_enabled:
type: array
description: Enabled Kill Switches
items:
type: string
description: Config id
kill_switches_disabled:
type: array
description: Disabled Kill Switches
items:
type: string
description: Config id
updated_at:
type: string
format: date-time
description: Time of last update
not_found:
description: list of configs not found
type: array
items:
type: string
description: id конфига
AdminConfigsRequestBody:
description: The body of the /admin/v1/configs request
type: object
additionalProperties: false
properties:
configs:
type: object
additionalProperties: true
description: |
Map key-values, config name and its value,
you can have multiple configs at the same time
kill_switches_enabled:
type: array
x-usrv-cpp-container: std::unordered_set
description: |
Enabled Kill Switches,
must consist of ids from configs,
must not overlap with kill_switches_disabled
items:
type: string
description: Config id
kill_switches_disabled:
type: array
x-usrv-cpp-container: std::unordered_set
description: |
Disabled Kill Switches,
must consist of ids from configs,
must not overlap with kill_switches_disabled
items:
type: string
description: Config id
service:
type: string
description: The name of the service to create/update for configs for
AdminConfigsDeleteRequestBody:
description: The body of the /admin/v1/configs/delete request
type: object
additionalProperties: false
properties:
ids:
type: array
description: |
list of config ids to delete
items:
type: string
description: id конфига
service:
type: string
description: The name of the service to delete for configs for
17 changes: 9 additions & 8 deletions src/cache/configs_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#include "configs_cache.hpp"
#include "userver/logging/log.hpp"
#include "userver/storages/postgres/query.hpp"
#include <cache/configs_cache.hpp>

#include "sql/sql_query.hpp"
#include "userver/utils/algo.hpp"
#include <string_view>

#include <userver/logging/log.hpp>
#include <userver/storages/postgres/query.hpp>
#include <userver/utils/algo.hpp>
#include <uservice_dynconf/sql_queries.hpp>

namespace uservice_dynconf::cache::settings_cache {

namespace {
constexpr static const char *kDefaultService = "__default__";
}

userver::storages::postgres::Query ConfigCachePolicy::kQuery =
userver::storages::postgres::Query(
uservice_dynconf::sql::kSelectSettingsForCache.data());
userver::storages::postgres::Query ConfigCachePolicy::GetQuery() {
return uservice_dynconf::sql::kSelectSettingsForCache;
}

void ConfigCacheContainer::insert_or_assign(Key &&key, Config &&config) {
auto config_ptr = std::make_shared<const Config>(std::move(config));
Expand Down
5 changes: 3 additions & 2 deletions src/cache/configs_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

#include <memory>
#include <unordered_map>

#include <userver/cache/base_postgres_cache.hpp>
#include <userver/storages/postgres/io/chrono.hpp>

#include "models/config.hpp"
#include <models/config.hpp>

namespace uservice_dynconf::cache::settings_cache {

Expand All @@ -29,11 +30,11 @@ class ConfigCacheContainer {
};

struct ConfigCachePolicy {
static userver::storages::postgres::Query GetQuery();
static constexpr auto kName = "configs-cache";
using ValueType = uservice_dynconf::models::Config;
using CacheContainer = ConfigCacheContainer;
static constexpr auto kKeyMember = &uservice_dynconf::models::Config::key;
static userver::storages::postgres::Query kQuery;
static constexpr auto kUpdatedField = "updated_at";
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
};
Expand Down
Loading

0 comments on commit d6c6a06

Please sign in to comment.