-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprev - Get-ObjectProperty.ps1
More file actions
364 lines (302 loc) · 10.8 KB
/
prev - Get-ObjectProperty.ps1
File metadata and controls
364 lines (302 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
function Get-ObjectType {
[Alias('TypeOf')]
<#
.notes
parametersets 2 position-type modes
1] object[] | TypeOf mode
2] TypeOf object[] mode
todo: FormatTypeName that does 'abbreviateNamespace'
pipeline notes:
It seems like for this function I should go with
- parameter type that is not an array [obj]
- process returns array ', $array'
- then optionally enumerate on [obj] if I want to inspect child element
#>
param(
# InputObject[s] to get type[s] of
[Parameter(Mandatory, ValueFromPipeline)]
[object]$InputObject,
# Format Mode
[Parameter(Position = 0)]
[ValidateSet('List', 'PSTypeNames', 'GetType')]
[string]$Format,
# Returns type as Typeinfo if possible
[Parameter()][switch]$PassThru,
# Returns a unique list of types
[Parameter()][switch]$Unique
)
begin {
# if ($PassThru) {
# throw "NotImplementedError: -PassThru"
# }
$typeList = [list[object]]::new()
if ([string]::IsNullOrWhiteSpace( $Format)) {
$Format = 'GetType'
}
}
process {
$typeList.Add( $InputObject )
}
end {
# $
switch ($Format) {
'GetType' {
$typeInstance = $curObject.GetType()
if ($PassThru) {
$typeInstance
break
}
$typeInstance | Format-TypeName
break
}
{ 'PSTypeNames' -or 'List' } {
if ($PassThru) {
, $curObject.pstypenames
break
}
# foreach ($Obj in $InputInputObjectect) {
$splat_JoinPSTypeName = @{
Separator = ', '
Property = { $_ | Format-TypeName }
}
$curObject.pstypenames | Join-String @splat_JoinPSTypeName
break
}
default { Throw "UnhandledCase: $Format" }
}
}
}
function old_Get-ObjectType {
<#
.synopsis
simplify getting type name of an object and child types
.example
PS> ls | Get-Unique -OnType | TypeOf
Count isList Type
----- ------ ----
1 False IO.DirectoryInfo
1 False IO.FileInfo
.example
ls | sort -Unique { $_.GetType().FullName } | TypeOf
ls . | sort { $_.GetType().Fullname } | Get-Unique -OnType | typeof
.notes
rename to Get-TypeName? Get-ItemTypeName? Get-TypeInfo?
future:
- [ ] output custom object type of this commands results, because
- [ ] display output uses | Format-TypeName
- [ ] but properties are still full 'Type' instances
future:
- simplify showing
- interfaces implemented
- base/parent type
- category = class, enum, typeReflectionInfo
future?
#>
<#
"NYI: Left off. todo:
- for objects
- [ ] obj.GetType()
- [ ] isContainer?
- [ ] @(obj)[0].GetType()
- [ ] obj.pstypenames
- [ ] @(obj)[0].pstypenames
- for [typeinfo]
- [ ] @(obj)[0].GetTypeInfo()
- [ ] obj.pstypenames ?
- [ ] @(obj)[0].pstypenames ?
"
#>
# [Alias('TypeOf')]
param(
# InputObject[s] to get type[s] of
[Parameter(Mandatory, ValueFromPipeline)]
[object[]]$InputObject,
# (to refactor as a custom Format.ps1xml) include more properties
[Parameter()][switch]$Detail,
# output format mode
[Parameter(Position = 0)]
[ValidateSet('', 'PSTypeNameList', 'Table', 'Default')]
[string]$FormatMode = 'PSTypeNameList',
# ignore colors using: PassThru (until refactor to move colors to format types)
# skip colors and returns an array of PSTypeNames
[Parameter()][switch]$PassThru
# All: Test all elements similar to Select-Object -Wait
# [Parameter()][switch]$AllElements
)
begin {
$UseColor = ! $PassThru
$joinAsList_splat = @{
Separator = ', '
OutputPrefix = '{ '
OutputSuffix = '}'
}
$joinTypesAsList_splat = @{
Separator = ', '
OutputPrefix = '{ '
OutputSuffix = '}'
Property = { $_ | Format-TypeName -WithBrackets }
}
# vs code has issues with non-printable characters in table widths
if ((Get-TerminalName).IsVSCode) {
$UseColor = $false
}
if ($UseColor) {
# Warning: Refactor to custom view, not raw output!
$ColorDarkGrey = 'aaaaaa'
$splatDarkGrey = @{
ForegroundColor = $ColorDarkGrey
}
# giant spam until refactor as formatter
$joinTypesAsList_splat.OutputPrefix = New-Text @splatDarkGrey $joinTypesAsList_splat.OutputPrefix | ForEach-Object tostring
$joinTypesAsList_splat.OutputSuffix = New-Text @splatDarkGrey $joinTypesAsList_splat.OutputSuffix | ForEach-Object tostring
$joinTypesAsList_splat.Separator = New-Text @splatDarkGrey $joinTypesAsList_splat.Separator | ForEach-Object tostring
$joinAsList_splat.OutputPrefix = New-Text @splatDarkGrey $joinAsList_splat.OutputPrefix | ForEach-Object tostring
$joinAsList_splat.OutputSuffix = New-Text @splatDarkGrey $joinAsList_splat.OutputSuffix | ForEach-Object tostring
$joinAsList_splat.Separator = New-Text @splatDarkGrey $joinAsList_splat.Separator | ForEach-Object tostring
}
}
Process {
# curTypeObj = instance of Object
# curTypeInfo = [type] of Object
if ($InputObject -is 'type') {
$curTypeInfo = $InputObject
$curTypeObj = $InputObject
}
elseif ($InputObject -is [string]) {
$curTypeInfo = $InputObject -as [type]
if ($null -eq $curTypeInfo) {
throw "Failed on typeName: '$curTypeInfo'"
}
$curTypeObj = $null
}
else {
$curTypeInfo = $InputObject.GetType()
$curTypeObj = $InputObject
}
# if has children
$element1 = $InputObject | Select-Object -First 1
if ($FormatMode -eq 'Default') {
# if assembly is verbose, replace FullName with 'namespace', 'name' to strip assembly
#no $PassThru ?
$tinfo = $curTypeInfo.Namespace, $curTypeInfo.Name -join '.'
# $tinfo = $curTypeInfo.GetType().Namespace, $curTypeInfo.GetType().Name -join '.'
$tinfo
return
}
if ($FormatMode -eq 'PSTypeNameList') {
if ($PassThru) {
if ($curTypeObj) {
, $curTypeObj.PSTypeNames
}
return
}
# *should* work regardless if process inputs an array or one elemenent
# foreach($item in $InputObject) {}
$splat_JoinPSTypeName = @{
Separator = "`n- "
OutputPrefix = '- '
Property = { $curTypeObj.PSTypeNames | Format-TypeName -WithBrackets | Join-String -sep ', ' }
}
$InputObject | Join-String @splat_JoinPSTypeName
# $splat_JoinPSTypeNamedsf = @{
# OutputPrefix = { $_.PSTypeNames | Format-TypeName -WithBrackets | Join-String -sep ', ' }
# }
# $InputObject | Join-String @splat_JoinPSTypeName
return
}
$meta = [ordered]@{
Count = $curTypeObj.Count
isList = $curTypeObj.Count -gt 1
Type = $curTypeObj.GetType() | Format-TypeName
}
$metaDetailed = [ordered]@{
elementType = $element1.GetType() | Format-TypeName
| Format-TypeName
# elementTypeNames = $element1.pstypenames | Join-String @joinAsList_splat # { $_ | Format-TypeName }
# elementTypeNamesAbbr = $element1.pstypenames | Join-String @joinAsList_splat -prop { $_ | Format-TypeName }
TypeNames = $cur.pstypenames | Join-String @joinTypesAsList_splat
TypeNamesFull = $cur.pstypenames | Join-String @joinTypesAsList_splat
# TypeNames = $cur.pstypenames | Join-String @joinAsList_splat -prop { $_ | Format-TypeName }
# TypeNamesAbbr = $cur.pstypenames | Join-String @joinAsList_splat -prop { $_ | Format-TypeName }
}
if ($Detail) {
$meta += $metaDetailed
}
# if ($AllElements) {
# $AllTypeList = $InputObject | ForEach-Object {
# $_ | ForEach-Object GetType
# | Format-TypeName -WithBrackets
# } | Join-String @joinAsList_splat
# $meta.Add('AllTypeList', $AllTypeList )
# }
[pscustomobject]$meta
# $meta
}
}
if ($isDebugMod) {
# test cases
$items = [ordered]@{}
$items.mod = Get-Module 'Ninmonkey.Console'
$items.cmd = Get-Command 'Get-Item'
$items.nums = 2, 4, 55
$items.hash = @{ Species = 'cat'; }
$items.object = [pscustomobject]($items['hash'])
$items.GetEnumerator() | ForEach-Object { $_.Value } | ForEach-Object gettype | ForEach-Object FullName
H1 'try 1: As Obj Instance (required)'
$items.GetEnumerator() | ForEach-Object { $_.Value }
| ForEach-Object FullName
| TypeOf
H1 'try 2: As Type instance (optional)'
$items.GetEnumerator() | ForEach-Object { $_.Value }
| ForEach-Object gettype | ForEach-Object FullName
| TypeOf
}
if ($false) {
H1 'mod'
$items.mod | TypeOf
H1 'sdf'
hr
234, 'sdf' | TypeOf
hr
TypeOf -InputObject 'dog' | Format-Table
hr
TypeOf -InputObject 'dog' -Detail | Format-Table
# # final
# hr;
# $items.mod | TypeOf
# hr;
# $items.cmd | TypeOf
# hr;
# $items.nums | TypeOf
# hr;
# $items.hash | TypeOf
# hr;
# $items.object | TypeOf
# hr
}
if ($true -and $isDebugMod) {
H1 'enumerate all'
$results = foreach ($Key in $items.Keys) {
[pscustomobject]@{
Label = $Key
Contents = $items[$Key]
Type = $items[$Key] | ForEach-Object gettype #| Format-TypeName -Brackets
}
}
$results
H1 'inner results'
foreach ($Item in $Results) {
$H2splat = @{
ForegroundColor = 'magenta'
Depth = 2
}
H1 $Item.Label @H2splat
$Item.Contents
}
# foreach ($Key in $results.GetEnumerator()) {
# $Key = $_.Key
# $Value = $_.Value
# H1 $Key
# $Value
# }
}