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

Api renaming #36

Merged
merged 2 commits into from
Sep 23, 2024
Merged
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
106 changes: 56 additions & 50 deletions src/DynamicObj/DynObj.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module DynObj =
/// Creates a new DynamicObj from a Dictionary containing dynamic properties.
/// </summary>
/// <param name="dynamicProperties">The dictionary with the dynamic properties</param>
let ofDict (dynamicProperties: Dictionary<string, obj>) = DynamicObj.fromDict dynamicProperties
let ofDict (dynamicProperties: Dictionary<string, obj>) = DynamicObj.ofDict dynamicProperties

/// <summary>
/// Creates a new DynamicObj from a sequence of key value pairs containing dynamic properties.
Expand All @@ -24,7 +24,7 @@ module DynObj =
dynamicProperties
|> dict
|> Dictionary
|> DynamicObj.fromDict
|> DynamicObj.ofDict

/// <summary>
/// Creates a new DynamicObj from a list of key value pairs containing dynamic properties.
Expand Down Expand Up @@ -57,127 +57,133 @@ module DynObj =
match kv.Value with
| :? DynamicObj as valueS ->
// is dynObj in second
match first.TryGetValue (kv.Key) with
match first.TryGetPropertyValue (kv.Key) with
| Some valueF ->
let tmp = combine (unbox valueF) (unbox valueS)
first.SetValue(kv.Key,tmp)
| None -> first.SetValue(kv.Key,valueS)
| _ -> first.SetValue(kv.Key,kv.Value)
first.SetProperty(kv.Key,tmp)
| None -> first.SetProperty(kv.Key,valueS)
| _ -> first.SetProperty(kv.Key,kv.Value)
first

/// <summary>
/// Returns Some('TPropertyValue) when a dynamic property with the given name and type exists on the input DynamicObj, otherwise None.
/// Returns Some('TPropertyValue) when a dynamic (or static) property with the given name and type exists on the input, otherwise None.
/// </summary>
/// <param name="name"></param>
/// <param name="dynObj"></param>
let inline tryGetTypedValue<'TPropertyValue> (propertyName:string) (dynObj : DynamicObj) : 'TPropertyValue option =
match (dynObj.TryGetValue propertyName) with
/// <param name="propertyName">the name of the property to get</param>
/// <param name="dynObj">the input DynamicObj</param>
let inline tryGetTypedPropertyValue<'TPropertyValue> (propertyName:string) (dynObj : DynamicObj) : 'TPropertyValue option =
match (dynObj.TryGetPropertyValue propertyName) with
| None -> None
| Some o ->
match o with
| :? 'TPropertyValue as o -> o |> Some
| _ -> None

/// <summary>
/// Sets the given dynamic property name and value on the given DynamicObj.
/// Sets the dynamic (or static) property value with the given name on the given DynamicObj, creating a new dynamic property if none exists.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to set</param>
/// <param name="propertyValue">The value of the dynamic property to set</param>
/// <param name="dynObj">The DynamicObj to set the property on</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
let setValue (propertyName:string) (propertyValue:'TPropertyValue) (dynObj : DynamicObj) =
dynObj.SetValue(propertyName,propertyValue)
let setProperty (propertyName:string) (propertyValue:'TPropertyValue) (dynObj : DynamicObj) =
dynObj.SetProperty(propertyName,propertyValue)

/// <summary>
/// Sets the given dynamic property name and value on the given DynamicObj and returns it.
/// Sets the dynamic (or static) property value with the given name, creating a new dynamic property if none exists on the given DynamicObj and returns it.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to set</param>
/// <param name="propertyValue">The value of the dynamic property to set</param>
/// <param name="propertyName">The name of the property to set</param>
/// <param name="propertyValue">The value of the property to set</param>
/// <param name="dynObj">The DynamicObj to set the property on</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
let withValue (propertyName:string) (propertyValue:'TPropertyValue) (dynObj: DynamicObj) =
setValue propertyName propertyValue dynObj
let withProperty (propertyName:string) (propertyValue:'TPropertyValue) (dynObj: DynamicObj) =
setProperty propertyName propertyValue dynObj
dynObj

/// <summary>
/// Sets the given dynamic property name and value on the given DynamicObj if the value is Some('TPropertyValue).
/// Sets the dynamic (or static) property value with the given name on the given DynamicObj if the value is Some('TPropertyValue), creating a new dynamic property if none exists.
/// If the given propertyValue is None, does nothing to the input DynamicObj.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to set</param>
/// <param name="propertyValue">The value of the dynamic property to set</param>
/// <param name="propertyName">The name of the property to set</param>
/// <param name="propertyValue">The value of the property to set</param>
/// <param name="dynObj">The DynamicObj to set the property on</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
let setValueOpt (propertyName: string) (propertyValue: 'TPropertyValue option) (dynObj: DynamicObj) =
let setOptionalProperty (propertyName: string) (propertyValue: 'TPropertyValue option) (dynObj: DynamicObj) =
match propertyValue with
| Some pv -> dynObj |> setValue propertyName pv
| Some pv -> dynObj |> setProperty propertyName pv
| None -> ()

/// <summary>
/// Sets the given dynamic property name and value on the given DynamicObj if the value is Some('TPropertyValue) and returns it.
/// If the given propertyValue is None, returns the unchanged DynamicObj.
/// Sets the dynamic (or static) property value with the given name on the given DynamicObj if the value is Some('TPropertyValue), creating a new dynamic property if none exists, and returns it.
/// If the given propertyValue is None, does nothing to the input DynamicObj.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to set</param>
/// <param name="propertyValue">The value of the dynamic property to set</param>
/// <param name="propertyName">The name of the property to set</param>
/// <param name="propertyValue">The value of the property to set</param>
/// <param name="dynObj">The DynamicObj to set the property on</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
let withValueOpt (propertyName: string) (propertyValue: 'TPropertyValue option) (dynObj: DynamicObj) =
let withOptionalProperty (propertyName: string) (propertyValue: 'TPropertyValue option) (dynObj: DynamicObj) =
match propertyValue with
| Some pv -> dynObj |> withValue propertyName pv
| Some pv -> dynObj |> withProperty propertyName pv
| None -> dynObj

/// <summary>
/// Sets the given dynamic property name with the result of a mapping function applied to the given property value on the given DynamicObj if the value is Some('TPropertyValue).
/// Sets the given dynamic (or static) property with the result of a mapping function applied to the given property value on the given DynamicObj if the value is Some('TPropertyValue).
/// If the given propertyValue is None, does nothing to the input DynamicObj.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to set</param>
/// <param name="propertyValue">The value of the dynamic property to set</param>
/// <param name="propertyName">The name of the property to set</param>
/// <param name="propertyValue">The value of the property to set</param>
/// <param name="mapping">A function to apply to the property value before setting it on the DynamicObj</param>
/// <param name="dynObj">The DynamicObj to set the property on</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
let setValueOptBy (propertyName: string) (propertyValue: 'TPropertyValue option) (mapping: 'TPropertyValue -> 'UPropertyValue) (dynObj: DynamicObj) =
let setOptionalPropertyBy (propertyName: string) (propertyValue: 'TPropertyValue option) (mapping: 'TPropertyValue -> 'UPropertyValue) (dynObj: DynamicObj) =
match propertyValue with
| Some pv -> dynObj |> setValue propertyName (mapping pv)
| Some pv -> dynObj |> setProperty propertyName (mapping pv)
| None -> ()

/// <summary>
/// Sets the given dynamic property name with the result of a mapping function applied to the given property value on the given DynamicObj if the value is Some('TPropertyValue) and returns it.
/// Sets the given dynamic (or static) property with the result of a mapping function applied to the given property value on the given DynamicObj if the value is Some('TPropertyValue) and returns it.
/// If the given propertyValue is None, returns the unchanged DynamicObj.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to set</param>
/// <param name="propertyValue">The value of the dynamic property to set</param>
/// <param name="propertyName">The name of the property to set</param>
/// <param name="propertyValue">The value of the property to set</param>
/// <param name="mapping">A function to apply to the property value before setting it on the DynamicObj</param>
/// <param name="dynObj">The DynamicObj to set the property on</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
let withValueOptBy (propertyName: string) (propertyValue: 'TPropertyValue option) (mapping: 'TPropertyValue -> 'UPropertyValue) (dynObj: DynamicObj) =
let withOptionalPropertyBy (propertyName: string) (propertyValue: 'TPropertyValue option) (mapping: 'TPropertyValue -> 'UPropertyValue) (dynObj: DynamicObj) =
match propertyValue with
| Some pv -> dynObj |> withValue propertyName (mapping pv)
| Some pv -> dynObj |> withProperty propertyName (mapping pv)
| None -> dynObj

/// <summary>
/// Returns Some(boxed value) if the DynamicObj contains a dynamic property with the given name, and None otherwise.
/// Returns Some(boxed property value) if a dynamic (or static) property with the given name exists on the input, otherwise None.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to get</param>
/// <param name="propertyName">The name of the property to get</param>
/// <param name="dynObj">The DynamicObj to get the property from</param>
let tryGetValue (propertyName: string) (dynObj: DynamicObj) =
dynObj.TryGetValue propertyName
let tryGetPropertyValue (propertyName: string) (dynObj: DynamicObj) =
dynObj.TryGetPropertyValue propertyName

/// <summary>
/// Removes any dynamic property with the given name from the input DynamicObj.
/// If the property is static and mutable, it will be set to null.
/// Static immutable properties cannot be removed.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to remove</param>
/// <param name="propertyName">The name of the property to remove</param>
/// <param name="dynObj">The DynamicObj to remove the property from</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
let remove (propertyName: string) (dynObj: DynamicObj) =
DynamicObj.remove (dynObj, propertyName) |> ignore
/// <exception cref="System.MemberAccessException">Thrown if the dynamic property does not exist</exception>
let removeProperty (propertyName: string) (dynObj: DynamicObj) =
dynObj.RemoveProperty(propertyName) |> ignore

/// <summary>
/// Returns the input DynamicObj with any dynamic property with the given name removed.
/// Removes any dynamic property with the given name from the input DynamicObj and returns it.
/// If the property is static and mutable, it will be set to null.
/// Static immutable properties cannot be removed.
/// </summary>
/// <param name="propertyName">The name of the dynamic property to remove</param>
/// <param name="propertyName">The name of the property to remove</param>
/// <param name="dynObj">The DynamicObj to remove the property from</param>
/// <remarks>This function mutates the input DynamicObj</remarks>
/// <exception cref="System.MemberAccessException">Thrown if the dynamic property does not exist</exception>
let withoutProperty(propertyName: string) (dynObj: DynamicObj) =
dynObj |> remove propertyName
dynObj |> removeProperty propertyName
dynObj

/// <summary>
Expand Down
Loading
Loading