Skip to content

Commit 728ce1b

Browse files
committed
2 parents c70e133 + 577414e commit 728ce1b

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
$script:VarTest_Null = $Null
2+
$Config = @{
3+
DeclareExplicitNull = $false
4+
ExportBeforeInit = $true
5+
}
6+
if( $Config.DeclareExplicitNull) {
7+
$script:VarTest_None = $null
8+
}
9+
10+
function ModuleInit {
11+
'running: ModuleInit' | write-verbose -verbose
12+
$script:VarTest_Null = 'hiworld'
13+
$script:VarTest_None = 'stuff'
14+
}
15+
16+
# $script:VarTest_None = $Null
17+
if($Config.ExportBeforeInit) {
18+
Export-ModuleMember -Variable @( 'VarTest_*')
19+
ModuleInit
20+
} else {
21+
ModuleInit
22+
Export-ModuleMember -Variable @( 'VarTest_*')
23+
}
24+
25+
# for either init order, VarTest_None == 'stuff' in module scope
26+
# but can be missing in the user scope (so the value itself isn't null)
27+
Get-Variable 'VarTest*' -scope Script | Ft -auto
28+
| oss
29+
| Write-verbose -verbose
30+
31+
$Config | Ft -auto | out-string | write-host
32+
@'
33+
Example usage:
34+
> ipmo './ExportVarTest.psm1' -PassThru -Force
35+
> Get-Variable 'VarTest*' -scope global | ft -auto
36+
37+
> $VarTest_Null, $VarTest_None' | write-warning
38+
'@ | write-warning
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#Requires -Version 7
2+
using namespace System.Collections.Generic
3+
using namespace System.Text
4+
using namespace System.Text.Json
5+
using namespace System.Text.Json.Serialization
6+
using namespace System.Linq
7+
8+
$assembly = Add-type -AssemblyName System.Text.Json -PassThru -ea 'stop'
9+
10+
# This isn't an example of what you should do, it's testing what you can do
11+
12+
function AutoJson {
13+
<#
14+
.SYNOPSIS
15+
Try one of the automatic methods
16+
.notes
17+
[System.Text.Json.JsonSerializer] has a ton of overloads
18+
.LINK
19+
https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer?view=net-8.0#methods
20+
#>
21+
[CmdletBinding()]
22+
param(
23+
[Parameter(ValueFromPipeline)]
24+
[Alias('InObj', 'In')]
25+
[object] $Object,
26+
27+
# Without a type, it falls back to GetType()
28+
[Alias('Tinfo', 'T')]
29+
[type] $TypeInfo
30+
)
31+
process {
32+
if(-not $TypeInfo ) { $TypeInfo = $Object.GetType() }
33+
[Text.Json.JsonSerializer]::Serialize( <# value: #> $Object, <# tinfo #> $TypeInfo )
34+
35+
'AutoJson: TryConvert type: {0}' -f $TypeInfo | Write-verbose
36+
}
37+
}
38+
39+
$error.clear()
40+
$PSDefaultParameterValues['AutoJson:Ea'] = 'break'
41+
$PSDefaultParameterValues['AutoJson:Ea'] = 'continue'
42+
43+
class SomePS {
44+
[string] $ProcessName
45+
[int] $Id
46+
47+
48+
# [example kind 1]: Property on instances to include, but, never serialize it
49+
[Serialization.JsonIgnoreAttribute()]
50+
[Diagnostics.ProcessModuleCollection] $Modules
51+
52+
# [example kind 2]: Serialize property if it's not null, but ignore it if it's null
53+
# note: It does not work here beacuse [object][string]::empty is not null
54+
[JsonIgnoreAttribute( Condition = [JsonIgnoreCondition]::WhenWritingNull) ]
55+
[object] $MainWindowTitle
56+
57+
SomePS ( [object]$Other ) {
58+
$WantedProps = [Linq.Enumerable]::Intersect( # because linq is fun
59+
[string[]] $This.PSObject.Properties.Name,
60+
[string[]] $Other.PSObject.Properties.Name )
61+
62+
foreach($PropName in $WantedProps) {
63+
$This.$PropName = $Other.$PropName
64+
}
65+
# hard coded version:
66+
# $This.ProcessName = $Other.ProcessName
67+
# $This.Id = $Other.Id
68+
# $this.MainWindowTitle = $Other.MainWindowTitle
69+
# $This.Modules = $Other.Module
70+
}
71+
}
72+
$hasModules = get-process | ? Modules | select -First 1
73+
$hasTitle = ( ps ) | ? MainWindowTitle | select -first 1
74+
$noTitle = ( ps ) | ? -not MainWindowTitle | select -first 1
75+
$notNullSite = (get-process | ?{ $Null -eq $_.site })
76+
77+
# [a] works
78+
$hasModules -as [SomePS[]]
79+
<# Out:
80+
ProcessName Id Modules
81+
----------- -- -------
82+
ApplicationFrameHost 19636 {System.Diagnostics.ProcessModule (ApplicationFrameHost.exe), System.Diagnost ....
83+
#>
84+
85+
$hasModules -as [SomePS[]] | %{ AutoJson -InObj $_ -TypeInfo ([SomePS]) }
86+
<# Out:
87+
{"ProcessName":"ApplicationFrameHost","Id":19636}
88+
#>
89+
90+
# cool, typedata is constrained to derived types
91+
{
92+
$hasModules -as [SomePS[]] | %{ AutoJson -InObj $_ -TypeInfo ([System.Diagnostics.Process]) }
93+
} | Should -Throw -because 'Typedef used did not derive from type'
94+
<# Out:
95+
Exception calling "Serialize" with "2" argument(s): "The specified
96+
type [System.Diagnostics.Process] must derive from the specific value's type [SomePS]."
97+
#>
98+
99+
# [b] not quite working
100+
h1 '[b]'
101+
$hasTitle -as [SomePs[]] | %{ AutoJson -InObj $_ }
102+
103+
104+
$hasModules, $hasTitle, $noTitle -as [SomePs[]]
105+
| AutoJson
106+
107+
<# Outputs:
108+
{"ProcessName":"ApplicationFrameHost","Id":19636,"MainWindowTitle":"Snip \u0026 Sketch"}
109+
{"ProcessName":"AggregatorHost","Id":9268,"MainWindowTitle":""}
110+
#>

html/references/github-html-test.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### About
2+
3+
Testing html formatting in github
4+
5+
#### Bullets
6+
7+
<!--
8+
<style>* {
9+
border: 2px solid red !important;
10+
color: purple !important;
11+
fonts-size: 20px !important;
12+
}</style>
13+
-->
14+
15+
<p style="font-size:100px !imporant;">hi world</p>
16+
17+
<ul><li>a</li><li>b</li></ul>

0 commit comments

Comments
 (0)