Example #1
0
        protected override void ProcessRecord()
        {
            var jmes   = new JmesPath();
            var result = jmes.Transform(json: Content, expression: JMESPath);

            WriteObject(result);
        }
Example #2
0
        private static ComplianceResult EvaluateExpression(JToken document, string expression)
        {
            try
            {
                var parser = new JmesPath();
                parser.FunctionRepository
                .Register <ItemsFunction>()
                .Register <ToObjectFunction>()
                .Register <ZipFunction>()
                ;

                var result = parser.Transform(document, expression);

                return(new ComplianceResult
                {
                    Success = true,
                    Result = result,
                });
            }
            catch (Exception exception)
            {
                return(new ComplianceResult
                {
                    Success = false,
                    Error = exception.Message,
                });
            }
        }
Example #3
0
        private object QueryCore(dynamic context, object[] arguments)
        {
            var hashParameters = arguments.OfType <HashParameterDictionary>();

            var args = arguments.Except(hashParameters);

            var formatString    = args.Take(1).SingleOrDefault()?.ToString() ?? "@";
            var formatArguments = args.Skip(1).Select(ToJsonString).ToArray();

            formatString = formatArguments.Select((_, index) => index).Aggregate(formatString.Replace("{", "{{").Replace("}", "}}"), (acc, index) => acc.Replace($"${index + 1}", $"{{{index}}}"));

            var query = string.Format(formatString, args: formatArguments.ToArray());
            var json  = arguments.OfType <HashParameterDictionary>().SingleOrDefault() ?? (object)context;

            var jmespath = new JmesPath();

            jmespath.FunctionRepository
            .Register <ItemsFunction>()
            .Register <ToObjectFunction>()
            .Register <ZipFunction>();

            var token           = Services.Serializers.JTokenTranserializer(json);
            var transformOutput = jmespath.Transform(token, query);
            var transformObject = Services.Serializers.YamlDeserializer.Deserialize <object>(transformOutput.ToString());

            return(transformObject);
        }
        public async Task WriteAsync(TextWriter writer, object content)
        {
            try
            {
                var settings = new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Converters       = (new JsonConverter[]
                    {
                        new VersionConverter(),
                        new FileInfoConverter()
                    }).ToList()
                };

                var json = JsonConvert.SerializeObject(content, Formatting.Indented, settings);

                if (string.IsNullOrEmpty(this.Query))
                {
                    await writer.WriteLineAsync(json);
                }
                else
                {
                    var jms    = new JmesPath();
                    var result = jms.Transform(json, this.Query);
                    var tmp    = JsonConvert.DeserializeObject(result);
                    await writer.WriteAsync(JsonConvert.SerializeObject(tmp, Formatting.Indented, settings));
                }
            }
            catch (Exception ex)
            {
                await writer.WriteLineAsync($"Error: {ex.Message}");
            }
        }
Example #5
0
        public JmesPathExpressionEvaluatorTests()
        {
            _jmesPath = new JmesPath();
            _singleValueExpressionEvaluator = new JmesPathExpressionEvaluator(_jmesPath, "testProperty");

            _projectedExpressionEvaluator = new JmesPathExpressionEvaluator(_jmesPath, "property[].name");
        }
Example #6
0
 /// <summary>
 /// Creates a new instance of JmesPathOutputFilter
 /// </summary>
 /// <param name="jmesPath">The JmesPath transformer instance.</param>
 public JmesPathOutputFilter(JmesPath jmesPath)
 {
     if (jmesPath is null)
     {
         throw new ArgumentNullException(nameof(jmesPath), $"Parameter '{nameof(jmesPath)}' is required.");
     }
     this.jmesPath = jmesPath;
 }
        protected void Assert(string expression, string input, string expected)
        {
            var path = new JmesPath();

            var result = path.Transform(input, expression);

            Xunit.Assert.Equal(expected, result);
        }
        public static string Transform()
        {
            jmes = new JmesPath();

            var resultJson = jmes.Transform(json[3], expression[3]);

            return(resultJson);
        }
Example #9
0
 public JmesPathQuery(IYamlSerializers serializers)
 {
     _jmespath = new JmesPath();
     _jmespath.FunctionRepository
     .Register <ItemsFunction>()
     .Register <ToObjectFunction>()
     .Register <ZipFunction>();
     _serializers = serializers;
 }
Example #10
0
        public RenderingEngine()
        {
            jmesPath = new JmesPath();

            jmesPath.FunctionRepository
            .Register <ItemsFunction>()
            .Register <ToObjectFunction>()
            .Register <ZipFunction>()
            .Register <ConcatFunction>()
            .Register("search", new SearchFunction(jmesPath));
        }
        public JmesPathExpressionEvaluator(JmesPath jmesPath, string expression)
        {
            EnsureArg.IsNotNull(jmesPath, nameof(jmesPath));
            EnsureArg.IsNotNullOrWhiteSpace(expression, nameof(expression));

            try
            {
                _jmespathExpression = jmesPath.Parse(expression);
            }
            catch (Exception e)
            {
                throw new TemplateExpressionException($"The following JmesPath expression could not be parsed: {expression}", e);
            }
        }
Example #12
0
 public JmesPathQuery(IYamlSerializers serializers)
 {
     _jmespath = new JmesPath();
     _jmespath.FunctionRepository
     .Register <ItemsFunction>()
     .Register <ToObjectFunction>()
     .Register <ZipFunction>()
     .Register <DistinctByFunction>()
     .Register <DistinctFunction>()
     .Register <Base64DecodeFunction>()
     .Register <Base64EncodeFunction>()
     .Register <BitsFunction>();
     _serializers = serializers;
 }
Example #13
0
        public async Task Call_JmesPath_Transform_Function()
        {
            var jmesPath = new JmesPath();
            var filter   = new JmesPathOutputFilter(jmesPath);
            var buffer   = Encoding.ASCII.GetBytes("{\"a\": 1, \"b\": true}");

            using var ms = new MemoryStream(buffer);

            using var result = await filter.FilterOutputAsync(ms, "a");

            using var reader = new StreamReader(result);
            var str = await reader.ReadToEndAsync();

            Assert.Equal("1", str);
        }
Example #14
0
        public CalculatedFunctionContentTemplateDocumentationTests()
        {
            var logger = Substitute.For <ITelemetryLogger>();

            var jmesPath         = new JmesPath();
            var functionRegister = new AssemblyExpressionRegister(typeof(IExpressionRegister).Assembly, logger);

            functionRegister.RegisterExpressions(jmesPath.FunctionRepository);

            _collectionContentTemplateFactory = new CollectionContentTemplateFactory(
                new JsonPathContentTemplateFactory(),
                new IotJsonPathContentTemplateFactory(),
                new IotCentralJsonPathContentTemplateFactory(),
                new CalculatedFunctionContentTemplateFactory(new TemplateExpressionEvaluatorFactory(jmesPath), logger));
        }
Example #15
0
 public MultiplyFunctionTests()
 {
     _jmesPath = new JmesPath();
     _jmesPath.FunctionRepository.Register <MultiplyFunction>();
     _expression = _jmesPath.Parse("multiply(left, right)");
 }
Example #16
0
 public MultiSelect()
 {
     jmes = new JmesPath();
 }
 public ObjectProjections()
 {
     jmes = new JmesPath();
 }
Example #18
0
 public TemplateExpressionEvaluatorFactory(JmesPath jmesPath)
 {
     _jmesPath = EnsureArg.IsNotNull(jmesPath, nameof(jmesPath));
 }