Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Keys into Map Text X by #440

Open
meghfossa opened this issue Apr 30, 2024 · 0 comments
Open

Dynamic Keys into Map Text X by #440

meghfossa opened this issue Apr 30, 2024 · 0 comments

Comments

@meghfossa
Copy link

meghfossa commented Apr 30, 2024

I've been trying to figure out how I can go about parsing something like following:

[server.a.metadata]
port = 1000

[server.b]
backoff = 1

[server.b.metadata]
port = 2000

In this,

  • server.*.metadata table exist - it could be a, b, c, prod, staging and so on (not known ahead of time)

I would like to parse it into following structure,

data ServerMetadata = ServerMetadata { port :: Int, backoff :: Maybe Int }

type Servers = Map Text ServerMetadata
-- fromList [
--  ("a", ServerMetadata 1000 Nothing), 
--  ("b", ServerMetadata 1000 (Just 1)
-- ]

I tried using tableMap, and variety of combinations - but I was not able to get it to work. Appreciate any pointers on how to accomplish this.

If it helps here is the raw toml.

Right
    ( TOML
        { tomlPairs = fromList []
        , tomlTables = fromList
            [
                ( "server"
                , Branch
                    ( "server" :| [] ) Nothing
                    ( fromList
                        [
                            ( "a"
                            , Leaf
                                ( "a" :| [ "metadata" ] )
                                ( TOML
                                    { tomlPairs = fromList
                                        [
                                            ( "port" :| []
                                            , Integer 1000
                                            )
                                        ]
                                    , tomlTables = fromList []
                                    , tomlTableArrays = fromList []
                                    }
                                )
                            )
                        ,
                            ( "b"
                            , Leaf
                                ( "b" :| [] )
                                ( TOML
                                    { tomlPairs = fromList
                                        [
                                            ( "backoff" :| []
                                            , Integer 1
                                            )
                                        ]
                                    , tomlTables = fromList
                                        [
                                            ( "metadata"
                                            , Leaf
                                                ( "metadata" :| [] )
                                                ( TOML
                                                    { tomlPairs = fromList
                                                        [
                                                            ( "port" :| []
                                                            , Integer 2000
                                                            )
                                                        ]
                                                    , tomlTables = fromList []
                                                    , tomlTableArrays = fromList []
                                                    }
                                                )
                                            )
                                        ]
                                    , tomlTableArrays = fromList []
                                    }
                                )
                            )
                        ]
                    )
                )
            ]
        , tomlTableArrays = fromList []
        }
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant