Size: 15098 bytes.


  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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env python3
# cs/net/proto/codegen/writers.py
import os
from os import makedirs
from os.path import dirname, basename, join
from typing import List, Dict
from pathlib import Path

from cs.net.proto.codegen.codegen_types import Proto, ValidationAttr
from cs.net.proto.codegen.helpers import cc_namespace, strip_bazel_out_paths
from cs.net.proto.codegen.constants import NEWLINE, BACKSLASH
from cs.net.proto.codegen.generators import (
    GenerateBuilderDeclaration,
    GenerateProtoImplClassDeclaration,
    GeneratedDeclarations,
    GenerateProtoImplClassDefinition,
    GenerateBuilderDefinition,
    GenerateMetaImplementation,
    GeneratedDefinitions,
    GenerateMatchersAndProtoTests,
    GenerateGetFieldPathExplicitInstantiations,
    GenerateGetFieldPathSpecialization,
    GenerateFieldPathBuilderSupport,
)


def header_define(filename: str) -> str:
    """Return a sanitized header guard name for a given proto.hh file."""
    guard_path = join(dirname(filename), "gencode", basename(filename))
    return guard_path.replace(".", "_").replace("/", "_").upper()


def _output_include_for_input(
    input_filename: str, gen_dir: str, out_root_rel: str | None
) -> str:
    if out_root_rel:
        base = Path(out_root_rel)
        rel = Path(input_filename)
        return (base / rel.parent / rel.name).as_posix()
    return strip_bazel_out_paths(join(dirname(gen_dir), basename(input_filename)))


def _output_include_for_generated(
    input_filename: str, gen_dir: str, out_root_rel: str | None
) -> str:
    if out_root_rel:
        base = Path(out_root_rel)
        rel = Path(input_filename)
        return (base / rel.parent / "gencode" / rel.name).as_posix()
    return strip_bazel_out_paths(join(gen_dir, basename(input_filename)))


def WriteGeneratedHhFile(
    PROTOS: Dict[str, Proto],
    gen_dir: str,
    input_filename: str,
    out_root_rel: str | None = None,
) -> None:
    """Generate the .hh header file for C++ protos."""
    previous_declarations: set[str] = set()
    proto_sample = next(iter(PROTOS.values()))
    ns = f"{proto_sample.namespace}::{cc_namespace(proto_sample.filename, gen=True)}"
    decl_include = _output_include_for_input(input_filename, gen_dir, out_root_rel)
    gen_header_include = _output_include_for_generated(
        input_filename, gen_dir, out_root_rel
    )
    header_guard = header_define(input_filename)
    output_path = join(gen_dir, basename(input_filename))

    with open(output_path, "w+", encoding="utf-8") as f:
        f.write(
            f"""#ifndef {header_guard}
#define {header_guard}

#include "cs/net/json/object.hh"
#include "cs/result.hh"

#include "{decl_include}"
#include "{gen_header_include}"

namespace {ns} {{

{NEWLINE.join([
    GeneratedDeclarations(struct, PROTOS, previous_declarations)
    for _, struct in PROTOS.items()
])}

{NEWLINE.join([
    GenerateProtoImplClassDeclaration(struct)
    for _, struct in PROTOS.items()
])}

{NEWLINE.join([
    GenerateBuilderDeclaration(struct, PROTOS)
    for _, struct in PROTOS.items()
])}

}} // namespace {ns}

{NEWLINE.join([
    GenerateGetFieldPathSpecialization(struct, PROTOS)
    for _, struct in PROTOS.items()
    if GenerateGetFieldPathSpecialization(struct, PROTOS)
])}

{NEWLINE.join([
    GenerateFieldPathBuilderSupport(struct, PROTOS)
    for _, struct in PROTOS.items()
    if GenerateFieldPathBuilderSupport(struct, PROTOS)
])}

#endif  // {header_guard}
"""
        )


def WriteGeneratedCcFile(
    PROTOS: Dict[str, Proto],
    gen_dir: str,
    input_filename: str,
    out_root_rel: str | None = None,
) -> None:
    """Generate the .cc implementation file for C++ protos."""
    makedirs(gen_dir, exist_ok=True)
    proto_sample = next(iter(PROTOS.values()))
    ns = f"{proto_sample.namespace}::{cc_namespace(proto_sample.filename, gen=True)}"
    path = join(gen_dir, basename(input_filename.replace(".proto.hh", ".proto.cc")))
    decl_include = _output_include_for_input(input_filename, gen_dir, out_root_rel)
    impl_decl_include = _output_include_for_generated(
        input_filename, gen_dir, out_root_rel
    )
    previous_definitions: set[str] = set()

    # Only include meta.proto.hh if this is not meta.proto itself
    is_meta_proto = basename(input_filename) == "meta.proto.hh"
    meta_include = (
        ""
        if is_meta_proto
        else '#include "cs/net/proto/protos/gencode/meta.proto.hh"\n'
    )

    # Skip explicit instantiations for meta.proto (internal, complex types)
    explicit_inst = ""
    if not is_meta_proto:
        explicit_inst = NEWLINE.join(
            [
                GenerateGetFieldPathExplicitInstantiations(struct, PROTOS)
                for _, struct in PROTOS.items()
                if GenerateGetFieldPathExplicitInstantiations(struct, PROTOS)
            ]
        )
    explicit_inst_block = ""
    if explicit_inst:
        explicit_inst_block = f"""
#include "cs/net/proto/db/field_path_builder.gpt.hh"

{explicit_inst}
"""

    with open(path, "w+", encoding="utf-8") as f:
        f.write(
            f"""#include "{decl_include}"
#include "{impl_decl_include}"
#include "cs/net/json/object.hh"
#include "cs/net/json/parsers.hh"
#include "cs/net/json/serialize.hh"
#include "cs/result.hh"
{meta_include}#include <string>

namespace {ns} {{

namespace {{
using ::cs::net::json::Object;
using ::cs::net::json::SerializeObject;
using ::cs::InvalidArgument;
using ::cs::net::json::parsers::ParseNumber;
using ::cs::net::json::parsers::ParseObject;
using ::cs::net::json::parsers::ParseString;
using ::cs::net::json::parsers::ParseArray;
using ::cs::ResultOr;
}}

{NEWLINE.join([
    GeneratedDefinitions(proto, PROTOS, previous_definitions)
    for _, proto in PROTOS.items()
])}

}} // namespace {ns}

{NEWLINE.join([
    GenerateProtoImplClassDefinition(struct)
    for _, struct in PROTOS.items()
])}

{NEWLINE.join([
    GenerateBuilderDefinition(struct, PROTOS)
    for _, struct in PROTOS.items()
])}

{NEWLINE.join([
    GenerateMetaImplementation(struct, PROTOS)
    for _, struct in PROTOS.items()
])}
{explicit_inst_block}
"""
        )


def WriteGeneratedTestFile(
    PROTOS: Dict[str, Proto],
    gen_dir: str,
    input_filename: str,
    out_root_rel: str | None = None,
) -> None:
    """Generate a gtest/gmock test file for the given proto definitions."""
    test_abs_path = join(
        gen_dir, basename(input_filename.replace(".proto.hh", ".proto_test.cc"))
    )
    decl_include = strip_bazel_out_paths(input_filename)
    generated_header = _output_include_for_generated(
        input_filename, gen_dir, out_root_rel
    )
    previous_definitions: set[str] = set()

    def GenerateUsings(proto: Proto) -> str:
        fqn_proto = f"::{proto.namespace}::{cc_namespace(proto.filename, gen=False)}"
        if (
            fqn_proto == "::::"
            or not proto.namespace
            or not cc_namespace(proto.filename, gen=False)
        ):
            return ""
        return f"using {fqn_proto};\nusing {fqn_proto}FromString;"

    with open(test_abs_path, "w+", encoding="utf-8") as f:
        f.write(
            f"""
#include "{decl_include}"
#include "{generated_header}"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace {{
{NEWLINE.join([
    GenerateUsings(proto)
    for _, proto in PROTOS.items() if GenerateUsings(proto)
])}
}}
using namespace testing;

template <typename InnerMatcher>
class AllElementsAreWithIndexMatcher {{
public:
    explicit AllElementsAreWithIndexMatcher(InnerMatcher inner) : inner_(inner) {{}}

    template <typename Range>
    bool MatchAndExplain(const Range& range, MatchResultListener* listener) const {{
        size_t index = 0;
        for (const auto& elem : range) {{
            if (!inner_(index, elem)) {{
                *listener << "element at index " << index << " does not match; value = " << elem;
                return false;
            }}
            ++index;
        }}
        return true;
    }}

    void DescribeTo(::std::ostream* os) const {{ *os << "each element satisfies inner matcher with index"; }}
    void DescribeNegationTo(::std::ostream* os) const {{ *os << "some element fails inner matcher with index"; }}

private:
    InnerMatcher inner_;
}};

template <typename InnerMatcher>
inline PolymorphicMatcher<AllElementsAreWithIndexMatcher<InnerMatcher>>
AllElementsAreWithIndex(InnerMatcher inner) {{
    return MakePolymorphicMatcher(AllElementsAreWithIndexMatcher<InnerMatcher>(inner));
}}

class Generated : public ::testing::Test {{}};

{NEWLINE.join([
    GenerateMatchersAndProtoTests(proto, PROTOS, previous_definitions)
    for _, proto in PROTOS.items()
])}
"""
        )


def _field_path_const(proto: Proto, field) -> str:
    return f'inline constexpr char k{proto.name}_{field.name}_path[] = "{field.name}";'


def _render_validator(
    attr: ValidationAttr, proto: Proto, field, extra_consts: list = None
) -> str:
    ns = proto.namespace
    field_expr = f"::cs::net::proto::validation::field<&{ns}::{proto.name}::{field.name}, k{proto.name}_{field.name}_path>"

    if attr.name == "required":
        return f"::cs::net::proto::validation::required<{field_expr}>"
    if attr.name == "email":
        return f"::cs::net::proto::validation::email<{field_expr}>"
    if attr.name in ("gt", "ge", "lt", "le", "len_gt", "len_lt"):
        if not attr.args:
            raise ValueError(f"{attr.name} requires an argument")
        arg = attr.args[0]
        return f"::cs::net::proto::validation::{attr.name}<{field_expr}, {arg}>"
    if attr.name == "enum_in":
        if not attr.args:
            raise ValueError("enum_in requires at least one allowed value")
        enum_refs = ", ".join(
            [f"k{proto.name}_{field.name}_enum_{idx}" for idx in range(len(attr.args))]
        )
        return f"::cs::net::proto::validation::enum_in<{field_expr}, {enum_refs}>"
    if attr.name == "matches":
        if not attr.args:
            raise ValueError("matches requires a pattern argument")
        pat_const = f"k{proto.name}_{field.name}_matches_{len(attr.args[0])}_pattern"
        return f"::cs::net::proto::validation::matches<{field_expr}, {pat_const}>"
    if attr.name == "iso8601":
        return f"::cs::net::proto::validation::iso8601<{field_expr}>"
    if attr.name == "oneof_set":
        return f"::cs::net::proto::validation::oneof_set<{field_expr}>"
    if attr.name == "custom":
        if not attr.args:
            raise ValueError("custom requires a token argument")
        token_const = f"k{proto.name}_{field.name}_custom_token"
        return f"::cs::net::proto::validation::custom<{field_expr}, {token_const}>"

    raise ValueError(f"Unsupported validation attribute '{attr.name}'")


def WriteGeneratedValidationFile(
    PROTOS: Dict[str, Proto],
    gen_dir: str,
    input_filename: str,
    out_root_rel: str | None = None,
) -> None:
    """Generate a .validate.hh file with validation Rules aliases."""
    if not PROTOS:
        return
    proto_sample = next(iter(PROTOS.values()))
    ns = f"{proto_sample.namespace}::{cc_namespace(proto_sample.filename, gen=True)}"
    decl_include = _output_include_for_input(input_filename, gen_dir, out_root_rel)
    header_guard = header_define(input_filename + ".validate")
    output_path = join(
        gen_dir, basename(input_filename).replace(".proto.hh", ".validate.hh")
    )

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(
            f"""#ifndef {header_guard}
#define {header_guard}

#include "cs/net/proto/validators.gpt.hh"
#include "{decl_include}"

namespace {ns} {{
namespace validation_generated {{
using namespace ::cs::net::proto::validation;

"""
        )
        # Field path constants
        for _, proto in PROTOS.items():
            for field in proto.fields:
                f.write(f"{_field_path_const(proto, field)};\n")
            f.write("\n")

        for _, proto in PROTOS.items():
            validators = []
            extra_consts = []
            for field in proto.fields:
                for attr in field.validations:
                    if attr.name == "matches" and attr.args:
                        pat_name = f"k{proto.name}_{field.name}_matches_{len(attr.args[0])}_pattern"
                        extra_consts.append(
                            f"inline constexpr char {pat_name}[] = {attr.args[0]};"
                        )
                    if attr.name == "custom" and attr.args:
                        tok_name = f"k{proto.name}_{field.name}_custom_token"
                        extra_consts.append(
                            f"inline constexpr char {tok_name}[] = {attr.args[0]};"
                        )
                    if attr.name == "enum_in" and attr.args:
                        for idx, arg in enumerate(attr.args):
                            enum_name = f"k{proto.name}_{field.name}_enum_{idx}"
                            extra_consts.append(
                                f"inline constexpr char {enum_name}[] = {arg};"
                            )
                    validators.append(_render_validator(attr, proto, field))
            # emit extra consts (patterns/tokens)
            for line in extra_consts:
                f.write(line + "\n")
            if validators:
                rules_expr = ",\n    ".join(validators)
                f.write(f"using {proto.name}Rules = all<\n    {rules_expr}\n>;\n\n")
            else:
                f.write(f"using {proto.name}Rules = noop;\n\n")

        f.write(
            f"""}}
}}  // namespace {ns}

#endif  // {header_guard}
"""
        )


def WriteGeneratedBazelRules(repofiles: List[str], gen_dir: str) -> None:
    """Generate a BUILD file containing cc_library and cc_test rules for protos."""

    def build_rule(filename: str) -> str:
        if filename.startswith("/"):
            filename = filename[1:]

        base_name = basename(filename)
        lib_name = basename(
            filename.replace(".proto.hh", ".proto").replace(f"{BACKSLASH}.", "_")
        )
        test_name = basename(filename.replace(".proto.hh", ".proto_test"))

        return f"""cc_library(
    name = "{lib_name}",
    hdrs = ["{base_name}"],
    srcs = ["{basename(filename.replace('.proto.hh', '.proto.cc'))}"],
    visibility = ["//visibility:public"],
    deps = [
        "//{dirname(filename)}:{basename(filename.replace('.proto.hh', '.proto'))}",
        "//cs/net/json:object",
        "//cs/net/json:serialize",
        "//cs/net/json:parsers",
        "//cs:result",
    ],
)

cc_test(
    name = "{test_name}",
    srcs = ["{basename(filename.replace('.proto.hh', '.proto_test.cc'))}"],
    deps = [
        "//{dirname(filename)}/gencode:{lib_name}",
        "@googletest//:gtest_main",
    ],
)

"""

    build_content = NEWLINE.join(build_rule(filename) for filename in repofiles)

    with open(join(gen_dir, "BUILD"), "w", encoding="utf-8") as f:
        f.write(
            f"""# Auto-generated BUILD file

load("@rules_cc//cc:defs.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])


{build_content}
"""
        )
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 6ms.