Skip to content

Commit e0e460e

Browse files
committed
Use Option[T] for fields
1 parent 013e44e commit e0e460e

File tree

2 files changed

+28
-38
lines changed

2 files changed

+28
-38
lines changed

tests/src/test/scala/org/scalanative/bindgen/BindgenSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class BindgenSpec extends FunSpec {
2121
Bindgen()
2222
.bindgenExecutable(new File(bindgenPath))
2323
.header(inputFile)
24-
.scalaObjectName(name)
24+
.name(name)
2525
.link("bindgentests")
2626
.packageName("org.scalanative.bindgen.samples")
2727
.excludePrefix("__")

tools/src/main/scala/org/scalanative/bindgen/Bindgen.scala

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ sealed trait Bindgen {
2626
* Name of Scala object that contains bindings.
2727
* Default is set to library name.
2828
*/
29-
def scalaObjectName(scalaObjectName: String): Bindgen
29+
def name(name: String): Bindgen
3030

3131
/**
3232
* Package name of generated Scala file
@@ -59,44 +59,44 @@ object Bindgen {
5959
def apply(): Bindgen = Impl()
6060

6161
private final case class Impl(
62-
executable: File = null,
63-
library: String = null,
64-
header: File = null,
65-
scalaObjectName: String = null,
66-
packageName: String = null,
67-
excludePrefix: String = null,
62+
executable: Option[File] = None,
63+
library: Option[String] = None,
64+
header: Option[File] = None,
65+
name: Option[String] = None,
66+
packageName: Option[String] = None,
67+
excludePrefix: Option[String] = None,
6868
extraArg: immutable.Seq[String] = immutable.Seq[String](),
6969
extraArgBefore: immutable.Seq[String] = immutable.Seq[String]())
7070
extends Bindgen {
7171

7272
def bindgenExecutable(executable: File): Bindgen = {
7373
require(executable.exists())
74-
copy(executable = executable)
74+
copy(executable = Option(executable))
7575
}
7676

7777
def header(header: File): Bindgen = {
7878
require(header.exists())
79-
copy(header = header)
79+
copy(header = Option(header))
8080
}
8181

8282
def link(library: String): Bindgen = {
8383
require(!library.isEmpty)
84-
copy(library = library)
84+
copy(library = Option(library))
8585
}
8686

87-
def scalaObjectName(scalaObjectName: String): Bindgen = {
88-
require(!scalaObjectName.isEmpty)
89-
copy(scalaObjectName = scalaObjectName)
87+
def name(name: String): Bindgen = {
88+
require(!name.isEmpty)
89+
copy(name = Option(name))
9090
}
9191

9292
def packageName(packageName: String): Bindgen = {
9393
require(!packageName.isEmpty)
94-
copy(packageName = packageName)
94+
copy(packageName = Option(packageName))
9595
}
9696

9797
def excludePrefix(prefix: String): Bindgen = {
9898
require(!prefix.isEmpty)
99-
copy(excludePrefix = prefix)
99+
copy(excludePrefix = Option(prefix))
100100
}
101101

102102
def extraArg(args: String*): Bindgen = {
@@ -110,41 +110,31 @@ object Bindgen {
110110
}
111111

112112
private def validateFields(): Unit = {
113-
if (executable == null) {
114-
throw new AssertionError("Specify bindgen executable")
115-
}
116-
if (header == null) {
117-
throw new AssertionError("Specify header file")
118-
}
119-
if (library == null) {
120-
throw new AssertionError("Specify library name")
121-
}
113+
require(executable.isDefined, "The executable must be specified")
114+
require(header.isDefined, "Header file must be specified")
115+
require(library.isDefined, "Library name must be specified")
122116
}
123117

124118
def generate(): Bindings = {
125119
validateFields()
126120

127-
val scalaObjectName =
128-
if (this.scalaObjectName != null)
129-
this.scalaObjectName
130-
else
131-
library
121+
val name = this.name.getOrElse(library.get)
132122

133123
var cmd = Seq(
134-
executable.getAbsolutePath,
135-
header.getAbsolutePath,
124+
executable.get.getAbsolutePath,
125+
header.get.getAbsolutePath,
136126
"--name",
137-
scalaObjectName,
127+
name,
138128
"--link",
139-
library
129+
library.get
140130
)
141131

142-
if (packageName != null) {
143-
cmd ++= Seq("--package", packageName)
132+
if (packageName.isDefined) {
133+
cmd ++= Seq("--package", packageName.get)
144134
}
145135

146-
if (excludePrefix != null) {
147-
cmd ++= Seq("--exclude-prefix", excludePrefix)
136+
if (excludePrefix.isDefined) {
137+
cmd ++= Seq("--exclude-prefix", excludePrefix.get)
148138
}
149139

150140
extraArg.foreach(arg => cmd ++= Seq("--extra-arg", arg))

0 commit comments

Comments
 (0)