xyncro / chiron

JSON for F#
https://xyncro.tech/chiron
MIT License
175 stars 41 forks source link

Q: better way to write? #37

Open haf opened 9 years ago

haf commented 9 years ago

I've been trying to find a better way to write this with the current API, but can't think of it at the moment:

  let writeJsonBody : SessionOptions -> Json<unit> =
    List.fold (fun acc -> function
              | LockDelay dur -> acc *> Json.write "LockDelay" (Duration.consulString dur)
              | Node node     -> acc *> Json.write "Node" node
              | Name name     -> acc *> Json.write "Name" name
              | Checks checks -> acc *> Json.write "Checks" checks
              | Behaviour sb  -> acc *> Json.write "Behavior" sb
              | TTL dur       -> acc *> Json.write "TTL" (Duration.consulString dur))
              (fun json -> Value (), Json.Object Map.empty)

  let reqBody = Json.format (snd (writeJsonBody sessionOpts (Json.Null ())))

I'm mostly thinking how the last two lines with contents both declare Json data and how I have to use snd to get the Json result of the monad value.

haf commented 9 years ago

Ping

kolektiv commented 9 years ago

Pong! Right, brain back in F# :) I'm not sure I can think of an obviously nicer way at the moment, but I'm not totally sure what SessionOptions looks like. Can you point me to the wider source, and I'll have a further think!

haf commented 9 years ago
type SessionOptions = SessionOption list
type SessionOption =
  | LockDelay of Duration
  ...
  | TTL of Duration

Thanks for helping out =) Creds!