How to get all config parameters from a .scala file ?












0















I want to call all the parameters from BeamConfig.scala in another scala class. The parameters stored in BeamConfig.scala are like below:



case class WarmStart(
enabled: scala.Boolean,
path: java.lang.String
)

object WarmStart {

def apply(c: com.typesafe.config.Config): BeamConfig.Beam.WarmStart = {
BeamConfig.Beam.WarmStart(
enabled = c.hasPathOrNull("enabled") && c.getBoolean("enabled"),
path = if (c.hasPathOrNull("path")) c.getString("path") else "output"
)
}
}


So There are more than 100 parameters object like above object in BeamConfig.scala. If I want to get the parameter from this file than I will do like this:



beam.warmStart.enable
beam.warmStart.path


Where beam is the root class.So is there any way so that i can call all the parameters in a bulk or I can store all the object in some Map or something else.
Thanks










share|improve this question



























    0















    I want to call all the parameters from BeamConfig.scala in another scala class. The parameters stored in BeamConfig.scala are like below:



    case class WarmStart(
    enabled: scala.Boolean,
    path: java.lang.String
    )

    object WarmStart {

    def apply(c: com.typesafe.config.Config): BeamConfig.Beam.WarmStart = {
    BeamConfig.Beam.WarmStart(
    enabled = c.hasPathOrNull("enabled") && c.getBoolean("enabled"),
    path = if (c.hasPathOrNull("path")) c.getString("path") else "output"
    )
    }
    }


    So There are more than 100 parameters object like above object in BeamConfig.scala. If I want to get the parameter from this file than I will do like this:



    beam.warmStart.enable
    beam.warmStart.path


    Where beam is the root class.So is there any way so that i can call all the parameters in a bulk or I can store all the object in some Map or something else.
    Thanks










    share|improve this question

























      0












      0








      0








      I want to call all the parameters from BeamConfig.scala in another scala class. The parameters stored in BeamConfig.scala are like below:



      case class WarmStart(
      enabled: scala.Boolean,
      path: java.lang.String
      )

      object WarmStart {

      def apply(c: com.typesafe.config.Config): BeamConfig.Beam.WarmStart = {
      BeamConfig.Beam.WarmStart(
      enabled = c.hasPathOrNull("enabled") && c.getBoolean("enabled"),
      path = if (c.hasPathOrNull("path")) c.getString("path") else "output"
      )
      }
      }


      So There are more than 100 parameters object like above object in BeamConfig.scala. If I want to get the parameter from this file than I will do like this:



      beam.warmStart.enable
      beam.warmStart.path


      Where beam is the root class.So is there any way so that i can call all the parameters in a bulk or I can store all the object in some Map or something else.
      Thanks










      share|improve this question














      I want to call all the parameters from BeamConfig.scala in another scala class. The parameters stored in BeamConfig.scala are like below:



      case class WarmStart(
      enabled: scala.Boolean,
      path: java.lang.String
      )

      object WarmStart {

      def apply(c: com.typesafe.config.Config): BeamConfig.Beam.WarmStart = {
      BeamConfig.Beam.WarmStart(
      enabled = c.hasPathOrNull("enabled") && c.getBoolean("enabled"),
      path = if (c.hasPathOrNull("path")) c.getString("path") else "output"
      )
      }
      }


      So There are more than 100 parameters object like above object in BeamConfig.scala. If I want to get the parameter from this file than I will do like this:



      beam.warmStart.enable
      beam.warmStart.path


      Where beam is the root class.So is there any way so that i can call all the parameters in a bulk or I can store all the object in some Map or something else.
      Thanks







      scala object config






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 9:28









      AyushAyush

      1418




      1418
























          2 Answers
          2






          active

          oldest

          votes


















          1














          there's a couple different ways you could do this:



          Using Typesafe Config in a somewhat unsafe-ish manner:
          https://github.com/lightbend/config#api-example



          This would give you map-like access but it can very easily explode if the names are wrong, types don't line up etc.



          Using PureConfig (a wrapper around typesafe config which allows automatic derivation of case class based config decoders, kinda like circe for json)
          https://pureconfig.github.io/docs/



          So you'd have to write you large caseclass with 100 fields once but you have a safe decoding of config into that case class and after that you have normal named properties with their correct types.



          (Note that this will lose you invariance under rename refactor)






          share|improve this answer
























          • thanks @Dominic

            – Ayush
            Nov 20 '18 at 12:57





















          0














          Firstly, I would separate the code that reads the config from the code that processes the results. In this case the default value "output" is embedded in the code that reads the config when it should probably be done in a separate pass.



          Secondly, I would use a package to automatically populate a case class from a config entry. You then need one line per config object, and you get the results checked for you. E.g.



          object BeamConfig {
          val warmStart = Config[WarmStart]("warmStart")
          val config2 = Config[Config2]("config2")
          ...
          }


          If you need some processing you can do this



          val warmStart = ProcessWarmStart(Config[WarmStart]("warmStart"))


          This approach still requires a bit of boiler plate code, but it has better type safety than a bulk import of the config.



          I would also consider combining the objects into fewer, nested objects with matching nested case classes.





          Here is a cut-down version of Config using json4s and jackson:



          import com.typesafe.config._
          import org.json4s._
          import org.json4s.jackson.JsonMethods._

          object Config {
          private val cfgFile = "configFile"
          private val conf = ConfigFactory.load(cfgFile).withFallback(ConfigFactory.load())
          private val jData = parse(conf.root.render(ConfigRenderOptions.concise))

          def apply[T](name: String)(implicit formats: Formats = DefaultFormats, mf: Manifest[T]): T =
          Extraction.extract(jData \ name)(formats, mf)
          }


          This will throw an exception if the particular config object does not exist or does not match the format of class T.






          share|improve this answer
























          • thanks @tim for your answer

            – Ayush
            Nov 20 '18 at 12:58











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389913%2fhow-to-get-all-config-parameters-from-a-scala-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          there's a couple different ways you could do this:



          Using Typesafe Config in a somewhat unsafe-ish manner:
          https://github.com/lightbend/config#api-example



          This would give you map-like access but it can very easily explode if the names are wrong, types don't line up etc.



          Using PureConfig (a wrapper around typesafe config which allows automatic derivation of case class based config decoders, kinda like circe for json)
          https://pureconfig.github.io/docs/



          So you'd have to write you large caseclass with 100 fields once but you have a safe decoding of config into that case class and after that you have normal named properties with their correct types.



          (Note that this will lose you invariance under rename refactor)






          share|improve this answer
























          • thanks @Dominic

            – Ayush
            Nov 20 '18 at 12:57


















          1














          there's a couple different ways you could do this:



          Using Typesafe Config in a somewhat unsafe-ish manner:
          https://github.com/lightbend/config#api-example



          This would give you map-like access but it can very easily explode if the names are wrong, types don't line up etc.



          Using PureConfig (a wrapper around typesafe config which allows automatic derivation of case class based config decoders, kinda like circe for json)
          https://pureconfig.github.io/docs/



          So you'd have to write you large caseclass with 100 fields once but you have a safe decoding of config into that case class and after that you have normal named properties with their correct types.



          (Note that this will lose you invariance under rename refactor)






          share|improve this answer
























          • thanks @Dominic

            – Ayush
            Nov 20 '18 at 12:57
















          1












          1








          1







          there's a couple different ways you could do this:



          Using Typesafe Config in a somewhat unsafe-ish manner:
          https://github.com/lightbend/config#api-example



          This would give you map-like access but it can very easily explode if the names are wrong, types don't line up etc.



          Using PureConfig (a wrapper around typesafe config which allows automatic derivation of case class based config decoders, kinda like circe for json)
          https://pureconfig.github.io/docs/



          So you'd have to write you large caseclass with 100 fields once but you have a safe decoding of config into that case class and after that you have normal named properties with their correct types.



          (Note that this will lose you invariance under rename refactor)






          share|improve this answer













          there's a couple different ways you could do this:



          Using Typesafe Config in a somewhat unsafe-ish manner:
          https://github.com/lightbend/config#api-example



          This would give you map-like access but it can very easily explode if the names are wrong, types don't line up etc.



          Using PureConfig (a wrapper around typesafe config which allows automatic derivation of case class based config decoders, kinda like circe for json)
          https://pureconfig.github.io/docs/



          So you'd have to write you large caseclass with 100 fields once but you have a safe decoding of config into that case class and after that you have normal named properties with their correct types.



          (Note that this will lose you invariance under rename refactor)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 9:48









          Dominic EggerDominic Egger

          78317




          78317













          • thanks @Dominic

            – Ayush
            Nov 20 '18 at 12:57





















          • thanks @Dominic

            – Ayush
            Nov 20 '18 at 12:57



















          thanks @Dominic

          – Ayush
          Nov 20 '18 at 12:57







          thanks @Dominic

          – Ayush
          Nov 20 '18 at 12:57















          0














          Firstly, I would separate the code that reads the config from the code that processes the results. In this case the default value "output" is embedded in the code that reads the config when it should probably be done in a separate pass.



          Secondly, I would use a package to automatically populate a case class from a config entry. You then need one line per config object, and you get the results checked for you. E.g.



          object BeamConfig {
          val warmStart = Config[WarmStart]("warmStart")
          val config2 = Config[Config2]("config2")
          ...
          }


          If you need some processing you can do this



          val warmStart = ProcessWarmStart(Config[WarmStart]("warmStart"))


          This approach still requires a bit of boiler plate code, but it has better type safety than a bulk import of the config.



          I would also consider combining the objects into fewer, nested objects with matching nested case classes.





          Here is a cut-down version of Config using json4s and jackson:



          import com.typesafe.config._
          import org.json4s._
          import org.json4s.jackson.JsonMethods._

          object Config {
          private val cfgFile = "configFile"
          private val conf = ConfigFactory.load(cfgFile).withFallback(ConfigFactory.load())
          private val jData = parse(conf.root.render(ConfigRenderOptions.concise))

          def apply[T](name: String)(implicit formats: Formats = DefaultFormats, mf: Manifest[T]): T =
          Extraction.extract(jData \ name)(formats, mf)
          }


          This will throw an exception if the particular config object does not exist or does not match the format of class T.






          share|improve this answer
























          • thanks @tim for your answer

            – Ayush
            Nov 20 '18 at 12:58
















          0














          Firstly, I would separate the code that reads the config from the code that processes the results. In this case the default value "output" is embedded in the code that reads the config when it should probably be done in a separate pass.



          Secondly, I would use a package to automatically populate a case class from a config entry. You then need one line per config object, and you get the results checked for you. E.g.



          object BeamConfig {
          val warmStart = Config[WarmStart]("warmStart")
          val config2 = Config[Config2]("config2")
          ...
          }


          If you need some processing you can do this



          val warmStart = ProcessWarmStart(Config[WarmStart]("warmStart"))


          This approach still requires a bit of boiler plate code, but it has better type safety than a bulk import of the config.



          I would also consider combining the objects into fewer, nested objects with matching nested case classes.





          Here is a cut-down version of Config using json4s and jackson:



          import com.typesafe.config._
          import org.json4s._
          import org.json4s.jackson.JsonMethods._

          object Config {
          private val cfgFile = "configFile"
          private val conf = ConfigFactory.load(cfgFile).withFallback(ConfigFactory.load())
          private val jData = parse(conf.root.render(ConfigRenderOptions.concise))

          def apply[T](name: String)(implicit formats: Formats = DefaultFormats, mf: Manifest[T]): T =
          Extraction.extract(jData \ name)(formats, mf)
          }


          This will throw an exception if the particular config object does not exist or does not match the format of class T.






          share|improve this answer
























          • thanks @tim for your answer

            – Ayush
            Nov 20 '18 at 12:58














          0












          0








          0







          Firstly, I would separate the code that reads the config from the code that processes the results. In this case the default value "output" is embedded in the code that reads the config when it should probably be done in a separate pass.



          Secondly, I would use a package to automatically populate a case class from a config entry. You then need one line per config object, and you get the results checked for you. E.g.



          object BeamConfig {
          val warmStart = Config[WarmStart]("warmStart")
          val config2 = Config[Config2]("config2")
          ...
          }


          If you need some processing you can do this



          val warmStart = ProcessWarmStart(Config[WarmStart]("warmStart"))


          This approach still requires a bit of boiler plate code, but it has better type safety than a bulk import of the config.



          I would also consider combining the objects into fewer, nested objects with matching nested case classes.





          Here is a cut-down version of Config using json4s and jackson:



          import com.typesafe.config._
          import org.json4s._
          import org.json4s.jackson.JsonMethods._

          object Config {
          private val cfgFile = "configFile"
          private val conf = ConfigFactory.load(cfgFile).withFallback(ConfigFactory.load())
          private val jData = parse(conf.root.render(ConfigRenderOptions.concise))

          def apply[T](name: String)(implicit formats: Formats = DefaultFormats, mf: Manifest[T]): T =
          Extraction.extract(jData \ name)(formats, mf)
          }


          This will throw an exception if the particular config object does not exist or does not match the format of class T.






          share|improve this answer













          Firstly, I would separate the code that reads the config from the code that processes the results. In this case the default value "output" is embedded in the code that reads the config when it should probably be done in a separate pass.



          Secondly, I would use a package to automatically populate a case class from a config entry. You then need one line per config object, and you get the results checked for you. E.g.



          object BeamConfig {
          val warmStart = Config[WarmStart]("warmStart")
          val config2 = Config[Config2]("config2")
          ...
          }


          If you need some processing you can do this



          val warmStart = ProcessWarmStart(Config[WarmStart]("warmStart"))


          This approach still requires a bit of boiler plate code, but it has better type safety than a bulk import of the config.



          I would also consider combining the objects into fewer, nested objects with matching nested case classes.





          Here is a cut-down version of Config using json4s and jackson:



          import com.typesafe.config._
          import org.json4s._
          import org.json4s.jackson.JsonMethods._

          object Config {
          private val cfgFile = "configFile"
          private val conf = ConfigFactory.load(cfgFile).withFallback(ConfigFactory.load())
          private val jData = parse(conf.root.render(ConfigRenderOptions.concise))

          def apply[T](name: String)(implicit formats: Formats = DefaultFormats, mf: Manifest[T]): T =
          Extraction.extract(jData \ name)(formats, mf)
          }


          This will throw an exception if the particular config object does not exist or does not match the format of class T.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 10:45









          TimTim

          4,9291317




          4,9291317













          • thanks @tim for your answer

            – Ayush
            Nov 20 '18 at 12:58



















          • thanks @tim for your answer

            – Ayush
            Nov 20 '18 at 12:58

















          thanks @tim for your answer

          – Ayush
          Nov 20 '18 at 12:58





          thanks @tim for your answer

          – Ayush
          Nov 20 '18 at 12:58


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389913%2fhow-to-get-all-config-parameters-from-a-scala-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith