-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
107 additions
and
84 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
13 changes: 6 additions & 7 deletions
13
...rc/main/scala-2/kyo/llm/ValueSchema.scala → ...ain/scala-2/kyo/llm/json/JsonDerive.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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
package kyo.llm | ||
package kyo.llm.json | ||
|
||
import zio.schema._ | ||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox | ||
|
||
case class ValueSchema[T](get: Schema[Value[T]]) | ||
|
||
object ValueSchema { | ||
trait JsonDerive { | ||
implicit def deriveJson[T]: Json[T] = macro JsonDerive.genMacro[T] | ||
} | ||
|
||
implicit def gen[T]: ValueSchema[T] = macro genMacro[T] | ||
object JsonDerive { | ||
|
||
def genMacro[T](c: blackbox.Context)(implicit t: c.WeakTypeTag[T]): c.Tree = { | ||
import c.universe._ | ||
q""" | ||
import kyo.llm.Value | ||
kyo.llm.ValueSchema[$t](zio.schema.DeriveSchema.gen) | ||
kyo.llm.json.Json.fromZio[$t](zio.schema.DeriveSchema.gen) | ||
""" | ||
} | ||
} |
11 changes: 0 additions & 11 deletions
11
kyo-llm-macros/shared/src/main/scala-3/kyo/llm/ValueSchema.scala
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
kyo-llm-macros/shared/src/main/scala-3/kyo/llm/json/JsonDerive.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,13 @@ | ||
package kyo.llm.json | ||
|
||
import kyo._ | ||
import kyo.ios._ | ||
import zio.schema.codec.JsonCodec | ||
import scala.compiletime.constValue | ||
import zio.schema._ | ||
import zio.Chunk | ||
|
||
trait JsonDerive { | ||
inline implicit def deriveJson[T]: Json[T] = | ||
Json.fromZio(DeriveSchema.gen) | ||
} |
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
kyo-llm-macros/shared/src/main/scala/kyo/llm/json/Json.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,46 @@ | ||
package kyo.llm.json | ||
|
||
import kyo._ | ||
import kyo.ios._ | ||
import zio.schema.codec.JsonCodec | ||
import zio.schema._ | ||
import zio.Chunk | ||
|
||
trait Json[T] { | ||
def schema: JsonSchema | ||
def encode(v: T): String > IOs | ||
def decode(s: String): T > IOs | ||
} | ||
|
||
object Json extends JsonDerive { | ||
|
||
def schema[T](implicit j: Json[T]): JsonSchema = | ||
j.schema | ||
|
||
def encode[T](v: T)(implicit j: Json[T]): String > IOs = | ||
j.encode(v) | ||
|
||
def decode[T](s: String)(implicit j: Json[T]): T > IOs = | ||
j.decode(s) | ||
|
||
implicit def primitive[T](implicit t: StandardType[T]): Json[T] = | ||
fromZio(Schema.Primitive(t, Chunk.empty)) | ||
|
||
def fromZio[T](z: Schema[T]) = | ||
new Json[T] { | ||
lazy val schema: JsonSchema = JsonSchema(z) | ||
private lazy val decoder = JsonCodec.jsonDecoder(z) | ||
private lazy val encoder = JsonCodec.jsonEncoder(z) | ||
|
||
def encode(v: T): String > IOs = | ||
IOs(encoder.encodeJson(v).toString) | ||
|
||
def decode(s: String): T > IOs = | ||
IOs { | ||
decoder.decodeJson(s) match { | ||
case Left(fail) => IOs.fail(fail) | ||
case Right(v) => v | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package kyo.llm.json | ||
|
||
import scala.annotation.StaticAnnotation | ||
|
||
final case class desc(value: String) extends StaticAnnotation |
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
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