How to get map of field names to field types of case class with shapeless
up vote
0
down vote
favorite
I browsed pretty much all tutorials and have "The Type Astronauts" on my desk but I still can't figure out how to write snippet which can extract map of names to types using shapeless from case class. To be precise I'm after following interface:
case class Sample(id: String, date: LocalDate)
def sampleExpectedMetadata = expectedMetadata[Sample] // = ("id" -> "String", "date" -> "LocalDate")
def expectedMetadata[T]: Map[String, String] = ???
Help from anyone who can assist with writing correct implementation would be greatly appreciated.
scala generic-programming shapeless
add a comment |
up vote
0
down vote
favorite
I browsed pretty much all tutorials and have "The Type Astronauts" on my desk but I still can't figure out how to write snippet which can extract map of names to types using shapeless from case class. To be precise I'm after following interface:
case class Sample(id: String, date: LocalDate)
def sampleExpectedMetadata = expectedMetadata[Sample] // = ("id" -> "String", "date" -> "LocalDate")
def expectedMetadata[T]: Map[String, String] = ???
Help from anyone who can assist with writing correct implementation would be greatly appreciated.
scala generic-programming shapeless
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I browsed pretty much all tutorials and have "The Type Astronauts" on my desk but I still can't figure out how to write snippet which can extract map of names to types using shapeless from case class. To be precise I'm after following interface:
case class Sample(id: String, date: LocalDate)
def sampleExpectedMetadata = expectedMetadata[Sample] // = ("id" -> "String", "date" -> "LocalDate")
def expectedMetadata[T]: Map[String, String] = ???
Help from anyone who can assist with writing correct implementation would be greatly appreciated.
scala generic-programming shapeless
I browsed pretty much all tutorials and have "The Type Astronauts" on my desk but I still can't figure out how to write snippet which can extract map of names to types using shapeless from case class. To be precise I'm after following interface:
case class Sample(id: String, date: LocalDate)
def sampleExpectedMetadata = expectedMetadata[Sample] // = ("id" -> "String", "date" -> "LocalDate")
def expectedMetadata[T]: Map[String, String] = ???
Help from anyone who can assist with writing correct implementation would be greatly appreciated.
scala generic-programming shapeless
scala generic-programming shapeless
edited Nov 22 at 15:58
Daniel Hinojosa
83458
83458
asked Nov 22 at 2:09
Petro Semeniuk
4,82523454
4,82523454
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I could be wrong that Shapeless would have nothing to do with this sort of introspection, using the answer from Get field names list from case class and refining it, your answer would be:
import scala.reflect.runtime.universe._
def expectedMetadata[T](implicit t:TypeTag[T]):Map[String, String] =
t.tpe.members
.collect { case m:MethodSymbol if m.isCaseAccessor => m }
.map(x => x.name.toString -> x.returnType.toString)
.toMap
Where, expectedMetadata[Sample]
will return Map[String,String] = Map(date -> java.time.LocalDate, id -> String)
add a comment |
up vote
0
down vote
Did you have something like this in mind?
import shapeless._
import shapeless.record._
import shapeless.labelled._
object expectedMetadata {
def apply[T](implicit g: GetFieldTypes[T]): Map[String, String] = g.getFieldTypes
sealed trait GetFieldTypes[T] {
def getFieldTypes: Map[String,String]
}
implicit val hnil = new GetFieldTypes[HNil] {
def getFieldTypes = Map.empty
}
implicit def hcons[K <: Symbol, V, T <: HList](implicit
wit: Witness.Aux[K],
typ: Typeable[V],
rest: GetFieldTypes[T]
) = new GetFieldTypes[FieldType[K, V] :: T] {
def getFieldTypes = rest.getFieldTypes + (wit.value.name -> typ.describe)
}
implicit def caseClass[T, G](implicit
lg: LabelledGeneric.Aux[T, G],
rest: GetFieldTypes[G]
) = new GetFieldTypes[T] {
def getFieldTypes = rest.getFieldTypes
}
}
Sample use:
scala> case class Sample(id: String, date: LocalDate)
scala> expectedMetadata[Sample]
res1: Map[String,String] = Map(date -> LocalDate, id -> String)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I could be wrong that Shapeless would have nothing to do with this sort of introspection, using the answer from Get field names list from case class and refining it, your answer would be:
import scala.reflect.runtime.universe._
def expectedMetadata[T](implicit t:TypeTag[T]):Map[String, String] =
t.tpe.members
.collect { case m:MethodSymbol if m.isCaseAccessor => m }
.map(x => x.name.toString -> x.returnType.toString)
.toMap
Where, expectedMetadata[Sample]
will return Map[String,String] = Map(date -> java.time.LocalDate, id -> String)
add a comment |
up vote
0
down vote
I could be wrong that Shapeless would have nothing to do with this sort of introspection, using the answer from Get field names list from case class and refining it, your answer would be:
import scala.reflect.runtime.universe._
def expectedMetadata[T](implicit t:TypeTag[T]):Map[String, String] =
t.tpe.members
.collect { case m:MethodSymbol if m.isCaseAccessor => m }
.map(x => x.name.toString -> x.returnType.toString)
.toMap
Where, expectedMetadata[Sample]
will return Map[String,String] = Map(date -> java.time.LocalDate, id -> String)
add a comment |
up vote
0
down vote
up vote
0
down vote
I could be wrong that Shapeless would have nothing to do with this sort of introspection, using the answer from Get field names list from case class and refining it, your answer would be:
import scala.reflect.runtime.universe._
def expectedMetadata[T](implicit t:TypeTag[T]):Map[String, String] =
t.tpe.members
.collect { case m:MethodSymbol if m.isCaseAccessor => m }
.map(x => x.name.toString -> x.returnType.toString)
.toMap
Where, expectedMetadata[Sample]
will return Map[String,String] = Map(date -> java.time.LocalDate, id -> String)
I could be wrong that Shapeless would have nothing to do with this sort of introspection, using the answer from Get field names list from case class and refining it, your answer would be:
import scala.reflect.runtime.universe._
def expectedMetadata[T](implicit t:TypeTag[T]):Map[String, String] =
t.tpe.members
.collect { case m:MethodSymbol if m.isCaseAccessor => m }
.map(x => x.name.toString -> x.returnType.toString)
.toMap
Where, expectedMetadata[Sample]
will return Map[String,String] = Map(date -> java.time.LocalDate, id -> String)
answered Nov 22 at 16:56
Daniel Hinojosa
83458
83458
add a comment |
add a comment |
up vote
0
down vote
Did you have something like this in mind?
import shapeless._
import shapeless.record._
import shapeless.labelled._
object expectedMetadata {
def apply[T](implicit g: GetFieldTypes[T]): Map[String, String] = g.getFieldTypes
sealed trait GetFieldTypes[T] {
def getFieldTypes: Map[String,String]
}
implicit val hnil = new GetFieldTypes[HNil] {
def getFieldTypes = Map.empty
}
implicit def hcons[K <: Symbol, V, T <: HList](implicit
wit: Witness.Aux[K],
typ: Typeable[V],
rest: GetFieldTypes[T]
) = new GetFieldTypes[FieldType[K, V] :: T] {
def getFieldTypes = rest.getFieldTypes + (wit.value.name -> typ.describe)
}
implicit def caseClass[T, G](implicit
lg: LabelledGeneric.Aux[T, G],
rest: GetFieldTypes[G]
) = new GetFieldTypes[T] {
def getFieldTypes = rest.getFieldTypes
}
}
Sample use:
scala> case class Sample(id: String, date: LocalDate)
scala> expectedMetadata[Sample]
res1: Map[String,String] = Map(date -> LocalDate, id -> String)
add a comment |
up vote
0
down vote
Did you have something like this in mind?
import shapeless._
import shapeless.record._
import shapeless.labelled._
object expectedMetadata {
def apply[T](implicit g: GetFieldTypes[T]): Map[String, String] = g.getFieldTypes
sealed trait GetFieldTypes[T] {
def getFieldTypes: Map[String,String]
}
implicit val hnil = new GetFieldTypes[HNil] {
def getFieldTypes = Map.empty
}
implicit def hcons[K <: Symbol, V, T <: HList](implicit
wit: Witness.Aux[K],
typ: Typeable[V],
rest: GetFieldTypes[T]
) = new GetFieldTypes[FieldType[K, V] :: T] {
def getFieldTypes = rest.getFieldTypes + (wit.value.name -> typ.describe)
}
implicit def caseClass[T, G](implicit
lg: LabelledGeneric.Aux[T, G],
rest: GetFieldTypes[G]
) = new GetFieldTypes[T] {
def getFieldTypes = rest.getFieldTypes
}
}
Sample use:
scala> case class Sample(id: String, date: LocalDate)
scala> expectedMetadata[Sample]
res1: Map[String,String] = Map(date -> LocalDate, id -> String)
add a comment |
up vote
0
down vote
up vote
0
down vote
Did you have something like this in mind?
import shapeless._
import shapeless.record._
import shapeless.labelled._
object expectedMetadata {
def apply[T](implicit g: GetFieldTypes[T]): Map[String, String] = g.getFieldTypes
sealed trait GetFieldTypes[T] {
def getFieldTypes: Map[String,String]
}
implicit val hnil = new GetFieldTypes[HNil] {
def getFieldTypes = Map.empty
}
implicit def hcons[K <: Symbol, V, T <: HList](implicit
wit: Witness.Aux[K],
typ: Typeable[V],
rest: GetFieldTypes[T]
) = new GetFieldTypes[FieldType[K, V] :: T] {
def getFieldTypes = rest.getFieldTypes + (wit.value.name -> typ.describe)
}
implicit def caseClass[T, G](implicit
lg: LabelledGeneric.Aux[T, G],
rest: GetFieldTypes[G]
) = new GetFieldTypes[T] {
def getFieldTypes = rest.getFieldTypes
}
}
Sample use:
scala> case class Sample(id: String, date: LocalDate)
scala> expectedMetadata[Sample]
res1: Map[String,String] = Map(date -> LocalDate, id -> String)
Did you have something like this in mind?
import shapeless._
import shapeless.record._
import shapeless.labelled._
object expectedMetadata {
def apply[T](implicit g: GetFieldTypes[T]): Map[String, String] = g.getFieldTypes
sealed trait GetFieldTypes[T] {
def getFieldTypes: Map[String,String]
}
implicit val hnil = new GetFieldTypes[HNil] {
def getFieldTypes = Map.empty
}
implicit def hcons[K <: Symbol, V, T <: HList](implicit
wit: Witness.Aux[K],
typ: Typeable[V],
rest: GetFieldTypes[T]
) = new GetFieldTypes[FieldType[K, V] :: T] {
def getFieldTypes = rest.getFieldTypes + (wit.value.name -> typ.describe)
}
implicit def caseClass[T, G](implicit
lg: LabelledGeneric.Aux[T, G],
rest: GetFieldTypes[G]
) = new GetFieldTypes[T] {
def getFieldTypes = rest.getFieldTypes
}
}
Sample use:
scala> case class Sample(id: String, date: LocalDate)
scala> expectedMetadata[Sample]
res1: Map[String,String] = Map(date -> LocalDate, id -> String)
answered Nov 22 at 22:25
Alec
24k44487
24k44487
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422945%2fhow-to-get-map-of-field-names-to-field-types-of-case-class-with-shapeless%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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