-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from gemini-hlsw/dynamic-enums
Dynamic enum logic
- Loading branch information
Showing
7 changed files
with
122 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ query { | |
utc | ||
} | ||
... on TimingWindowEndAfter { | ||
duration { | ||
after { | ||
milliseconds | ||
} | ||
repeat { | ||
|
12 changes: 12 additions & 0 deletions
12
model/.js/src/main/scala/lucuma/schemas/enums/Globals.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.schemas.enums | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation.JSGlobalScope | ||
|
||
@js.native | ||
@JSGlobalScope | ||
private object Globals extends js.Object: | ||
val enumMetadataString: String = js.native // Define in main.jsx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.schemas.enums | ||
|
||
private object Globals: | ||
val enumMetadataString: String = ??? |
13 changes: 13 additions & 0 deletions
13
model/src/main/scala/lucuma/schemas/enums/DynamicEnums.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.schemas.enums | ||
|
||
import io.circe.* | ||
import io.circe.parser.* | ||
|
||
object DynamicEnums: | ||
val parsedEnums: ACursor = | ||
parse(Globals.enumMetadataString) match | ||
case Left(err) => err.printStackTrace; throw err | ||
case Right(json) => json.hcursor |
56 changes: 56 additions & 0 deletions
56
model/src/main/scala/lucuma/schemas/enums/ObsAttachmentType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package lucuma.schemas.enums | ||
|
||
import cats.Eq | ||
import cats.derived.* | ||
import io.circe.* | ||
import io.circe.generic.semiauto | ||
import io.circe.generic.semiauto.* | ||
import lucuma.core.util.Display | ||
import lucuma.core.util.Enumerated | ||
import lucuma.core.util.NewType | ||
import monocle.Focus | ||
import monocle.Lens | ||
|
||
object FileExtension extends NewType[String] | ||
type FileExtension = FileExtension.Type | ||
|
||
case class ObsAttachmentType( | ||
tag: String, | ||
shortName: String, | ||
longName: String, | ||
fileExtensions: List[FileExtension] | ||
) derives Eq: | ||
def accept: String = fileExtensions.map("." + _.value).mkString(",") | ||
|
||
object ObsAttachmentType: | ||
given Display[ObsAttachmentType] = Display.by(_.shortName, _.longName) | ||
|
||
val tag: Lens[ObsAttachmentType, String] = Focus[ObsAttachmentType](_.tag) | ||
val shortName: Lens[ObsAttachmentType, String] = Focus[ObsAttachmentType](_.shortName) | ||
val longName: Lens[ObsAttachmentType, String] = Focus[ObsAttachmentType](_.longName) | ||
val fileExtensions: Lens[ObsAttachmentType, List[FileExtension]] = | ||
Focus[ObsAttachmentType](_.fileExtensions) | ||
|
||
def Finder(using e: Enumerated[ObsAttachmentType]): ObsAttachmentType = e.unsafeFromTag("FINDER") | ||
|
||
val values: List[ObsAttachmentType] = | ||
given Decoder[FileExtension] = Decoder.instance: c => | ||
c.downField("fileExtension").as[String].map(FileExtension(_)) | ||
// This is a meta decoder, not a decoder for enum instances (which comes from the `Enumerated` instance) | ||
given Decoder[ObsAttachmentType] = semiauto.deriveDecoder | ||
|
||
DynamicEnums.parsedEnums.downField("obsAttachmentTypeMeta").as[List[ObsAttachmentType]] match | ||
case Left(err) => err.printStackTrace; throw err | ||
case Right(json) => json | ||
|
||
// The givens are apparently (probably) constructed lazily. | ||
// See https://alexn.org/blog/2022/05/11/implicit-vs-scala-3-given/ | ||
// We want to fail immediately if there is a problem, so we'll reference | ||
// the enumerated givens here. | ||
Enumerated[ObsAttachmentType] | ||
|
||
given Enumerated[ObsAttachmentType] = | ||
Enumerated.from(values.head, values.tail: _*).withTag(_.tag) |