scriban / scriban

A fast, powerful, safe and lightweight scripting language and engine for .NET
BSD 2-Clause "Simplified" License
3.1k stars 350 forks source link

Accessing object property using indexer notation #116

Closed ebekker closed 5 years ago

ebekker commented 5 years ago

Based on the docs I was under the impression I can access a property of an object using either the simple dot-notation or the indexer-notation, e.g. if I have an object foo with a property name, I thought both of these are legal:

{{ foo.name }}
{{ foo["name"] }}

But a sample of my code that uses the indexer notation fails with this exception:

Unhandled Exception: Scriban.Syntax.ScriptRuntimeException: Expecting a list. Invalid value `Foo.Model.Attribute/Attribute` for the target `att` for the indexer: att[key]

Where att is a global variable of the type Foo.Model.Attribute -- a native .NET type.

Do I misunderstand the usage?

The reason I was trying to resort to the index-notation is that the Attribute type has a property named Null which surfaces as null in the scriban template and seemingly causes a conflict with the reserved keyword of the same name. For example, both of these are failing:

{{ if att.null }}Not Required{{ end }}

{{ if att["null"] }}Not Required{{ end }}
ebekker commented 5 years ago

BTW, I'm using what I believe is the latest stable version of the library, v1.2.9.

xoofx commented 5 years ago

Don't remember, but it might work only for ScriptObject

ebekker commented 5 years ago

If that's the case, is that by design?

xoofx commented 5 years ago

If that's the case, is that by design?

I don't remember 😉 (currently don't have time to look into the code)

xoofx commented 5 years ago

Should be fixed by commit 548998c that will be part of 2.0.1+