Repository: julioz/FloorPlan (kotlin)

Issue:
Doesn't accept relative paths
`--args=~/foo/bar/1.json`

but 

`--args=~/Users/me/foo/bar/1.json` works.

Pull request:
Fixes #1

Patch:
diff --git a/src/main/kotlin/com/zynger/floorplan/App.kt b/src/main/kotlin/com/zynger/floorplan/App.kt
index 03cc579..9ecd780 100644
--- a/src/main/kotlin/com/zynger/floorplan/App.kt
+++ b/src/main/kotlin/com/zynger/floorplan/App.kt
@@ -1,9 +1,10 @@
 package com.zynger.floorplan
 
 import com.zynger.floorplan.dbml.Table
-import com.zynger.floorplan.model.*
+import com.zynger.floorplan.model.Schema
+import kotlinx.serialization.json.Json
+import kotlinx.serialization.json.JsonConfiguration
 import java.io.File
-import kotlinx.serialization.json.*
 
 fun main(args: Array<String>) {
     require(args.isNotEmpty()) {
@@ -14,7 +15,8 @@ fun main(args: Array<String>) {
         """.trimIndent()
     }
 
-    val src = File(args.first())
+    val input = InputParser.parse(args.first())
+    val src = File(input.schemaPath)
     val json = Json(JsonConfiguration.Stable.copy(ignoreUnknownKeys = true, isLenient = true))
 
     val dbml = json
diff --git a/src/main/kotlin/com/zynger/floorplan/InputParser.kt b/src/main/kotlin/com/zynger/floorplan/InputParser.kt
new file mode 100644
index 0000000..3e230ac
--- /dev/null
+++ b/src/main/kotlin/com/zynger/floorplan/InputParser.kt
@@ -0,0 +1,25 @@
+package com.zynger.floorplan
+
+import java.io.File
+
+object InputParser {
+
+    data class Input(val schemaPath: String)
+
+    fun parse(inputFilePath: String): Input {
+        return Input(sanitizeInputFilePath(inputFilePath))
+    }
+
+    private fun sanitizeInputFilePath(inputFilePath: String): String {
+        return when {
+            inputFilePath.startsWith("~" + File.separator) -> System.getProperty("user.home") + inputFilePath.substring(1)
+            inputFilePath.startsWith("~") -> {
+                // Don't support explicit username relative paths as '~otheruser/Documents'
+                // https://stackoverflow.com/a/7163446
+                throw UnsupportedOperationException("Home directory expansion is not supported for explicit user names.\n" +
+                        "Provide an absolute path to your input schema file.")
+            }
+            else -> inputFilePath
+        }
+    }
+}


Test patch:
diff --git a/src/test/kotlin/com/zynger/floorplan/InputParserTest.kt b/src/test/kotlin/com/zynger/floorplan/InputParserTest.kt
new file mode 100644
index 0000000..829192d
--- /dev/null
+++ b/src/test/kotlin/com/zynger/floorplan/InputParserTest.kt
@@ -0,0 +1,41 @@
+package com.zynger.floorplan
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.fail
+import org.junit.Test
+import java.lang.UnsupportedOperationException
+
+
+class InputParserTest {
+    
+    @Test
+    fun `parses absolute file paths`() {
+        val path = "samples/db.json"
+
+        val input = InputParser.parse(path)
+
+        assertEquals(path, input.schemaPath)
+    }
+
+    @Test
+    fun `parses home relative file paths`() {
+        val path = "~/foo/bar/db.json"
+        val expected = System.getProperty("user.home") + "/foo/bar/db.json"
+
+        val input = InputParser.parse(path)
+
+        assertEquals(expected, input.schemaPath)
+    }
+
+    @Test
+    fun `do not support home directory expansion for explicit user names`() {
+        val path = "~otheruser/Documents/foo/bar/db.json"
+
+        try {
+            InputParser.parse(path)
+            fail()
+        } catch(e: UnsupportedOperationException) {
+            // ok
+        }
+    }
+}