-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Implicit model names from preset functions
Updated annotations regexes to support special chars in assignments
- Loading branch information
Showing
6 changed files
with
97 additions
and
25 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
35 changes: 35 additions & 0 deletions
35
core/src/main/scala-2.12/com/opticdev/experimental_features/ImplicitObjectRefs.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,35 @@ | ||
package com.opticdev.experimental_features | ||
|
||
import com.opticdev.common.{ObjectRef, SchemaRef} | ||
import play.api.libs.json.{JsObject, JsString} | ||
|
||
import scala.util.Try | ||
|
||
object ImplicitObjectRefs { | ||
|
||
val presets: Map[String, JsObject => Try[String]] = { | ||
|
||
val endpointName = (value: JsObject) => Try { | ||
val method = (value \ "method").get.as[JsString].value | ||
val url = (value \ "url").get.as[JsString].value | ||
|
||
s"""${method.toUpperCase} ${url}""" | ||
} | ||
|
||
Map( | ||
"optic:rest/route" -> endpointName, | ||
"apiatlas:schemas/endpoint" -> endpointName | ||
) | ||
|
||
} | ||
|
||
def objectRefForModelNode(schemaRef: SchemaRef, value: JsObject) = { | ||
val result = presets.get(schemaRef.internalFull).map(_(value)) | ||
if (result.isDefined && result.get.isSuccess) { | ||
Some(ObjectRef(result.get.get)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
core/src/test/scala-2.12/com/opticdev/experimental_features/ImplicitObjectRefsSpec.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,28 @@ | ||
package com.opticdev.experimental_features | ||
|
||
import com.opticdev.common.{ObjectRef, SchemaRef} | ||
import org.scalatest.FunSpec | ||
import play.api.libs.json.{JsObject, JsString} | ||
|
||
class ImplicitObjectRefsSpec extends FunSpec { | ||
|
||
it("will provide a default name for endpoints") { | ||
|
||
val result = ImplicitObjectRefs.objectRefForModelNode(SchemaRef.fromString("optic:[email protected]/route").get, JsObject(Seq( | ||
"url" -> JsString("/hello-world"), | ||
"method" -> JsString("post") | ||
))) | ||
|
||
assert(result.contains(ObjectRef("POST /hello-world"))) | ||
|
||
} | ||
|
||
it("will return an empty option if a naming function isn't specified") { | ||
|
||
val result = ImplicitObjectRefs.objectRefForModelNode(SchemaRef.fromString("optic:[email protected]/otherpackage").get, JsObject.empty) | ||
|
||
assert(result.isEmpty) | ||
|
||
} | ||
|
||
} |