Hello. I basically want to mimic this feature that I found on Flutter, which maps dart objects like this:
@JsonSerializable()
class Person {
Person();
int? id;
String? name;
@JsonKey(name: 'date_of_birth')
int? dateOfBirth;
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}
and then when we run in the cli:
flutter pub run build_runner build
the script will create an additional model.g.dart
file which contains the de/serialization code like this:
Person _$PersonFromJson(Map<String, dynamic> json) => Person()
..id = json['id'] as int?
..name = json['name'] as String?
..dateOfBirth = json['date_of_birth'] as int?;
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
'id': instance.id,
'name': instance.name,
'date_of_birth': instance.dateOfBirth,
};
Now I want this for Elm. For example if we have an elm class like this
type alias Customer =
{ -- Base
uuid : Maybe String
, createdAt : Maybe Int
, updatedAt : Maybe Int
, deletedAt : Maybe Int
-- Base end
, name : Maybe String
, phone : Maybe String
, address : Maybe String
}
and then we run something like this:
npm run elm-record-codegen --case camelCase --type pipeline
we get a file containing the automatically mapped en/decoders for the class
customerEncoder : Customer -> Encode.Value
customerEncoder customer =
Encode.object
[ -- Base
( "uuid", EncodeExtra.maybe Encode.string customer.uuid )
, ( "createdAt", EncodeExtra.maybe Encode.int customer.createdAt )
, ( "updatedAt", EncodeExtra.maybe Encode.int customer.updatedAt )
, ( "deletedAt", EncodeExtra.maybe Encode.int customer.deletedAt )
-- Base end
, ( "name", EncodeExtra.maybe Encode.string customer.name )
, ( "phone", EncodeExtra.maybe Encode.string customer.phone )
, ( "address", EncodeExtra.maybe Encode.string customer.address )
]
customerDecoder : Decode.Decoder Customer
customerDecoder =
Decode.succeed Customer
-- Base
|> Pipeline.required "uuid" (Decode.maybe Decode.string)
|> Pipeline.required "createdAt" (Decode.maybe Decode.int)
|> Pipeline.required "updatedAt" (Decode.maybe Decode.int)
|> Pipeline.required "deletedAt" (Decode.maybe Decode.int)
-- Base end
|> Pipeline.required "name" (Decode.maybe Decode.string)
|> Pipeline.required "phone" (Decode.maybe Decode.string)
|> Pipeline.required "address" (Decode.maybe Decode.string)
Is there any package which behaves like this? If not, what should I learn to create something like this? I imagine I need an elm source code parser similar to rust’s token parser, detect type alias
es in source code, and then finally generate json en/decoders.
It will tremendously help me in creating elm apps which use lots of data with lots of complex data structures, so that i don’t have to type too much boilerplate en/decoders.