Skip to content

Commit

Permalink
Get extensions reflection separate sources
Browse files Browse the repository at this point in the history
  • Loading branch information
daddykotex committed Dec 19, 2023
1 parent d86860a commit e9e1ee0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
35 changes: 35 additions & 0 deletions modules/compiler-core/src-2/internals/ExtensionsPlatform.scala
Original file line number Diff line number Diff line change
@@ -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)
}
36 changes: 36 additions & 0 deletions modules/compiler-core/src-3/internals/ExtensionsPlatform.scala
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 2 additions & 13 deletions modules/compiler-core/src/internals/GetExtensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit e9e1ee0

Please sign in to comment.