private[twitter] def renderConstValue()

in scrooge-generator/src/main/scala/com/twitter/scrooge/java_generator/PrintConstController.scala [86:144]


  private[twitter] def renderConstValue(constant: RHS, fieldType: FieldType): ConstValue = {
    fieldType match {
      case at: AnnotatedFieldType => renderConstValue(constant, at.unwrap)
      case TString => {
        val constValue = constant.asInstanceOf[StringLiteral].value
        new ConstValue(null, "\"" + constValue + "\"")
      }
      case TBool => {
        constant match {
          case intValue: IntLiteral =>
            new ConstValue(null, if (intValue.value > 0) "true" else "false")
          case bool: BoolLiteral =>
            new ConstValue(null, if (bool.value) "true" else "false")
          case _ => throw new ScroogeInternalException("BoolType has invalid value: " + constant)
        }
      }
      case TByte => new ConstValue(null, "(byte)" + constant.asInstanceOf[IntLiteral].value)
      case TI16 => new ConstValue(null, "(short)" + constant.asInstanceOf[IntLiteral].value)
      case TI32 => new ConstValue(null, constant.asInstanceOf[IntLiteral].value.toString)
      case TI64 => new ConstValue(null, constant.asInstanceOf[IntLiteral].value + "L")
      case TDouble => {
        constant match {
          case DoubleLiteral(value) => {
            // TODO: this is here to match apache code but probably can be removed.
            if (value.floor == value) {
              new ConstValue(null, value.toInt.toString)
            } else {
              new ConstValue(null, value.toString)
            }
          }
          case IntLiteral(value) => new ConstValue(null, "(double)" + value.toString)
          case _ => throw new ScroogeInternalException("Invalid state renderConstValue")
        }
      }
      case EnumType(enumValue, scope) => {
        val ordinalValue = constant match {
          case intValue: IntLiteral => intValue.value.toInt
          case enumValue: EnumRHS => enumValue.value.value
          case _ => throw new ScroogeInternalException("Invalid state for renderConstValue")
        }
        val namedValue = enumValue.values filter { v => v.value == ordinalValue }
        if (namedValue.isEmpty) {
          throw new ScroogeInternalException("Enum value not found")
        } else {
          val enumFqn =
            generator.qualifyNamedType(enumValue.sid, scope)
          val enumValueFqn = namedValue(0).sid.addScope(enumFqn)
          new ConstValue(null, enumValueFqn.fullName)
        }
      }
      case _ => {
        val tmpVal = generator.tmp()
        new ConstValue(
          generator.printConstValue(tmpVal, fieldType, constant, ns, in_static = true),
          tmpVal
        )
      }
    }
  }