1. Maxim Moiseev
  2. Linq2vk

Source

Linq2vk / src / UserAPI / Common / Conversions.fs

namespace Linq2vk.Core

[<RequireQualifiedAccess>] 
module Conversions =
    open System
    open JsonParser
    open Linq2vk.Core.Implementation.DataKind
    
    let apply parser xs =
        JsonParser.parse parser (Seq.toArray xs)
    
    let toFriend = function
        | JsonArr arr -> apply Parsers.friend arr
        | _ -> raise <| NotSupportedException "Conversion only supported for JsonArr"
        
    let toItemArray conv = function
        |JsonArr arr -> arr |> Seq.map (string >> conv) |> Seq.toArray
        | _ -> raise <| NotSupportedException "Conversion only supported for JsonArr"

    let toStatus = function
        | JsonArr arr ->
            apply Parsers.status arr
        | JsonData o ->
            apply Parsers.status (o.PropertyValues())
        | _ -> raise <| NotSupportedException "Conversion only supported for JsonArr or JsonData"

    let toPhoto = function
        | JsonArr arr -> apply Parsers.photo arr
        | _ -> raise <| NotSupportedException "Conversion only supported for JsonArr"

    // TODO: implement a Json Object parsers equivalent to Json Array parsers to handle this kind of situation
    open Tools
    let toUserProfile = function
        | JsonData o ->
            { new IUserProfile with
                override this.UserId = o.["id"] |> int64
                override this.FirstName = o.["fn"] |> string'
                override this.LastName = o.["ln"] |> string'
                override this.MaidenName = o.["mn"] |> string'
                override this.CurrentStatus = o.["actv"] |> string |> toStatus
                override this.CountryId = o.["ht"].["coi"] |> int64
                override this.ISOCountryCode = o.["ht"].["con"] |> string'
                override this.CityId = o.["ht"].["cii"] |> int64
                override this.CityName = o.["ht"].["cin"] |> string'
                override this.Gender = o.["sx"] |> int |> enum<Gender>
                override this.PhotoUrl = o.["bp"] |> string'
                override this.BirthdayDay = o.["bd"] |> int
                override this.BirthdayMonth = o.["bm"] |> int
                override this.BirthdayYear = o.["by"] |> int
                override this.MaritalStatus = o.["fs"] |> int |> enum<MaritalStatus>
                override this.PoliticalViews = o.["pv"] |> int |> enum<PoliticalViews>
                override this.Friends = o.["fr"].["d"] |> string |> toItemArray toFriend
                override this.FriendsOnline = o.["fro"].["d"] |> string |> toItemArray toFriend
                override this.FriendsMutual = o.["frm"].["d"] |> string |> toItemArray toFriend
                override this.UploadedPhotos = o.["ph"].["d"] |> string |> toItemArray toPhoto
                override this.PhotosWith = o.["phw"].["d"] |> string |> toItemArray toPhoto
            }
        | _ -> raise <| NotSupportedException "Conversion only supported for JsonData"
        
    let toMessage = function
        | JsonArr arr -> apply Parsers.message arr
        | _ -> raise <| NotSupportedException "Conversion only supported for JsonArr"