Skip to content

Commit 0ab802a

Browse files
Add scalafmt (#354)
Fixes #322
1 parent 6563295 commit 0ab802a

88 files changed

Lines changed: 4868 additions & 3181 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/pr-build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
distribution: 'zulu'
2828
cache: sbt
2929
- uses: sbt/setup-sbt@v1
30+
- name: Check Formating
31+
run: ./mill _.${{ matrix.lang }}[_].checkFormat
3032
- name: Compile with mill
3133
run: ./mill _.${{ matrix.lang }}[_].compile
3234
- name: Run mill tests for ${{ matrix.lang }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ out/
1212
/project/.bloop
1313
/project/project/
1414
/project/metals.sbt
15+
*.orig
16+
*.rej
17+
*.bak

.scalafmt.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version = "3.9.5"
2+
3+
docstrings.style = Asterisk
4+
maxColumn = 100
5+
continuationIndent {
6+
callSite = 2
7+
defnSite = 4
8+
}
9+
10+
assumeStandardLibraryStripMargin = false
11+
align.stripMargin = false
12+
verticalAlignMultilineOperators = true
13+
14+
project.git = true
15+
runner.dialect = scala3
16+
17+
danglingParentheses = {defnSite: false, callSite: true}

bench/src/main/scala/sjsonnet/MainBenchmark.scala

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ object MainBenchmark {
1010
val mainArgs = Array[String](
1111
"../../universe/rulemanager/deploy/rulemanager.jsonnet",
1212
// "../../universe/kubernetes/admission-controller/gatekeeper/deploy/gatekeeper.jsonnet",
13-
"-J", "../../universe",
14-
"-J", "../../universe/mt-shards/dev/az-westus-c2",
15-
"-J", "../../universe/bazel-bin",
16-
"--ext-code", "isKubecfg=false"
13+
"-J",
14+
"../../universe",
15+
"-J",
16+
"../../universe/mt-shards/dev/az-westus-c2",
17+
"-J",
18+
"../../universe/bazel-bin",
19+
"--ext-code",
20+
"isKubecfg=false"
1721
)
1822

1923
def findFiles(): (IndexedSeq[(Path, String)], EvalScope) = {
2024
val parser = mainargs.ParserForClass[Config]
21-
val config = parser.constructEither(MainBenchmark.mainArgs.toIndexedSeq, autoPrintHelpAndExit = None).getOrElse(???)
25+
val config = parser
26+
.constructEither(MainBenchmark.mainArgs.toIndexedSeq, autoPrintHelpAndExit = None)
27+
.getOrElse(???)
2228
val file = config.file
2329
val wd = os.pwd
2430
val path = OsPath(os.Path(file, wd))
@@ -31,7 +37,9 @@ object MainBenchmark {
3137
parseCache = parseCache
3238
)
3339
val renderer = new Renderer(new StringWriter, indent = 3)
34-
interp.interpret0(interp.resolver.read(path, binaryData = false).get.readString(), path, renderer).getOrElse(???)
40+
interp
41+
.interpret0(interp.resolver.read(path, binaryData = false).get.readString(), path, renderer)
42+
.getOrElse(???)
3543
(parseCache.keySet.toIndexedSeq, interp.evaluator)
3644
}
3745

@@ -55,15 +63,17 @@ class MainBenchmark {
5563

5664
@Benchmark
5765
def main(bh: Blackhole): Unit = {
58-
bh.consume(SjsonnetMain.main0(
59-
MainBenchmark.mainArgs,
60-
new DefaultParseCache,
61-
System.in,
62-
dummyOut,
63-
System.err,
64-
os.pwd,
65-
None
66-
))
66+
bh.consume(
67+
SjsonnetMain.main0(
68+
MainBenchmark.mainArgs,
69+
new DefaultParseCache,
70+
System.in,
71+
dummyOut,
72+
System.err,
73+
os.pwd,
74+
None
75+
)
76+
)
6777
}
6878
}
6979

bench/src/main/scala/sjsonnet/MaterializerBenchmark.scala

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class MaterializerBenchmark {
2222
@Setup
2323
def setup(): Unit = {
2424
val parser = mainargs.ParserForClass[Config]
25-
val config = parser.constructEither(MainBenchmark.mainArgs.toIndexedSeq, autoPrintHelpAndExit = None).getOrElse(???)
25+
val config = parser
26+
.constructEither(MainBenchmark.mainArgs.toIndexedSeq, autoPrintHelpAndExit = None)
27+
.getOrElse(???)
2628
val file = config.file
2729
val wd = os.pwd
2830
val path = os.Path(file, wd)
@@ -31,14 +33,15 @@ class MaterializerBenchmark {
3133
Map.empty[String, String],
3234
Map.empty[String, String],
3335
OsPath(wd),
34-
importer = SjsonnetMain.resolveImport(config.jpaths.map(os.Path(_, wd)).map(OsPath(_)).toIndexedSeq, None),
36+
importer = SjsonnetMain
37+
.resolveImport(config.jpaths.map(os.Path(_, wd)).map(OsPath(_)).toIndexedSeq, None),
3538
parseCache = new DefaultParseCache
3639
)
3740
value = interp.evaluate(os.read(path), OsPath(path)).getOrElse(???)
3841
assert(renderYaml() == oldRenderYaml())
3942
val r1 = render()
4043
assert(r1 == oldRender())
41-
System.err.println("JSON length: "+r1.length)
44+
System.err.println("JSON length: " + r1.length)
4245
assert(renderPython() == oldRenderPython())
4346
}
4447

@@ -51,14 +54,16 @@ class MaterializerBenchmark {
5154

5255
@Benchmark def renderPrettyYamlB(bh: Blackhole): Unit = bh.consume(renderPrettyYaml())
5356

54-
private def render() = renderWith(new Renderer(_, indent=3))
55-
private def renderPython() = renderWith(new PythonRenderer(_, indent=3))
56-
private def renderYaml() = renderWith(new YamlRenderer(_, indent=3))
57-
private def oldRender() = renderWith(new OldRenderer(_, indent=3))
58-
private def oldRenderPython() = renderWith(new OldPythonRenderer(_, indent=3))
59-
private def oldRenderYaml() = renderWith(new OldYamlRenderer(_, indent=3))
57+
private def render() = renderWith(new Renderer(_, indent = 3))
58+
private def renderPython() = renderWith(new PythonRenderer(_, indent = 3))
59+
private def renderYaml() = renderWith(new YamlRenderer(_, indent = 3))
60+
private def oldRender() = renderWith(new OldRenderer(_, indent = 3))
61+
private def oldRenderPython() = renderWith(new OldPythonRenderer(_, indent = 3))
62+
private def oldRenderYaml() = renderWith(new OldYamlRenderer(_, indent = 3))
6063

61-
private def renderPrettyYaml() = renderWith(new PrettyYamlRenderer(_, indent=3, getCurrentPosition = () => null))
64+
private def renderPrettyYaml() = renderWith(
65+
new PrettyYamlRenderer(_, indent = 3, getCurrentPosition = () => null)
66+
)
6267

6368
private def renderWith[T <: Writer](r: StringWriter => JsVisitor[T, T]): String = {
6469
val writer = new StringWriter

bench/src/main/scala/sjsonnet/MultiThreadedBenchmark.scala

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ class MultiThreadedBenchmark {
2222
def main(bh: Blackhole): Unit = {
2323
val cache: ParseCache = new ParseCache {
2424
val map = new mutable.HashMap[(Path, String), Either[Error, (Expr, FileScope)]]()
25-
override def getOrElseUpdate(key: (Path, String), defaultValue: => Either[Error, (Expr, FileScope)]): Either[Error, (Expr, FileScope)] = {
25+
override def getOrElseUpdate(
26+
key: (Path, String),
27+
defaultValue: => Either[Error, (Expr, FileScope)]): Either[Error, (Expr, FileScope)] = {
2628
var v = map.synchronized(map.getOrElse(key, null))
27-
if(v == null) {
29+
if (v == null) {
2830
v = defaultValue
2931
map.synchronized(map.put(key, v))
3032
}
@@ -34,23 +36,27 @@ class MultiThreadedBenchmark {
3436

3537
val pool: ExecutorService = Executors.newFixedThreadPool(threads)
3638
val futs = (1 to threads).map { _ =>
37-
pool.submit { (() =>
38-
if(SjsonnetMain.main0(
39-
MainBenchmark.mainArgs,
40-
cache, // new DefaultParseCache
41-
System.in,
42-
MainBenchmark.createDummyOut,
43-
System.err,
44-
os.pwd,
45-
None
46-
) != 0) throw new Exception): Runnable
39+
pool.submit {
40+
(() =>
41+
if (
42+
SjsonnetMain.main0(
43+
MainBenchmark.mainArgs,
44+
cache, // new DefaultParseCache
45+
System.in,
46+
MainBenchmark.createDummyOut,
47+
System.err,
48+
os.pwd,
49+
None
50+
) != 0
51+
) throw new Exception): Runnable
4752
}
4853
}
4954
var err: Throwable = null
5055
bh.consume(futs.map { f =>
51-
try f.get() catch { case e: Throwable => err = e }
56+
try f.get()
57+
catch { case e: Throwable => err = e }
5258
})
5359
pool.shutdown()
54-
if(err != null) throw err
60+
if (err != null) throw err
5561
}
5662
}

bench/src/main/scala/sjsonnet/OptimizerBenchmark.scala

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ class OptimizerBenchmark {
2424
def setup(): Unit = {
2525
val (allFiles, ev) = MainBenchmark.findFiles()
2626
this.inputs = allFiles.map { case (p, s) =>
27-
fastparse.parse(s, new Parser(p, true, mutable.HashMap.empty, mutable.HashMap.empty).document(_)) match {
27+
fastparse.parse(
28+
s,
29+
new Parser(p, true, mutable.HashMap.empty, mutable.HashMap.empty).document(_)
30+
) match {
2831
case Success(v, _) => v
2932
case f: fastparse.Parsed.Failure =>
3033
throw new Exception(s"Failed to parse ${p}: ${f.msg}")
3134
}
3235
}
3336
this.ev = ev
34-
val static = inputs.map {
35-
case (expr, fs) => ((new StaticOptimizer(ev,_ => None, new Std().Std, mutable.HashMap.empty, mutable.HashMap.empty)).optimize(expr), fs)
37+
val static = inputs.map { case (expr, fs) =>
38+
(
39+
(new StaticOptimizer(
40+
ev,
41+
_ => None,
42+
new Std().Std,
43+
mutable.HashMap.empty,
44+
mutable.HashMap.empty
45+
)).optimize(expr),
46+
fs
47+
)
3648
}
3749
val countBefore, countStatic = new Counter
3850
inputs.foreach(t => assert(countBefore.transform(t._1) eq t._1))
@@ -45,53 +57,66 @@ class OptimizerBenchmark {
4557
@Benchmark
4658
def main(bh: Blackhole): Unit = {
4759
bh.consume(inputs.foreach { case (expr, fs) =>
48-
bh.consume((new StaticOptimizer(ev,
49-
_ => None,
50-
new Std().Std, mutable.HashMap.empty, mutable.HashMap.empty)).optimize(expr))
60+
bh.consume(
61+
(new StaticOptimizer(
62+
ev,
63+
_ => None,
64+
new Std().Std,
65+
mutable.HashMap.empty,
66+
mutable.HashMap.empty
67+
)).optimize(expr)
68+
)
5169
})
5270
}
5371

5472
class Counter extends ExprTransform {
5573
var total, vals, exprs, arrVals, staticArrExprs, otherArrExprs, staticObjs, missedStaticObjs,
56-
otherObjs, namedApplies, applies, arityApplies, builtin = 0
74+
otherObjs, namedApplies, applies, arityApplies, builtin = 0
5775
val applyArities = new mutable.LongMap[Int]()
5876
val ifElseChains = new mutable.LongMap[Int]()
5977
val selectChains = new mutable.LongMap[Int]()
6078
def transform(e: Expr) = {
6179
total += 1
62-
if(e.isInstanceOf[Val]) vals += 1
80+
if (e.isInstanceOf[Val]) vals += 1
6381
else exprs += 1
6482
e match {
6583
case _: Val.Arr => arrVals += 1
6684
case a: Expr.Arr =>
67-
if(a.value.forall(_.isInstanceOf[Val])) staticArrExprs += 1
85+
if (a.value.forall(_.isInstanceOf[Val])) staticArrExprs += 1
6886
else otherArrExprs += 1
6987
case _: Val.Obj => staticObjs += 1
7088
case e: Expr.ObjBody.MemberList =>
71-
if(e.binds == null && e.asserts == null && e.fields.forall(_.isStatic)) missedStaticObjs += 1
89+
if (e.binds == null && e.asserts == null && e.fields.forall(_.isStatic))
90+
missedStaticObjs += 1
7291
else otherObjs += 1
7392
case e: Expr.Apply =>
74-
if(e.namedNames == null) {
93+
if (e.namedNames == null) {
7594
applies += 1
7695
val a = e.args.length
7796
applyArities.put(a.toLong, applyArities.getOrElse(a.toLong, 0) + 1)
7897
} else namedApplies += 1
7998

8099
case _: Expr.Apply0 | _: Expr.Apply1 | _: Expr.Apply2 | _: Expr.Apply3 => arityApplies += 1
81100
case _: Expr.ApplyBuiltin | _: Expr.ApplyBuiltin1 | _: Expr.ApplyBuiltin2 => builtin += 1
82-
case _ =>
101+
case _ =>
83102
}
84103
val ifElseCount = countIfElse(e)
85-
if(ifElseCount > 0) {
104+
if (ifElseCount > 0) {
86105
ifElseChains.put(ifElseCount.toLong, ifElseChains.getOrElse(ifElseCount.toLong, 0) + 1)
87-
if(ifElseCount > 1)
88-
ifElseChains.put(ifElseCount.toLong-1L, ifElseChains.getOrElse(ifElseCount.toLong-1L, 0) - 1)
106+
if (ifElseCount > 1)
107+
ifElseChains.put(
108+
ifElseCount.toLong - 1L,
109+
ifElseChains.getOrElse(ifElseCount.toLong - 1L, 0) - 1
110+
)
89111
}
90112
val selectCount = countSelectOnId(e)
91-
if(selectCount >= 0) {
113+
if (selectCount >= 0) {
92114
selectChains.put(selectCount.toLong, selectChains.getOrElse(selectCount.toLong, 0) + 1)
93-
if(selectCount > 0)
94-
selectChains.put(selectCount.toLong-1L, selectChains.getOrElse(selectCount.toLong-1L, 0) - 1)
115+
if (selectCount > 0)
116+
selectChains.put(
117+
selectCount.toLong - 1L,
118+
selectChains.getOrElse(selectCount.toLong - 1L, 0) - 1
119+
)
95120
}
96121
rec(e)
97122
}
@@ -102,20 +127,21 @@ class OptimizerBenchmark {
102127
}
103128
def countSelectOnId(e: Expr): Int = e match {
104129
case Expr.Select(_, x, _) =>
105-
val c = countSelectOnId(x)
106-
if(c == -1) -1 else c + 1
130+
val c = countSelectOnId(x)
131+
if (c == -1) -1 else c + 1
107132
case _: Expr.ValidId => 0
108-
case _ => -1
133+
case _ => -1
109134
}
110135
override def toString = {
111-
val arities = applyArities.toSeq.sortBy(_._1).map { case (a,b) => s"$a: $b" }.mkString(", ")
112-
val chains = ifElseChains.toSeq.sortBy(_._1).map { case (a,b) => s"$a: $b" }.mkString(", ")
113-
val selChains = selectChains.toSeq.sortBy(_._1).map { case (a,b) => s"$a: $b" }.mkString(", ")
114-
s"Total: $total, Val: $vals, Expr: $exprs, Val.Arr: $arrVals, static Expr.Arr: $staticArrExprs, "+
115-
s"other Expr.Arr: $otherArrExprs, Val.Obj: $staticObjs, static MemberList: $missedStaticObjs, "+
116-
s"other MemberList: $otherObjs, named Apply: $namedApplies, other Apply: $applies, "+
117-
s"ApplyN: $arityApplies, ApplyBuiltin*: $builtin; Apply arities: {$arities}, "+
118-
s"if/else chains: $chains, Select/ValidId chains: $selChains"
136+
val arities = applyArities.toSeq.sortBy(_._1).map { case (a, b) => s"$a: $b" }.mkString(", ")
137+
val chains = ifElseChains.toSeq.sortBy(_._1).map { case (a, b) => s"$a: $b" }.mkString(", ")
138+
val selChains =
139+
selectChains.toSeq.sortBy(_._1).map { case (a, b) => s"$a: $b" }.mkString(", ")
140+
s"Total: $total, Val: $vals, Expr: $exprs, Val.Arr: $arrVals, static Expr.Arr: $staticArrExprs, " +
141+
s"other Expr.Arr: $otherArrExprs, Val.Obj: $staticObjs, static MemberList: $missedStaticObjs, " +
142+
s"other MemberList: $otherObjs, named Apply: $namedApplies, other Apply: $applies, " +
143+
s"ApplyN: $arityApplies, ApplyBuiltin*: $builtin; Apply arities: {$arities}, " +
144+
s"if/else chains: $chains, Select/ValidId chains: $selChains"
119145
}
120146
}
121147
}

0 commit comments

Comments
 (0)