Deliverable for D3.2

Decoders.elm 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. module Decoders exposing (..)
  2. import Json.Decode exposing (Decoder, string, int, float, dict, list, bool, map, value, decodeValue, decodeString, lazy, succeed, fail, andThen)
  3. import Dict exposing (Dict)
  4. type alias Items =
  5. List Item
  6. decodeItems : Json.Decode.Decoder (List Item)
  7. decodeItems =
  8. Json.Decode.list decodeItem
  9. type alias Item =
  10. { key : String
  11. , location : Location
  12. , sample : String
  13. , tags : List String
  14. , uid : String
  15. }
  16. decodeItem : Json.Decode.Decoder Item
  17. decodeItem =
  18. Json.Decode.map5 Item
  19. (Json.Decode.field "key" Json.Decode.string)
  20. (Json.Decode.field "location" decodeLocation)
  21. (Json.Decode.field "sample" Json.Decode.string)
  22. (Json.Decode.field "tags" (Json.Decode.list Json.Decode.string))
  23. (Json.Decode.field "uid" Json.Decode.string)
  24. type alias Location =
  25. { ipAddress : String
  26. , ipPort : Int
  27. , scheme : String
  28. , uid : String
  29. , right : Right
  30. }
  31. type Right
  32. = Unknown
  33. | RequestAccess
  34. | Requesting
  35. decodeLocation : Json.Decode.Decoder Location
  36. decodeLocation =
  37. Json.Decode.map5 Location
  38. (Json.Decode.field "ip-address" Json.Decode.string)
  39. (Json.Decode.field "port" Json.Decode.int)
  40. (Json.Decode.field "scheme" Json.Decode.string)
  41. (Json.Decode.field "uid" Json.Decode.string)
  42. (Json.Decode.succeed Unknown)
  43. -- optimistic assumption is that we can view
  44. type alias DataResponse =
  45. { data : List DataItem
  46. }
  47. decodeDataResponse : Decoder DataResponse
  48. decodeDataResponse =
  49. Json.Decode.map DataResponse
  50. (Json.Decode.field "data" (Json.Decode.list decodeDataItem))
  51. type alias DataItem =
  52. { value : JsVal
  53. , timeStamp : String
  54. }
  55. decodeDataItem : Decoder DataItem
  56. decodeDataItem =
  57. Json.Decode.map2 DataItem
  58. (Json.Decode.field "value" jsValDecoder)
  59. (Json.Decode.field "ts" Json.Decode.string)
  60. type JsVal
  61. = JsString String
  62. | JsInt Int
  63. | JsFloat Float
  64. | JsArray (List JsVal)
  65. | JsObject (Dict String JsVal)
  66. | JsNull
  67. jsValDecoder : Decoder JsVal
  68. jsValDecoder =
  69. Json.Decode.oneOf
  70. [ Json.Decode.map JsString Json.Decode.string
  71. , Json.Decode.map JsInt Json.Decode.int
  72. , Json.Decode.map JsFloat Json.Decode.float
  73. , Json.Decode.list (Json.Decode.lazy (\_ -> jsValDecoder)) |> Json.Decode.map JsArray
  74. , Json.Decode.dict (Json.Decode.lazy (\_ -> jsValDecoder)) |> Json.Decode.map JsObject
  75. , Json.Decode.null JsNull
  76. ]
  77. type alias Entitlement =
  78. { subject : String
  79. , level : AccessLevel
  80. , uid : String
  81. , status : String
  82. }
  83. type AccessLevel
  84. = OwnerOnly
  85. | CanAccess
  86. | CanDiscover
  87. decodeEntitlement : Decoder Entitlement
  88. decodeEntitlement =
  89. Json.Decode.map4 Entitlement
  90. (Json.Decode.field "subject" Json.Decode.string)
  91. (Json.Decode.field "level" decodeAccessLevel)
  92. (Json.Decode.field "uid" Json.Decode.string)
  93. (Json.Decode.field "status" Json.Decode.string)
  94. decodeAccessLevel : Decoder AccessLevel
  95. decodeAccessLevel =
  96. Json.Decode.string
  97. |> Json.Decode.andThen
  98. (\str ->
  99. case str of
  100. "owner-only" ->
  101. Json.Decode.succeed OwnerOnly
  102. "can-access" ->
  103. Json.Decode.succeed CanAccess
  104. "can-discover" ->
  105. Json.Decode.succeed CanDiscover
  106. somethingElse ->
  107. Json.Decode.fail <| "Unknown AccessLevel: " ++ somethingElse
  108. )
  109. type alias Entitlements =
  110. List Entitlement
  111. decodeEntitlements : Decoder Entitlements
  112. decodeEntitlements =
  113. Json.Decode.list decodeEntitlement
  114. type alias MetadataItem =
  115. { subject : String
  116. , description : String
  117. , name : String
  118. , path : String
  119. }
  120. decodeMetadataItem : Decoder MetadataItem
  121. decodeMetadataItem =
  122. Json.Decode.map4 MetadataItem
  123. (Json.Decode.field "subject" Json.Decode.string)
  124. (Json.Decode.field "description" Json.Decode.string)
  125. (Json.Decode.field "name" Json.Decode.string)
  126. (Json.Decode.field "path" Json.Decode.string)
  127. type alias Metadata =
  128. List MetadataItem
  129. decodeMetadata : Decoder Metadata
  130. decodeMetadata =
  131. Json.Decode.list decodeMetadataItem
  132. type alias Device =
  133. { typez : String
  134. , description : String
  135. , uid : String
  136. , name : String
  137. }
  138. decodeDevice : Decoder Device
  139. decodeDevice =
  140. Json.Decode.map4 Device
  141. (Json.Decode.field "type" Json.Decode.string)
  142. (Json.Decode.field "description" Json.Decode.string)
  143. (Json.Decode.field "uid" Json.Decode.string)
  144. (Json.Decode.field "name" Json.Decode.string)
  145. type alias Devices =
  146. List Device
  147. decodeDevices : Decoder Devices
  148. decodeDevices =
  149. Json.Decode.list decodeDevice