haskell-hvr / HsYAML

YAML 1.2 implementation in pure Haskell
https://hackage.haskell.org/package/HsYAML
GNU General Public License v2.0
55 stars 17 forks source link

Maping fields order is not preserved #64

Open majkrzak opened 2 years ago

majkrzak commented 2 years ago

Encoding the following mapping

encode [mapping ["a" .= (1 :: Int), "d" .= (2 :: Int), "c" .= (3 :: Int), "b" .= (4 :: Int)]]

results in:

a: 1
b: 4
c: 3
d: 2
sjakobi commented 2 years ago

Is this a feature request?

majkrzak commented 2 years ago

Depend how to treat it. According to https://yaml.org/spec/1.2.2/#3221-mapping-key-order mapping key order is not important, but from the point of readability is.

sjakobi commented 2 years ago

I agree that preserving the field order is desirable. I'd be happy to accept a PR that implements this.

I think we need to be a bit careful not to introduce a performance regression with this though:

https://github.com/haskell-hvr/HsYAML/blob/be6040095fa7ad442c28d5c56ce9da3578807ea4/src/Data/YAML/Internal.hs#L40-L41

A simple list of pairs might be too slow during lookups:

 type Mapping loc = [(Node loc, Node loc)]

So a type similar to Dhall.Map might be a better choice.