From e1d3ef236dbe0108049a37673ce528991eaff580 Mon Sep 17 00:00:00 2001 From: Dave Hinton Date: Sun, 21 Apr 2024 20:35:18 +0100 Subject: [PATCH 1/7] explain how to wrap Set --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 28ef451..b0e1bd4 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,32 @@ To wrap `Dict`, the module definition is an object containing these keys: with a type like `PrivateKey -> PublicKey` here — you may need to write a wrapper function in your code with the correct type -(Currently only `Dict` is supported.) +To wrap `Set`, the module definition is an object containing these keys: + +- `underlying-type` + - Must be `"Set"` +- `wrapper-type` + - The fully-qualified name of the type to generate. The generated + code will be stored in the module named here. e.g. to generate + a `Foo.Bar` module containing a `MySet` type, you would put + `"Foo.Bar.MySet"` here +- `public-key-type` + - The fully-qualified name of your custom type that you want to use + as elements in the set +- `private-key-type` + - The type of elements to use in the underlying `Set`. This will + typically be `Int` or `String`, but can be any concrete + `comparable` type +- `public-key-to-private-key` + - The fully-qualified name of a function that converts values from + `public-key-type` to `private-key-type`. i.e. it has a type + like `PublicKey -> PrivateKey` +- `private-key-to-public-key` + - The fully-qualified name of a function that converts values from + `private-key-type` to `public-key-type`. It has a type + like `PrivateKey -> Maybe PublicKey`. You can’t use a function + with a type like `PrivateKey -> PublicKey` here — you may need + to write a wrapper function in your code with the correct type Then, run `gen-elm-wrappers`. It expects `elm.json` to be in the current directory. It writes the generated code to the appropriate @@ -75,6 +100,10 @@ For `Dict`s, the generated code wraps all functions from the core `Dict` module. If your program also has `elm-community/dict-extra` as a direct dependency, it will also wrap several functions from `Dict.Extra`. +For `Set`s, the generated code wraps all functions from the core `Set` +module. If your program also has `stoeffel/set-extra` as a direct +dependency, it will also wrap some functions from `Set.Extra`. + If `elm-format` is on your PATH (and not a relative path, i.e. not starting with `.` or `..`) then the generated code will be beautifully formatted. (This is the case, for example, when `elm-format` and From 916d3712ec01b67c6477b1d2e3c19e2de99c8023 Mon Sep 17 00:00:00 2001 From: Dave Hinton Date: Sun, 21 Apr 2024 20:39:41 +0100 Subject: [PATCH 2/7] add integration test case covering writing both Set and Dict --- bin/test.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/bin/test.sh b/bin/test.sh index 28e498b..1f5840d 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -250,6 +250,29 @@ gen_elm_wrappers_set_json=' } ' +gen_elm_wrappers_both_json=' +{ + "generate": [ + { + "underlying-type": "Dict", + "wrapper-type": "Type.DictTimePosix.DictTimePosix", + "public-key-type": "Time.Posix", + "private-key-type": "Int", + "private-key-to-public-key": "Helpers.maybePosixFromMillis", + "public-key-to-private-key": "Time.posixToMillis" + }, + { + "underlying-type": "Set", + "wrapper-type": "Type.SetTimePosix.SetTimePosix", + "public-key-type": "Time.Posix", + "private-key-type": "Int", + "private-key-to-public-key": "Helpers.maybePosixFromMillis", + "public-key-to-private-key": "Time.posixToMillis" + } + ] +} +' + go test github.com/dave4420/gen-elm-wrappers/src BINARY_NAME=gen-elm-wrappers BINARY_VERSION='?.?.?' bin/build-binary.sh @@ -263,6 +286,8 @@ expect_success 'set with set-extra included' "$elm_json_with_set_extra" "$gen_el expect_failure_to_generate 'set with far future elm/core' "$elm_json_with_far_future_elm_core" "$gen_elm_wrappers_set_json" Type.SetTimePosix expect_failure_to_generate 'set with v1.1 set-extra' "$elm_json_with_v1_1_set_extra" "$gen_elm_wrappers_set_json" Type.SetTimePosix +expect_success 'both dict and set with core only' "$elm_json_core_only" "$gen_elm_wrappers_both_json" Type.DictTimePosix Type.SetTimePosix + expect_files_to_contain_current_year LICENSE echo PASS From ca377cbb2410ba7643ec509145c7663688baca0d Mon Sep 17 00:00:00 2001 From: Dave Hinton Date: Sun, 21 Apr 2024 21:23:53 +0100 Subject: [PATCH 3/7] flesh out examples section --- README.md | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b0e1bd4..f3a4b08 100644 --- a/README.md +++ b/README.md @@ -110,9 +110,53 @@ formatted. (This is the case, for example, when `elm-format` and `gen-elm-wrappers` were both installed locally using npm, and you’re running `gen-elm-wrappers` via npm.) -## Examples - -See the [`bin/test.sh`](bin/test.sh) script. +## Example + +If you put this in `src/Helpers.elm`: + +``` +module Helpers exposing (..) + +import Time + +maybePosixFromMillis : Int -> Maybe Time.Posix +maybePosixFromMillis millis = + Just <| Time.millisToPosix millis +``` + +and then you put this in `gen-elm-wrappers.json`: + +``` +{ + "generate": [ + { + "underlying-type": "Dict", + "wrapper-type": "Type.DictTimePosix.DictTimePosix", + "public-key-type": "Time.Posix", + "private-key-type": "Int", + "private-key-to-public-key": "Helpers.maybePosixFromMillis", + "public-key-to-private-key": "Time.posixToMillis" + }, + { + "underlying-type": "Set", + "wrapper-type": "Type.SetTimePosix.SetTimePosix", + "public-key-type": "Time.Posix", + "private-key-type": "Int", + "private-key-to-public-key": "Helpers.maybePosixFromMillis", + "public-key-to-private-key": "Time.posixToMillis" + } + ] +} +``` + +then `gen-elm-wrappers` will produce + +- a `Type.DictTimePosix` module in `src/Type/DictTimePosix.elm`, + containing a `DictTimePosix v` type that acts like a `Dict` with + `Time.Posix` keys and `v` values +- a `Type.SetTimePosix` module in `src/Type/SetTimePosix.elm`, + containing a `SetTimePosix` type that acts like a `Set` with + `Time.Posix` elements ## Portability From 20d198cbcb7afdd470d27c543dad6afa4b616bc4 Mon Sep 17 00:00:00 2001 From: Dave Hinton Date: Sun, 21 Apr 2024 21:35:22 +0100 Subject: [PATCH 4/7] update roadmap, job hunt --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f3a4b08..14e7934 100644 --- a/README.md +++ b/README.md @@ -170,10 +170,11 @@ Actually… the test script won’t run on Windows. (Unless you use WSL?) This isn’t in priority order yet and I’ve probably forgotten something. -- Support `Set` -- Support type variables in key types +- Support type variables in dict key types and set element types - Support versions of `elm-community/dict-extra` before 2.4.0 -- Wrap more functions from `elm-community/dict-extra` +- Support versions of `stoeffel/set-extra` before 1.2.0 +- Wrap more functions from `elm-community/dict-extra` and + `stoeffel/set-extra` - Support writing the generated code to a directory other than `src`; optionally wipe it first - Write more unit tests around reading the config from `elm.json` @@ -196,7 +197,8 @@ This runs the unit tests whenever the source code changes. ## If you’re a hiring manager or a recruiter -I’m not looking for a job, no. +I’m not looking for a job, no. Although I’m always interested in hearing +about jobs involving Elm and/or climate tech. - Senior / tech lead roles - Full stack or backend @@ -210,4 +212,5 @@ I’m not looking for a job, no. und Englisch is meine Muttersprache. Please [connect to me on LinkedIn](https://www.linkedin.com/in/dave-hinton-7507b4ab) -and mention this repo in your invitation. +and mention this repo in your invitation (and don’t bury the lede if you want to +talk to me about an Elm or climate tech job). From e309e0b34847daf02abd4caf6f7619ce55e67810 Mon Sep 17 00:00:00 2001 From: Dave Hinton Date: Sun, 21 Apr 2024 21:52:58 +0100 Subject: [PATCH 5/7] add limitations section --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 14e7934..c146d7f 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,17 @@ then `gen-elm-wrappers` will produce containing a `SetTimePosix` type that acts like a `Set` with `Time.Posix` elements +## Limitations + +- It won’t work if you try to wrap more than one type into the same module +- `Set.map` is not wrapped +- `Dict.Extra`: + - `removeMany` and `keepOnly` are not wrapped, even when `Set` is also + being wrapped for the key type + - `mapKeys` and `invert` are not wrapped +- `Set.Extra`: + - `concatMap` and `filterMap` are not wrapped + ## Portability I’ve only tested this on my Mac. But it’s written in Go, and I hear From 438be71612b6c8f34294c721981aa2a9f739c37f Mon Sep 17 00:00:00 2001 From: Dave Hinton Date: Sun, 21 Apr 2024 21:53:12 +0100 Subject: [PATCH 6/7] [skip ci] From 3f300a13654a1bbc057e190efca65a4047daf59a Mon Sep 17 00:00:00 2001 From: Dave Hinton Date: Sun, 21 Apr 2024 21:57:30 +0100 Subject: [PATCH 7/7] [run ci]