-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove null properties encoder and decoder
Signed-off-by: Aleksei Gurianov <[email protected]>
- Loading branch information
Showing
6 changed files
with
199 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import decode/zero | ||
import examples/to_dynamic_decoder.{to_dynamic_decoder} | ||
import gleam/bool | ||
import gleam/float | ||
import gleam/int | ||
import gleam/io | ||
import gleam/json | ||
import gleam/option | ||
import gleojson | ||
|
||
// Define custom properties type for your features | ||
pub type ParkProperties { | ||
ParkProperties( | ||
name: String, | ||
area_sq_km: Float, | ||
year_established: Int, | ||
is_protected: Bool, | ||
) | ||
} | ||
|
||
// Define decoder for your custom properties | ||
fn park_properties_decoder() { | ||
use name <- zero.field("name", zero.string) | ||
use area_sq_km <- zero.field("area_sq_km", zero.float) | ||
use year_established <- zero.field("year_established", zero.int) | ||
use is_protected <- zero.field("is_protected", zero.bool) | ||
ParkProperties(name:, area_sq_km:, year_established:, is_protected:) | ||
|> zero.success | ||
} | ||
|
||
pub fn main() { | ||
// Example GeoJSON string representing a national park | ||
let json_string = | ||
"{ | ||
\"type\": \"Feature\", | ||
\"geometry\": { | ||
\"type\": \"Point\", | ||
\"coordinates\": [-119.5383, 37.8651] | ||
}, | ||
\"properties\": { | ||
\"name\": \"Yosemite National Park\", | ||
\"area_sq_km\": 3029.87, | ||
\"year_established\": 1890, | ||
\"is_protected\": true | ||
}, | ||
\"id\": \"yosemite\" | ||
}" | ||
|
||
// Decode the JSON string into a GeoJSON object | ||
let decoded = | ||
json.decode( | ||
from: json_string, | ||
using: to_dynamic_decoder( | ||
gleojson.geojson_decoder(park_properties_decoder()), | ||
), | ||
) | ||
|
||
// Handle the decoded result | ||
case decoded { | ||
Ok(geojson) -> { | ||
case geojson { | ||
gleojson.GeoFeature(feature) -> { | ||
case feature.properties { | ||
option.Some(ParkProperties(name, area, year, is_protected)) -> { | ||
io.println( | ||
"Decoded " <> name <> ", established in " <> int.to_string(year), | ||
) | ||
io.println( | ||
"Area: " | ||
<> float.to_string(area) | ||
<> " sq km, Protected: " | ||
<> bool.to_string(is_protected), | ||
) | ||
} | ||
option.None -> io.println("Feature has no properties") | ||
} | ||
} | ||
_ -> io.println("Decoded a different type of GeoJSON object") | ||
} | ||
} | ||
Error(_error) -> io.println("Failed to decode") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import decode/zero.{type Decoder} | ||
import gleam/dynamic.{type Dynamic} | ||
|
||
pub fn to_dynamic_decoder(zero_decoder: Decoder(a)) { | ||
fn(dynamic_value: Dynamic) { zero.run(dynamic_value, zero_decoder) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters