README.md 4.3 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1 2 3 4
# scala-macro-tools [![Build](https://github.com/jxnu-liguobin/scala-macro-tools/actions/workflows/ScalaCI.yml/badge.svg)](https://github.com/jxnu-liguobin/scala-macro-tools/actions/workflows/ScalaCI.yml)

Motivation
--
梦境迷离's avatar
梦境迷离 已提交
5 6 7

scala macro and abstract syntax tree learning code.

梦境迷离's avatar
梦境迷离 已提交
8 9 10
# Features

## @toString
梦境迷离's avatar
梦境迷离 已提交
11

梦境迷离's avatar
梦境迷离 已提交
12
- Note
梦境迷离's avatar
梦境迷离 已提交
13 14
    - `verbose` Whether to enable detailed log.
    - `withFieldName` Whether to include the name of the field in the toString.
梦境迷离's avatar
pre  
梦境迷离 已提交
15 16
    - `withInternalField` Whether to include the fields defined within a class.
    - Support `case class` and `class`.
梦境迷离's avatar
梦境迷离 已提交
17

梦境迷离's avatar
pre  
梦境迷离 已提交
18
- Example
梦境迷离's avatar
梦境迷离 已提交
19

梦境迷离's avatar
梦境迷离 已提交
20
```scala
梦境迷离's avatar
梦境迷离 已提交
21 22 23 24 25
class TestClass(val i: Int = 0, var j: Int) {
  val y: Int = 0
  var z: String = "hello"
  var x: String = "world"
}
梦境迷离's avatar
梦境迷离 已提交
26

梦境迷离's avatar
pre  
梦境迷离 已提交
27
println(new TestClass(1, 2));
梦境迷离's avatar
梦境迷离 已提交
28
```
梦境迷离's avatar
梦境迷离 已提交
29

梦境迷离's avatar
梦境迷离 已提交
30
|withInternalField / withFieldName| false  |true
梦境迷离's avatar
梦境迷离 已提交
31
|  ---------------------------------  | ----------------------------------  |----------------------------------|
梦境迷离's avatar
pre  
梦境迷离 已提交
32
|false|```TestClass(1, 2)``` |```TestClass(i=0, j=2)```|
梦境迷离's avatar
梦境迷离 已提交
33 34
|true|```TestClass(1, 2, 0, hello, world)```|```TestClass(i=1, j=2, y=0, z=hello, x=world)```|

梦境迷离's avatar
梦境迷离 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
## @json

The `@json` scala macro annotation is the quickest way to add a JSON format to your Play project's case classes.

- Note
    - This annotation is drawn from [json-annotation](https://github.com/kifi/json-annotation) and have some
      optimization.
    - It can also be used when there are other annotations on the case classes.
    - Only an implicit object was generated automatically(Maybe need a companion object), and there are no other
      operations.
- Example

```scala
@json case class Person(name: String, age: Int)
```

You can now serialize/deserialize your objects using Play's convenience methods:

```scala
import play.api.libs.json._

val person = Person("Victor Hugo", 46)
val json = Json.toJson(person)
Json.fromJson[Person](json)
```

梦境迷离's avatar
梦境迷离 已提交
61 62 63
# How to use

Add library dependency
梦境迷离's avatar
梦境迷离 已提交
64

梦境迷离's avatar
梦境迷离 已提交
65 66 67 68
```scala
"io.github.jxnu-liguobin" %% "scala-macro-tools" % "<VERSION>"
```

梦境迷离's avatar
梦境迷离 已提交
69
The artefacts have been uploaded to Maven Central.
梦境迷离's avatar
梦境迷离 已提交
70 71 72

| Library Version | Scala 2.11 | Scala 2.12 | Scala 2.13 |
|---------|------------|------------|------------|
梦境迷离's avatar
up  
梦境迷离 已提交
73
| 0.0.3   | [![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.11/0.0.3)](https://search.maven.org/artifact/io.github.jxnu-liguobin/scala-macro-tools_2.11/0.0.3/jar)        | [![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.12/0.0.3)](https://search.maven.org/artifact/io.github.jxnu-liguobin/scala-macro-tools_2.12/0.0.3/jar)        | [![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.13/0.0.3)](https://search.maven.org/artifact/io.github.jxnu-liguobin/scala-macro-tools_2.13/0.0.3/jar)        |
梦境迷离's avatar
梦境迷离 已提交
74 75
| 0.0.2   | [![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.11/0.0.2)](https://search.maven.org/artifact/io.github.jxnu-liguobin/scala-macro-tools_2.11/0.0.2/jar)        | [![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.12/0.0.2)](https://search.maven.org/artifact/io.github.jxnu-liguobin/scala-macro-tools_2.12/0.0.2/jar)        | [![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.13/0.0.2)](https://search.maven.org/artifact/io.github.jxnu-liguobin/scala-macro-tools_2.13/0.0.2/jar)        |
| 0.0.1   |-|-| [![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.13/0.0.1)](https://search.maven.org/artifact/io.github.jxnu-liguobin/scala-macro-tools_2.13/0.0.1/jar)        |
梦境迷离's avatar
梦境迷离 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

Importing the library into your build system (e.g gradle, sbt), is not enough. You need to follow an extra step.

| Scala 2.11 | Scala 2.12 | Scala 2.13 |
|------------|-------------|------------|
| Import macro paradise plugin  | Import macro paradise plugin | Enable compiler flag `-Ymacro-annotations` required |

```scala
addCompilerPlugin("org.scalamacros" % "paradise_<your-scala-version>" % "<plugin-version>")
```

Where `<your-scala-version>` must be the full scala version. For example 2.12.13, and not 2.12.

If that doesn't work, google for alternatives.

梦境迷离's avatar
梦境迷离 已提交
91 92
In version scala`2.13.x`, the functionality of macro paradise has been included in the scala compiler directly. However,
you must still enable the compiler flag `-Ymacro-annotations`. Please see examples from `examples212` sub-project.