Skip to content

Commit

Permalink
pycodegen: idfields can never be None
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Dec 25, 2024
1 parent 9fd211e commit 7b1021f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion schema_salad/cpp_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
# g++ versions older than version 10 may need "--std=c++2a" instead of "--std=c++20"
"""

import re
import os
import re
from typing import IO, Any, Optional, Union, cast

from . import _logger
Expand Down
24 changes: 16 additions & 8 deletions schema_salad/metaschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ class RecordField(Documented):
A field of a record.
"""

name: str
class_uri = "https://w3id.org/cwl/salad#RecordField"

def __init__(
Expand All @@ -1181,7 +1182,7 @@ def __init__(
else:
self.loadingOptions = LoadingOptions()
self.doc = doc
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.type_ = type_

def __eq__(self, other: Any) -> bool:
Expand Down Expand Up @@ -1636,6 +1637,7 @@ class EnumSchema(Saveable):
"""

name: str
class_uri = "https://w3id.org/cwl/salad#EnumSchema"

def __init__(
Expand All @@ -1654,7 +1656,7 @@ def __init__(
self.loadingOptions = loadingOptions
else:
self.loadingOptions = LoadingOptions()
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.symbols = symbols
self.type_ = type_

Expand Down Expand Up @@ -3479,6 +3481,7 @@ class SaladRecordField(RecordField):
A field of a record.
"""

name: str
class_uri = "https://w3id.org/cwl/salad#SaladRecordField"

def __init__(
Expand All @@ -3500,7 +3503,7 @@ def __init__(
else:
self.loadingOptions = LoadingOptions()
self.doc = doc
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.type_ = type_
self.jsonldPredicate = jsonldPredicate
self.default = default
Expand Down Expand Up @@ -3862,6 +3865,7 @@ def save(


class SaladRecordSchema(NamedType, RecordSchema, SchemaDefinedType):
name: str
class_uri = "https://w3id.org/cwl/salad#SaladRecordSchema"

def __init__(
Expand Down Expand Up @@ -3890,7 +3894,7 @@ def __init__(
self.loadingOptions = loadingOptions
else:
self.loadingOptions = LoadingOptions()
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.inVocab = inVocab
self.fields = fields
self.type_ = type_
Expand Down Expand Up @@ -4725,6 +4729,7 @@ class SaladEnumSchema(NamedType, EnumSchema, SchemaDefinedType):
"""

name: str
class_uri = "https://w3id.org/cwl/salad#SaladEnumSchema"

def __init__(
Expand All @@ -4751,7 +4756,7 @@ def __init__(
self.loadingOptions = loadingOptions
else:
self.loadingOptions = LoadingOptions()
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.inVocab = inVocab
self.symbols = symbols
self.type_ = type_
Expand Down Expand Up @@ -5468,6 +5473,7 @@ class SaladMapSchema(NamedType, MapSchema, SchemaDefinedType):
"""

name: str
class_uri = "https://w3id.org/cwl/salad#SaladMapSchema"

def __init__(
Expand All @@ -5493,7 +5499,7 @@ def __init__(
self.loadingOptions = loadingOptions
else:
self.loadingOptions = LoadingOptions()
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.inVocab = inVocab
self.type_ = type_
self.values = values
Expand Down Expand Up @@ -6155,6 +6161,7 @@ class SaladUnionSchema(NamedType, UnionSchema, DocType):
"""

name: str
class_uri = "https://w3id.org/cwl/salad#SaladUnionSchema"

def __init__(
Expand All @@ -6179,7 +6186,7 @@ def __init__(
self.loadingOptions = loadingOptions
else:
self.loadingOptions = LoadingOptions()
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.inVocab = inVocab
self.names = names
self.type_ = type_
Expand Down Expand Up @@ -6783,6 +6790,7 @@ class Documentation(NamedType, DocType):
"""

name: str
class_uri = "https://w3id.org/cwl/salad#Documentation"

def __init__(
Expand All @@ -6805,7 +6813,7 @@ def __init__(
self.loadingOptions = loadingOptions
else:
self.loadingOptions = LoadingOptions()
self.name = name
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
self.inVocab = inVocab
self.doc = doc
self.docParent = docParent
Expand Down
8 changes: 8 additions & 0 deletions schema_salad/python_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ def begin_class(
self.out.write(" pass\n\n\n")
return

idfield_safe_name = self.safe_name(idfield) if idfield != "" else None
if idfield_safe_name is not None:
self.out.write(f" {idfield_safe_name}: str\n")
self.out.write(f' class_uri = "{class_uri}"\n\n')

required_field_names = [f for f in field_names if f not in optional_fields]
Expand Down Expand Up @@ -203,6 +206,11 @@ def begin_class(
""".format(
classname
)
elif name == idfield_safe_name:
field_inits += """ self.{0} = {0} if {0} is not None else "_:" + str(_uuid__.uuid4())
""".format(
self.safe_name(name)
)
else:
field_inits += """ self.{0} = {0}
""".format(
Expand Down

0 comments on commit 7b1021f

Please sign in to comment.