-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from mint-lang/decode
Automatic decoders for primitive types and records
- Loading branch information
Showing
31 changed files
with
604 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
record X { | ||
name : String | ||
} | ||
|
||
component A { | ||
fun x (input : Object) : Result(Object.Error, x) { | ||
decode input as X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
const $$X = (input) => { | ||
let name = Decoder.field(`name`, Decoder.string)(input) | ||
if (name instanceof Err) { | ||
return name | ||
} | ||
|
||
return new Ok({ | ||
name: name.value | ||
}) | ||
} | ||
|
||
class $A extends Component { | ||
x(input) { | ||
return $$X(input) | ||
} | ||
|
||
render() { | ||
return _createElement("div", {}) | ||
} | ||
} | ||
|
||
$A.displayName = "A" |
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,93 @@ | ||
record Y { | ||
size : Number from "SIIIZEEE" | ||
} | ||
|
||
record X { | ||
string : String, | ||
number : Number, | ||
bool : Bool, | ||
time : Time, | ||
maybe : Maybe(String), | ||
array : Array(String), | ||
y : Y | ||
} | ||
|
||
component A { | ||
fun x (input : Object) : Result(Object.Error, x) { | ||
decode input as X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
const $$Y = (input) => { | ||
let size = Decoder.field(`SIIIZEEE`, Decoder.number)(input) | ||
if (size instanceof Err) { | ||
return size | ||
} | ||
|
||
return new Ok({ | ||
size: size.value | ||
}) | ||
} | ||
|
||
const $$X = (input) => { | ||
let string = Decoder.field(`string`, Decoder.string)(input) | ||
if (string instanceof Err) { | ||
return string | ||
} | ||
|
||
let number = Decoder.field(`number`, Decoder.number)(input) | ||
if (number instanceof Err) { | ||
return number | ||
} | ||
|
||
let bool = Decoder.field(`bool`, Decoder.boolean)(input) | ||
if (bool instanceof Err) { | ||
return bool | ||
} | ||
|
||
let time = Decoder.field(`time`, Decoder.time)(input) | ||
if (time instanceof Err) { | ||
return time | ||
} | ||
|
||
let maybe = Decoder.field(`maybe`, Decoder.maybe(Decoder.string))(input) | ||
if (maybe instanceof Err) { | ||
return maybe | ||
} | ||
|
||
let array = Decoder.field(`array`, Decoder.array(Decoder.string))(input) | ||
if (array instanceof Err) { | ||
return array | ||
} | ||
|
||
let y = Decoder.field(`y`, $$Y)(input) | ||
if (y instanceof Err) { | ||
return y | ||
} | ||
|
||
return new Ok({ | ||
string: string.value, | ||
number: number.value, | ||
bool: bool.value, | ||
time: time.value, | ||
maybe: maybe.value, | ||
array: array.value, | ||
y: y.value | ||
}) | ||
} | ||
|
||
class $A extends Component { | ||
x(input) { | ||
return $$X(input) | ||
} | ||
|
||
render() { | ||
return _createElement("div", {}) | ||
} | ||
} | ||
|
||
$A.displayName = "A" |
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,19 @@ | ||
enum Test { | ||
X, | ||
Y | ||
} | ||
|
||
module X { | ||
fun a : Test { | ||
Test::X | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
$Test_X = Symbol.for(`Test_X`) | ||
$Test_Y = Symbol.for(`Test_Y`) | ||
|
||
const $X = new(class { | ||
a() { | ||
return $Test_X | ||
} | ||
}) |
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,29 @@ | ||
record X { | ||
name : String | ||
} | ||
|
||
component A { | ||
fun x (input : Object) : Result(Object.Error, x) { | ||
decode | ||
input as | ||
X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
record X { | ||
name : String | ||
} | ||
|
||
component A { | ||
fun x (input : Object) : Result(Object.Error, x) { | ||
decode input as X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} |
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,6 @@ | ||
recordTest{a:String from "blah",b:Blah from "what"} | ||
-------------------------------------------------------------------------------- | ||
record Test { | ||
a : String from "blah", | ||
b : Blah from "what" | ||
} |
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,18 @@ | ||
require "../spec_helper" | ||
|
||
describe "Decode Expression" do | ||
subject decode | ||
|
||
expect_ignore "." | ||
expect_ignore "::" | ||
expect_ignore "asd" | ||
|
||
expect_error "decode", Mint::Parser::DecodeExpectedExpression | ||
expect_error "decode x", Mint::Parser::DecodeExpectedAs | ||
expect_error "decode x x", Mint::Parser::DecodeExpectedAs | ||
expect_error "decode x ?", Mint::Parser::DecodeExpectedAs | ||
expect_error "decode x as", Mint::Parser::DecodeExpectedType | ||
expect_error "decode x as x", Mint::Parser::DecodeExpectedType | ||
|
||
expect_ok "decode x as T" | ||
end |
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,55 @@ | ||
record X { | ||
name : String | ||
} | ||
|
||
component A { | ||
fun x (input : Object) : Result(Object.Error, x) { | ||
decode input as X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} | ||
------------------------------------------------------------DecodeExpectedObject | ||
record X { | ||
name : String | ||
} | ||
|
||
component A { | ||
fun x (input : String) : Result(Object.Error, x) { | ||
decode input as X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} | ||
---------------------------------------------------------------DecodeComplexType | ||
record X { | ||
name : Blah | ||
} | ||
|
||
component A { | ||
fun x (input : Object) : Result(Object.Error, x) { | ||
decode input as X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} | ||
---------------------------------------------------------------DecodeComplexType | ||
record X { | ||
name : Maybe(a) | ||
} | ||
|
||
component A { | ||
fun x (input : Object) : Result(Object.Error, x) { | ||
decode input as X | ||
} | ||
|
||
fun render : Html { | ||
<div/> | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,14 @@ | ||
module Mint | ||
class Ast | ||
class Decode < Node | ||
getter expression, type | ||
|
||
def initialize(@expression : Expression, | ||
@input : Data, | ||
@from : Int32, | ||
@type : Type, | ||
@to : Int32) | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Mint | ||
class Compiler | ||
def compile(node : Ast::Decode) : String | ||
expression = | ||
compile node.expression | ||
|
||
code = | ||
@decoder.generate types[node] | ||
|
||
"#{code}(#{expression})" | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Mint | ||
class Formatter | ||
def format(node : Ast::Decode) | ||
expression = | ||
format node.expression | ||
|
||
type = | ||
format node.type | ||
|
||
"decode #{expression} as #{type}" | ||
end | ||
end | ||
end |
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
Oops, something went wrong.