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

Provide Default behaviour of "Nullable" types in plain objects when value is NULL #5

Closed
garzy opened this issue Oct 24, 2019 · 4 comments

Comments

@garzy
Copy link

garzy commented Oct 24, 2019

Hello!, I'm currently generating javascript files with a template, injecting the values with morestachio.

The Problem

Part of my template file (javascript.tpl):

 var loginBonusAmount = {{LoginBonusAmount}};

Part of my plain object (JavascriptParams.cs):

public int? LoginBonusAmount { get; set; }

Wanted output if property is NULL:

var loginBonusAmount = null;

Current output:

var loginBonusAmount = ;

The Solution

public class NullableValuesResolver : IValueResolver {
        public bool CanResolve(Type type, object value, string path, ContextObject context) {
            var propertyType = type.GetProperty(path).PropertyType;
            return Nullable.GetUnderlyingType(propertyType) != null;
        }

        public object Resolve(Type type, object value, string path, ContextObject context) {
            var result = type.GetProperty(path).GetValue(value);
            if (result == null)
            {
                return "null";
            }
            return result;
        }
    }

[...]

 var options = new ParserOptions(sourceTemplate)
 {
    ValueResolver = new NullableValuesResolver()
 };
@garzy
Copy link
Author

garzy commented Oct 24, 2019

Additionally, Null properties of Parser options doesn't works for me in String Properties of my plain .cs object.

Non Working Code 👎

var options = new ParserOptions(sourceTemplate)
 {
    ValueResolver = new NullableValuesResolver(),
     Null = "null" //doesn't works :(
 };

I've fixed this problem using always my NullableValuesResolver, as this:

Working Code 👍

public class NullableValuesResolver : IValueResolver {
        public bool CanResolve(Type type, object value, string path, ContextObject context) {
           return true;
        }

        public object Resolve(Type type, object value, string path, ContextObject context) {
            var result = type.GetProperty(path).GetValue(value);
            if (result == null)
            {
                return "null";
            }
            return result;
        }
    }

@JPVenson JPVenson added the bug label Oct 24, 2019
@JPVenson JPVenson self-assigned this Oct 24, 2019
JPVenson pushed a commit that referenced this issue Oct 24, 2019
JPVenson pushed a commit that referenced this issue Oct 24, 2019
@JPVenson
Copy link
Owner

JPVenson commented Oct 24, 2019

@garzy Thank you for this pretty good written bug i appreciate that. I was able to reproduce the bug and fixed it. A Nuget Release is on its way and should be available soon.

https://www.nuget.org/packages/Morestachio/2.3.7

You should be able to use the "Null" property of the Options object again.

@garzy
Copy link
Author

garzy commented Oct 25, 2019

Thanks for the very fast bugfix, I've tested the new version this morning and its works like a charm with only this code:

var options = new ParserOptions(sourceTemplate)
            {
                Null = "null"
            };

I don't need my NullableValuesResolver anymore :)

Thanks!

@JPVenson
Copy link
Owner

You are welcome.

That are the perks of an HomeOffice ;-)

@JPVenson JPVenson added the fixed label Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants