https://kotlinlang.org logo
n

nimakro

04/02/2017, 2:05 PM
Hey guy I was thinking about the
config
helper, this is what my first draft is looking like, I am open for any improvement suggestions or criticism. In my opinion we should have the concept of component wide settings and system wide settings (which don't use the
java.preferences
) and store them as json ( to allow hierarchical data structures). My first draft:
Copy code
// Defines the config context
enum class Context {
                    LOCAL,
                    SYSTEM
}

// The the default configBasePath is Paths.get("conf") but can be changed by overriding
// the path variable in App like so:
class MyApp : App(MyView::class) {
      override val configBase = Paths.get("custom-folder")
      // The system path has to be a relative path since it will wie placed beneath configBase
     // If you don't override val system the system settings will be stored under
     //  configBase/system.properties
      override val system = Paths.get("system")
 }
 
class MyView : View() {
    // All configs are stored under configBase;
    // The LOCAL context contains all the component depending configurations
    // config_base_path default is conf inside the current program folder.
    config(context: Context = LOCAL) {
     // ...
    }

     // The default context path is configBase/javaClass.name + ".properties"
     // So you can write the following to save to the component config
     config {
          set("username" to "user")
          set("password" to "pwd")
      }

      // The SYSTEM context contains all system wide configurations und configBase/system
      // The SYSTEM context can by changed by overriding the system Path variable
      config(SYSTEM) {
            set("username" to "user")
            set("password" to "pwd")
            set("window") to jsonObject {
                  set("x" to 30.0)
                  set("y" to 40.0)
                  set("list" to jsonArray(2, 3, 4))
             }
        }

        config(LOCAL) {
            val usr = string("username")
            val pwd = string("password")
         }

         config(SYSTEM) {
              val usr = string("username")
               val pwd = string("password")
               val x = double("x", 50.0)

               // We allow jsonPath to retrieve elements.
               val windowX = double("windwo.x")
                val listFirst = double("windwo.list[0]")
           }
 }