From e9e1ee033322828c5e8ead74e1c7c6c6828ad30e Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Tue, 19 Dec 2023 13:58:41 -0500 Subject: [PATCH] Get extensions reflection separate sources --- .../src-2/internals/ExtensionsPlatform.scala | 35 ++++++++++++++++++ .../src-3/internals/ExtensionsPlatform.scala | 36 +++++++++++++++++++ .../src/internals/GetExtensions.scala | 15 ++------ 3 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 modules/compiler-core/src-2/internals/ExtensionsPlatform.scala create mode 100644 modules/compiler-core/src-3/internals/ExtensionsPlatform.scala diff --git a/modules/compiler-core/src-2/internals/ExtensionsPlatform.scala b/modules/compiler-core/src-2/internals/ExtensionsPlatform.scala new file mode 100644 index 0000000..50c3e0f --- /dev/null +++ b/modules/compiler-core/src-2/internals/ExtensionsPlatform.scala @@ -0,0 +1,35 @@ +/* Copyright 2022 Disney Streaming + * + * Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://disneystreaming.github.io/TOST-1.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smithytranslate.compiler +package internals + +import scala.language.reflectiveCalls +import scala.jdk.CollectionConverters._ + +trait ExtensionsPlatform { + // Using reflective calls because openapi does not seem to have a common interface + // that exposes the presence of extensions. + type HasExtensions = { def getExtensions(): java.util.Map[String, Any] } + object HasExtensions { + def unsafeFrom(s: Any): HasExtensions = s.asInstanceOf[HasExtensions] + } + + def getExtensions(s: HasExtensions) = + Option(s) + .flatMap(s => Option(s.getExtensions())) + .map(_.asScala) + .filterNot(_.isEmpty) +} diff --git a/modules/compiler-core/src-3/internals/ExtensionsPlatform.scala b/modules/compiler-core/src-3/internals/ExtensionsPlatform.scala new file mode 100644 index 0000000..39c819a --- /dev/null +++ b/modules/compiler-core/src-3/internals/ExtensionsPlatform.scala @@ -0,0 +1,36 @@ +/* Copyright 2022 Disney Streaming + * + * Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://disneystreaming.github.io/TOST-1.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smithytranslate.compiler +package internals + +import scala.language.reflectiveCalls +import scala.jdk.CollectionConverters._ +import reflect.Selectable.reflectiveSelectable + +trait ExtensionsPlatform { + // Using reflective calls because openapi does not seem to have a common interface + // that exposes the presence of extensions. + type HasExtensions = { def getExtensions(): java.util.Map[String, Any] } + object HasExtensions { + def unsafeFrom(s: Any): HasExtensions = s.asInstanceOf[HasExtensions] + } + + def getExtensions(s: HasExtensions) = + Option(s) + .flatMap(s => Option(s.getExtensions())) + .map(_.asScala) + .filterNot(_.isEmpty) +} diff --git a/modules/compiler-core/src/internals/GetExtensions.scala b/modules/compiler-core/src/internals/GetExtensions.scala index 310c661..d0c7484 100644 --- a/modules/compiler-core/src/internals/GetExtensions.scala +++ b/modules/compiler-core/src/internals/GetExtensions.scala @@ -18,27 +18,16 @@ package internals import software.amazon.smithy.model.node.Node import scala.jdk.CollectionConverters._ -import scala.language.reflectiveCalls import smithytranslate.compiler.internals._ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.{node => jackson} import java.time.format.DateTimeFormatter import java.time.ZoneId -private[compiler] object GetExtensions { - - // Using reflective calls because openapi does not seem to have a common interface - // that exposes the presence of extensions. - type HasExtensions = { def getExtensions(): java.util.Map[String, Any] } - object HasExtensions { - def unsafeFrom(s: Any): HasExtensions = s.asInstanceOf[HasExtensions] - } +private[compiler] object GetExtensions extends ExtensionsPlatform { def from(s: HasExtensions): List[Hint] = - Option(s) - .flatMap(s => Option(s.getExtensions())) - .map(_.asScala) - .filterNot(_.isEmpty) + getExtensions(s) .map[Hint] { ext => val nodeMap = ext.map { case (k, v) => (k, anyToNode(v))