0

I want to map multiple types in a model class, which is being referred from controller and repository class.

For Example :

   case class ManagedService(
     ...some parameters,
     attributes: Seq[Attribute],
     ...etc
   ) extends RelatedResource {
     override def resourceId = name
     override def resourceType = "instance"
   }


   trait RelatedResource {
     def resourceId: Option[String]
     def resourceType: String
   }

Now, if I want to add override def resourceType = "memory" and override def resourceType = "readers" along with instance, how can I add them? This is to execute the URL with www.example.com/type=memory.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
AKDGP
  • 25
  • 9
  • @Scala Users, pls help me on this as i am really stuck on this work. dono how to proceed further – AKDGP Feb 08 '18 at 07:12

1 Answers1

2

You can just override the trait's methods as constructor val's in your case class as below:

  case class ManagedService(
     ...some parameters,
     resourceId: Option[String],
     resourceType: String
     attributes: Seq[Attribute],
     ...etc
   ) extends RelatedResource
joesan
  • 13,963
  • 27
  • 95
  • 232
  • Could you please elaborate on what you mean by creating a seperate type to call memory and reader from URL? – joesan Feb 08 '18 at 07:58
  • So you want to have some pattern matching for the type request parameter and based on it, you want to instantiate different case classes? Is that correct? If my understanding is correct, then this seems pretty simple. What are you stuck at? – joesan Feb 08 '18 at 08:04
  • Yes you are correct, I am trying to instantiate different case class. im stuck here , abstract class readerservice() extends RelatedResource{ override def resourceType = "reader" } – AKDGP Feb 08 '18 at 08:12
  • I would advice that you start simple. Have 2 case classes without any trait. Based on the incoming request parameter, instantiate one of them accordingly. You can later on move the common properties between the case classes into a sealed trait. – joesan Feb 08 '18 at 08:20
  • Thanks, I tried like this , but getting error in overriding case class StorageService( resourceType : String ) extends RelatedResource { override def resourceType = "storage" } – AKDGP Feb 08 '18 at 09:27