package com.twitter.inject.app.internal

import com.google.inject.Key
import com.twitter.finatra.annotations.FlagImpl
import com.twitter.inject.{TwitterModule, Logging}

object FlagsModule {
  def create(flags: Seq[com.twitter.app.Flag[_]]) = {
    val flagsMap = (for (flag <- flags) yield {
      flag.name -> flag.getWithDefault
    }).toMap

    new FlagsModule(flagsMap)
  }
}

//TODO: Use type information in Flag instead of hardcoding java.lang.String
class FlagsModule(
  flagsMap: Map[String, Option[Any]])
  extends TwitterModule
  with Logging {

  override def configure() {
    for ((flagName, valueOpt) <- flagsMap) {
      valueOpt match {
        case Some(value) =>
          debug("Binding flag: " + flagName + " = " + value)
          val key = Key.get(classOf[java.lang.String], new FlagImpl(flagName))
          binder.bind(key).toInstance(value.toString)
        case None =>
          warn("flag without default: " + flagName + " has an unspecified value and is not eligible for @Flag injection")
      }
    }
  }
}

package com.twitter.inject.app.internal

import com.google.inject.Key
import com.twitter.finatra.annotations.FlagImpl
import com.twitter.inject.{TwitterModule, Logging}

object FlagsModule {
  def create(flags: Seq[com.twitter.app.Flag[_]]) = {
    val flagsMap = (for (flag <- flags) yield {
      flag.name -> flag()
    }).toMap

    new FlagsModule(flagsMap)
  }
}

//TODO: Use type information in Flag instead of hardcoding java.lang.String
class FlagsModule(
  flagsMap: Map[String, Any])
  extends TwitterModule
  with Logging {

  override def configure() {
    for ((flagName, value) <- flagsMap) {
      debug("Binding flag: " + flagName + " = " + value)
      val key = Key.get(classOf[java.lang.String], new FlagImpl(flagName))
      binder.bind(key).toInstance(value.toString)
    }
  }
}

--- 
+++ 
@@ -7,7 +7,7 @@
 object FlagsModule {
   def create(flags: Seq[com.twitter.app.Flag[_]]) = {
     val flagsMap = (for (flag <- flags) yield {
-      flag.name -> flag()
+      flag.name -> flag.getWithDefault
     }).toMap
 
     new FlagsModule(flagsMap)
@@ -16,15 +16,20 @@
 
 //TODO: Use type information in Flag instead of hardcoding java.lang.String
 class FlagsModule(
-  flagsMap: Map[String, Any])
+  flagsMap: Map[String, Option[Any]])
   extends TwitterModule
   with Logging {
 
   override def configure() {
-    for ((flagName, value) <- flagsMap) {
-      debug("Binding flag: " + flagName + " = " + value)
-      val key = Key.get(classOf[java.lang.String], new FlagImpl(flagName))
-      binder.bind(key).toInstance(value.toString)
+    for ((flagName, valueOpt) <- flagsMap) {
+      valueOpt match {
+        case Some(value) =>
+          debug("Binding flag: " + flagName + " = " + value)
+          val key = Key.get(classOf[java.lang.String], new FlagImpl(flagName))
+          binder.bind(key).toInstance(value.toString)
+        case None =>
+          warn("flag without default: " + flagName + " has an unspecified value and is not eligible for @Flag injection")
+      }
     }
   }
 }
Fix flagNotFound exception if flag doesn't have default value

RB_ID=661070
