Skip to content

Commit

Permalink
type refactoring ( defualt value )
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Oct 10, 2024
1 parent 0eac70e commit e06296d
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 72 deletions.
28 changes: 4 additions & 24 deletions common/src/main/kotlin/org/sirius/common/type/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,19 @@ package org.sirius.common.type
* All rights reserved
*/

//TODOimport org.sirius.dorm.persistence.entity.EntityStatus



typealias Check<T> = (obj: T) -> Boolean

typealias DefaultValue<T> = () -> T

open class Type<T:Any>(val baseType: Class<T>) {
abstract class Type<T:Any>(val baseType: Class<T>) {
// instance data

val tests = ArrayList<Test<Any>>()
var sealed = false

val defaultValue : DefaultValue<T> = computeDefaultValue(baseType)

private fun <T> computeDefaultValue(type: Class<T>) : DefaultValue<T> {
return when (type) {
String::class.javaObjectType -> { -> "" as T }
Short::class.javaObjectType -> { -> 0.toShort() as T }
Integer::class.javaObjectType -> { -> 0 as T }
Int::class.javaObjectType -> { -> 0 as T }
Long::class.javaObjectType -> { -> 0L as T }
Float::class.javaObjectType -> { -> 0.0f as T }
Double::class.javaObjectType -> { -> 0.0 as T }
Boolean::class.javaObjectType -> { -> false as T }
Char::class.javaObjectType -> { -> ' ' as T }
//EntityStatus::class.javaObjectType -> { -> EntityStatus.NEW as T }
else -> {
throw Error("unsupported type ${type.simpleName}")
}
}
}
val defaultValue : DefaultValue<T> = computeDefaultValue()

abstract fun computeDefaultValue() : DefaultValue<T>

// init

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ package org.sirius.common.type.base
* All rights reserved
*/

import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class BooleanType : Type<Boolean>(Boolean::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<Boolean> {
return { -> false }
}

// fluent

fun isTrue() : BooleanType {
test<Boolean>(
"isTrue",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ package org.sirius.common.type.base
*
* All rights reserved
*/
import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class CharacterType : Type<Char>(Char::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<Char> {
return { -> ' ' }
}
}

fun character() : CharacterType {return CharacterType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ package org.sirius.common.type.base
*
* All rights reserved
*/
import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class DoubleType : Type<Double>(Double::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<Double> {
return { -> 0.0 }
}

fun min(min: Double) : DoubleType {
test<Double>(
"min",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ package org.sirius.common.type.base
*
* All rights reserved
*/
import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class FloatType : Type<Float>(Float::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<Float> {
return { -> 0.0f }
}

fun min(min: Float) : FloatType {
test<Float>(
"min",
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/kotlin/org/sirius/common/type/base/IntType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ package org.sirius.common.type.base
*
* All rights reserved
*/
import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class IntType : Type<Int>(Int::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<Int> {
return { -> 0 }
}

// fluent

fun min(min: Int) : IntType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ package org.sirius.common.type.base
*
* All rights reserved
*/
import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class LongType : Type<Long>(Long::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<Long> {
return { -> 0L }
}


fun min(min: Long) : LongType {
test<Long>(
"min",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ package org.sirius.common.type.base
*
* All rights reserved
*/
import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class ShortType : Type<Short>(Short::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<Short> {
return { -> 0 }
}

fun min(min: Short) : ShortType {
test<Short>(
"min",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ package org.sirius.common.type.base
*
* All rights reserved
*/
import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type

class StringType : Type<String>(String::class.java) {
// override Type

override fun computeDefaultValue() : DefaultValue<String> {
return { -> "" }
}

// fluent

fun length(length: Int/*, info?: ConstraintInfo*/) : StringType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sirius.dorm
package org.sirius.common
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sirius.dorm
package org.sirius.common
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
Expand Down
38 changes: 0 additions & 38 deletions common/src/test/resources/application.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package org.sirius.dorm.model
* All rights reserved
*/

import org.sirius.common.type.DefaultValue
import org.sirius.common.type.Type
import org.sirius.common.type.base.long
import org.sirius.dorm.ObjectManager
import org.sirius.dorm.persistence.entity.EntityStatus
import java.time.LocalDateTime


abstract class PropertyBuilder() {
Expand Down Expand Up @@ -124,7 +126,13 @@ fun relation(name: String) : RelationBuilder {
return RelationBuilder().name(name)
}

class StatusType : Type<EntityStatus>(EntityStatus::class.javaObjectType)
class StatusType : Type<EntityStatus>(EntityStatus::class.javaObjectType) {
// override Type

override fun computeDefaultValue() : DefaultValue<EntityStatus> {
return { -> EntityStatus(LocalDateTime.now(), "", LocalDateTime.now(), "") }
}
}

class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) {
// instance data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
=== TODO

- applikation
- maven submodule
- admin service ausplitten + finalisieren
- angular test

- sql sieht komisch aus
- doku
- fehler join graphql?
- benchmark?
- kopieren nach service :-)

- string + like ( oder startsWith, endsWith, contains, like )
- join?
- describe
Expand All @@ -21,12 +15,18 @@

=== LATER

angular :-) mit builder simple
- graphql benchmark?
- kopieren nach service :-)
- angular :-) mit builder simple

=== NOW

- default value

=== DONE

- applikation
- maven submodule
- date support in graphql ( scalar )
- session überlegen
- status
Expand Down

0 comments on commit e06296d

Please sign in to comment.