lue-bird / elm-morph / Value

Generic, case-able elm value

json encoders/decoders are pretty low-level which makes them mildly unpleasant for serialization, explicitly describing how to serialize individual data types. Data that has same shape could at the low level be coded differently, which means you have to spell it out explicitly each time.

Switching to a different format would also be a lot of work; some low-level primitives like bools might not be supported etc.

This module has all the types and operations for these generic Values while Value.Morph and most other modules contain morphs to convert your types.


type alias Value tag =
AtomOrComposed Atom (Composed tag)

Generic representation of any value representable in elm

Use the MorphValues present in most modules of this package to convert a Value.


type Atom
    = Unit ()
    | Number Decimal
    | String String

An elm literal (that doesn't itself contain other values)


type Composed tag
    = List (List (Value tag))
    | Record (Record tag)
    | Variant (Tagged tag)

elm structure that can itself contain values

Other kinds of structures should be converted to one of these. A tuple for example can be represented as a record.

Note: To not have overly complex recursive-ish types, a Value.Composed can describe an invalid elm value, for example

List [ Unit (), String "huh" ]


type alias Record tag =
List (Tagged tag)

The structure of a record which can hold multiple tagged field values


type alias Tagged tag =
RecordWithoutConstructorFunction { tag : tag
, value : Value tag 
}

tag-Value pair used to represent a field or a variant

atomKindToString : Atom -> String

Describe the type of Atom

composedKindToString : Composed tag_ -> String

Describe the type of Composed

tag


type IndexOrName
    = Index Basics.Int
    | Name String

Either an index Int or a name String that can identify a record part or variant.

Used as the tag in the narrowed result of a MorphValue


type alias IndexAndName =
RecordWithoutConstructorFunction { index : Basics.Int
, name : String 
}

Both an index Int and a name String that can identify a record part or variant.

Used as the tag in the broadened result of a MorphValue

tagMap : (tag -> tagMapped) -> Value tag -> Value tagMapped

Reduce the amount of tag information of the Value

an abstract concept: atom or composed


type AtomOrComposed atom composed
    = Atom atom
    | Composed composed

Either

composedMap : (composed -> composedMapped) -> AtomOrComposed atom composed -> AtomOrComposed atom composedMapped

If the AtomOrComposed is a Composed, change in a given way

atomMap : (atom -> atomMapped) -> AtomOrComposed atom composed -> AtomOrComposed atomMapped composed

If the AtomOrComposed is an Atom, change in a given way

oh look! other projects do similar things