brettviren / moo

ruminants on module oriented programming
GNU General Public License v3.0
4 stars 3 forks source link

hyphen in string #37

Closed plasorak closed 1 year ago

plasorak commented 1 year ago

The following code in the configuration generator for DUNE-DAQ:

 /nfs/home/plasorak/dunedaq-v3.1.0/swdir/install/appfwk/lib64/python/appfwk/utils.py:31 in mspec  │
│                                                                                                  │
│    28 │   :returns:   A constructed ModSpec object                                               │
│    29 │   :rtype:     dunedaq.appfwk.app.ModSpec                                                 │
│    30 │   """                                                                                    │
│ ❱  31 │   return app.ModSpec(inst=inst, plugin=plugin,                                           │
│    32 │   │   │   data=app.ModInit(                                                              │
│    33 │   │   │   │   conn_refs=conn.ConnectionRefs_t(conn_refs)                                 │
│    34 │   │   │   │   )

Leads to the following when the name instance name is flx-card-localhost-0:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│                                                                                                  │
│ /nfs/home/plasorak/dunedaq-v3.1.0/swdir/dbt-pyvenv/lib/python3.8/site-packages/moo/otypes.py:375 │
│ in update                                                                                        │
│                                                                                                  │
│   372 │   │   from jsonschema import draft7_format_checker                                       │
│   373 │   │   from jsonschema.exceptions import ValidationError                                  │
│   374 │   │   try:                                                                               │
│ ❱ 375 │   │   │   js_validate(instance=val, schema=schema,                                       │
│   376 │   │   │   │   │   │   format_checker=draft7_format_checker)                              │
│   377 │   │   except ValidationError as verr:                                                    │
│   378 │   │   │   raise ValueError(f'format mismatch for string {cname}') from verr              │
│ /nfs/home/plasorak/dunedaq-v3.1.0/swdir/dbt-pyvenv/lib/python3.8/site-packages/jsonschema/valida │
│ tors.py:934 in validate                                                                          │
│                                                                                                  │
│   931 │   validator = cls(schema, *args, **kwargs)                                               │
│   932 │   error = exceptions.best_match(validator.iter_errors(instance))                         │
│   933 │   if error is not None:                                                                  │
│ ❱ 934 │   │   raise error                                                                        │
│   935                                                                                            │
│   936                                                                                            │
│   937 def validator_for(schema, default=_LATEST_VERSION):                                        │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ValidationError: 'flx-card-localhost-0' does not match '^[a-zA-Z][a-zA-Z0-9_]*$'

Failed validating 'pattern' in schema:
    {'pattern': '^[a-zA-Z][a-zA-Z0-9_]*$', 'type': 'string'}

On instance:
    'flx-card-localhost-0'

Why can't a string have hyphens? Also the _ shouldn't be in the regex pattern, because these apps name can be domain name with K8s.

Is there a way to change the regex that it's trying to match?

brettviren commented 1 year ago

Hi @plasorak

Sorry, I just noticed this issue! Probably you found a fix already but I'll try to answer anyways.

I think this is a case of schema actually doing its job but perhaps not doing the job that you actually want. The original developer of the dunedaq.appfwk.app.ModSpec chose this string to be governed by this regex and so governed it is. The question then goes to that schema: should - be allowed and should _ be excluded.

I note that this regex is the same defined by moo.re.ident in moo's Jsonnet. An ident is intended to be a limited string schema which in particular matches legal Python/C++ variable names. So, it must exclude - and include _.

If the particular string must match a different pattern then a different regex should be used in the string schema used in the larger ModSpec.

To help that, moo provides other predefined regex. For example there is moo.re.dnshost regex which can be used to define a string schema which is intended to match only legal DNS host names. That includes - and excludes _.

You can browse the built-in regex's here:

https://github.com/brettviren/moo/blob/master/moo/jsonnet-code/schema/re.jsonnet

If any are close but not perfect it would be reasonable to copy-paste them into some main DAQ schema file and edit as desired.

plasorak commented 1 year ago

Oh, I see, so this is probably an appfwk problem, rather than moo, we just need to specify the correct string schema in ModSpec. Thanks