Storing possibly-arrays as arrays

Valentin V. Bartenev vbart at nginx.com
Sat Dec 18 20:10:30 UTC 2021


On Saturday, 18 December 2021 22:55:22 MSK Alejandro Colomar (man-pages) wrote:
> Hi Valentin,
> 
> While implementing the "index" option, I found this code, which didn't 
> convince me:
> 
> [
>      shr_is_array = (nxt_conf_type(acf->share) == NXT_CONF_ARRAY);
>      conf->nshares = shr_is_array ? 
> nxt_conf_array_elements_count(acf->share)
>                                   : 1;
>      conf->shares = nxt_mp_zget(mp, sizeof(nxt_http_static_share_t)
>                                     * conf->nshares);
>      if (nxt_slow_path(conf->shares == NULL)) {
>          return NXT_ERROR;
>      }
> 
>      if (shr_is_array) {
>          for (i = 0; i < conf->nshares; i++) {
>              cv = nxt_conf_get_array_element(acf->share, i);
>              nxt_conf_get_string(cv, &str);
> 
>              var = nxt_var_compile(&str, mp, 1);
>              if (nxt_slow_path(var == NULL)) {
>                  return NXT_ERROR;
>              }
> 
>              conf->shares[i].var = var;
>              conf->shares[i].is_const = nxt_var_is_const(var);
>          }
> 
>      } else {
>          nxt_conf_get_string(acf->share, &str);
> 
>          var = nxt_var_compile(&str, mp, 1);
>          if (nxt_slow_path(var == NULL)) {
>              return NXT_ERROR;
>          }
> 
>          conf->shares[0].var = var;
>          conf->shares[0].is_const = nxt_var_is_const(var);
>      }
> ]
> 
> Most of the code is duplicated for the cases that 'share is not an 
> array' and 'share is an array', changing just a single line.  How about 
> reorganizing the data structures so that when both a string/object and 
> an array are a valid type
> 	.type = NXT_CONF_VLDT_* | NXT_CONF_VLDT_ARRAY,
> the data is _always_ stored as an array (of size 1 if it was not an 
> array originally)?
[..]

The code you've just mentioned above is the first place where the data
from a user's config is actually processed.  It's not stored anywhere
when NXT_CONF_VLDT_* rules validate JSON scheme.


[..]
> Another alternative, but maybe not so good since it kind of defeats the 
> difference between (nxt) arrays and non-arrays, would be to modify 
> nxt_conf_array_elements_count() so that
>      if (value->type != NXT_CONF_VALUE_ARRAY) {
>          return (index == 0) ? value : NULL;
>      }
> This would be similar to C pointers to variables, which can always be 
> considered as pointers to the first element of an array of size 1.
> 

That makes sense.

--
Valentin





More information about the unit mailing list