Skip to content

Commit ca9919a

Browse files
copyContext -> modifyContext
1 parent b4f016e commit ca9919a

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

modules/cli/src/main/scala/dev/guardrail/cli/CLICommon.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ trait CLICommon extends GuardrailRunner {
125125
case (sofar :: already, "--models" :: xs) =>
126126
Continue((empty.withKind(CodegenTarget.Models) :: sofar :: already, xs))
127127
case (sofar :: already, "--framework" :: value :: xs) =>
128-
Continue((sofar.copyContext(framework = Some(value)) :: already, xs))
128+
Continue((sofar.modifyContext(_.withFramework(Some(value))) :: already, xs))
129129
case (sofar :: already, "--help" :: xs) =>
130130
Continue((sofar.withPrintHelp(true) :: already, List.empty))
131131
case (sofar :: already, "--specPath" :: value :: xs) =>
132132
Continue((sofar.withSpecPath(Option(expandTilde(value))) :: already, xs))
133133
case (sofar :: already, "--tracing" :: xs) =>
134-
Continue((sofar.copyContext(tracing = true) :: already, xs))
134+
Continue((sofar.modifyContext(_.withTracing(true)) :: already, xs))
135135
case (sofar :: already, "--outputPath" :: value :: xs) =>
136136
Continue((sofar.withOutputPath(Option(expandTilde(value))) :: already, xs))
137137
case (sofar :: already, "--packageName" :: value :: xs) =>
@@ -141,11 +141,11 @@ trait CLICommon extends GuardrailRunner {
141141
case (sofar :: already, "--import" :: value :: xs) =>
142142
Continue((sofar.withImports(sofar.imports :+ value) :: already, xs))
143143
case (sofar :: already, "--module" :: value :: xs) =>
144-
Continue((sofar.copyContext(modules = sofar.context.modules :+ value) :: already, xs))
144+
Continue((sofar.modifyContext(_.withModules(sofar.context.modules :+ value)) :: already, xs))
145145
case (sofar :: already, "--custom-extraction" :: xs) =>
146-
Continue((sofar.copyContext(customExtraction = true) :: already, xs))
146+
Continue((sofar.modifyContext(_.withCustomExtraction(true)) :: already, xs))
147147
case (sofar :: already, "--package-from-tags" :: xs) =>
148-
Continue((sofar.copyContext(tagsBehaviour = TagsBehaviour.PackageFromTags) :: already, xs))
148+
Continue((sofar.modifyContext(_.withTagsBehaviour(TagsBehaviour.PackageFromTags)) :: already, xs))
149149
case (sofar :: already, (arg @ "--optional-encode-as") :: value :: xs) =>
150150
for {
151151
propertyRequirement <- parseOptionalProperty(arg.drop(2), value)
@@ -159,7 +159,7 @@ trait CLICommon extends GuardrailRunner {
159159
case (sofar :: already, (arg @ "--auth-implementation") :: value :: xs) =>
160160
for {
161161
auth <- parseAuthImplementation(arg, value)
162-
res <- Continue((sofar.copyContext(authImplementation = auth) :: already, xs))
162+
res <- Continue((sofar.modifyContext(_.withAuthImplementation(auth)) :: already, xs))
163163
} yield res
164164
case (_, unknown) =>
165165
debug("Unknown argument") >> Bail(UnknownArguments(unknown))

modules/core/src/main/scala/dev/guardrail/syntax/args.scala

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.guardrail.syntax
22

33
import dev.guardrail.terms.protocol.PropertyRequirement
4-
import dev.guardrail.{ Args, AuthImplementation, CodegenTarget, Context, TagsBehaviour }
4+
import dev.guardrail.{ Args, CodegenTarget, Context }
55
import dev.guardrail.syntax.context._
66

77
/* Syntax for `dev.guardrail.Args`
@@ -22,34 +22,19 @@ object args {
2222
}
2323

2424
implicit class ArgsSyntax(args: Args) {
25-
def copyContext(
26-
framework: Option[String] = args.context.framework,
27-
customExtraction: Boolean = args.context.customExtraction,
28-
tracing: Boolean = args.context.tracing,
29-
modules: List[String] = args.context.modules,
30-
propertyRequirement: PropertyRequirement.Configured = args.context.propertyRequirement,
31-
tagsBehaviour: TagsBehaviour = args.context.tagsBehaviour,
32-
authImplementation: AuthImplementation = args.context.authImplementation
33-
): Args =
34-
args.withContext(
35-
args.context
36-
.withFramework(framework)
37-
.withCustomExtraction(customExtraction)
38-
.withTracing(tracing)
39-
.withModules(modules)
40-
.withPropertyRequirement(propertyRequirement)
41-
.withTagsBehaviour(tagsBehaviour)
42-
.withAuthImplementation(authImplementation)
43-
)
25+
def modifyContext(func: Context => Context): Args =
26+
args.withContext(func(args.context))
4427

4528
def copyPropertyRequirement(
4629
encoder: PropertyRequirement.OptionalRequirement = args.context.propertyRequirement.encoder,
4730
decoder: PropertyRequirement.OptionalRequirement = args.context.propertyRequirement.decoder
4831
): Args =
49-
copyContext(
50-
propertyRequirement = args.context.propertyRequirement.copy(
51-
encoder = encoder,
52-
decoder = decoder
32+
modifyContext(ctx =>
33+
ctx.withPropertyRequirement(
34+
ctx.propertyRequirement.copy(
35+
encoder = encoder,
36+
decoder = decoder
37+
)
5338
)
5439
)
5540
}

0 commit comments

Comments
 (0)