Code Monkey home page Code Monkey logo

object-schema's Introduction

object-schema Gitter Chat CircleCI Coverage Status

A library for inspecting Object schema (e.g., parameter names and its types, constructor, etc.)

Usage

Maven Central

libraryDependencies += "org.wvlet" %% "object-schema" % "(version)"
import wvlet.obj.ObjectSchema

case class Person(id:Int, name:String)
val scheam = ObjectSchema.of[Person]

The retrieved ObjectSchema has detailed information of your class, including:

  • Constructor and its argument types and names.
  • case class parameters
  • Methods

Extracting parameter names is impossible in Java programs, since JVM drops parameters names used in the source code at compile-time. While Scaal retains parameter names within the byte code of the class as ScalaSig. wvlet-obj reads ScalaSig and extract method parameter names and detailed types (e.g., type parameters used in Generics: e.g., String in List[String]).

Applications

Dependency injection

wvlet-obj is highly utilized in Airframe, wvlet-jmx to build programs by inspecting the shape of objects.

For example, in the following code wvlet-inject finds the constructor of MyApp class, then list dependencies (Module1, Module2, ...) that are necessary to build an MyApp instance:

class MyApp(m1:Module1, m2:Module2)

trait AppMain {
  val p = inject[MyApp]
}

Runtime code generation

In wvlet-jmx, @JMX annotation is used to register JMX parameter to JMX registry server so that we can monitor the behavior of the application outside JVM. You only need to aad @JMX annotation to start collecting JMX metrics:

case class Metrics(@JMX loadAvg:Double, @JMX memoryUsage:Double)

class MonitoringApp {
  @JMX def reportMetrics : Metrics = ...
}

wvlet-jmx checks the object schema of Metrics and registers its parameters to JMX registry.

Object mapping

wvlet-config reads Yaml files and binds the data into objects with wvlet-obj.

config.yml

default:
   timeout-millis: 100
   max-connection: 250

You can read this yaml file as follows:

case class ConnectionConfig(timeoutMillis:Long, maxConnection:Int)
val config = YamlReader.load[ConnectionConfig]("config.yml", "default")

Internally wvlet-obj inspects ConnectionConfig parameterss, and builds the config object by mapping property values written in the Yaml file.

object-schema's People

Contributors

xerial avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

object-schema's Issues

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.