Comments (4)
Could you give me more context?
from airframe.
val config = Config(env = configEnv, configPaths = Seq(configPath))
.registerFromYaml[FooConfig]("foo.yml")
This doesn't work when FooConfig
is a JavaBean instead of a scala case class.
My workaround is like this
import java.io.{File, FileNotFoundException}
import scala.collection.JavaConverters._
import scala.reflect.runtime.universe._
import org.yaml.snakeyaml.{TypeDescription, Yaml}
import org.yaml.snakeyaml.constructor.Constructor
import wvlet.config.Config
import wvlet.log.LogSupport
import wvlet.log.io.IOUtil.readAsString
import wvlet.surface
object Yaml2JavaBean {
implicit class ConfigWithJavaBeanSupport(config: Config) extends LogSupport {
def registerJavaBeanFromYaml[T: TypeTag](yamlFile: String): Config = {
val env = config.env
val tpe = surface.of[T]
val realPath = env.configPaths
.map(p => new File(p, yamlFile))
.find(_.exists())
.map(_.getPath)
.getOrElse {
throw new FileNotFoundException(
s"$yamlFile is not found in ${env.configPaths.mkString(":")}")
}
val constructor = new Constructor()
constructor.addTypeDescription(
new TypeDescription(
typeTag[T].mirror.runtimeClass(typeOf[T].typeSymbol.asClass),
"!bean"))
val yaml = new Yaml(constructor)
val m = yaml
.loadAs(readAsString(realPath), classOf[java.util.Map[String, T]])
.asScala
val conf = m.get(env.env) match {
case Some(x) =>
info(s"Loading $tpe from $realPath, env:${env.env}")
x
case None =>
// Load default
debug(
s"Configuration for ${env.env} is not found in $realPath. Load ${env.defaultEnv} configuration instead")
m.get(env.defaultEnv) map { x =>
info(s"Loading $tpe from $realPath, default env:${env.defaultEnv}")
x
} getOrElse {
throw new IllegalArgumentException(
s"No configuration for $tpe (${env.env} or ${env.defaultEnv}) is found")
}
}
config.register[T](conf)
}
}
}
import Yaml2JavaBean._
val config = Config(env = configEnv, configPaths = Seq(configPath))
.registerJavaBeanFromYaml[FooConfig]("foo.yml")
where foo.yml
is like this
default: !bean
application:
name: foo-service
registry:
protocol: zookeeper
address: 10.10.0.1:2181,10.10.0.2:2182,10.10.0.3:2183
id: fooService
interface: com.foo.service.FooService
version: 1.0.0
timeout: 100
from airframe.
In #110, I've changed how to map Yaml -> Objects. Yaml data is converted to MessagePack binary data, and it is read in ObjectCodec (airframe-tablet). By adding MsgPack -> JavaBeans codec, it would be possible to support this type of binding in a more generic manner.
Alternatively simply adding your workaround to airframe-config might be convenient for you.
from airframe.
Using mutable JavaBeans is not a good practice in Scala. I think we will not address this.
from airframe.
Related Issues (20)
- http: Generic CORS filter
- airspec: Remove return type surface generation
- airspec: Bind test cases to lazy val to avoid too large method error in <init> HOT 1
- Drop JDK8 Support HOT 1
- di: Drop support for instantiating trait HOT 1
- rpc: Show a warning when RPC implementation is missing HOT 1
- http-netty: NoSuchMethodError
- http: Support reading http response with InputStream interface
- rpc: Scala 3 Enum causes illegal multithreaded access to ContextBase
- airspec: Support setting log level from the command line
- http-netty: an implementation is missing
- surface: Support intersection type in Scala 3
- parquet: Failed to write Map, ListMap data
- Milestone: Airframe 2024
- Surface for `Option` with a default value crashing with Scala 3 HOT 2
- Method surface for methods with multiple parameter lists fails with Scala 3
- Unable to get surface of a class with protected members in Scala 3
- http client: Record RPCException stack trace to http-client.json logs
- Surface.methodsOf - assertion failed: Tree had an unexpected owner for method HOT 1
- sbt-airframe: getNextTarEntry is deprecated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from airframe.