Skip to content

Commit e434c16

Browse files
committed
add typed property retrieval function
1 parent 745997a commit e434c16

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

src/DynamicObj/DynObj.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ module DynObj =
6363
| _ -> first.SetValue(kv.Key,kv.Value)
6464
first
6565

66+
67+
let inline tryGetTypedValue<'a> (name) (dynObj : DynamicObj) =
68+
match (dynObj.TryGetValue name) with
69+
| None -> None
70+
| Some o ->
71+
match o with
72+
| :? 'a as o -> o |> Some
73+
| _ -> None
74+
6675
let setValue (dyn:DynamicObj) propName o =
6776
dyn.SetValue(propName,o)
6877

src/DynamicObj/DynamicObj.fs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ type DynamicObj() =
2727
member this.GetValue (name) =
2828
this.TryGetValue(name).Value
2929

30-
/// Gets property value
30+
/// Gets typed property value if type matches, otherwise None.
31+
///
32+
/// WARNING: This Method is not supported in Fable transpiled code. Use static method tryGetTypedValue instead.
3133
member this.TryGetTypedValue<'a> name =
34+
#if !FABLE_COMPILER
3235
match (this.TryGetValue name) with
3336
| None -> None
34-
| Some o ->
37+
| Some o ->
3538
match o with
36-
| :? 'a -> o :?> 'a |> Some
39+
| :? 'a as o -> o |> Some
3740
| _ -> None
38-
41+
#else
42+
failwith "Method TryGetTypedValue is not supported in Fable transpiled code. Use static method tryGetTypedValue instead."
43+
#endif
3944

4045
member this.TryGetStaticPropertyInfo name : PropertyHelper option =
4146
ReflectionUtils.tryGetStaticPropertyInfo this name

tests/DynamicObject.Tests/DynamicObj.fs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,67 @@ open System
44
open Fable.Pyxpecto
55
open DynamicObj
66

7+
let tests_tryGetTypedValue = testList "TryGetTypedValue" [
8+
9+
testCase "typeof" <| fun _ ->
10+
let a = typeof<int>
11+
Expect.equal a.Name "Int32" "Type should be Int32"
12+
13+
testCase "NonExisting" <| fun _ ->
14+
let a = DynamicObj()
15+
let b = DynObj.tryGetTypedValue<int> "a" a
16+
Expect.isNone b "Value should not exist"
17+
18+
testCase "Correct Int" <| fun _ ->
19+
let a = DynamicObj()
20+
a.SetValue("a", 1)
21+
let b = DynObj.tryGetTypedValue<int> "a" a
22+
Expect.equal b (Some 1) "Value should be 1"
23+
24+
testCase "Incorrect Int" <| fun _ ->
25+
let a = DynamicObj()
26+
a.SetValue("a", "1")
27+
let b = DynObj.tryGetTypedValue<int> "a" a
28+
Expect.isNone b "Value should not be an int"
29+
30+
testCase "Correct String" <| fun _ ->
31+
let a = DynamicObj()
32+
a.SetValue("a", "1")
33+
let b = DynObj.tryGetTypedValue<string> "a" a
34+
Expect.equal b (Some "1") "Value should be '1'"
35+
36+
testCase "Incorrect String" <| fun _ ->
37+
let a = DynamicObj()
38+
a.SetValue("a", 1)
39+
let b = DynObj.tryGetTypedValue<string> "a" a
40+
Expect.isNone b "Value should not be a string"
41+
42+
testCase "Correct List" <| fun _ ->
43+
let a = DynamicObj()
44+
a.SetValue("a", [1; 2; 3])
45+
let b = DynObj.tryGetTypedValue<int list> "a" a
46+
Expect.equal b (Some [1; 2; 3]) "Value should be [1; 2; 3]"
47+
48+
ptestCase "Incorrect List" <| fun _ ->
49+
let a = DynamicObj()
50+
a.SetValue("a", [1; 2; 3])
51+
let b = DynObj.tryGetTypedValue<string list> "a" a
52+
Expect.isNone b "Value should not be a string list"
53+
54+
testCase "Correct DynamicObj" <| fun _ ->
55+
let a = DynamicObj()
56+
let b = DynamicObj()
57+
a.SetValue("a", b)
58+
let c = DynObj.tryGetTypedValue<DynamicObj> "a" a
59+
Expect.equal c (Some b) "Value should be a DynamicObj"
60+
61+
testCase "Incorrect DynamicObj" <| fun _ ->
62+
let a = DynamicObj()
63+
a.SetValue("a", 1)
64+
let b = DynObj.tryGetTypedValue<DynamicObj> "a" a
65+
Expect.isNone b "Value should not be a DynamicObj"
66+
]
67+
768

869
let tests_set = testList "Set" [
970

@@ -304,6 +365,7 @@ let tests_copyDynamicProperties = testList "CopyDynamicProperties" [
304365
]
305366

306367
let main = testList "DynamicObj" [
368+
tests_tryGetTypedValue
307369
tests_set
308370
tests_remove
309371
tests_formatString

0 commit comments

Comments
 (0)