Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform "0" to boolean #53

Open
ilijaljubicic opened this issue Mar 27, 2017 · 1 comment
Open

transform "0" to boolean #53

ilijaljubicic opened this issue Mar 27, 2017 · 1 comment

Comments

@ilijaljubicic
Copy link

Hello

I have a case where json has keys with boolean values encoded as strings "0" and "1".
It is from external system and cannot influence it.

Did implement coercer and custom type, and managed to get
coerced values from "0" or "1" to IntBoolean(false) and IntBoolean(true)
but cannot manged to get those values with record.data()
nor to get them into avro.

Simple example of what I did is

IntoBooleanRecord.courier

namespace test
record IntBooleanRecord {
  key : IntBoolean
}

IntBooleanCoercer.scala

package test

import com.linkedin.data.template.{Custom, DirectCoercer}

case class IntBoolean(value: Boolean) extends AnyVal

class IntBooleanCoercer extends DirectCoercer[IntBoolean] {

  override def coerceInput(obj: IntBoolean): AnyRef = {
    Boolean.box(obj.value)
  }

  override def coerceOutput(obj: Any): IntBoolean = {
    obj match {
      case value: String =>
        if (value =="0") {
          IntBoolean(false)
        }else if (value =="1") {
            IntBoolean(true)
        } else {
          throw new IllegalArgumentException(s"$value is not 0 or 1")
        }
      case _: Any =>
        throw new IllegalArgumentException(
          s"Field must be string with value 0 or 1, but was ${obj.getClass}"
        )
    }
  }
}

object IntBooleanCoercer {
  registerCoercer()
  def registerCoercer(): Unit = {
    Custom.registerCoercer(new IntBooleanCoercer, classOf[IntBoolean])
  }
}

IntBoolean.courier

namespace test

@scala.class = "test.IntBoolean"
@scala.coercerClass = "test.IntBooleanCoercer"
typeref IntBoolean = boolean

scala code to test

val json=

        """{
        |     "key": "1"
        |}""".stripMargin


  val dataMap = DataTemplates.readDataMap(json)
  val record=IntBooleanRecord(dataMap,DataConversion.SetReadOnly)

  println(record) // IntBooleanRecord(IntBoolean(true))
  println(record.data()) // {key=1}

I was expecting for record.data() to output {key=true}

Is there something I am doing wrong or this is not supposed to function this way?
if not, what would be the way to do it?
Help would be appreciated.

@ilijaljubicic
Copy link
Author

why I think there is a bugs there is because when extracting avro schema from a record
this is output:
{"type":"record","name":"IntBooleanRecord","namespace":"test","fields":[{"name":"key","type":"boolean"}]}

thus avro schema matches what I would expect record.data to yield

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant