Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Internal/Builtin/Codec.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Internal.Builtin.Codec exposing (codeGen)

import CodeGenerator exposing (CodeGenerator)
import Elm.CodeGen as CG
import Internal.Helpers
import ResolvedType
import String.Extra
import TypePattern exposing (TypePattern(..))
Expand Down Expand Up @@ -75,9 +76,9 @@ codeGen =

thingsToPatterns : List a -> List CG.Pattern
thingsToPatterns =
List.indexedMap (\i _ -> CG.varPattern ("arg" ++ String.fromInt i))
List.indexedMap (\i _ -> CG.varPattern ("arg" ++ Internal.Helpers.intToLetter i))


thingsToValues : List a -> List CG.Expression
thingsToValues =
List.indexedMap (\i _ -> CG.val ("arg" ++ String.fromInt i))
List.indexedMap (\i _ -> CG.val ("arg" ++ Internal.Helpers.intToLetter i))
6 changes: 3 additions & 3 deletions src/Internal/Builtin/JsonEncoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ codeGen =
[ CG.fqFun [ "Json", "Encode" ] "object"
, CG.list
(CG.tuple [ CG.string "tag", CG.apply [ CG.fqFun [ "Json", "Encode" ] "string", CG.string ref.name ] ]
:: List.indexedMap (\i expr -> CG.tuple [ CG.string (String.fromInt i), CG.apply [ expr, CG.val ("arg" ++ String.fromInt i) ] ]) exprs
:: List.indexedMap (\i expr -> CG.tuple [ CG.string (String.fromInt i), CG.apply [ expr, CG.val ("arg" ++ Internal.Helpers.intToLetter i) ] ]) exprs
)
]
)
Expand Down Expand Up @@ -159,9 +159,9 @@ codeGen =

thingsToPatterns : List a -> List CG.Pattern
thingsToPatterns =
List.indexedMap (\i _ -> CG.varPattern ("arg" ++ String.fromInt i))
List.indexedMap (\i _ -> CG.varPattern ("arg" ++ Internal.Helpers.intToLetter i))


thingsToValues : List a -> List CG.Expression
thingsToValues =
List.indexedMap (\i _ -> CG.val ("arg" ++ String.fromInt i))
List.indexedMap (\i _ -> CG.val ("arg" ++ Internal.Helpers.intToLetter i))
5 changes: 3 additions & 2 deletions src/Internal/Builtin/MiniBillCodec.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Internal.Builtin.MiniBillCodec exposing (codeGen)

import CodeGenerator exposing (CodeGenerator)
import Elm.CodeGen as CG
import Internal.Helpers
import ResolvedType
import String.Extra
import TypePattern exposing (TypePattern(..))
Expand Down Expand Up @@ -119,9 +120,9 @@ codeGen =

thingsToPatterns : List a -> List CG.Pattern
thingsToPatterns =
List.indexedMap (\i _ -> CG.varPattern ("arg" ++ String.fromInt i))
List.indexedMap (\i _ -> CG.varPattern ("arg" ++ Internal.Helpers.intToLetter i))


thingsToValues : List a -> List CG.Expression
thingsToValues =
List.indexedMap (\i _ -> CG.val ("arg" ++ String.fromInt i))
List.indexedMap (\i _ -> CG.val ("arg" ++ Internal.Helpers.intToLetter i))
18 changes: 17 additions & 1 deletion src/Internal/Helpers.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Internal.Helpers exposing (applyBindings, hasDebugTodo, lambda1, node, rangeContains, traverseExpression, traversePattern, traverseTypeAnnotation, writeExpression)
module Internal.Helpers exposing (applyBindings, hasDebugTodo, intToLetter, lambda1, node, rangeContains, traverseExpression, traversePattern, traverseTypeAnnotation, writeExpression)

import Dict
import Elm.CodeGen as CG
Expand Down Expand Up @@ -373,3 +373,19 @@ traversePattern fn initial pattern =

_ ->
( newPattern, accumulator )


lettersInAlphabet : number
lettersInAlphabet =
26


intToLetter : Int -> String
intToLetter int =
(if int < lettersInAlphabet then
""

else
String.fromChar (Char.fromCode (65 - 1 + int // lettersInAlphabet))
)
++ String.fromChar (Char.fromCode (65 + modBy lettersInAlphabet int))
24 changes: 24 additions & 0 deletions tests/HelperTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module HelperTest exposing (suite)

import Expect
import Internal.Helpers
import Test exposing (Test)


suite : Test
suite =
Test.describe "intToLetter"
(List.map
(\( input, expected ) ->
Test.test (String.fromInt input ++ " -> " ++ expected) <|
\_ -> Internal.Helpers.intToLetter input |> Expect.equal expected
)
[ ( 0, "A" )
, ( 25, "Z" )
, ( 26, "AA" )
, ( 51, "AZ" )
, ( 52, "BA" )
, ( 53, "BB" )
, ( 701, "ZZ" )
]
)
96 changes: 48 additions & 48 deletions tests/IncrementalTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ type Foo
encode : Foo -> Value
encode foo =
case foo of
A arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "A" ), ( "0", Json.Encode.int arg0 ) ]
A argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "A" ), ( "0", Json.Encode.int argA ) ]

B arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", Json.Encode.string arg0 ) ]
B argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", Json.Encode.string argA ) ]
"""
, codeGenIncrementalTest "Picks up an encoder from another file"
[ elmJson ]
Expand Down Expand Up @@ -200,8 +200,8 @@ type B =
encode : B -> Value
encode b =
case b of
B arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", A.encode arg0 ) ]
B argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", A.encode argA ) ]
"""
, codeGenIncrementalTest "Picks up a generator from another file with different import notation"
[ elmJson ]
Expand Down Expand Up @@ -239,8 +239,8 @@ type B =
encode : B -> Encode.Value
encode b =
case b of
B arg0 ->
Encode.object [ ( "tag", Encode.string "B" ), ( "0", A.encode arg0 ) ]
B argA ->
Encode.object [ ( "tag", Encode.string "B" ), ( "0", A.encode argA ) ]
"""
, codeGenIncrementalTest "Picks up an encoder from another file with generics"
[ elmJson ]
Expand Down Expand Up @@ -278,8 +278,8 @@ type B =
encode : B -> Value
encode b =
case b of
B arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", A.encode Json.Encode.int arg0 ) ]
B argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", A.encode Json.Encode.int argA ) ]
"""
, codeGenIncrementalTest "Picks up an encoder from another file with generics type alias"
[ elmJson ]
Expand Down Expand Up @@ -317,8 +317,8 @@ type B =
encode : B Int -> Value
encode b =
case b of
B arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", A.encode Json.Encode.int arg0 ) ]
B argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", A.encode Json.Encode.int argA ) ]
"""
, codeGenIncrementalTest "Doesn't pick up an encoder if not exposed"
[ elmJson ]
Expand Down Expand Up @@ -356,8 +356,8 @@ type B =
encode : B -> Value
encode b =
case b of
B arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", encodeA Json.Encode.int arg0 ) ]
B argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", encodeA Json.Encode.int argA ) ]

encodeA : (a -> Value) -> A a -> Value
encodeA a =
Expand Down Expand Up @@ -397,8 +397,8 @@ type C =
encode : C -> Value
encode c =
case c of
C arg0 arg1 ->
Json.Encode.object [ ( "tag", Json.Encode.string "C" ), ( "0", encodeA arg0 ), ( "1", encodeB arg1 ) ]
C argA argB ->
Json.Encode.object [ ( "tag", Json.Encode.string "C" ), ( "0", encodeA argA ), ( "1", encodeB argB ) ]

encodeA : A -> Value
encodeA =
Expand Down Expand Up @@ -450,8 +450,8 @@ type Foo =
encode : Foo -> Value
encode b =
case b of
B arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", encodeA arg0 ) ]
B argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", encodeA argA ) ]

encodeA : A -> Value
encodeA =
Expand Down Expand Up @@ -491,8 +491,8 @@ type Foo =
encode : Foo -> Value
encode b =
case b of
B arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", encodeA arg0 ) ]
B argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "B" ), ( "0", encodeA argA ) ]

encodeA : A -> Value
encodeA =
Expand Down Expand Up @@ -524,8 +524,8 @@ type A =
encode : A -> Value
encode arg =
case arg of
A arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "A" ), ( "0", Json.Encode.int arg0 ) ]
A argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "A" ), ( "0", Json.Encode.int argA ) ]

main =
Json.Encode.encode 2 (encode (A 2))
Expand All @@ -547,11 +547,11 @@ encode encodeVal a =
encodeResult : (err -> Value) -> (ok -> Value) -> Result err ok -> Value
encodeResult err ok val =
case val of
Err arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Err" ), ( "0", err arg0 ) ]
Err argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Err" ), ( "0", err argA ) ]

Ok arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Ok" ), ( "0", ok arg0 ) ]
Ok argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Ok" ), ( "0", ok argA ) ]
""" ]
"""module A exposing (A, encode)
import Json.Encode exposing (Value)
Expand All @@ -563,18 +563,18 @@ type A
encode : A val -> Value
encode encodeVal a =
case a of
A arg0 ->
A argA ->
Json.Encode.object
[ ( "tag", Json.Encode.string "A" ), ( "0", encodeResult Json.Encode.string Json.Encode.int arg0 ) ]
[ ( "tag", Json.Encode.string "A" ), ( "0", encodeResult Json.Encode.string Json.Encode.int argA ) ]

encodeResult : (err -> Value) -> (ok -> Value) -> Result err ok -> Value
encodeResult err ok val =
case val of
Err arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Err" ), ( "0", err arg0 ) ]
Err argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Err" ), ( "0", err argA ) ]

Ok arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Ok" ), ( "0", ok arg0 ) ]
Ok argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Ok" ), ( "0", ok argA ) ]
"""
, codeGenIncrementalTest "Picks up the definition of Result from dependency"
[ elmJson ]
Expand All @@ -600,9 +600,9 @@ type A
encode : A val -> Value
encode encodeVal a =
case a of
A arg0 ->
A argA ->
Json.Encode.object
[ ( "tag", Json.Encode.string "A" ), ( "0", encodeResult Json.Encode.string Json.Encode.int arg0 ) ]
[ ( "tag", Json.Encode.string "A" ), ( "0", encodeResult Json.Encode.string Json.Encode.int argA ) ]

encodeResult : (error -> Value) -> (value -> Value) -> Result error value -> Value
encodeResult error value =
Expand Down Expand Up @@ -636,8 +636,8 @@ type B
encode : A -> Value
encode arg =
case arg of
A arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "A" ), ( "0", encodeB arg0 ) ]
A argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "A" ), ( "0", encodeB argA ) ]

encodeB : B -> Value
encodeB =
Expand Down Expand Up @@ -667,12 +667,12 @@ type Tree a
encode : (a -> Value) -> Tree a -> Value
encode childEncoder tree =
case tree of
Node arg0 arg1 arg2 ->
Node argA argB argC ->
Encode.object
[ ( "tag", Encode.string "Node" )
, ( "0", encode childEncoder arg0 )
, ( "1", childEncoder arg1 )
, ( "2", encode childEncoder arg2 )
, ( "0", encode childEncoder argA )
, ( "1", childEncoder argB )
, ( "2", encode childEncoder argC )
]

Empty ->
Expand Down Expand Up @@ -800,8 +800,8 @@ type Always =
encode : Foo { capabilites | x : Always } -> Value
encode foo =
case foo of
Foo arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Foo" ), ( "0", Json.Encode.int arg0 ) ]
Foo argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Foo" ), ( "0", Json.Encode.int argA ) ]
"""
, codeGenIncrementalTest "Generates an encoder with a list"
[ elmJson ]
Expand Down Expand Up @@ -916,14 +916,14 @@ import Json.Encode exposing (Value)
encode : A Int -> Value
encode arg =
case arg of
Rec arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Rec" ), ( "0", encodeB arg0 ) ]
Rec argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Rec" ), ( "0", encodeB argA ) ]

Gen arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Gen" ), ( "0", Json.Encode.int arg0 ) ]
Gen argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Gen" ), ( "0", Json.Encode.int argA ) ]

Recursive arg0 ->
Json.Encode.object [ ( "tag", Json.Encode.string "Recursive" ), ( "0", encode arg0 ) ]
Recursive argA ->
Json.Encode.object [ ( "tag", Json.Encode.string "Recursive" ), ( "0", encode argA ) ]

encodeB : B -> Value
encodeB =
Expand Down
Loading