@@ -466,3 +466,80 @@ pub fn geometrycollection_example_test() {
466
466
467
467
assert_decode ( json_string , expected_geojson )
468
468
}
469
+
470
+ pub fn feature_decode_properties_test ( ) {
471
+ let json_string =
472
+ "
473
+ {
474
+ \" type\" : \" Feature\" ,
475
+ \" geometry\" : {
476
+ \" type\" : \" Point\" ,
477
+ \" coordinates\" : [102.0, 0.5]
478
+ },
479
+ \" properties\" : {
480
+ \" prop0\" : \" value0\" ,
481
+ \" prop1\" : 0.0,
482
+ \" prop2\" : {
483
+ \" this\" : \" that\"
484
+ },
485
+ \" prop3\" : [1, 2, 3],
486
+ \" prop4\" : true,
487
+ \" prop5\" : null
488
+ }
489
+ }
490
+ "
491
+
492
+ let decoded_result =
493
+ json . decode ( from : json_string , using : gleojson . geojson_decoder )
494
+
495
+ let assert Ok ( gleojson . GeoJSONFeature ( gleojson . Feature (
496
+ geometry ,
497
+ properties ,
498
+ id ,
499
+ ) ) ) = decoded_result
500
+
501
+ geometry
502
+ |> should . be_some
503
+ |> should . equal ( gleojson . Point ( [ 102.0 , 0.5 ] ) )
504
+
505
+ id
506
+ |> should . be_none
507
+
508
+ gleojson . get_feature_property ( properties , "prop0" , dynamic . string )
509
+ |> should . be_ok
510
+ |> should . equal ( "value0" )
511
+
512
+ gleojson . get_feature_property ( properties , "prop1" , dynamic . float )
513
+ |> should . be_ok
514
+ |> should . equal ( 0.0 )
515
+
516
+ gleojson . get_feature_property (
517
+ properties ,
518
+ "prop2" ,
519
+ dynamic . dict ( of : dynamic . string , to : dynamic . string ) ,
520
+ )
521
+ |> should . be_ok
522
+ |> should . equal ( dict . from_list ( [ # ( "this" , "that" ) ] ) )
523
+
524
+ gleojson . get_feature_property ( properties , "prop3" , dynamic . list ( dynamic . int ) )
525
+ |> should . be_ok
526
+ |> should . equal ( [ 1 , 2 , 3 ] )
527
+
528
+ gleojson . get_feature_property ( properties , "prop4" , dynamic . bool )
529
+ |> should . be_ok
530
+ |> should . equal ( True )
531
+
532
+ gleojson . get_feature_property (
533
+ properties ,
534
+ "prop5" ,
535
+ dynamic . optional ( dynamic . dynamic ) ,
536
+ )
537
+ |> should . be_ok
538
+ |> should . equal ( option . None )
539
+
540
+ gleojson . get_feature_property ( properties , "missing" , dynamic . string )
541
+ |> should . be_error
542
+
543
+ gleojson . get_feature_property ( properties , "prop0" , dynamic . float )
544
+ |> should . be_error
545
+ }
0 commit comments