Skip to content

Commit 1301708

Browse files
authored
Merge pull request #84 from AVSystem/adt-metadata
ADT metadata & OpenAPI generation for REST
2 parents fd990bf + 43ddbaf commit 1301708

File tree

68 files changed

+4426
-1321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+4426
-1321
lines changed

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,13 @@ def sourceDirsSettings(baseMapper: File => File) = Seq(
175175
)
176176

177177
lazy val `commons-annotations` = project
178+
.dependsOn(`commons-macros`)
178179
.settings(jvmCommonSettings)
179180

180181
lazy val `commons-annotations-js` = project.in(`commons-annotations`.base / "js")
181182
.enablePlugins(ScalaJSPlugin)
182183
.configure(p => if (forIdeaImport) p.dependsOn(`commons-annotations`) else p)
184+
.dependsOn(`commons-macros`)
183185
.settings(
184186
jsCommonSettings,
185187
name := (name in `commons-annotations`).value,

commons-akka/src/main/scala/com/avsystem/commons/rpc/akka/AkkaRPCFramework.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import java.io.{DataInputStream, DataOutputStream}
55

66
import akka.actor.{ActorRef, ActorSystem}
77
import akka.util.ByteString
8+
import com.avsystem.commons.meta._
9+
import com.avsystem.commons.rpc._
810
import com.avsystem.commons.rpc.akka.client.ClientRawRPC
911
import com.avsystem.commons.rpc.akka.server.ServerActor
10-
import com.avsystem.commons.rpc.{FunctionRPCFramework, GetterRPCFramework, MetadataAnnotation, ProcedureRPCFramework, RpcMetadataCompanion, TypedMetadata, infer, multi, reifyAnnot, reifyName, verbatim}
1112
import com.avsystem.commons.serialization.{GenCodec, StreamInput, StreamOutput}
1213
import monix.reactive.Observable
1314

@@ -31,10 +32,10 @@ object AkkaRPCFramework extends GetterRPCFramework with ProcedureRPCFramework wi
3132
case class RPCMetadata[T](
3233
@reifyName name: String,
3334
@reifyAnnot @multi annotations: List[MetadataAnnotation],
34-
@multi @verbatim procedureSignatures: Map[String, ProcedureSignature],
35-
@multi functionSignatures: Map[String, FunctionSignature[_]],
36-
@multi observeSignatures: Map[String, ObserveSignature[_]],
37-
@multi getterSignatures: Map[String, GetterSignature[_]]
35+
@multi @verbatim @rpcMethodMetadata procedureSignatures: Map[String, ProcedureSignature],
36+
@multi @rpcMethodMetadata functionSignatures: Map[String, FunctionSignature[_]],
37+
@multi @rpcMethodMetadata observeSignatures: Map[String, ObserveSignature[_]],
38+
@multi @rpcMethodMetadata getterSignatures: Map[String, GetterSignature[_]]
3839
)
3940
object RPCMetadata extends RpcMetadataCompanion[RPCMetadata]
4041

commons-akka/src/main/scala/com/avsystem/commons/rpc/akka/MonixRPCFramework.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.avsystem.commons
22
package rpc.akka
33

4+
import com.avsystem.commons.meta._
45
import com.avsystem.commons.rpc._
56
import monix.reactive.Observable
67

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.avsystem.commons
2+
package annotation
3+
4+
import scala.annotation.StaticAnnotation
5+
6+
/**
7+
* Marker trait for annotations which don't want to be inherited by subtypes
8+
* of a sealed trait or class that has this annotation applied. Intended for annotations that should apply
9+
* only to the sealed trait itself.
10+
*/
11+
trait NotInheritedFromSealedTypes extends StaticAnnotation
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.avsystem.commons
2+
package annotation
3+
4+
import scala.annotation.StaticAnnotation
5+
6+
/**
7+
* Annotate a symbol (i.e. class, method, parameter, etc.) with `@positioned(positioned.here)` to retain source
8+
* position information for that symbol to be available in macro implementations which inspect that symbol.
9+
* This is necessary e.g. for determining declaration order of subtypes of sealed hierarchies in macro implementations.
10+
* This annotation is only needed when macro is invoked in a different source file than the source file of inspected
11+
* symbol. If macro is invoked in the same file, source position is always available.
12+
*/
13+
class positioned(val point: Int) extends StaticAnnotation
14+
object positioned {
15+
def here: Int = macro macros.misc.MiscMacros.posPoint
16+
}

commons-annotations/src/main/scala/com/avsystem/commons/meta/metaAnnotations.scala

Lines changed: 261 additions & 0 deletions
Large diffs are not rendered by default.

commons-annotations/src/main/scala/com/avsystem/commons/rpc/rpcAnnotations.scala

Lines changed: 27 additions & 246 deletions
Large diffs are not rendered by default.

commons-annotations/src/main/scala/com/avsystem/commons/serialization/name.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.avsystem.commons
22
package serialization
33

4+
import com.avsystem.commons.annotation.NotInheritedFromSealedTypes
5+
46
import scala.annotation.StaticAnnotation
57

68
/**
@@ -24,4 +26,5 @@ import scala.annotation.StaticAnnotation
2426
* For instance, if a case class field overrides a method of some base trait, the `@name` annotation may
2527
* be used on that method and will affect the case class field.
2628
*/
27-
class name(val name: String) extends StaticAnnotation
29+
class name(val name: String) extends StaticAnnotation with NotInheritedFromSealedTypes
30+

commons-core/src/main/scala/com/avsystem/commons/SharedExtensions.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,35 @@ object SharedExtensions extends SharedExtensions {
176176
def optRef: OptRef[A] = OptRef(a)
177177
}
178178

179+
private val RemovableLineBreak = "\\n+".r
180+
179181
class StringOps(private val str: String) extends AnyVal {
180182
def ensureSuffix(suffix: String): String =
181183
if (str.endsWith(suffix)) str else str + suffix
182184

183185
def ensurePrefix(prefix: String): String =
184186
if (str.startsWith(prefix)) str else prefix + str
187+
188+
def uncapitalize: String =
189+
if (str.isEmpty || str.charAt(0).isLower) str
190+
else str.charAt(0).toLower + str.substring(1)
191+
192+
/**
193+
* Removes a newline character from every sequence of consecutive newline characters. If the sequence contained
194+
* just one newline character without any whitespace before and after it, a space is inserted.
195+
*
196+
* e.g. `My hovercraft\nis full of eels.\n\nMy hovercraft is\n full of eels.` becomes
197+
* `My hovercraft is full of eels.\nMy hovercraft is full of eels.`
198+
*
199+
* Useful for multi-line string literals with lines wrapped in source code but without intention of including
200+
* these line breaks in actual runtime string.
201+
*/
202+
def unwrapLines: String =
203+
RemovableLineBreak.replaceAllIn(str, { m =>
204+
val insertSpace = m.end == m.start + 1 && m.start - 1 >= 0 && m.end < str.length &&
205+
!Character.isWhitespace(str.charAt(m.start - 1)) && !Character.isWhitespace(str.charAt(m.end))
206+
if (insertSpace) " " else m.matched.substring(1)
207+
})
185208
}
186209

187210
class IntOps(private val int: Int) extends AnyVal {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.avsystem.commons
2+
package meta
3+
4+
import com.avsystem.commons.macros.meta.AdtMetadataMacros
5+
import com.avsystem.commons.misc.MacroGenerated
6+
7+
trait AdtMetadataCompanion[M[_]] extends MetadataCompanion[M] {
8+
def materialize[T]: M[T] = macro AdtMetadataMacros.materialize[T]
9+
10+
implicit def materializeMacroGenerated[T]: MacroGenerated[M[T]] = macro AdtMetadataMacros.materializeMacroGenerated[T]
11+
}

0 commit comments

Comments
 (0)