Skip to content

Commit 1615a6c

Browse files
author
roman
committed
plugins types UPDATE simplify type plugin getter
1 parent ab23d01 commit 1615a6c

26 files changed

+181
-130
lines changed

src/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ struct ly_ctx;
212212
#define LY_CTX_STATIC_PLUGINS_ONLY 0x2000 /**< By default, external plugins from directories the path to which is obtained
213213
from the `LIBYANG_TYPES_PLUGINS_DIR` and `LIBYANG_EXTENSIONS_PLUGINS_DIR` environmental variables
214214
are loaded. This option prevents loading of all external plugins and only
215-
the static (built-in) plugins are loaded.
215+
the static (built-in) plugins are loaded. */
216216

217217
/* 0x80000000 reserved for internal use */
218218

src/plugins.c

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,41 @@ static struct ly_set plugins_handlers = {0};
132132
static struct ly_set plugins_types = {0};
133133
static struct ly_set plugins_extensions = {0};
134134

135-
LIBYANG_API_DEF struct lyplg_type *
136-
lysc_get_type_plugin(uintptr_t plugin_id)
135+
/**
136+
* @brief Get the plugin of the given @p type.
137+
*
138+
* @param[in] plugin_id Either an index of the built-in plugin (offset by +1) or a pointer to the external plugin.
139+
* @param[in] type Type of the plugin to get.
140+
* @param[in] plugins Array of the built-in plugins used in case @p plugin_id is an index of a built-in plugin.
141+
* @return Plugin of the given @p type or NULL if not found.
142+
*/
143+
static void *
144+
lysc_get_plugin(uintptr_t plugin_id, enum LYPLG type, const struct ly_set *plugins)
137145
{
138-
if (plugin_id < plugins_types.count) {
139-
return &((struct lyplg_type_record *)plugins_types.objs[plugin_id])->plugin;
146+
/* plugin_id is offset by +1, so 0 is invalid (NULL ptr equivalent) */
147+
if (!plugin_id) {
148+
return NULL;
149+
}
150+
151+
if (plugin_id <= plugins->count) {
152+
/* plugin is built-in, fetch it from the global list */
153+
if (type == LYPLG_EXTENSION) {
154+
return &((struct lyplg_ext_record *)plugins->objs[plugin_id - 1])->plugin;
155+
} else {
156+
return &((struct lyplg_type_record *)plugins->objs[plugin_id - 1])->plugin;
157+
}
140158
} else {
141-
return (struct lyplg_type *)plugin_id;
159+
/* plugin is external, return the pointer */
160+
return (void *)plugin_id;
142161
}
143162
}
144163

164+
LIBYANG_API_DEF struct lyplg_type *
165+
lysc_get_type_plugin(uintptr_t plugin_id)
166+
{
167+
return lysc_get_plugin(plugin_id, LYPLG_TYPE, &plugins_types);
168+
}
169+
145170
/**
146171
* @brief Iterate over list of loaded plugins of the given @p type.
147172
*
@@ -200,11 +225,11 @@ lyplg_record_find(const struct ly_ctx *ctx, enum LYPLG type, const char *module,
200225
return NULL;
201226
}
202227

203-
LY_ERR
204-
_lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name, uintptr_t *out)
228+
uintptr_t
229+
lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name)
205230
{
206231
struct lyplg_type_record *record = NULL;
207-
uint32_t record_idx;
232+
uint32_t record_idx = 0;
208233

209234
if (ctx) {
210235
/* try to find context specific plugin */
@@ -217,18 +242,17 @@ _lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char
217242
}
218243

219244
if (!record) {
220-
return LY_ENOTFOUND;
245+
/* not found */
246+
return 0;
221247
}
222248

223249
if (!strncmp(record->plugin.id, "ly2 - ", 6)) {
224-
/* internal plugin, return an index */
225-
*out = record_idx;
250+
/* internal plugin, return an index with an offset of +1 in order to keep 0 as an invalid index (a NULL ptr) */
251+
return record_idx + 1;
226252
} else {
227253
/* external plugin, return the pointer */
228-
*out = (uintptr_t)&record->plugin;
254+
return (uintptr_t)&record->plugin;
229255
}
230-
231-
return LY_SUCCESS;
232256
}
233257

234258
struct lyplg_type *

src/plugins_internal.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ LY_ERR lyplg_init(ly_bool builtin_type_plugins_only, ly_bool static_plugins_only
6161
*/
6262
void lyplg_clean(void);
6363

64-
LY_ERR _lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name, uintptr_t *out);
65-
6664
/**
6765
* @brief Find a type plugin.
6866
*
@@ -72,9 +70,10 @@ LY_ERR _lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, con
7270
* @param[in] revision Revision of the module for which the plugin is implemented. NULL is not a wildcard, it matches
7371
* only the plugins with NULL revision specified.
7472
* @param[in] name Name of the type which the plugin implements.
75-
* @return Found type plugin, NULL if none found.
73+
* @return ID of the found type plugin, 0 if none found. The type plugin can be obtained
74+
* by passing the returned ID to ::lysc_get_type_plugin().
7675
*/
77-
struct lyplg_type *lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name);
76+
uintptr_t lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name);
7877

7978
/**
8079
* @brief Find an extension plugin.

src/plugins_types.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,13 @@ lyplg_type_print_val(const struct lysc_node *node, const char *canon, LY_VALUE_F
900900
struct ly_err_item *err = NULL;
901901
const char *v;
902902
ly_bool dyn;
903+
struct lyplg_type *type_plugin;
903904

904905
type = ((struct lysc_node_leaf *)node)->type;
906+
type_plugin = lysc_get_type_plugin(type->plugin);
905907

906908
/* store the value */
907-
r = lysc_get_type_plugin(type->plugin)->store(node->module->ctx, type, canon, strlen(canon), LYPLG_TYPE_STORE_ONLY, LY_VALUE_CANON,
909+
r = type_plugin->store(node->module->ctx, type, canon, strlen(canon), LYPLG_TYPE_STORE_ONLY, LY_VALUE_CANON,
908910
NULL, LYD_HINT_DATA, node, &storage, NULL, &err);
909911
if (r && (r != LY_EINCOMPLETE)) {
910912
if (err) {
@@ -915,7 +917,7 @@ lyplg_type_print_val(const struct lysc_node *node, const char *canon, LY_VALUE_F
915917
}
916918

917919
/* print it in the specific format */
918-
v = lysc_get_type_plugin(type->plugin)->print(node->module->ctx, &storage, format, prefix_data, &dyn, NULL);
920+
v = type_plugin->print(node->module->ctx, &storage, format, prefix_data, &dyn, NULL);
919921

920922
/* store it in the dictionary, storage will be freed */
921923
if (dyn) {
@@ -924,7 +926,7 @@ lyplg_type_print_val(const struct lysc_node *node, const char *canon, LY_VALUE_F
924926
lydict_dup(node->module->ctx, v, value);
925927
}
926928

927-
lysc_get_type_plugin(type->plugin)->free(node->module->ctx, &storage);
929+
type_plugin->free(node->module->ctx, &storage);
928930
return LY_SUCCESS;
929931
}
930932

@@ -1050,6 +1052,7 @@ lyplg_type_resolve_leafref(const struct lysc_type_leafref *lref, const struct ly
10501052
const char *val_str, *xp_err_msg;
10511053
uint32_t i;
10521054
int r;
1055+
struct lyplg_type *type;
10531056

10541057
LY_CHECK_ARG_RET(NULL, lref, node, value, errmsg, LY_EINVAL);
10551058

@@ -1096,6 +1099,8 @@ lyplg_type_resolve_leafref(const struct lysc_type_leafref *lref, const struct ly
10961099
goto cleanup;
10971100
}
10981101

1102+
type = lysc_get_type_plugin(lref->plugin);
1103+
10991104
/* check the result */
11001105
if (target_path) {
11011106
/* no or exact match(es) */
@@ -1110,7 +1115,7 @@ lyplg_type_resolve_leafref(const struct lysc_type_leafref *lref, const struct ly
11101115
continue;
11111116
}
11121117

1113-
if (!lysc_get_type_plugin(lref->plugin)->compare(LYD_CTX(node), &((struct lyd_node_term *)set.val.nodes[i].node)->value, value)) {
1118+
if (!type->compare(LYD_CTX(node), &((struct lyd_node_term *)set.val.nodes[i].node)->value, value)) {
11141119
break;
11151120
}
11161121
}
@@ -1135,7 +1140,7 @@ lyplg_type_resolve_leafref(const struct lysc_type_leafref *lref, const struct ly
11351140
continue;
11361141
}
11371142

1138-
if (!lysc_get_type_plugin(lref->plugin)->compare(LYD_CTX(node), &((struct lyd_node_term *)set.val.nodes[i].node)->value, value)) {
1143+
if (!type->compare(LYD_CTX(node), &((struct lyd_node_term *)set.val.nodes[i].node)->value, value)) {
11391144
rc = ly_set_add(*targets, set.val.nodes[i].node, 0, NULL);
11401145
LY_CHECK_GOTO(rc, cleanup);
11411146
}

src/plugins_types/union.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
178178
LY_VALUE_FORMAT format;
179179
void *prefix_data;
180180
uint32_t opts = 0, ti;
181+
struct lyplg_type *type_plugin;
181182

182183
*err = NULL;
183184

@@ -196,7 +197,7 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
196197

197198
assert(subvalue->value.realtype);
198199
value = lysc_get_type_plugin(subvalue->value.realtype->plugin)->print(ctx, &subvalue->value,
199-
LY_VALUE_JSON, NULL, &dynamic, &value_len);
200+
LY_VALUE_JSON, NULL, &dynamic, &value_len);
200201

201202
/* to avoid leaks, free subvalue->value, but we need the value, which may be stored there */
202203
if (!dynamic) {
@@ -225,7 +226,9 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
225226
opts |= LYPLG_TYPE_STORE_DYNAMIC;
226227
}
227228

228-
rc = lysc_get_type_plugin(type->plugin)->store(ctx, type, value, value_len, opts, format, prefix_data,
229+
type_plugin = lysc_get_type_plugin(type->plugin);
230+
231+
rc = type_plugin->store(ctx, type, value, value_len, opts, format, prefix_data,
229232
subvalue->hints, subvalue->ctx_node, &subvalue->value, unres, err);
230233
if ((rc != LY_SUCCESS) && (rc != LY_EINCOMPLETE)) {
231234
/* clear any leftover/freed garbage */
@@ -235,10 +238,10 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
235238

236239
if (validate && (rc == LY_EINCOMPLETE)) {
237240
/* we need the value validated */
238-
rc = lysc_get_type_plugin(type->plugin)->validate(ctx, type, ctx_node, tree, &subvalue->value, err);
241+
rc = type_plugin->validate(ctx, type, ctx_node, tree, &subvalue->value, err);
239242
if (rc) {
240243
/* validate failed, we need to free the stored value */
241-
lysc_get_type_plugin(type->plugin)->free(ctx, &subvalue->value);
244+
type_plugin->free(ctx, &subvalue->value);
242245
}
243246
}
244247

@@ -271,6 +274,7 @@ union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct
271274
uint32_t *prev_lo, temp_lo = 0;
272275
char *msg = NULL;
273276
int msg_len = 0;
277+
struct lyplg_type *type;
274278

275279
*err = NULL;
276280

@@ -308,9 +312,11 @@ union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct
308312
continue;
309313
}
310314

311-
msg = ly_realloc(msg, msg_len + 4 + strlen(lysc_get_type_plugin(type_u->types[u]->plugin)->id) + 2 + strlen(errs[u]->msg) + 2);
315+
type = lysc_get_type_plugin(type_u->types[u]->plugin);
316+
317+
msg = ly_realloc(msg, msg_len + 4 + strlen(type->id) + 2 + strlen(errs[u]->msg) + 2);
312318
LY_CHECK_ERR_GOTO(!msg, ret = LY_EMEM, cleanup);
313-
msg_len += sprintf(msg + msg_len, " %s: %s\n", lysc_get_type_plugin(type_u->types[u]->plugin)->id, errs[u]->msg);
319+
msg_len += sprintf(msg + msg_len, " %s: %s\n", type->id, errs[u]->msg);
314320
}
315321

316322
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "%s", msg);
@@ -445,14 +451,16 @@ lyplg_type_validate_union(const struct ly_ctx *ctx, const struct lysc_type *type
445451
struct lyd_value orig = {0};
446452
uint32_t type_idx;
447453
ly_bool validated = 0;
454+
struct lyplg_type *subvalue_type;
448455

449456
*err = NULL;
450457

451458
/* because of types that do not store their own type as realtype (leafref), we are not able to call their
452459
* validate callback (there is no way to get the type) but even if possible, the value may be invalid
453460
* for the type, so we may have to perform union value storing again from scratch, but keep a value backup */
454-
LY_CHECK_RET(lysc_get_type_plugin(subvalue->value.realtype->plugin)->duplicate(ctx, &subvalue->value, &orig));
455-
lysc_get_type_plugin(subvalue->value.realtype->plugin)->free(ctx, &subvalue->value);
461+
subvalue_type = lysc_get_type_plugin(subvalue->value.realtype->plugin);
462+
LY_CHECK_RET(subvalue_type->duplicate(ctx, &subvalue->value, &orig));
463+
subvalue_type->free(ctx, &subvalue->value);
456464

457465
if (subvalue->format == LY_VALUE_LYB) {
458466
/* use the specific type to store and validate the value */
@@ -492,7 +500,8 @@ lyplg_type_compare_union(const struct ly_ctx *ctx, const struct lyd_value *val1,
492500
if (val1->subvalue->value.realtype != val2->subvalue->value.realtype) {
493501
return LY_ENOT;
494502
}
495-
return lysc_get_type_plugin(val1->subvalue->value.realtype->plugin)->compare(ctx, &val1->subvalue->value, &val2->subvalue->value);
503+
return lysc_get_type_plugin(val1->subvalue->value.realtype->plugin)->compare(ctx,
504+
&val1->subvalue->value, &val2->subvalue->value);
496505
}
497506

498507
LIBYANG_API_DEF int
@@ -503,7 +512,8 @@ lyplg_type_sort_union(const struct ly_ctx *ctx, const struct lyd_value *val1, co
503512
struct lysc_type **types;
504513

505514
if (val1->subvalue->value.realtype == val2->subvalue->value.realtype) {
506-
return lysc_get_type_plugin(val1->subvalue->value.realtype->plugin)->sort(ctx, &val1->subvalue->value, &val2->subvalue->value);
515+
return lysc_get_type_plugin(val1->subvalue->value.realtype->plugin)->sort(ctx,
516+
&val1->subvalue->value, &val2->subvalue->value);
507517
}
508518

509519
/* compare according to the order of types */
@@ -561,8 +571,8 @@ lyb_union_print(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct
561571
LY_CHECK_RET((r != LY_SUCCESS) && (r != LY_EINCOMPLETE), NULL);
562572

563573
/* Print subvalue in LYB format. */
564-
pval = (void *)lysc_get_type_plugin(subvalue->value.realtype->plugin)->print(NULL, &subvalue->value, LY_VALUE_LYB, prefix_data, &dynamic,
565-
&pval_len);
574+
pval = (void *)lysc_get_type_plugin(subvalue->value.realtype->plugin)->print(NULL, &subvalue->value, LY_VALUE_LYB,
575+
prefix_data, &dynamic, &pval_len);
566576
LY_CHECK_RET(!pval, NULL);
567577

568578
/* Create LYB data. */
@@ -590,6 +600,7 @@ lyplg_type_print_union(const struct ly_ctx *ctx, const struct lyd_value *value,
590600
struct lyd_value_union *subvalue = value->subvalue;
591601
struct lysc_type_union *type_u = (struct lysc_type_union *)value->realtype;
592602
size_t lyb_data_len = 0;
603+
struct lyplg_type *type;
593604

594605
if ((format == LY_VALUE_LYB) && (subvalue->format == LY_VALUE_LYB)) {
595606
/* The return value is already ready. */
@@ -609,7 +620,8 @@ lyplg_type_print_union(const struct ly_ctx *ctx, const struct lyd_value *value,
609620
}
610621

611622
assert(format != LY_VALUE_LYB);
612-
ret = (void *)lysc_get_type_plugin(subvalue->value.realtype->plugin)->print(ctx, &subvalue->value, format, prefix_data, dynamic, value_len);
623+
type = lysc_get_type_plugin(subvalue->value.realtype->plugin);
624+
ret = (void *)type->print(ctx, &subvalue->value, format, prefix_data, dynamic, value_len);
613625
if (!value->_canonical && (format == LY_VALUE_CANON)) {
614626
/* the canonical value is supposed to be stored now */
615627
lydict_insert(ctx, subvalue->value._canonical, 0, (const char **)&value->_canonical);

src/printer_lyb.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,16 @@ lyb_print_term_value(struct lyd_node_term *term, struct ly_out *out, struct lyly
678678
size_t value_len = 0;
679679
int32_t lyb_data_len;
680680
lyplg_type_print_clb print;
681+
struct lyplg_type *type;
681682

682-
assert(term->value.realtype && lysc_get_type_plugin(term->value.realtype->plugin)->print && term->schema);
683+
assert(term->value.realtype && (type = lysc_get_type_plugin(term->value.realtype->plugin)) &&
684+
type->print && term->schema);
683685

684686
/* Get length of LYB data to print. */
685-
lyb_data_len = lysc_get_type_plugin(term->value.realtype->plugin)->lyb_data_len;
687+
lyb_data_len = type->lyb_data_len;
686688

687689
/* Get value and also print its length only if size is not fixed. */
688-
print = lysc_get_type_plugin(term->value.realtype->plugin)->print;
690+
print = type->print;
689691
if (lyb_data_len < 0) {
690692
/* Variable-length data. */
691693

src/printer_xml.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ xml_print_meta(struct xmlpr_ctx *pctx, const struct lyd_node *node)
217217
ly_set_add(&ns_list, NULL, 0, NULL);
218218

219219
/* print the value */
220-
value = lysc_get_type_plugin(meta->value.realtype->plugin)->print(LYD_CTX(node), &meta->value, LY_VALUE_XML, &ns_list, &dynamic, NULL);
220+
value = lysc_get_type_plugin(meta->value.realtype->plugin)->print(LYD_CTX(node),
221+
&meta->value, LY_VALUE_XML, &ns_list, &dynamic, NULL);
221222

222223
/* print namespaces connected with the value's prefixes */
223224
for (i = 1; i < ns_list.count; ++i) {
@@ -339,8 +340,8 @@ xml_print_term(struct xmlpr_ctx *pctx, const struct lyd_node_term *node)
339340
}
340341

341342
/* print the value */
342-
value = lysc_get_type_plugin(((struct lysc_node_leaf *)node->schema)->type->plugin)->print(LYD_CTX(node), &node->value, LY_VALUE_XML,
343-
&ns_list, &dynamic, NULL);
343+
value = lysc_get_type_plugin(((struct lysc_node_leaf *)node->schema)->type->plugin)->print(LYD_CTX(node),
344+
&node->value, LY_VALUE_XML, &ns_list, &dynamic, NULL);
344345
LY_CHECK_ERR_GOTO(!value, rc = LY_EINVAL, cleanup);
345346

346347
/* print node opening */

src/schema_compile.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,11 @@ lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc
896896
struct lyd_value storage = {0};
897897
struct ly_err_item *err = NULL;
898898
LY_VALUE_FORMAT format;
899+
struct lyplg_type *type_plugin;
899900

900901
options = (ctx->ctx->opts & LY_CTX_REF_IMPLEMENTED) ? LYPLG_TYPE_STORE_IMPLEMENT : 0;
901-
rc = lysc_get_type_plugin(type->plugin)->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_VALUE_SCHEMA, (void *)dflt_pmod,
902+
type_plugin = lysc_get_type_plugin(type->plugin);
903+
rc = type_plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_VALUE_SCHEMA, (void *)dflt_pmod,
902904
LYD_HINT_SCHEMA, node, &storage, unres, &err);
903905
if (rc == LY_ERECOMPILE) {
904906
/* fine, but we need to recompile */
@@ -925,7 +927,7 @@ lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc
925927
}
926928

927929
cleanup:
928-
lysc_get_type_plugin(type->plugin)->free(ctx->ctx, &storage);
930+
type_plugin->free(ctx->ctx, &storage);
929931
return rc;
930932
}
931933

0 commit comments

Comments
 (0)