-
Notifications
You must be signed in to change notification settings - Fork 2
feat: validate promoted properties and enhance Scriban array access #154
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
| using Scriban; | ||
| using Scriban.Parsing; | ||
| using Scriban.Runtime; | ||
|
|
||
| namespace SW.Bitween.NativeAdapters.JsonMapper; | ||
|
|
@@ -102,11 +103,33 @@ private static ScriptObject BuildScriptObject(JObject obj) | |
| private static object? ToScribanValue(JToken token) => token switch | ||
| { | ||
| JObject o => BuildScriptObject(o), | ||
| JArray a => a.Select(ToScribanValue).ToList(), | ||
| JArray a => new SmartArray(a.Select(ToScribanValue)), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Risk of silent data loss when accessing multi-element arrays. Converting all Consider adding validation or logging when 🤖 Prompt for AI Agents |
||
| JValue v => v.Value, | ||
| _ => null | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// A Scriban array that also delegates member access to its first element, | ||
| /// so templates can write either <c>data[0].field</c> or <c>data.field</c> | ||
| /// when the source JSON value is a single-element (or first-item) array. | ||
| /// </summary> | ||
| private sealed class SmartArray : ScriptArray | ||
| { | ||
| public SmartArray(IEnumerable<object?> items) : base(items) { } | ||
|
|
||
| public override bool TryGetValue(TemplateContext context, SourceSpan span, string member, out object? value) | ||
| { | ||
| if (base.TryGetValue(context, span, member, out value)) | ||
| return true; | ||
|
|
||
| if (Count > 0 && this[0] is ScriptObject first) | ||
| return first.TryGetValue(context, span, member, out value); | ||
|
|
||
| value = null; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Sets a value at a dot-separated path inside a JObject, creating intermediate objects as needed.</summary> | ||
| private static void SetByPath(JObject root, string path, JToken value) | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.