Skip to content

Commit c870224

Browse files
committed
Updated directive
1 parent 30bef79 commit c870224

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/readme.graph.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,23 @@ directive:
336336
337337
//The following regex below adds a property tracker to ensure that users can also pass $Null as an alternative to the current "null" string which gets inferred to null.
338338
339+
const regexP = /AddIf\(\s*null\s*!=\s*\(\(\(object\)this\._(\w+).*?(\(Microsoft.*.PowerShell\.Runtime\.Json\.JsonNode\)).*?"(\w+)".*?container\.Add\s*\);/gm
340+
$ = $.replace(regexP, (match, p1, p2, p3) => {
341+
let capitalizedP1 = p1.charAt(0).toUpperCase() + p1.slice(1); // Capitalize first letter
342+
return `if(this.IsPropertySet("${p1}"))\n\t\t{\n\t\t\tvar propertyInfo = this.GetType().GetProperty("${capitalizedP1}");\n\t\t\tif (propertyInfo != null)\n\t\t\t{\n\t\t\tSystem.Type propertyType = propertyInfo.PropertyType;\n\t\t\t\t\tAddIf(${p2}PropertyTracker.ConvertToJsonNode(propertyType, this._${p1}),"${p1}",container.Add);\n\t\t\t}\n\t\t}`;
343+
});
344+
345+
$ = $.replace(/if\s*\(\s*null\s*!=\s*this\._(\w+)\s*\)/gm, 'if(this.IsPropertySet("$1"))')
346+
347+
let nameSpacePrefixRegex = /(Microsoft(?:\.\w+)*?\.PowerShell)/gm
348+
let nameSpacePrefix = 'Microsoft.Graph.PowerShell';
349+
if($.match(nameSpacePrefixRegex)){
350+
let prefixMatch = nameSpacePrefixRegex.exec($);
351+
nameSpacePrefix = prefixMatch[1];
352+
}
353+
$ = $.replace(/container\.Add\("(\w+)",\s*(__\w+)\);/gm, 'var nullFlag = ('+nameSpacePrefix+'.Runtime.Json.JsonNode)new '+nameSpacePrefix+'.Runtime.Json.JsonString("nullarray");\n\t\tif($2.Count == 0)\n\t\t{\n\t\t\t$2.Add(nullFlag);\n\t\t}\n\t\tcontainer.Add("$1", $2);');
354+
355+
$ =$.replace(/AddIf\(\s+null\s+!=\s+(this\._\w+)\s+\?\s+\((Microsoft\.Graph\..*?)\)\s+this\._(\w+)\.ToJson\(null,serializationMode\)\s+:\s+null,\s+"\w+"\s+,container.Add\s+\);/gm, 'if (this.IsPropertySet("$3")) \n{\n if ($1 != null)\n{\n container.Add("$3", ($2)$1.ToJson(null, serializationMode)); \n}\nelse\n{\n container.Add("$3", "null"); \n}\n}');
339356
340357
return $;
341358
}
@@ -402,7 +419,28 @@ directive:
402419
if($.match(additionalPropertiesRegex)) {
403420
$ = $.replace(additionalPropertiesRegex, '$1$2 new $3');
404421
}
422+
//The following regex below adds a property tracker to ensure that users can also pass $Null as an alternative to the current "null" string which gets inferred to null.
423+
$ = $.replace(/\bpublic\s+(\w+\??)\s+(\w+)\s*{\s*get\s*=>\s*this\.(\w+);\s*set\s*=>\s*this\.\3\s*=\s*value;\s*}/gmi,'public $1 $2\n\t{\n\t\tget=>this.$3;\n\t\tset\n\t\t{\n\t\t\tthis.$3=SanitizeValue<$1>(value);\n\t\t\tTrackProperty(nameof($2));\n\t\t}\n\t}')
405424
425+
$ = $.replace(/\bpublic\s+(\w+\[\])\s+(\w+)\s*{\s*get\s*=>\s*this\.(\w+);\s*set\s*=>\s*this\.\3\s*=\s*value;\s*}/gm,'public $1 $2\n\t{\n\t\tget=>this.$3;\n\t\tset\n\t\t{\n\t\t\tthis.$3=value;\n\t\t\tTrackProperty(nameof($2));\n\t\t}\n\t}')
426+
427+
$ = $.replace(/\bpublic\s+(Microsoft\.Graph\.[\w.]+\[\])\s+(\w+)\s*{\s*get\s*=>\s*this\.(\w+);\s*set\s*=>\s*this\.\3\s*=\s*value;\s*}/gm,'public $1 $2\n\t{\n\t\tget=>this.$3;\n\t\tset\n\t\t{\n\t\t\tthis.$3=value;\n\t\t\tTrackProperty(nameof($2));\n\t\t}\n\t}')
428+
429+
const match = $documentPath.match(/generated%2Fapi%2FModels%2F([\w]*[\w\d]*)\.cs/gm);
430+
if (match) {
431+
let fileName = match[0];
432+
fileName = fileName.replace('generated%2Fapi%2FModels%2F','')
433+
fileName = fileName.replace('.cs','')
434+
const interfaceName = 'I'+fileName
435+
$ = $.replace('interface '+interfaceName+' :', 'interface '+interfaceName+' : IPropertyTracker,')
436+
const className = fileName
437+
const regexP = new RegExp(`public\\s+partial\\s+class\\s+${className}\\s*:\\s*[\\s\\S]*?{`, "gm");
438+
var matches = regexP.exec($);
439+
let originalMatch = matches[0];
440+
$ = $.replace(regexP, originalMatch+'\n\t\tprivate readonly PropertyTracker _propertyTracker = new PropertyTracker();\n\t\tpublic void TrackProperty(string propertyName) => _propertyTracker.TrackProperty(propertyName);\n\t\tpublic bool IsPropertySet(string propertyName) =>_propertyTracker.IsPropertySet(propertyName);\n\t\tpublic T SanitizeValue<T>(object value) => PropertyTracker.SanitizeValue<T>(value);');
441+
}
442+
443+
$ = $.replace(/public\s+(Microsoft\.Graph\..*?)\s+(\w+)\s+{\s+get\s+=>\s+\(\s*this\.(\w+)\s+=\s*this\.\3\s+\?\?\s+new\s+(Microsoft\.Graph\..*?)\s+set\s+=>\s+this._\w+\s+=\s+value;\s+}/gm, 'public $1 $2 { \n get => (this.$3 = this.$3 ?? new $4\n set\n {\n this.$3 = value;\n TrackProperty(nameof($2));\n }\n}')
406444
407445
return $;
408446
@@ -458,7 +496,35 @@ directive:
458496
});
459497
}
460498
499+
let nameSpacePrefixRegex = /(Microsoft(?:\.\w+)*?\.PowerShell)/gm
500+
let nameSpacePrefix = 'Microsoft.Graph.PowerShell';
501+
if($.match(nameSpacePrefixRegex)){
502+
let prefixMatch = nameSpacePrefixRegex.exec($);
503+
nameSpacePrefix = prefixMatch[1];
504+
}
505+
506+
let ensureCall = '';
507+
if ($.includes('BodyParameter')) {
508+
ensureCall = `
509+
if (BodyParameter != null)
510+
{
511+
${nameSpacePrefix}.ModelExtensions.ModelExtensions.EnsurePropertiesAreReady(BodyParameter, failOnExplicitNulls: false).GetAwaiter().GetResult();
512+
}`;
513+
} else if ($.includes('_body')) {
514+
ensureCall = `
515+
if (_body != null)
516+
{
517+
${nameSpacePrefix}.ModelExtensions.ModelExtensions.EnsurePropertiesAreReady(_body, failOnExplicitNulls: false).GetAwaiter().GetResult();
518+
}`;
519+
}
461520
521+
$ = $.replace(
522+
/protected override void BeginProcessing\(\)\s*\{\s*([\s\S]*?)(if\s*\(Break\))/g,
523+
`protected override void BeginProcessing() {
524+
Module.Instance.SetProxyConfiguration(Proxy, ProxyCredential, ProxyUseDefaultCredentials);
525+
${ensureCall}
526+
$1$2`
527+
);
462528
463529
464530
return $;

0 commit comments

Comments
 (0)