elm-protobuf icon indicating copy to clipboard operation
elm-protobuf copied to clipboard

Option to ignore deprecated fields

Open jalandis opened this issue 4 years ago • 1 comments

Overview

Adds a new protobuf plugin option to ignore all deprecated fields + messages.

--elm_opt=remove-deprecated

While implementing the new feature I refactored a bit as I went. The number of changes snowballed a ~little~ lotta bit.

  • Switches to GO templating
  • Upgrades dependencies + switches to GO modules
  • Refactors using recommended GO package structure
  • Converts to Github Actions for test suite

Driving Use Case

Deprecating an API field requires the removal of all references from the frontend codebase. It would be nice to rely on compile-time errors to enforce this required deprecation.

Example protobuf with deprecated fields + messages.

message Bar {
    bool field = 1;
    bool deprecatedField = 2 [deprecated=true];
}
  
message Foo {
    option deprecated = true;

    bool field = 1;
}

Breaking Changes

The change to the supported GO version technically makes this a backward-incompatible change. Not sure if that is important for the project as it is a standalone executable. Just in case that does matter, it might be nice to include a few other breaking changes before any new release.

  1. Nested oneof values - the same solution for enums could be used (https://github.com/tiziano88/elm-protobuf/issues/8)
  2. Name collisions with multiple files (https://github.com/tiziano88/elm-protobuf/issues/28)
  3. Optional decoders should fail with bad data (https://github.com/tiziano88/elm-protobuf/blob/master/tests/Main.elm#L43)
        ...
        , describe "better required"
            [ test "Decode required field with bad data" <| \() -> JD.decodeString (required "fail"  JD.int 0) badData |> Expect.err
            , test "Decode missing required field" <| \() -> JD.decodeString (required "succeed" JD.int 0) badData |> equal (Ok 0)
            ]
        ]


required : String -> JD.Decoder a -> a -> JD.Decoder a
required name decoder default =
    JD.maybe (JD.field name (JD.succeed Nothing))
        |> JD.andThen
            (\r ->
                case r of
                    Just _ ->
                        JD.field name decoder

                    Nothing ->
                        JD.succeed default
            )


badData : String
badData =
    """{ "fail": "wrong type" }"""

New decoder is similar to elm-json-decode-pipeline solution https://github.com/NoRedInk/elm-json-decode-pipeline/blob/1.0.0/src/Json/Decode/Pipeline.elm#L111

jalandis avatar Feb 27 '21 17:02 jalandis

Wow, this is great, thanks for contributing! It will take me a while to review it, but I'll be happy to merge it as soon as I can!

tiziano88 avatar Feb 28 '21 23:02 tiziano88