Giter Site home page Giter Site logo

Comments (4)

xerial avatar xerial commented on May 13, 2024

Could you give me more context?

from airframe.

0x1997 avatar 0x1997 commented on May 13, 2024
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.

xerial avatar xerial commented on May 13, 2024

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.

xerial avatar xerial commented on May 13, 2024

Using mutable JavaBeans is not a good practice in Scala. I think we will not address this.

from airframe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.