Skip to content

ConvertTo-Expression Make the potential loss of type fidelity clearer in the read-me #73

Closed

Description

@iRon7

It is implied by your Get-UICulture example, for instance, but I suggest pointing out the constraints on type fidelity explicitly (which types can actually be fully round-tripped).

Activity

  1. added
    documentation
    enhancement
    on Apr 10, 2024
  2. self-assigned this
    on Apr 10, 2024
  3. iRon7 commented on Apr 11, 2024

    @iRon7
    OwnerAuthor

    The object parser's fidelity is defined by:

    [PSLeafNode]

    A PSLeafNode terminates a PSNode branch and doesn't have any child nodes attached.
    A value contained by a PSLeafNode is [Value], [String] or a [ScriptBlock] and is not
    enumerable and doesn't contain any object properties (except for the three mentioned types).

    The code piece that defines the PSNode fidelity is:

    if ($Null -eq $Object)                                      { return 'PSLeafNode' }
    elseif ($Object -is [Management.Automation.PSCustomObject]) { return 'PSObjectNode' }
    elseif ($Object -is [Collections.IDictionary])              { return 'PSDictionaryNode' }
    elseif ($Object -is [Specialized.StringDictionary])         { return 'PSDictionaryNode' }
    elseif ($Object -is [Collections.ICollection])              { return 'PSListNode' }
    elseif ($Object -is [ValueType])                            { return 'PSLeafNode' }
    elseif ($Object -is [String])                               { return 'PSLeafNode' }
    elseif ($Object -is [ScriptBlock])                          { return 'PSLeafNode' }
    elseif ($Object.PSObject.Properties)                        { return 'PSObjectNode' }
    else                                                        { return 'PSLeafNode' }

    The ConvertTo-Expression cmdlet's fidelity is further limited to:

    -HighFidelity

    If the -HighFidelity switch is supplied, all nested object properties will be serialized.

    By default the fidelity of an object expression will end if:

    1. the (embedded) object is a leaf node (see: PSNode Object Parser)
    2. the (embedded) object expression is able to round trip.

    An object is able to roundtrip if the resulted expression of the object itself or one of
    its properties (prefixed with the type initializer) can be used to rebuild the object.

    The advantage of the default fidelity is that the resulted expression round trips (aka the
    object might be rebuild from the expression), the disadvantage is that information hold by
    less significant properties is lost (as e.g. timezone information in a DateTime] object).

    The advantage of the high fidelity switch is that all the information of the underlying
    properties is shown, yet any constrained or full object type will likely fail to rebuild
    due to constructor limitations such as readonly property.

    The static [PSSerialize]::RoundTripProperty dictionary defines the round trip property.
    Unless the -HighFidelity switch is set, the serialization will stop (even if it concerns a PSCollectionNode)
    when the specific property type is reached.

    • An empty string will return the string representation of the object: "<Object>"
    • Any other string will return the string representation of the object property: "$(<Object>.<Property>)"
    • A ScriptBlock will be invoked and the result will be used for the object value

    (The scope for this fidelity feature is basically only for constrained language mode)

  4. changed the title [-]Make the potential loss of type fidelity clearer in the read-me[/-] [+]`ConvertTo-Expression` Make the potential loss of type fidelity clearer in the read-me[/+] on Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentationenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions