Skip to content

Commit

Permalink
added type unwrapped TryGetProperty calls to markup.
Browse files Browse the repository at this point in the history
This allows you directly get the value of properties without have to bounce it through a markup value type first.
  • Loading branch information
McJones committed Feb 20, 2025
1 parent 96a6fa4 commit 1364266
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Language Server: Nodes that are part of a node group now show their condition complexity as a code lens.
- Added a new method, `Dialogue.HasSalientContent(nodegroup)`, which returns a bool if there is any salient content for the requested nodegroup.
- Added `min(a,b)` and `max(a,b)` to the standard library.
- Added unwrapped `TryGetProperty` calls to markup, allowing direct access to the type if you know it ahead of time.

### Changed

Expand Down
176 changes: 163 additions & 13 deletions YarnSpinner/YarnSpinner.Markup/MarkupParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,10 @@ public override string ToString()
return sb.ToString();
}

public bool TryGetProperty(string name, out MarkupValue result)
// these methods and the ones for MarkupAttributeMarker are identical
// don't have time for v3 release but later we should merge these two types
// they are too similar to be different
private bool TryGetPropertyInternal(string name, out MarkupValue result)
{
foreach (var prop in this.Properties)
{
Expand All @@ -445,6 +448,86 @@ public bool TryGetProperty(string name, out MarkupValue result)
}
}

result = default;
return false;
}
public bool TryGetProperty(string name, out MarkupValue result)
{
var worked = TryGetPropertyInternal(name, out var value);

result = value;
return worked;
}
public bool TryGetProperty(string name, out float result)
{
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.Float:
{
result = property.FloatValue;
return true;
}
case MarkupValueType.Integer:
{
result = property.IntegerValue;
return true;
}
}
}

result = default;
return false;
}
public bool TryGetProperty(string name, out int result)
{
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.Integer:
{
result = property.IntegerValue;
return true;
}
}
}

result = default;
return false;
}
public bool TryGetProperty(string name, out string? result)
{
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.String:
{
result = property.StringValue;
return true;
}
}
}

result = default;
return false;
}
public bool TryGetProperty(string name, out bool result)
{
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.Bool:
{
result = property.BoolValue;
return true;
}
}
}

result = default;
return false;
}
Expand Down Expand Up @@ -656,6 +739,20 @@ internal MarkupAttributeMarker(string name, int position, int sourcePosition, Li
/// </summary>
internal int SourcePosition { get; set; }

private bool TryGetPropertyInternal(string name, out MarkupValue result)
{
foreach (var prop in this.Properties)
{
if (prop.Name.Equals(name, System.StringComparison.OrdinalIgnoreCase))
{
result = prop.Value;
return true;
}
}

result = default;
return false;
}
/// <summary>
/// Gets the property associated with the specified key, if
/// present.
Expand All @@ -682,25 +779,78 @@ public bool TryGetProperty(string name, out MarkupValue result)
result = default;
return false;
}

public override bool Equals(object obj)
public bool TryGetProperty(string name, out float result)
{
throw new NotImplementedException();
}
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.Float:
{
result = property.FloatValue;
return true;
}
case MarkupValueType.Integer:
{
result = property.IntegerValue;
return true;
}
}
}

public override int GetHashCode()
{
throw new NotImplementedException();
result = default;
return false;
}
public bool TryGetProperty(string name, out int result)
{
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.Integer:
{
result = property.IntegerValue;
return true;
}
}
}

public static bool operator ==(MarkupAttributeMarker left, MarkupAttributeMarker right)
result = default;
return false;
}
public bool TryGetProperty(string name, out string? result)
{
return left.Equals(right);
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.String:
{
result = property.StringValue;
return true;
}
}
}

result = default;
return false;
}

public static bool operator !=(MarkupAttributeMarker left, MarkupAttributeMarker right)
public bool TryGetProperty(string name, out bool result)
{
return !(left == right);
if (TryGetPropertyInternal(name, out var property))
{
switch (property.Type)
{
case MarkupValueType.Bool:
{
result = property.BoolValue;
return true;
}
}
}

result = default;
return false;
}
}
}

0 comments on commit 1364266

Please sign in to comment.