<commit_before>package twentysix.rest

import play.core.Router
import play.api.mvc.EssentialAction
import scala.runtime.AbstractPartialFunction
import play.api.mvc.RequestHeader
import play.api.mvc.Handler
import play.api.mvc.Controller
import play.api.Logger

object ResourceCaps extends Enumeration {
  type ResourceCaps = Value
  val Identity, Read, Write, Create, Delete, Update, Parent, Child, Action = Value
}

trait Resource{
  var caps = ResourceCaps.ValueSet.empty
  def name: String
}

case class ResourceAction(name: String, method: String) extends Resource {
  caps += ResourceCaps.Action
}

/**
 * Define the conversion from an url id to a real object
 */
trait IdentifiedResource[R] extends Resource{
  caps += ResourceCaps.Identity

  def toNumber[N](id: String, f: String => N): Option[N] = {
    import scala.util.control.Exception._
    catching(classOf[NumberFormatException]) opt f(id)
  }

  def toInt(id: String) = toNumber(id, _.toInt)
  def toLong(id: String) = toNumber(id, _.toLong)

  def fromId(id: String): Option[R]

  def requestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = block(sid)
}

/**
 * Respond to HTTP GET method
 */
trait ResourceRead[R] extends IdentifiedResource[R] {
  caps+=ResourceCaps.Read

  def read(id: R): EssentialAction
  def list(): EssentialAction

  def readRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}

/**
 * Respond to HTTP PUT method
 */
trait ResourceWrite[R] extends IdentifiedResource[R]{
  caps+=ResourceCaps.Write

  def write(id: R): EssentialAction

  def writeRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}

/**
 * Respond to HTTP POST method
 */
trait ResourceCreate extends Resource{
  caps+=ResourceCaps.Create

  def create(): EssentialAction
}

/**
 * Respond to HTTP DELETE method
 */
trait ResourceDelete[R] extends IdentifiedResource[R]{
  caps+=ResourceCaps.Delete

  def delete(id: R): EssentialAction

  def deleteRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}

/**
 * Respond to HTTP PATCH method
 */
trait ResourceUpdate[R] extends IdentifiedResource[R]{
  caps+=ResourceCaps.Update

  def update(id: R): EssentialAction

  def updateRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}


/**
 * Define link to other resources accessible via a sub paths
 */
trait ResourceRoutes[R] extends IdentifiedResource[R] {
  caps+=ResourceCaps.Parent

  def RouteMap = ResourceRouteMap[R]()
  val routeMap: ResourceRouteMap[R]
}

/**
 * Can create new instances tailored for a specific parent resource
 */
trait SubResource[P, S<:SubResource[P, S]] extends Resource {
  self: S =>
  caps+=ResourceCaps.Child

  def withParent(parentResource: P): S
}<commit_msg>Split resource controller traits in base trait with specific type and a generic version.<commit_after>package twentysix.rest

import play.core.Router
import play.api.mvc.EssentialAction
import scala.runtime.AbstractPartialFunction
import play.api.mvc.RequestHeader
import play.api.mvc.Handler
import play.api.mvc.Controller
import play.api.Logger

object ResourceCaps extends Enumeration {
  type ResourceCaps = Value
  val Identity, Read, Write, Create, Delete, Update, Parent, Child, Action = Value
}

trait Resource{
  var caps = ResourceCaps.ValueSet.empty
  def name: String
}

case class ResourceAction(name: String, method: String) extends Resource {
  caps += ResourceCaps.Action
}

/**
 * Define the conversion from an url id to a real object
 */
trait BaseIdentifiedResource extends Resource{
  type ResourceType
  caps += ResourceCaps.Identity

  def toNumber[N](id: String, f: String => N): Option[N] = {
    import scala.util.control.Exception._
    catching(classOf[NumberFormatException]) opt f(id)
  }

  def toInt(id: String) = toNumber(id, _.toInt)
  def toLong(id: String) = toNumber(id, _.toLong)

  def fromId(id: String): Option[ResourceType]

  def requestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = block(sid)
}
trait IdentifiedResource[R] extends BaseIdentifiedResource {
  type ResourceType = R
}


/**
 * Respond to HTTP GET method
 */
trait BaseResourceRead extends BaseIdentifiedResource {
  caps+=ResourceCaps.Read

  def read(id: ResourceType): EssentialAction
  def list(): EssentialAction

  def readRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}
trait ResourceRead[R] extends BaseResourceRead with IdentifiedResource[R]

/**
 * Respond to HTTP PUT method
 */
trait BaseResourceWrite extends BaseIdentifiedResource{
  caps+=ResourceCaps.Write

  def write(id: ResourceType): EssentialAction

  def writeRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}
trait ResourceWrite[R] extends BaseResourceWrite with IdentifiedResource[R]

/**
 * Respond to HTTP POST method
 */
trait ResourceCreate extends Resource{
  caps+=ResourceCaps.Create

  def create(): EssentialAction
}

/**
 * Respond to HTTP DELETE method
 */
trait BaseResourceDelete extends BaseIdentifiedResource{
  caps+=ResourceCaps.Delete

  def delete(id: ResourceType): EssentialAction

  def deleteRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}
trait ResourceDelete[R] extends BaseResourceDelete with IdentifiedResource[R]


/**
 * Respond to HTTP PATCH method
 */
trait BaseResourceUpdate extends BaseIdentifiedResource{
  caps+=ResourceCaps.Update

  def update(id: ResourceType): EssentialAction

  def updateRequestWrapper(sid: String, block: (String => Option[EssentialAction])): Option[EssentialAction] = requestWrapper(sid, block)
}
trait ResourceUpdate[R] extends BaseResourceUpdate with IdentifiedResource[R]



/**
 * Define link to other resources accessible via a sub paths
 */
trait BaseResourceRoutes extends BaseIdentifiedResource {
  caps+=ResourceCaps.Parent

  def RouteMap = ResourceRouteMap[ResourceType]()
  val routeMap: ResourceRouteMap[ResourceType]
}
trait ResourceRoutes[R] extends BaseResourceRoutes with IdentifiedResource[R]

/**
 * Can create new instances tailored for a specific parent resource
 */
trait SubResource[P, S<:SubResource[P, S]] extends Resource {
  self: S =>
  caps+=ResourceCaps.Child

  def withParent(parentResource: P): S
}
{'fraction_non_alphanumeric': 0.10304942166140904, 'fraction_numerical': 0.0, 'mean_word_length': 5.414258188824663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4223958', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1928, 'n_words': 562}