Skip to content

Commit

Permalink
Make the json mode toggleable
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Nov 22, 2023
1 parent 8d73b10 commit 1fcafed
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
2 changes: 2 additions & 0 deletions hocon-compiler/src/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object Compiler {
val allowMissing = opt[Boolean](default = Some(false))
val includeComments = toggle("comments", default = Some(true), noshort = true)
val doResolve = toggle("resolve", default = Some(true), noshort = true)
val json = toggle("json", default = Some(false), noshort = true)
val src = trailArg[File]()

verify()
Expand All @@ -50,6 +51,7 @@ object Compiler {
val renderOptions = ConfigRenderOptions
.defaults()
.setOriginComments(false)
.setJson(opts.json())
.setComments(opts.includeComments())

writeConfig(finalConfig, renderOptions, opts.output(), opts.header())
Expand Down
8 changes: 8 additions & 0 deletions rules/hocon.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def _hocon_library_impl(ctx):
if not ctx.attr.resolve:
args.add("--noresolve")

if ctx.attr.json:
args.add("--json")

args.add_all("-D", ctx.attr.optional_includes, omit_if_empty = True, uniquify = True)

if ctx.attr.warnings:
Expand Down Expand Up @@ -113,6 +116,11 @@ hocon_library = rule(
doc = "If true, the output will resolve any references where possible, so that the only remaining references are to values that will be supplied at runtime.",
default = True,
),
"json": attr.bool(
doc = """If true, then output in a json-like format.
Note that it may include comments and references that aren't actually valid JSON""",
default = False,
),
"_hocon_compiler": attr.label(
executable = True,
cfg = "host",
Expand Down
80 changes: 39 additions & 41 deletions tests/general/expected.conf
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
# Resolved configuration

{
"base-conf" : {
"overwrite" : "Overwrote the base config here",
"strings" : {
"a" : "a",
"b" : "b"
},
"up-ref" : {
"foobar" : "foobar",
"n" : null
base-conf {
overwrite="Overwrote the base config here"
strings {
a=a
b=b
}
up-ref {
foobar=foobar
n=null
}
}
test-conf {
bool=true
env-ref=something-from-${env.key}
from-env=${env.key}
num=500
obj {
bar=bar
foo=foo
num=10
obj2 {
foobar=foobar
n=null
}
}
overwrite="overwritten!"
ref=a
strings {
one {
a=a
b=b
c=c
}
two {
x=x
y=y
z=z
}
},
"test-conf" : {
"bool" : true,
"env-ref" : "something-from-"${env.key},
"from-env" : ${env.key},
"num" : 500,
"obj" : {
"bar" : "bar",
"foo" : "foo",
"num" : 10,
"obj2" : {
"foobar" : "foobar",
"n" : null
}
},
"overwrite" : "overwritten!",
"ref" : "a",
"strings" : {
"one" : {
"a" : "a",
"b" : "b",
"c" : "c"
},
"two" : {
"x" : "x",
"y" : "y",
"z" : "z"
}
},
"subref" : "prefix-b",
"undef" : null
}
subref=prefix-b
undef=null
}
2 changes: 1 addition & 1 deletion tests/optional_includes/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if [[ $? -ne 0 ]]; then echo "$out"; exit 1; fi

# Verify 3.conf was included
config_path="bazel-bin/tests/optional_includes/good.conf"
cat "$config_path" | grep -oq '"some-config" *: *"hello"'
cat "$config_path" | grep -oq 'some-config=hello'
if [[ $? -ne 0 ]]; then echo "Malformed config: "; cat "$config_path"; exit 1; fi

out=$(! bazel build //tests/optional_includes:bad 2>&1)
Expand Down

0 comments on commit 1fcafed

Please sign in to comment.