Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging IDOs API #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/DynamicObj/ImmutableDynamicObj.fs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ type ImmutableDynamicObj internal (map : Map<string, obj>) =
.ToDictionary((fun (key, _) -> key), fun (_, value) -> value)
dict

static member combine (first : 'T when 'T :> ImmutableDynamicObj) (second : ImmutableDynamicObj) =
let addProp (ido : 'T) (prop : KeyValuePair<string, obj>) =
ido
|> ImmutableDynamicObj.add prop.Key prop.Value
Seq.fold addProp first second.Properties



and ImmutableDynamicObjJsonConverter () =
Expand Down
17 changes: 16 additions & 1 deletion tests/UnitTests/ImmTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,19 @@ let ``Json serialization nested`` () =
++ ("inner", o2)
let actual = JsonConvert.SerializeObject o

Assert.Equal("""{"aaa":5,"hh":[1,2,3],"inner":{"aaa":5,"hh":[1,2,3],"ohno":10,"quack":"tt"},"ohno":10,"quack":"tt"}""", actual)
Assert.Equal("""{"aaa":5,"hh":[1,2,3],"inner":{"aaa":5,"hh":[1,2,3],"ohno":10,"quack":"tt"},"ohno":10,"quack":"tt"}""", actual)

[<Fact>]
let ``Merging two`` () =
let a =
ImmutableDynamicObj.empty
++ ("aaa", 5)
++ ("bb", 10)
let b =
ImmutableDynamicObj.empty
++ ("aaa", 11)

let merged = ImmutableDynamicObj.combine a b

Assert.Equal(11 :> obj, merged.["aaa"])
Assert.Equal(10 :> obj, merged.["bb"])