Skip to content

Commit

Permalink
Refactored based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
steweg committed Apr 2, 2024
1 parent 5369cd4 commit 306252c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
31 changes: 30 additions & 1 deletion src/plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,24 @@ lyplg_add(const char *pathname)
#endif
}

LIBYANG_API_DEF LY_ERR
/**
* @brief Manually load an extension plugins from memory
*
* Note, that a plugin can be loaded only if there is at least one context. The loaded plugins are connected with the
* existence of a context. When all the contexts are destroyed, all the plugins are unloaded.
*
* @param[in] ctx The context to which the plugin should be associated with. If NULL, the plugin is considered to be shared
* between all existing contexts.
* @param[in] version The version of plugin records.
* @param[in] type The type of plugins records.
* @param[in] recs An array of plugin records provided by the plugin implementation. The array must be terminated by a zeroed
* record.
*
* @return LY_SUCCESS if the plugins with compatible version were successfully loaded.
* @return LY_EDENIED in case there is no context and the plugin cannot be loaded.
* @return LY_EINVAL when recs is NULL or the plugin contains invalid content for this libyang version.
*/
static LY_ERR
lyplg_add_plugin(struct ly_ctx *ctx, uint32_t version, enum LYPLG type, const void *recs)
{
LY_ERR ret = LY_SUCCESS;
Expand All @@ -621,3 +638,15 @@ lyplg_add_plugin(struct ly_ctx *ctx, uint32_t version, enum LYPLG type, const vo

return ret;
}

LIBYANG_API_DEF LY_ERR
lyplg_add_extension_plugin(struct ly_ctx *ctx, uint32_t version, const struct lyplg_ext_record *recs)
{
return lyplg_add_plugin(ctx, version, LYPLG_EXTENSION, recs);
}

LIBYANG_API_DEF LY_ERR
lyplg_add_type_plugin(struct ly_ctx *ctx, uint32_t version, const struct lyplg_type_record *recs)
{
return lyplg_add_plugin(ctx, version, LYPLG_TYPE, recs);
}
26 changes: 23 additions & 3 deletions src/plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include "log.h"

struct lyplg_ext_record;
struct lyplg_type_record;

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -86,23 +89,40 @@ enum LYPLG {
LIBYANG_API_DECL LY_ERR lyplg_add(const char *pathname);

/**
* @brief Manually load a plugins from memory
* @brief Manually load extension plugins from memory
*
* Note, that a plugin can be loaded only if there is at least one context. The loaded plugins are connected with the
* existence of a context. When all the contexts are destroyed, all the plugins are unloaded.
*
* @param[in] ctx The context to which the plugin should be associated with. If NULL, the plugin is considered to be shared
* between all existing contexts.
* @param[in] version The version of plugin records.
* @param[in] recs An array of plugin records provided by the plugin implementation. The array must be terminated by a zeroed
* record.
*
* @return LY_SUCCESS if the plugins with compatible version were successfully loaded.
* @return LY_EDENIED in case there is no context and the plugin cannot be loaded.
* @return LY_EINVAL when recs is NULL or the plugin contains invalid content for this libyang version.
*/
LIBYANG_API_DECL LY_ERR lyplg_add_extension_plugin(struct ly_ctx *ctx, uint32_t version, const struct lyplg_ext_record *recs);

/**
* @brief Manually load type plugins from memory
*
* Note, that a plugin can be loaded only if there is at least one context. The loaded plugins are connected with the
* existence of a context. When all the contexts are destroyed, all the plugins are unloaded.
*
* @param[in] ctx The context to which the plugin should be associated with. If NULL, the plugin is considered to be shared
* between all existing contexts.
* @param[in] version The version of plugin records.
* @param[in] type The type of plugins records.
* @param[in] recs An array of plugin records provided by the plugin implementation. The array must be terminated by a zeroed
* record.
*
* @return LY_SUCCESS if the plugins with compatible version were successfully loaded.
* @return LY_EDENIED in case there is no context and the plugin cannot be loaded.
* @return LY_EINVAL when recs is NULL or the plugin contains invalid content for this libyang version.
*/
LIBYANG_API_DECL LY_ERR lyplg_add_plugin(struct ly_ctx *ctx, uint32_t version, enum LYPLG type, const void *recs);
LIBYANG_API_DECL LY_ERR lyplg_add_type_plugin(struct ly_ctx *ctx, uint32_t version, const struct lyplg_type_record *recs);
/** @} plugins */

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion tests/utests/basic/test_plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ test_simple_from_memory(void **state)
struct lys_module *mod;
struct lysc_node_leaf *leaf;

lyplg_add_plugin(UTEST_LYCTX, LYPLG_EXT_API_VERSION, LYPLG_EXTENSION, memory_recs);
lyplg_add_extension_plugin(UTEST_LYCTX, LYPLG_EXT_API_VERSION, memory_recs);
UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);

leaf = (struct lysc_node_leaf *)mod->compiled->data;
Expand Down

0 comments on commit 306252c

Please sign in to comment.