I use https://pureconfig.github.io/ to load configuration values. For example for each table in a database, I store (db: String, table: String)
.However, I need to denote specific tables. Therefore, each one has a separate trait. I.e.:
trait Thing
trait ThingWithStuff extends Thing {
def value:String
}
trait FooThing extends Thing{
def fooThing: ThingWithStuff
}
trait BarThing extends Thing{
def barThing: ThingWithStuff
}
They all have a different attribute name with the same type which in return holds i.e. db
and table
. When processing these with some methods:
def myMethodFoo(thing:FooThing)= println(thing.fooThing)
def myMethodBar(thing:BarThing)= println(thing.barThing)
it leads to code duplication. Trying to fix these using generics I am not able to write a function like:
def myMethod[T<: Thing] = println(thing.thing)
as the attribute name would be different. Is there a smart way around it? Note:
table-first {
db = "a"
table = "b"
}
table-second {
db = "foo"
table = "baz"
}
cannot have the same identifier up front as otherwise it would overwrite each value to hold only the value of the last item for this identifier. Therefore, I resorted to use different attribute names (table-first, table-second
or specifically for the example: fooThing, barThing
)
How can I fix this issue to prevent code duplication?