Skip to content

Commit

Permalink
gh-2: adds ktlint based linting
Browse files Browse the repository at this point in the history
  • Loading branch information
pmhsfelix committed Oct 3, 2022
1 parent 235f8d0 commit 7f64949
Show file tree
Hide file tree
Showing 24 changed files with 730 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.pipeline.filters

import com.example.demo.RequestScopedComponent
import com.example.demo.pipeline.handlerinterceptors.ExampleHandlerInterceptor
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
Expand All @@ -14,7 +15,9 @@ import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpServletResponseWrapper

@Component
class ResponseHeaderFilter : HttpFilter() {
class ResponseHeaderFilter(
val requestScopedComponent: RequestScopedComponent
) : HttpFilter() {

companion object {
private val logger = LoggerFactory.getLogger(ResponseHeaderFilter::class.java)
Expand All @@ -27,6 +30,7 @@ class ResponseHeaderFilter : HttpFilter() {
) {
val start = System.nanoTime()
val wrappedResponse = ResponseWrapper(response)
requestScopedComponent.path = request.requestURI
try {
chain.doFilter(request, wrappedResponse)
} finally {
Expand Down
628 changes: 628 additions & 0 deletions code/tic-tac-tow-service/.editorconfig

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions code/tic-tac-tow-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id("io.spring.dependency-management") version "1.0.14.RELEASE"
kotlin("jvm") version "1.6.21"
kotlin("plugin.spring") version "1.6.21"
java
}

group = "pt.isel.daw"
Expand All @@ -15,6 +16,8 @@ repositories {
mavenCentral()
}

val ktlint by configurations.creating

dependencies {
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
Expand All @@ -31,6 +34,8 @@ dependencies {

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation(kotlin("test"))

ktlint("com.pinterest:ktlint:0.47.1")
}

tasks.withType<KotlinCompile> {
Expand All @@ -43,3 +48,22 @@ tasks.withType<KotlinCompile> {
tasks.withType<Test> {
useJUnitPlatform()
}

// from https://pinterest.github.io/ktlint/install/integrations/#custom-gradle-integration-with-kotlin-dsl
val outputDir = "${project.buildDir}/reports/ktlint/"
val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt"))

val ktlintCheck by tasks.creating(JavaExec::class) {
inputs.files(inputFiles)
outputs.dir(outputDir)

description = "Check Kotlin code style."
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
args = listOf("src/**/*.kt")
}

tasks.named("check") {
dependsOn(ktlintCheck)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ interface Clock {
object RealClock : Clock {
// To only have second precision
override fun now(): Instant = Instant.ofEpochSecond(Instant.now().epochSecond)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import org.springframework.boot.runApplication
class TicTacTowApplication

fun main(args: Array<String>) {
runApplication<TicTacTowApplication>(*args)
}
runApplication<TicTacTowApplication>(*args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ data class Board(
) {
init {
require(
cells.size == 3 &&
cells.all { it.size == 3 }
cells.size == 3 && cells.all { it.size == 3 }
)
}

Expand All @@ -21,17 +20,19 @@ data class Board(
fun hasWon(state: State): Boolean {
require(state != State.EMPTY)
// TODO can be optimized
return cells.any { row -> row.all { it == state } }
|| (0..2).any { col -> cells.all { row -> row[col] == state } }
|| cells[0][0] == state && cells[1][1] == state && cells[2][2] == state
|| cells[0][2] == state && cells[1][1] == state && cells[2][0] == state
return cells.any { row -> row.all { it == state } } ||
(0..2).any { col -> cells.all { row -> row[col] == state } } ||
cells[0][0] == state && cells[1][1] == state && cells[2][2] == state ||
cells[0][2] == state && cells[1][1] == state && cells[2][0] == state
}

companion object {

fun create() = Board(Array(3) {
Array(3) { State.EMPTY }
})
fun create() = Board(
Array(3) {
Array(3) { State.EMPTY }
}
)

fun fromString(s: String): Board {
require(s.length == 9)
Expand Down Expand Up @@ -88,10 +89,9 @@ data class Board(
return true
}

fun isFull(): Boolean = cells.all { row -> row.all {it != State.EMPTY} }
fun isFull(): Boolean = cells.all { row -> row.all { it != State.EMPTY } }

override fun hashCode(): Int {
return cells.contentDeepHashCode()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ sealed class RoundResult {

private fun Game.isPlayerX(player: User) = this.playerX.id == player.id

private fun Game.isPlayerO(player: User) = this.playerO.id == player.id
private fun Game.isPlayerO(player: User) = this.playerO.id == player.id
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package pt.isel.daw.tictactow.domain

data class PasswordValidationInfo (
data class PasswordValidationInfo(
val validationInfo: String
)


)
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package pt.isel.daw.tictactow.domain
class Round(
val position: Position,
val player: User,
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pt.isel.daw.tictactow.domain
import java.security.SecureRandom
import java.util.*

class UserLogic{
class UserLogic {

fun generateToken(): String =
ByteArray(TOKEN_BYTE_SIZE).let { byteArray ->
Expand All @@ -24,4 +24,4 @@ class UserLogic{
companion object {
private const val TOKEN_BYTE_SIZE = 256 / 8
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import pt.isel.daw.tictactow.domain.Game
import java.util.UUID

interface GamesRepository {

fun insert(game: Game)
fun getById(id: UUID): Game?
fun update(game: Game)

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ interface Transaction {
val gamesRepository: GamesRepository

fun rollback()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,5 @@ class GameDbModel(
@Nested("playerO")
val playerO: User,
) {
fun toGame() = Game(
id, state, board, created, updated, deadline,
playerX, playerO
)
}
fun toGame() = Game(id, state, board, created, updated, deadline, playerX, playerO)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ class JdbiTransaction(
override fun rollback() {
handle.rollback()
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class JdbiTransactionManager(
val transaction = JdbiTransaction(handle)
block(transaction)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class JdbiUsersRepository(
.singleOrNull()

override fun storeUser(username: String, passwordValidation: PasswordValidationInfo): Boolean =
handle.createUpdate("insert into dbo.Users (username, password_validation) values (:username, :password_validation)")
handle.createUpdate(
"""
insert into dbo.Users (username, password_validation) values (:username, :password_validation)
"""
)
.bind("username", username)
.bind("password_validation", passwordValidation.validationInfo)
.execute() == 1
Expand All @@ -37,15 +41,16 @@ class JdbiUsersRepository(
}

override fun getUserByTokenValidationInfo(tokenValidationInfo: TokenValidationInfo): User? =
handle.createQuery("""
handle.createQuery(
"""
select id, username, password_validation
from dbo.Users as users
inner join dbo.Tokens as tokens
on users.id = tokens.user_id
where token_validation = :validation_information
""")
"""
)
.bind("validation_information", tokenValidationInfo.validationInfo)
.mapTo<User>()
.singleOrNull()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import java.time.Instant

class InstantMapper : ColumnMapper<Instant> {
override fun map(rs: ResultSet, columnNumber: Int, ctx: StatementContext): Instant {
return Instant.ofEpochSecond(rs.getLong(columnNumber))
return Instant.ofEpochSecond(rs.getLong(columnNumber))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class UsersService(
) {

fun createUser(username: String, password: String): UserCreationResult {

if (!userLogic.isSafePassword(password)) {
return Either.Left(UserCreationError.InsecurePassword)
}
Expand Down Expand Up @@ -64,7 +63,7 @@ class UsersService(
}

fun getUserByToken(token: String): User? {
if(!userLogic.canBeToken(token)) {
if (!userLogic.canBeToken(token)) {
return null
}
return transactionManager.run {
Expand All @@ -78,4 +77,4 @@ class UsersService(
passwordEncoder.encode("changeit")
return Either.Left(TokenCreationError.UserOrPasswordAreInvalid)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class TicTacTowApplicationTests {

@Test
fun contextLoads() {
}

}
@Test
fun contextLoads() {
}
}
Loading

0 comments on commit 7f64949

Please sign in to comment.