Пример #1
0
        /**
         * @param chainrSpec Plain vanilla hydrated JSON representation of a Chainr spec .json file.
         */
        public ChainrSpec(JToken chainrSpec, IReadOnlyDictionary <string, Type> transforms)
        {
            if (!(chainrSpec is JArray operations))
            {
                throw new SpecException("JOLT Chainr expects a JSON array of objects - Malformed spec.");
            }

            if (operations.Count == 0)
            {
                throw new SpecException("JOLT Chainr passed an empty JSON array.");
            }

            var entries = new List <ChainrEntry>(operations.Count);

            for (int index = 0; index < operations.Count; index++)
            {
                var chainrEntryObj = operations[index];

                ChainrEntry entry = new ChainrEntry(index, chainrEntryObj, transforms);

                entries.Add(entry);
            }

            _chainrEntries = entries.AsReadOnly();
        }
        public IJoltTransform HydrateTransform(ChainrEntry entry)
        {
            var  spec          = entry.GetSpec();
            Type transformType = entry.GetJoltTransformType();

            try
            {
                // If the transform class is a SpecTransform, we try to construct it with the provided spec.
                if (entry.IsSpecDriven())
                {
                    // Lookup a Constructor with a Single "object" arg.
                    var constructor = transformType.GetConstructor(new[] { typeof(JObject) });
                    if (constructor == null)
                    {
                        // This means the transform class "violated" the SpecTransform marker interface
                        throw new SpecException("JOLT Chainr encountered an constructing SpecTransform className:" + transformType.Name +
                                                ".  Specifically, no single arg constructor found" + entry.GetErrorMessageIndexSuffix());
                    }

                    return((IJoltTransform)constructor.Invoke(new object[] { spec }));
                }
                else
                {
                    // The opClass is just a Transform, so just create a newInstance of it.
                    var constructor = transformType.GetConstructor(new Type[0]);
                    if (constructor == null)
                    {
                        throw new Exception("JOLT Chainr encountered an error constructing className:" + transformType.Name +
                                            ".  Specifically, a no arg constructor was not found" + entry.GetErrorMessageIndexSuffix());
                    }
                    return((IJoltTransform)constructor.Invoke(new object[0]));
                }
            }
            catch (Exception e)
            {
                // FYI 3 exceptions are known to be thrown here
                // IllegalAccessException, InvocationTargetException, InstantiationException
                throw new SpecException("JOLT Chainr encountered an exception constructing Transform className:"
                                        + transformType.Name + entry.GetErrorMessageIndexSuffix(), e);
            }
        }