Skip to content
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

Unable to deserialize short-form intrinsic functions #521

Open
ganeshnj opened this issue Aug 20, 2020 · 2 comments
Open

Unable to deserialize short-form intrinsic functions #521

ganeshnj opened this issue Aug 20, 2020 · 2 comments
Labels

Comments

@ganeshnj
Copy link

ganeshnj commented Aug 20, 2020

What is intrinsic functions?
AWS CloudFormation provides several built-in functions that help you manage your stacks. Use intrinsic functions in your templates to assign values to properties that are not available until runtime. Read more

Syntax for the full function name:

Fn::Sub:
  - String
  - Var1Name: Var1Value
    Var2Name: Var2Value

Syntax for the short form:

!Sub
  - String
  - Var1Name: Var1Value
    Var2Name: Var2Value

Issue
Short form intrinsic function deserialization fails with

YamlDotNet.Core.YamlException: '(Line: 47, Col: 20, Idx: 1302) - (Line: 47, Col: 94, Idx: 1376): Encountered an unresolved tag '!Sub''

Sample project
ConsoleApp1.zip

@ganeshnj
Copy link
Author

I played around with node deserilization classes and able to correctly parse the short form intrinsic functions. (Using YamlDotNet 4.2.1.0)

class NodeDeserializer : INodeDeserializer
{
    private readonly HashSet<string> intrinsicFunctionShortForms;
    private readonly IDeserializer deserializer;

    public IntrinsicFunctionShortFormNodeDeserializer(IDeserializer deserializer)
    {
        this.deserializer = deserializer;
    }

    public IntrinsicFunctionShortFormNodeDeserializer(IDeserializer deserializer, HashSet<string> intrinsicFunctionShortForms)
    {
        this.deserializer = deserializer;
        this.intrinsicFunctionShortForms = intrinsicFunctionShortForms;
    }

    public bool Deserialize(IParser reader, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
    {
        if (reader.Accept<Scalar>(out var scalar))
        {
            if (intrinsicFunctionShortForms.Contains(scalar.Tag))
            {
                value = $"{scalar.Tag} {scalar.Value}";
                reader.MoveNext();
                return true;
            }
        }

        value = null;
        return false;
    }
}

class NodeTypeResolver : INodeTypeResolver
{
    private readonly HashSet<string> intrinsicFunctionShortForms;

    public IntrinsicFunctionShortFormINodeTypeResolver(HashSet<string> intrinsicFunctionShortForms)
    {
        this.intrinsicFunctionShortForms = intrinsicFunctionShortForms;
    }

    public bool Resolve(NodeEvent nodeEvent, ref Type currentType)
    {
        if (!string.IsNullOrEmpty(nodeEvent.Tag) && intrinsicFunctionShortForms.Contains(nodeEvent.Tag))
        {
            currentType = typeof(string);
            return true;
        }
        return false;
    }
} 

@philasmar
Copy link

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants