File: dlang_codegen.py

package info (click to toggle)
python-schema-salad 8.9.20251102115403-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,060 kB
  • sloc: python: 19,247; cpp: 2,631; cs: 1,869; java: 1,341; makefile: 187; xml: 184; sh: 103; javascript: 46
file content (446 lines) | stat: -rw-r--r-- 14,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""D code generator for a given schema salad definition."""

import datetime
import functools
import json
import textwrap
from typing import IO, Any, Optional, Union, cast

from . import _logger, schema
from .codegen_base import CodeGenBase, TypeDef
from .cpp_codegen import (
    isArray,
    isEnumSchema,
    isMapSchema,
    isRecordSchema,
    isUnionSchema,
    pred,
)
from .exceptions import SchemaException
from .schema import shortname


class DlangCodeGen(CodeGenBase):
    """Generation of D code for a given Schema Salad definition."""

    def __init__(
        self,
        base: str,
        target: IO[str],
        examples: Optional[str],
        package: str,
        copyright_: Optional[str],
        parser_info: Optional[str],
        salad_version: str,
    ) -> None:
        """Initialize the D codegen."""
        super().__init__()
        self.base_uri = base
        self.examples = examples
        self.target = target
        self.package = package
        self.copyright = copyright_
        self.parser_info = parser_info
        self.salad_version = salad_version
        self.doc_root_types: list[str] = []

    def prologue(self) -> None:
        """Trigger to generate the prolouge code."""
        module_comment = []

        if self.parser_info:
            module_comment.append(self.parser_info + "\n")

        module_comment += [
            "This module was generated using schema-salad code generator.",
            "",
            "The embedded document is subject to the license of the original schema.",
        ]

        if self.copyright:
            module_comment.append(f"The original schema is {self.copyright}.")

        module_comment += [
            "",
            "License: Apache-2.0",
            f"Date: {datetime.date.today().isoformat()}",
        ]

        self.target.write(self.to_doc_comment(module_comment))
        self.target.write(
            f"""module {self.package};

import salad.meta.impl : genBody_;
import salad.meta.parser : import_ = importFromURI;
import salad.meta.uda : defaultValue, documentRoot, id, idMap, link, LinkResolver, secondaryFilesDSL, typeDSL;
import salad.primitives : EnumSchemaBase, MapSchemaBase, RecordSchemaBase, UnionSchemaBase;
import salad.type : None, Union;

"""
        )
        if self.parser_info:
            self.target.write(
                f"""/// parser information
enum parserInfo = "{self.parser_info}";
"""  # noqa: B907
            )

        self.target.write(
            f"""
enum saladVersion = "{self.salad_version}";

mixin template genBody()
{{
    mixin genBody_!saladVersion;
}}
"""  # noqa: B907
        )

    def epilogue(self, root_loader: TypeDef) -> None:
        """Trigger to generate the epilouge code."""
        doc_root_type_str = ", ".join(self.doc_root_types)
        doc_root_type = f"Union!({doc_root_type_str})"
        self.target.write(
            f"""
///
alias DocumentRootType = {doc_root_type};

///
alias importFromURI = import_!DocumentRootType;
"""
        )
        if self.examples:
            self.target.write(
                f"""
@("Test for generated parser")
unittest
{{
    import std : dirEntries, SpanMode, stdThreadLocalLog, NullLogger;

    auto currentLogger = stdThreadLocalLog;
    stdThreadLocalLog = new NullLogger;
    scope(exit) stdThreadLocalLog = currentLogger;

    auto resourceDir = "{self.examples}";
    foreach (file; dirEntries(resourceDir, SpanMode.depth))
    {{
        import std : assertNotThrown, baseName, format, startsWith;
        import salad.resolver : absoluteURI;

        if (!file.baseName.startsWith("valid"))
        {{
            continue;
        }}
        importFromURI(file.absoluteURI).assertNotThrown(format!"Failed to load %s"(file));
    }}
}}
"""  # noqa: B907
            )

    @staticmethod
    def safe_name(name: str) -> str:
        """Generate a safe version of the given name."""
        avn = schema.avro_field_name(name)
        if avn in ("class", "abstract", "default", "package"):
            # reserved words
            avn = avn + "_"
        if avn and avn.startswith("anon."):
            avn = avn[5:]
        return avn

    def to_doc_comment(self, doc: Union[None, str, list[str]]) -> str:
        """Return an embedded documentation comments for a given string."""
        if doc is None:
            return "///\n"
        if isinstance(doc, str):
            lines = doc.split("\n")
        else:
            lines = sum((d.split("\n") for d in doc), [])

        if not lines[-1]:
            lines = lines[0:-1]

        lines = [line.replace("`(`", "`$(LPAREN)`").replace("`)`", "`$(RPAREN)`") for line in lines]

        doc_lines = "\n".join(f" * {line}" for line in lines)

        return f"""/**
{doc_lines}
 */
"""

    def parse_record_field_type(
        self,
        type_: Any,
        jsonld_pred: Union[None, str, dict[str, Any]],
        parent_has_idmap: bool = False,
        has_default: bool = False,
    ) -> tuple[str, str]:
        """Return an annotation string and a type string."""
        annotations: list[str] = []
        has_idmap = False or parent_has_idmap
        if isinstance(jsonld_pred, str):
            if jsonld_pred == "@id":
                annotations.append("@id")
        elif isinstance(jsonld_pred, dict):
            if jsonld_pred.get("typeDSL", False):
                annotations.append("@typeDSL")
            if jsonld_pred.get("secondaryFilesDSL", False):
                annotations.append("@secondaryFilesDSL")
            if "mapSubject" in jsonld_pred:
                subject = jsonld_pred["mapSubject"]
                has_idmap = True
                if "mapPredicate" in jsonld_pred:
                    predicate = jsonld_pred["mapPredicate"]
                    annotations.append(f'@idMap("{subject}", "{predicate}")')  # noqa: B907
                else:
                    annotations.append(f'@idMap("{subject}")')  # noqa: B907
            if jsonld_pred.get("_type", "") == "@id":
                if jsonld_pred.get("identity", False):
                    annotations.append("@link(LinkResolver.id)")
                else:
                    annotations.append("@link()")
        if annotations:
            annotate_str = " ".join(annotations) + " "
        else:
            annotate_str = ""

        if isinstance(type_, str):
            stype = shortname(type_)
            if stype == "boolean":
                type_str = "bool"
            elif stype == "null":
                type_str = "None"
            else:
                type_str = stype
        elif isinstance(type_, list):
            t_str = [
                self.parse_record_field_type(t, None, parent_has_idmap=has_idmap)[1] for t in type_
            ]
            if has_default:
                t_str = [t for t in t_str if t != "None"]
            if len(t_str) == 1:
                type_str = t_str[0]
            else:
                if are_dispatchable(type_, has_idmap):
                    t_str += ["Any"]
                union_types = ", ".join(t_str)
                type_str = f"Union!({union_types})"
        elif shortname(type_["type"]) == "array":
            item_type = self.parse_record_field_type(
                type_["items"], None, parent_has_idmap=has_idmap
            )[1]
            type_str = f"{item_type}[]"
        elif shortname(type_["type"]) == "record":
            return annotate_str, shortname(type_.get("name", "record"))
        elif shortname(type_["type"]) == "enum":
            return annotate_str, "'not yet implemented'"
        elif shortname(type_["type"]) == "map":
            value_type = self.parse_record_field_type(
                type_["values"], None, parent_has_idmap=has_idmap, has_default=True
            )[1]
            type_str = f"{value_type}[string]"
        return annotate_str, type_str

    def parse_record_field(self, field: dict[str, Any], parent_name: Optional[str] = None) -> str:
        """Return a declaration string for a given record field."""
        fname = shortname(field["name"]) + "_"
        jsonld_pred = field.get("jsonldPredicate", None)
        doc_comment = self.to_doc_comment(field.get("doc", None))
        type_ = field["type"]
        if is_constant_field(field):
            # special case
            if isinstance(type_, dict):
                # assert len(type["symbols"]) == 1
                value = shortname(type_["symbols"][0])
            else:
                value = cast(str, parent_name)
            return f'{doc_comment}static immutable {fname} = "{value}";'  # noqa: B907

        if field.get("default", None) is not None:
            default_value = json.dumps(field["default"])
            default_str = f'@defaultValue(q"<{default_value}>") '
        else:
            default_str = ""

        annotate_str, type_str = self.parse_record_field_type(
            type_, jsonld_pred, has_default="default" in field
        )
        return f"{doc_comment}{default_str}{annotate_str}{type_str} {fname};"

    def parse_record_schema(self, stype: dict[str, Any]) -> str:
        """Return a declaration string for a given record schema."""
        name = cast(str, stype["name"])
        classname = self.safe_name(name)

        field_decls = []
        if "fields" in stype:
            for field in stype["fields"]:
                field_decls.append(self.parse_record_field(field, classname))
        decl_str = "\n".join(textwrap.indent(f"{d}", " " * 4) for d in field_decls)

        if stype.get("documentRoot", False):
            doc_root_annotation = "@documentRoot "
            self.doc_root_types.append(classname)
        else:
            doc_root_annotation = ""

        doc_comment = self.to_doc_comment(stype.get("doc", None))

        return f"""
{doc_comment}{doc_root_annotation}class {classname} : RecordSchemaBase
{{
{decl_str}

    mixin genBody;
}}"""

    def parse_enum(self, stype: dict[str, Any]) -> str:
        """Return a declaration string for a given enum schema."""
        name = cast(str, stype["name"])
        if shortname(name) == "Any":
            return "\n///\npublic import salad.primitives : Any;"
        if shortname(name) == "Expression":
            return "\n///\npublic import salad.primitives : Expression;"

        classname = self.safe_name(name)
        syms = "\n".join(
            (
                f'        s{i} = "{shortname(sym)}", ///'  # noqa: B907
                for i, sym in enumerate(stype["symbols"])
            )
        )

        if stype.get("documentRoot", False):
            doc_root_annotation = "@documentRoot "
            self.doc_root_types.append(classname)
        else:
            doc_root_annotation = ""

        if "doc" in stype:
            doc_comment = self.to_doc_comment(stype["doc"])
        else:
            doc_comment = ""

        return f"""
{doc_comment}{doc_root_annotation}class {classname} : EnumSchemaBase
{{
    ///
    enum Symbol
    {{
{syms}
    }}

    Symbol value;

    mixin genBody;
}}"""

    def parse_union(self, stype: dict[str, Any]) -> str:
        """Return a declaration string for a given union schema."""
        name = cast(str, stype["name"])
        classname = self.safe_name(name)

        types = self.parse_record_field_type(stype["names"], None)[1]

        if "doc" in stype:
            doc_comment = self.to_doc_comment(stype["doc"])
        else:
            doc_comment = ""

        return f"""
{doc_comment}class {classname} : UnionSchemaBase
{{
    {types} payload;

    mixin genBody;
}}"""

    def parse_map(self, stype: dict[str, Any]) -> str:
        """Return a declaration string for a given map schema."""
        name = cast(str, stype["name"])
        classname = self.safe_name(name)

        values = self.parse_record_field_type(stype["values"], None, has_default=True)[1]

        if "doc" in stype:
            doc_comment = self.to_doc_comment(stype["doc"])
        else:
            doc_comment = ""

        return f"""
{doc_comment}class {classname} : MapSchemaBase
{{
    {values}[string] payload;

    mixin genBody;
}}"""

    def parse(self, items: list[dict[str, Any]]) -> None:
        """Generate D code from items and write it to target."""
        dlang_defs = []

        self.prologue()

        for stype in items:
            if "type" in stype and stype["type"] == "documentation":
                continue

            if not (pred(stype) or isArray(stype)):
                raise SchemaException("not a valid SaladRecordField")

            # parsing a record
            if isRecordSchema(stype):
                if stype.get("abstract", False):
                    continue
                dlang_defs.append(self.parse_record_schema(stype))
            elif isEnumSchema(stype):
                dlang_defs.append(self.parse_enum(stype))
            elif isUnionSchema(stype):
                dlang_defs.append(self.parse_union(stype))
            elif isMapSchema(stype):
                dlang_defs.append(self.parse_map(stype))
            else:
                _logger.error("not parsed %s", json.dumps(stype))

        self.target.write("\n".join(dlang_defs))
        self.target.write("\n")

        self.epilogue(TypeDef("dummy", "data"))

        self.target.close()


def is_constant_field(field: dict[str, Any]) -> bool:
    """Return True if a given field only takes the specified string."""
    jsonld_pred = field.get("jsonldPredicate", None)
    type_ = field["type"]
    if (
        (
            (isinstance(type_, dict) and shortname(type_.get("type", "")) == "enum")
            or (isinstance(type_, str) and shortname(type_) == "string")
        )
        and isinstance(jsonld_pred, dict)
        and (
            shortname(jsonld_pred.get("_id", "")) == "type"
            or shortname(jsonld_pred.get("_id", "")) == "@type"
        )
        and jsonld_pred.get("_type", "") == "@vocab"
    ):
        return True
    return False


def constant_fields_of(type_: Any) -> set[str]:
    """Return a list of constant fields name from a given record schema."""
    if isinstance(type_, dict):
        return {shortname(f["name"]) for f in type_.get("fields", []) if is_constant_field(f)}
    return set()


def are_dispatchable(types: list[Any], parent_has_idmap: bool) -> bool:
    """Return True if a given list of types are dispatchable."""
    if any(t for t in types if not isinstance(t, dict)):
        return False
    constants = (constant_fields_of(t) for t in types)
    return len(functools.reduce(lambda lhs, rhs: lhs & rhs, constants)) > 0 and parent_has_idmap