Example #1
0
        public override ValueTask <FluidValue> GetValueAsync(string name, TemplateContext context)
        {
            // The model type has a custom ability to allow any of its members optionally
            _isModelType ??= context.Model != null && context.Model?.ToObjectValue()?.GetType() == Value.GetType();

            var accessor = context.Options.MemberAccessStrategy.GetAccessor(Value.GetType(), name);

            if (accessor == null && _isModelType.Value && context.AllowModelMembers)
            {
                accessor = MemberAccessStrategyExtensions.GetNamedAccessor(Value.GetType(), name, context.Options.MemberAccessStrategy.MemberNameStrategy);
            }

            if (name.IndexOf(".", StringComparison.OrdinalIgnoreCase) != -1)
            {
                // Try to access the property with dots inside
                if (accessor != null)
                {
                    if (accessor is IAsyncMemberAccessor asyncAccessor)
                    {
                        return(Awaited(asyncAccessor, Value, name, context));
                    }

                    var directValue = accessor.Get(Value, name, context);

                    if (directValue != null)
                    {
                        return(new ValueTask <FluidValue>(FluidValue.Create(directValue, context.Options)));
                    }
                }

                // Otherwise split the name in different segments
                return(GetNestedValueAsync(name, context));
            }
            else
            {
                if (accessor != null)
                {
                    if (accessor is IAsyncMemberAccessor asyncAccessor)
                    {
                        return(Awaited(asyncAccessor, Value, name, context));
                    }

                    return(FluidValue.Create(accessor.Get(Value, name, context), context.Options));
                }
            }

            return(new ValueTask <FluidValue>(NilValue.Instance));
Example #2
0
        public override ValueTask <FluidValue> ResolveAsync(Scope value, TemplateContext context)
        {
            async ValueTask <FluidValue> Awaited(
                IAsyncMemberAccessor asyncAccessor,
                TemplateContext ctx,
                string identifier)
            {
                var o = await asyncAccessor.GetAsync(ctx.Model, identifier, ctx);

                return(FluidValue.Create(o, context.Options));
            }

            var result = value.GetValue(Identifier);

            // If there are no named property for this identifier, check in the Model
            if (result.IsNil() && context.Model != null)
            {
                // Check for a custom registration
                var modelType = context.Model.GetType();
                if (modelType != _type)
                {
                    _accessor = context.Options.MemberAccessStrategy.GetAccessor(modelType, Identifier);
                    _accessor ??= MemberAccessStrategyExtensions.GetNamedAccessor(modelType, Identifier, MemberNameStrategies.Default);

                    if (_accessor != null)
                    {
                        _type = modelType;
                    }
                }

                if (_accessor != null)
                {
                    if (_accessor is IAsyncMemberAccessor asyncAccessor)
                    {
                        return(Awaited(asyncAccessor, context, Identifier));
                    }

                    return(new ValueTask <FluidValue>(FluidValue.Create(_accessor.Get(context.Model, Identifier, context), context.Options)));
                }

                return(new ValueTask <FluidValue>(NilValue.Instance));
            }

            return(new ValueTask <FluidValue>(result));
        }