Skip to content

Commit accba2b

Browse files
authored
Merge pull request #23 from blackmo18/dev/decouple-custom-datatypes
Dev/decouple custom datatypes
2 parents e06646b + 6b38a20 commit accba2b

File tree

56 files changed

+1873
-17
lines changed

Some content is hidden

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

56 files changed

+1873
-17
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,6 @@ tasks.jacocoTestReport {
173173

174174
reports {
175175
xml.isEnabled = true
176-
html.isEnabled = false
176+
html.isEnabled = true
177177
}
178178
}

build.gradle.kts.back

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
plugins {
2+
kotlin("multiplatform") version "1.4.0"
3+
id("maven-publish")
4+
id("org.jetbrains.dokka").version("0.9.18")
5+
id("com.jfrog.bintray") version "1.8.5"
6+
id("java-library")
7+
jacoco
8+
}
9+
10+
val kotlinVersion = "1.4.0"
11+
val csvVersion = "0.11.0"
12+
13+
group = "com.vhl.blackmo"
14+
version = "0.4.1"
15+
16+
repositories {
17+
jcenter()
18+
}
19+
20+
val dokkaJar = task<Jar>("dokkaJar") {
21+
group = JavaBasePlugin.DOCUMENTATION_GROUP
22+
archiveClassifier.set("javadoc")
23+
}
24+
val sourceJar = task<Jar>("sourceJar") {
25+
archiveClassifier.set("sources")
26+
from(sourceSets.getByName("main").allSource)
27+
}
28+
val metadata = task<Jar>("metadata") {
29+
archiveClassifier.set("metadata")
30+
from(sourceSets.getByName("main").allSource)
31+
}
32+
33+
val jvm = task<Jar>("jvm") {
34+
archiveClassifier.set("jvm")
35+
from(sourceSets.getByName("main").allSource)
36+
}
37+
38+
kotlin {
39+
jvm {
40+
compilations.all {
41+
kotlinOptions {
42+
jvmTarget = "1.8"
43+
noReflect = false
44+
}
45+
}
46+
}
47+
48+
sourceSets {
49+
val commonMain by getting {
50+
dependencies {
51+
implementation(kotlin("stdlib-common"))
52+
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
53+
}
54+
}
55+
val commonTest by getting {
56+
dependencies {
57+
implementation(kotlin("test-annotations-common"))
58+
}
59+
}
60+
jvm().compilations["main"].defaultSourceSet {
61+
dependencies {
62+
implementation(kotlin("stdlib-common"))
63+
}
64+
}
65+
jvm().compilations["test"].defaultSourceSet {
66+
this.
67+
dependencies {
68+
implementation(kotlin("test"))
69+
implementation(kotlin("test-junit"))
70+
implementation("io.kotlintest:kotlintest-runner-junit5:3.3.2")
71+
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:$csvVersion")
72+
}
73+
}
74+
}
75+
}
76+
77+
val bintrayUser = (project.findProperty("bintray.user") ?: "").toString()
78+
val bintrayKey = (project.findProperty("bintray.apikey")?: "").toString()
79+
80+
publishing {
81+
repositories {
82+
maven(url = "https://api.bintray.com/maven/blackmo18/kotlin-libraries/kotlin-grass/;publish=1") {
83+
name = "bintray"
84+
credentials {
85+
username = bintrayUser
86+
password = bintrayKey
87+
}
88+
}
89+
}
90+
}
91+
92+
val jvmTest by tasks.getting(Test::class) {
93+
useJUnitPlatform { }
94+
}
95+
96+
jacoco {
97+
toolVersion = "0.8.5"
98+
}
99+
100+
tasks.jacocoTestReport {
101+
val coverageSourceDirs = arrayOf(
102+
"src/commonMain",
103+
"src/jvmMain"
104+
)
105+
106+
val classFiles = File("${buildDir}/classes/kotlin/jvm/")
107+
.walkBottomUp()
108+
.toSet()
109+
.filter { it.isFile }
110+
.filterNot {
111+
val fileNamePath = it.absolutePath
112+
val dir = fileNamePath.substring(0, fileNamePath.lastIndexOf(File.separator))
113+
dir.contains("com/vhl/blackmo/grass/data")
114+
}
115+
116+
classDirectories.setFrom(classFiles)
117+
sourceDirectories.setFrom(files(coverageSourceDirs))
118+
additionalSourceDirs.setFrom(files(coverageSourceDirs))
119+
120+
121+
executionData
122+
.setFrom(files("${buildDir}/jacoco/jvmTest.exec"))
123+
124+
reports {
125+
xml.isEnabled = true
126+
html.isEnabled = false
127+
}
128+
}

docs/CustomDataType.MD

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Custom Data Type
2+
3+
### Creating Your Custom Data Type
4+
5+
1. Add kotlin grass core to your project dependencies
6+
`implementation("com.vhl.blackmo:kotlin-grass-core-jvm:1.0.0")`
7+
2. implement `Datatype` interface to your custom `data type class` <br />
8+
a. Create Necessary Transform Functions. This `transform function` will handle the conversion of
9+
string values into desired data type
10+
11+
b. Transform function must take into the form of:
12+
13+
`fun fx(value: String): Any?`
14+
15+
where the return is the desired value of data type
16+
17+
c. Add the mapping into the `mapTypes` including the null value
18+
19+
```kotlin
20+
object Java8DateTime: DateTimeTypes() {
21+
// for this example DateTimeTypes extends DataTypes
22+
// transforms functions
23+
override val formatDate = fun (value: String): Any? {
24+
val dateFormatter = DateTimeFormatter.ofPattern(DateTimeFormats.dateFormat)
25+
return LocalDate.parse(value, dateFormatter)
26+
}
27+
28+
// transforms functions
29+
override val formatDateTime = fun(value: String): Any? {
30+
val dateTimeFormatter = DateTimeFormatter.ofPattern("${DateTimeFormats.dateFormat}${DateTimeFormats.dateTimeSeparator}${DateTimeFormats.timeFormat}")
31+
return LocalDateTime.parse(value, dateTimeFormatter)
32+
}
33+
34+
// transforms functions
35+
override val formatTime = fun(value: String): Any? {
36+
val timeFormatter = DateTimeFormatter.ofPattern(DateTimeFormats.timeFormat)
37+
return LocalTime.parse(value, timeFormatter)
38+
}
39+
40+
override val mapTypes = mapOf(
41+
typeOf<LocalDate>() to formatDate,
42+
typeOf<LocalDateTime>() to formatDateTime,
43+
typeOf<LocalTime>() to formatTime,
44+
//-- nullable types
45+
typeOf<LocalDate?>() to formatDate,
46+
typeOf<LocalDateTime?>() to formatDateTime,
47+
typeOf<LocalTime?>() to formatTime
48+
)
49+
}
50+
```
51+
52+
3. Declare you Custom Data Type Handler
53+
```kotlin
54+
55+
val actual = grass<DateTimeOnly> {
56+
dateFormat = "MM-dd-yyyy"
57+
timeFormat = "HH:mm:ss"
58+
dateTimeSeparator = "/"
59+
customDataTypes = arrayListOf(Java8DateTime)
60+
}.harvest(contents).first()
61+
```

kotlin-grass-core/build.gradle.kts

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
plugins {
2+
java
3+
kotlin("multiplatform")
4+
id("org.jetbrains.dokka")
5+
`maven-publish`
6+
signing
7+
jacoco
8+
}
9+
10+
val kotlinVersion = "1.4.32"
11+
val csvVersion = "0.15.2"
12+
val coroutineVersion = "1.4.3"
13+
14+
group = "io.github.blackmo18"
15+
version = "0.7.1"
16+
17+
buildscript {
18+
repositories {
19+
mavenCentral()
20+
}
21+
dependencies {
22+
classpath("org.jetbrains.dokka:dokka-gradle-plugin:0.9.18")
23+
}
24+
}
25+
26+
repositories {
27+
mavenCentral()
28+
}
29+
30+
val dokkaJar = task<Jar>("dokkaJar") {
31+
group = JavaBasePlugin.DOCUMENTATION_GROUP
32+
archiveClassifier.set("javadoc")
33+
}
34+
val sourceJar = task<Jar>("sourceJar") {
35+
archiveClassifier.set("sources")
36+
from(sourceSets.getByName("main").allSource)
37+
}
38+
val metadata = task<Jar>("metadata") {
39+
archiveClassifier.set("metadata")
40+
from(sourceSets.getByName("main").allSource)
41+
}
42+
43+
val jvm = task<Jar>("jvm") {
44+
archiveClassifier.set("jvm")
45+
from(sourceSets.getByName("main").allSource)
46+
}
47+
48+
kotlin {
49+
jvm {
50+
compilations.all {
51+
kotlinOptions {
52+
jvmTarget = "1.8"
53+
noReflect = false
54+
}
55+
}
56+
mavenPublication {
57+
artifact(dokkaJar)
58+
}
59+
}
60+
61+
sourceSets {
62+
val commonMain by getting {
63+
dependencies {
64+
implementation(kotlin("test"))
65+
implementation(kotlin("stdlib-common"))
66+
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
67+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
68+
}
69+
}
70+
val commonTest by getting {
71+
dependencies {
72+
implementation(kotlin("test-annotations-common"))
73+
}
74+
}
75+
jvm().compilations["main"].defaultSourceSet {
76+
dependencies {
77+
implementation(kotlin("stdlib-common"))
78+
}
79+
}
80+
jvm().compilations["test"].defaultSourceSet {
81+
this.
82+
dependencies {
83+
implementation(kotlin("test-junit"))
84+
implementation("io.kotlintest:kotlintest-runner-junit5:3.3.2")
85+
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:$csvVersion")
86+
}
87+
}
88+
}
89+
}
90+
91+
92+
93+
publishing {
94+
publications.all {
95+
(this as MavenPublication).pom {
96+
name.set("kotlin-grass-core")
97+
description.set("Core components for Csv File to Kotlin Data Class Parser")
98+
url.set("https://github.com/blackmo18/kotlin-grass")
99+
100+
organization {
101+
name.set("io.github.blackmo18")
102+
url.set("https://github.com/blackmo18")
103+
}
104+
licenses {
105+
license {
106+
name.set("Apache License 2.0")
107+
url.set("https://github.com/blackmo18/kotlin-grass/blob/master/LICENSE")
108+
}
109+
}
110+
scm {
111+
url.set("https://github.com/blackmo18/kotlin-grass")
112+
connection.set("scm:git:git://github.com/blackmo18/kotlin-grass.git")
113+
developerConnection.set("https://github.com/blackmo18/kotlin-grass")
114+
}
115+
developers {
116+
developer {
117+
name.set("blackmo18")
118+
}
119+
}
120+
}
121+
}
122+
repositories {
123+
maven {
124+
credentials {
125+
val nexusUsername: String? by project
126+
val nexusPassword: String? by project
127+
username = nexusUsername
128+
password = nexusPassword
129+
}
130+
131+
val releasesRepoUrl = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
132+
val snapshotsRepoUrl = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
133+
url = if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl
134+
}
135+
}
136+
}
137+
138+
signing {
139+
sign(publishing.publications)
140+
}
141+
142+
143+
val jvmTest by tasks.getting(Test::class) {
144+
useJUnitPlatform { }
145+
}
146+
147+
jacoco {
148+
toolVersion = "0.8.5"
149+
}
150+
151+
tasks.jacocoTestReport {
152+
val coverageSourceDirs = arrayOf(
153+
"src/commonMain",
154+
"src/jvmMain"
155+
)
156+
157+
val classFiles = File("${buildDir}/classes/kotlin/jvm/")
158+
.walkBottomUp()
159+
.toSet()
160+
.filter { it.isFile }
161+
.filterNot {
162+
val fileNamePath = it.absolutePath
163+
val dir = fileNamePath.substring(0, fileNamePath.lastIndexOf(File.separator))
164+
dir.contains("io/github/blackmo18/grass/core/PrimitiveType.kt")
165+
}
166+
167+
classDirectories.setFrom(classFiles)
168+
sourceDirectories.setFrom(files(coverageSourceDirs))
169+
additionalSourceDirs.setFrom(files(coverageSourceDirs))
170+
171+
executionData
172+
.setFrom(files("${buildDir}/jacoco/jvmTest.exec"))
173+
174+
reports {
175+
xml.isEnabled = true
176+
html.isEnabled = true
177+
}
178+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = "kotlin-grass-core"

0 commit comments

Comments
 (0)