Files
sbt/project/DatatypeConfig.scala
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.8 KiB
Scala
Raw Normal View History

import sbt.contraband.ast.*
2017-01-05 13:33:19 +00:00
import sbt.contraband.CodecCodeGen
2016-11-02 15:22:55 +00:00
object DatatypeConfig {
/** Extract the only type parameter from a TpeRef */
2017-01-05 13:33:19 +00:00
def oneArg(tpe: Type): Type = {
val pat = s"""${tpe.removeTypeParameters.name}[<\\[](.+?)[>\\]]""".r
2026-07-27 00:02:28 -04:00
val pat(arg0) = tpe.name.runtimeChecked
NamedType(arg0.split('.').toList)
2016-11-02 15:22:55 +00:00
}
/** Extract the two type parameters from a TpeRef */
2017-01-05 13:33:19 +00:00
def twoArgs(tpe: Type): List[Type] = {
val pat = s"""${tpe.removeTypeParameters.name}[<\\[](.+?), (.+?)[>\\]]""".r
2026-07-27 00:02:28 -04:00
val pat(arg0, arg1) = tpe.name.runtimeChecked
NamedType(arg0.split('.').toList) :: NamedType(arg1.split('.').toList) :: Nil
2016-11-02 15:22:55 +00:00
}
/** Codecs that were manually written. */
2017-01-05 13:33:19 +00:00
val myCodecs: PartialFunction[String, Type => List[String]] = {
2016-11-02 15:22:55 +00:00
case "scala.xml.NodeSeq" => { _ =>
"sbt.internal.librarymanagement.formats.NodeSeqFormat" :: Nil
}
case "org.apache.ivy.plugins.resolver.DependencyResolver" => { _ =>
"sbt.internal.librarymanagement.formats.DependencyResolverFormat" :: Nil
}
case "xsbti.GlobalLock" => { _ =>
"sbt.internal.librarymanagement.formats.GlobalLockFormat" :: Nil
}
case "xsbti.Logger" => { _ =>
"sbt.internal.librarymanagement.formats.LoggerFormat" :: Nil
}
2025-08-09 01:03:13 -04:00
case "sbt.librarymanagement.IvyPaths" => { _ =>
"sbt.librarymanagement.IvyPathsFormats" :: Nil
}
case "sbt.internal.librarymanagement.ivy.UpdateOptions" => { _ =>
"sbt.internal.librarymanagement.ivy.formats.UpdateOptionsFormat" :: Nil
2017-07-11 18:39:06 -04:00
}
case "sbt.librarymanagement.LogicalClock" => { _ =>
"sbt.internal.librarymanagement.formats.LogicalClockFormats" :: Nil
2016-11-02 15:22:55 +00:00
}
2019-04-01 23:35:39 -04:00
case "sbt.librarymanagement.CrossVersion" => { _ =>
"sbt.librarymanagement.CrossVersionFormats" ::
2020-11-15 00:58:52 -05:00
"sbt.librarymanagement.DisabledFormats" ::
"sbt.librarymanagement.BinaryFormats" ::
"sbt.librarymanagement.ConstantFormats" ::
"sbt.librarymanagement.PatchFormats" ::
"sbt.librarymanagement.FullFormats" ::
2021-01-08 18:06:49 +01:00
"sbt.librarymanagement.For3Use2_13Formats" ::
"sbt.librarymanagement.For2_13Use3Formats" ::
2020-11-15 00:58:52 -05:00
Nil
2019-04-01 23:35:39 -04:00
}
2021-11-20 19:39:18 -05:00
case "sbt.librarymanagement.ConfigRef" => { _ =>
"sbt.librarymanagement.ConfigRefFormats" :: Nil
}
2025-09-28 20:07:16 -04:00
case "sbt.testing.Status" => { _ =>
"sbt.internal.testing.StatusFormats" :: Nil
}
case "scalajson.ast.unsafe.JValue" | "sjsonnew.shaded.scalajson.ast.unsafe.JValue" => { _ =>
"sbt.internal.util.codec.JValueFormats" :: Nil
}
case "xsbti.HashedVirtualFileRef" => { _ =>
"sbt.internal.util.codec.HashedVirtualFileRefFormats" :: Nil
}
case "java.nio.ByteBuffer" => { _ =>
"sbt.internal.util.codec.ByteBufferFormats" :: Nil
}
2016-11-02 15:22:55 +00:00
// TODO: These are handled by BasicJsonProtocol, and sbt-datatype should handle them by default, imo
2017-01-05 13:33:19 +00:00
case "Option" | "Set" | "scala.Vector" => { tpe =>
getFormats(oneArg(tpe))
}
2016-11-02 15:22:55 +00:00
case "Map" | "Tuple2" | "scala.Tuple2" => { tpe =>
twoArgs(tpe).flatMap(getFormats)
}
2026-05-13 23:27:38 -07:00
case "Int" | "Long" | "sbt.util.Digest" => { _ =>
2016-11-02 15:22:55 +00:00
Nil
}
}
/** Types for which we don't include the format -- they're just aliases to InclExclRule */
val excluded = Set("sbt.librarymanagement.InclusionRule", "sbt.librarymanagement.ExclusionRule")
/** Returns the list of formats required to encode the given `TpeRef`. */
2017-01-05 13:33:19 +00:00
val getFormats: Type => List[String] =
2016-11-02 15:22:55 +00:00
CodecCodeGen.extensibleFormatsForType {
2017-01-05 13:33:19 +00:00
case NamedType(List("sbt", "internal", "librarymanagement", "RetrieveConfiguration"), _) =>
2016-11-02 15:22:55 +00:00
"sbt.librarymanagement.RetrieveConfigurationFormats" :: Nil
2017-01-05 13:33:19 +00:00
case tpe: Type if myCodecs isDefinedAt tpe.removeTypeParameters.name =>
myCodecs(tpe.removeTypeParameters.name)(tpe)
case tpe: Type if excluded contains tpe.removeTypeParameters.name =>
2016-11-02 15:22:55 +00:00
Nil
case other =>
CodecCodeGen.formatsForType(other)
}
}