Skip to content

Commit

Permalink
update(fildesh): with yaml file output support
Browse files Browse the repository at this point in the history
  • Loading branch information
grencez committed Jun 3, 2024
1 parent 70827d3 commit c720229
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test_bazel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
- name: Bazel Setup
uses: jwlawson/actions-setup-bazel@v2
with:
bazel-version: '6'
bazel-version: 'latest'

- name: Checkout
uses: actions/checkout@v4
Expand All @@ -93,6 +93,10 @@ jobs:
--
//...
- name: Strip Branch Coverage
# Bazel 7 generates branch coverage, but we prefer line coverage reports.
run: sed -i '/^BR/d' bazel-out/_coverage/_coverage_report.dat

- name: Coveralls
uses: coverallsapp/github-action@master
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/bazel-*
/MODULE.bazel.lock
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ bazel_dep(name = "rules_proto", version = "4.0.0")
git_override(
module_name = "fildesh",
remote = "https://github.com/grencez/fildesh.git",
commit="29ef2da4a4c4aeda9c0af917b95fc2c493d4f96d",
commit="11ff65ed78103bc1b1caefb1deb8fa42110eea08",
)
4 changes: 2 additions & 2 deletions src/protobuf_transcode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ new_message_from_json_content(
return nullptr;
}

google::protobuf::util::Status status = JsonStringToMessage(
auto status = JsonStringToMessage(
in_content, message.get());
if (!status.ok()) {
std::cerr << "Error parsing json." << std::endl;
Expand Down Expand Up @@ -128,7 +128,7 @@ write_message_to_json_file(
std::string out_content;
JsonPrintOptions json_options;
json_options.preserve_proto_field_names = !camelcase;
google::protobuf::util::Status status = MessageToJsonString(
auto status = MessageToJsonString(
message, &out_content, json_options);
if (!status.ok()) {
std::cerr << "Error encoding json." << std::endl;
Expand Down
40 changes: 35 additions & 5 deletions sxproto/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,38 @@ def _run_sxpb2json(ctx, sxpb_file, json_file):
)
return [json_file]

def _run_sxproto2textproto(ctx, sxproto_file, textproto_file):
def _run_sxpb2txtpb(ctx, sxproto_file, textproto_file):
"""Translate .sxproto file to .txtpb file."""
args = ctx.actions.args()
args.add_joined(["stdin=open_readonly", sxproto_file], join_with = ":")
args.add_joined(["stdout=open_writeonly", textproto_file], join_with = ":")
args.add("--")
args.add(ctx.executable._sxproto2textproto)
args.add(ctx.executable._sxpb2txtpb)
ctx.actions.run(
executable = ctx.executable._fildespawn,
arguments = [args],
inputs = [sxproto_file],
outputs = [textproto_file],
tools = [ctx.executable._sxproto2textproto],
tools = [ctx.executable._sxpb2txtpb],
)
return [textproto_file]

def _run_sxpb2yaml(ctx, sxpb_file, yaml_file):
"""Translate .sxpb file to .json file."""
args = ctx.actions.args()
args.add_joined(["stdin=open_readonly", sxpb_file], join_with = ":")
args.add_joined(["stdout=open_writeonly", yaml_file], join_with = ":")
args.add("--")
args.add(ctx.executable._sxpb2yaml)
ctx.actions.run(
executable = ctx.executable._fildespawn,
arguments = [args],
inputs = [sxpb_file],
outputs = [yaml_file],
tools = [ctx.executable._sxpb2yaml],
)
return [yaml_file]


def _sxproto_data_impl(ctx):
descriptor_set_depset = _descriptor_set_depset(ctx)
Expand All @@ -209,7 +225,7 @@ def _sxproto_data_impl(ctx):

textproto_file = None
if ctx.outputs.out_textproto:
outfiles += _run_sxproto2textproto(
outfiles += _run_sxpb2txtpb(
ctx, ctx.file.src, ctx.outputs.out_textproto)
_run_protobuf_transcode(
ctx, ctx.executable._textproto2binaryproto,
Expand All @@ -231,6 +247,10 @@ def _sxproto_data_impl(ctx):
binaryproto_file, ctx.outputs.out_json_camelcase,
ctx.attr.proto_message, descriptor_set_depset)

if ctx.outputs.out_yaml:
outfiles += _run_sxpb2yaml(
ctx, ctx.file.src, ctx.outputs.out_yaml)

return DefaultInfo(
files = depset([binaryproto_file]),
runfiles = ctx.runfiles(files = outfiles),
Expand All @@ -255,6 +275,10 @@ sxproto_data = rule(
mandatory = False,
doc = "The .json file to write with camelCase fields.",
),
"out_yaml": attr.output(
mandatory = False,
doc = "The .yaml file to write.",
),
"_fildespawn": attr.label(
default = Label("@fildesh//tool:fildespawn"),
allow_single_file = True,
Expand All @@ -273,12 +297,18 @@ sxproto_data = rule(
executable = True,
cfg = "exec",
),
"_sxproto2textproto": attr.label(
"_sxpb2txtpb": attr.label(
default = Label("//tool:sxpb2txtpb"),
allow_single_file = True,
executable = True,
cfg = "exec",
),
"_sxpb2yaml": attr.label(
default = Label("//tool:sxpb2yaml"),
allow_single_file = True,
executable = True,
cfg = "exec",
),
"_textproto2binaryproto": attr.label(
default = Label("//tool:textproto2binaryproto"),
allow_single_file = True,
Expand Down
10 changes: 10 additions & 0 deletions test/manyof/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@fildesh//tool/bazel:cmptxt_test.bzl", "cmptxt_test")
load("@rules_proto//proto:defs.bzl", "proto_library")
load(
"//sxproto:defs.bzl",
Expand All @@ -18,6 +19,7 @@ sxproto_data(
src = "boolexpr.sxpb",
out_json = "boolexpr_sxproto_data.json",
out_textproto = "boolexpr_sxproto_data.txtpb",
out_yaml = "boolexpr_sxproto_data.yaml",
proto_deps = [":boolexpr_proto"],
proto_message = "sxproto.test.manyof.BooleanExpressionList",
testonly = 1,
Expand Down Expand Up @@ -123,3 +125,11 @@ protobuf_equality_test(
proto_deps = [":boolexpr_proto"],
proto_message = "sxproto.test.manyof.BooleanExpressionList",
)

cmptxt_test(
name = "boolexpr_data_yaml_equality_test",
srcs = [
":boolexpr.yaml",
"boolexpr_sxproto_data.yaml",
],
)
23 changes: 23 additions & 0 deletions test/manyof/boolexpr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
expressions:
- value: true
- not:
value: false
- or:
- value: false
- value: true
- or:
- value: false
- value: true
- or:
- value: false
- value: true
- or:
- value: false
- and:
- value: true
- value: false
- or:
- value: false
- value: false
- value: true
5 changes: 5 additions & 0 deletions tool/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ alias(
actual = "@fildesh//tool:sxpb2txtpb",
)

alias(
name = "sxpb2yaml",
actual = "@fildesh//tool:sxpb2yaml",
)

alias(
name = "textproto2binaryproto",
actual = "//src:textproto2binaryproto",
Expand Down

0 comments on commit c720229

Please sign in to comment.