Play

Check-in [b5d7173dee]
Login
Overview
Comment:Refactor code to improve readability. Also validate key entries of dependencies field in play.json.
Timelines: family | ancestors | descendants | both | modules
Files: files | file ages | folders
SHA3-256: b5d7173deeb9ed6474e688ee193e8fc7e496f13548bb1d34721b5cde1a0f320b
User & Date: robin.hansen on 2021-02-15 09:38:00
Other Links: branch diff | manifest | tags
Context
2021-02-19
16:29
PackageLoader now supports reading in all relevant play.json files. check-in: 1285a94244 user: robin.hansen tags: modules
2021-02-15
09:38
Refactor code to improve readability. Also validate key entries of dependencies field in play.json. check-in: b5d7173dee user: robin.hansen tags: modules
2021-02-14
11:47
Add json decoder for PackageMetadata. Missing validation of key values in dependencies field, and sh... check-in: 38d406f888 user: robin.hansen tags: modules
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Data/PackageMetadata.elm from [291107f608] to [cab47e39e5].

20
21
22
23
24
25
26











27





28
29





30
31





32














33
34
35
36
37
38
39
40
41
42
    , packagePaths : List PackagePath
    }


decoder : Json.Decoder PackageMetadata
decoder =
    Json.map6 PackageMetadata











        (Json.field "name" Json.string |> Json.andThen (resultDecodeAdapt << PackageName.fromString))





        (Json.field "version" Json.string |> Json.andThen (resultDecodeAdapt << SemanticVersion.fromString))
        (Json.field "language-version" Json.string |> Json.andThen (resultDecodeAdapt << SemanticVersion.fromString))





        (Json.field "exposed-modules" (Json.list (Json.string |> Json.andThen (resultDecodeAdapt << ModuleName.fromString))))
        (Json.field "dependencies" (Json.dict (Json.string |> Json.andThen (resultDecodeAdapt << SemanticVersion.fromString))))





        (Json.field "package-paths" (Json.list (Json.string |> Json.map PackagePath.fromString)))
















resultDecodeAdapt : Result err ok -> Json.Decoder ok
resultDecodeAdapt result =
    case result of
        Ok value ->
            Json.succeed value

        Err err ->
            Json.fail "Something went wrong"







>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
|
<
>
>
>
>
>
|
<
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>










20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    , packagePaths : List PackagePath
    }


decoder : Json.Decoder PackageMetadata
decoder =
    Json.map6 PackageMetadata
        (Json.field "name" packageNameDecoder)
        (Json.field "version" semverDecoder)
        (Json.field "language-version" semverDecoder)
        (Json.field "exposed-modules" (Json.list moduleNameDecoder))
        (Json.field "dependencies" (Json.dict semverDecoder |> Json.andThen validateDependencyKeys))
        (Json.field "package-paths" (Json.list packagePathDecoder))


packageNameDecoder : Json.Decoder PackageName
packageNameDecoder =
    Json.string
        |> Json.andThen (resultDecodeAdapt << PackageName.fromString)


semverDecoder : Json.Decoder SemanticVersion
semverDecoder =
    Json.string
        |> Json.andThen (resultDecodeAdapt << SemanticVersion.fromString)



moduleNameDecoder : Json.Decoder ModuleName
moduleNameDecoder =
    Json.string
        |> Json.andThen (resultDecodeAdapt << ModuleName.fromString)



packagePathDecoder : Json.Decoder PackagePath
packagePathDecoder =
    Json.string
        |> Json.map PackagePath.fromString


validateDependencyKeys : Dict String SemanticVersion -> Json.Decoder (Dict String SemanticVersion)
validateDependencyKeys deps =
    let
        isValid =
            Dict.keys deps
                |> List.all (\k -> Result.toMaybe (PackageName.fromString k) /= Nothing)
    in
    if isValid then
        Json.succeed deps

    else
        Json.fail "Invalid dependency package name"


resultDecodeAdapt : Result err ok -> Json.Decoder ok
resultDecodeAdapt result =
    case result of
        Ok value ->
            Json.succeed value

        Err err ->
            Json.fail "Something went wrong"

Modified tests/Test/PackageLoader.elm from [393af2926c] to [a2e078e664].

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
                        "name": "robheghan/fnv",
                        "version": "1.0.0",
                        "language-version": "0.2.0",
                        "exposed-modules": [
                            "fnv"
                        ],
                        "dependencies": {
                            "template_string": "1.2.0"
                        },
                        "package-paths": [
                            "lib/*"
                        ]
                    }
                    """
                    |> Expect.ok
        ]







|








18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
                        "name": "robheghan/fnv",
                        "version": "1.0.0",
                        "language-version": "0.2.0",
                        "exposed-modules": [
                            "fnv"
                        ],
                        "dependencies": {
                            "jarvis/template_strings": "1.2.0"
                        },
                        "package-paths": [
                            "lib/*"
                        ]
                    }
                    """
                    |> Expect.ok
        ]