Пример #1
0
 public StmtClassForgeableJsonUnderlying(
     string className,
     string classNameFull,
     CodegenNamespaceScope namespaceScope,
     StmtClassForgeableJsonDesc desc)
 {
     this.className      = className;
     this.classNameFull  = classNameFull;
     this.namespaceScope = namespaceScope;
     this.desc           = desc;
 }
Пример #2
0
        private static void GenerateApplicationClassForgables(
            Type optionalUnderlyingProvided,
            IDictionary <Type, JsonApplicationClassSerializationDesc> deepClasses,
            IList <StmtClassForgeableFactory> additionalForgeables,
            Attribute[] annotations,
            StatementCompileTimeServices services)
        {
            foreach (var entry in deepClasses)
            {
                if (entry.Key == optionalUnderlyingProvided)
                {
                    continue;
                }

                var fields = new LinkedHashMap <string, FieldInfo>();
                entry.Value.Fields.ForEach(field => fields.Put(field.Name, field));

                var properties       = ResolvePropertiesFromFields(fields);
                var fieldNames       = ComputeFieldNamesFromProperties(properties);
                var forges           = ComputeValueForges(properties, fields, deepClasses, annotations, services);
                var fieldDescriptors = ComputeFields(properties, fieldNames, null, fields);
                var forgeableDesc    = new StmtClassForgeableJsonDesc(properties, fieldDescriptors, false, 0, null, forges);

                var deserializerClassNameSimple = entry.Value.DeserializerClassName;
                var deserializer = new ProxyStmtClassForgeableFactory()
                {
                    ProcMake = (
                        namespaceScope,
                        classPostfix) => new StmtClassForgeableJsonDeserializer(
                        CodegenClassType.JSONDESERIALIZER,
                        deserializerClassNameSimple,
                        namespaceScope,
                        entry.Key.FullName,
                        forgeableDesc),
                };

                var serializerClassNameSimple = entry.Value.SerializerClassName;
                var serializer = new ProxyStmtClassForgeableFactory()
                {
                    ProcMake = (
                        namespaceScope,
                        classPostfix) => new StmtClassForgeableJsonSerializer(
                        CodegenClassType.JSONSERIALIZER,
                        serializerClassNameSimple,
                        true,
                        namespaceScope,
                        entry.Key.FullName,
                        forgeableDesc),
                };

                additionalForgeables.Add(deserializer);
                additionalForgeables.Add(serializer);
            }
        }
Пример #3
0
        public static CodegenExpression[] GetCasesNumberNtoM(StmtClassForgeableJsonDesc desc)
        {
            var cases = new CodegenExpression[desc.PropertiesThisType.Count];
            var index = 0;
            foreach (var property in desc.PropertiesThisType) {
                //var field = desc.FieldDescriptorsInclSupertype.Get(property.Key);
                cases[index] = Constant(property.Key);
                index++;
            }

            return cases;
        }
 public StmtClassForgeableJsonDeserializer(
     CodegenClassType classType,
     string className,
     CodegenNamespaceScope namespaceScope,
     string underlyingClassName,
     StmtClassForgeableJsonDesc desc)
 {
     _classType           = classType;
     _className           = className;
     _namespaceScope      = namespaceScope;
     _underlyingClassName = underlyingClassName;
     _desc = desc;
 }
 public StmtClassForgeableJsonSerializer(
     CodegenClassType classType,
     string className,
     bool makeWriteMethod,
     CodegenNamespaceScope namespaceScope,
     string underlyingClassName,
     StmtClassForgeableJsonDesc desc)
 {
     _classType           = classType;
     _className           = className;
     _makeWriteMethod     = makeWriteMethod;
     _namespaceScope      = namespaceScope;
     _underlyingClassName = underlyingClassName;
     _desc = desc;
 }
Пример #6
0
        public static EventTypeForgeablesPair MakeJsonTypeCompileTimeNewType(
            EventTypeMetadata metadata,
            IDictionary <string, object> compiledTyping,
            Pair <EventType[], ISet <EventType> > superTypes,
            ConfigurationCommonEventTypeWithSupertype config,
            StatementRawInfo raw,
            StatementCompileTimeServices services)
        {
            if (metadata.ApplicationType != EventTypeApplicationType.JSON)
            {
                throw new IllegalStateException("Expected Json application type");
            }

            // determine supertype
            var optionalSuperType = (JsonEventType)
                                    (superTypes == null
                                        ? null
                                        : (superTypes.First == null || superTypes.First.Length == 0
                                                ? null
                                                : superTypes.First[0]));
            var numFieldsSuperType = optionalSuperType?.Detail.FieldDescriptors.Count ?? 0;

            // determine dynamic
            var jsonSchema = (JsonSchemaAttribute)AnnotationUtil.FindAnnotation(raw.Annotations, typeof(JsonSchemaAttribute));
            var dynamic    = DetermineDynamic(jsonSchema, optionalSuperType, raw);

            // determine json underlying type class
            var optionalUnderlyingProvided = DetermineUnderlyingProvided(jsonSchema, services);

            // determine properties
            IDictionary <string, object> properties;
            IDictionary <string, string> fieldNames;
            IDictionary <Type, JsonApplicationClassSerializationDesc> deepClasses;
            IDictionary <string, FieldInfo> fields;

            if (optionalUnderlyingProvided == null)
            {
                properties  = ResolvePropertyTypes(compiledTyping, services.EventTypeCompileTimeResolver);
                properties  = RemoveEventBeanTypes(properties);
                fieldNames  = ComputeFieldNames(properties);
                deepClasses = JsonEventTypeUtilityReflective.ComputeClassesDeep(properties, metadata.Name, raw.Annotations, services);
                fields      = EmptyDictionary <string, FieldInfo> .Instance;
            }
            else
            {
                if (dynamic)
                {
                    throw new ExprValidationException("The dynamic flag is not supported when used with a provided JSON event class");
                }

                if (optionalSuperType != null)
                {
                    throw new ExprValidationException("Specifying a supertype is not supported with a provided JSON event class");
                }

                if (!optionalUnderlyingProvided.IsPublic && !optionalUnderlyingProvided.IsNestedPublic)
                {
                    throw new ExprValidationException("Provided JSON event class is not public");
                }

                if (!optionalUnderlyingProvided.HasDefaultConstructor())
                {
                    throw new ExprValidationException("Provided JSON event class does not have a public default constructor or is a non-static inner class");
                }

                deepClasses = JsonEventTypeUtilityReflective.ComputeClassesDeep(optionalUnderlyingProvided, metadata.Name, raw.Annotations, services);
                fields      = new LinkedHashMap <string, FieldInfo>();
                deepClasses.Get(optionalUnderlyingProvided).Fields.ForEach(field => fields.Put(field.Name, field));
                properties     = ResolvePropertiesFromFields(fields);
                fieldNames     = ComputeFieldNamesFromProperties(properties);
                compiledTyping = ResolvePropertyTypes(compiledTyping, services.EventTypeCompileTimeResolver);
                ValidateFieldTypes(optionalUnderlyingProvided, fields, compiledTyping);

                // use the rich-type definition for properties that may come from events
                foreach (var compiledTypingEntry in compiledTyping)
                {
                    if (compiledTypingEntry.Value is TypeBeanOrUnderlying || compiledTypingEntry.Value is TypeBeanOrUnderlying[])
                    {
                        properties.Put(compiledTypingEntry.Key, compiledTypingEntry.Value);
                    }
                }
            }

            var fieldDescriptors = ComputeFields(properties, fieldNames, optionalSuperType, fields);
            // Computes a forge for each property presented.
            var forgesByProperty = ComputeValueForges(properties, fields, deepClasses, raw.Annotations, services);
            // Determines a name for the internal class representation for this json event.
            var jsonClassNameSimple = DetermineJsonClassName(metadata, raw, optionalUnderlyingProvided);

            var forgeableDesc = new StmtClassForgeableJsonDesc(properties, fieldDescriptors, dynamic, numFieldsSuperType, optionalSuperType, forgesByProperty);

            var underlyingClassNameSimple       = jsonClassNameSimple;
            var underlyingClassNameForReference = optionalUnderlyingProvided != null
                                ? optionalUnderlyingProvided.Name
                                : underlyingClassNameSimple;
            var underlyingClassNameFull = optionalUnderlyingProvided == null
                                ? $"{services.Namespace}.{underlyingClassNameSimple}"
                                : optionalUnderlyingProvided.FullName;

            var underlying = new ProxyStmtClassForgeableFactory()
            {
                ProcMake = (
                    namespaceScope,
                    classPostfix) => new StmtClassForgeableJsonUnderlying(
                    underlyingClassNameSimple,
                    underlyingClassNameFull,
                    namespaceScope,
                    forgeableDesc)
            };

            var delegateClassNameSimple = jsonClassNameSimple + "__Delegate";
            var @delegate = new ProxyStmtClassForgeableFactory()
            {
                ProcMake = (
                    namespaceScope,
                    classPostfix) => new StmtClassForgeableJsonDelegate(
                    CodegenClassType.JSONDELEGATE,
                    delegateClassNameSimple,
                    namespaceScope,
                    underlyingClassNameFull,
                    forgeableDesc)
            };

            var deserializerClassNameSimple = jsonClassNameSimple + "__Deserializer";
            var deserializer = new ProxyStmtClassForgeableFactory()
            {
                ProcMake = (
                    namespaceScope,
                    classPostfix) => new StmtClassForgeableJsonDeserializer(
                    CodegenClassType.JSONDESERIALIZER,
                    deserializerClassNameSimple,
                    namespaceScope,
                    underlyingClassNameFull,
                    forgeableDesc)
            };

            var serializerClassNameSimple = jsonClassNameSimple + "__Serializer";
            var serializer = new ProxyStmtClassForgeableFactory()
            {
                ProcMake = (
                    namespaceScope,
                    classPostfix) => new StmtClassForgeableJsonSerializer(
                    CodegenClassType.JSONSERIALIZER,
                    serializerClassNameSimple,
                    optionalUnderlyingProvided != null,
                    namespaceScope,
                    underlyingClassNameFull,
                    forgeableDesc)
            };

            var serializerClassNameFull   = $"{services.Namespace}.{serializerClassNameSimple}";
            var deserializerClassNameFull = $"{services.Namespace}.{deserializerClassNameSimple}";
            var delegateClassNameFull     = $"{services.Namespace}.{delegateClassNameSimple}";

            // include event type name as underlying-class may occur multiple times
            var serdeClassNameFull = $"{services.Namespace}.{jsonClassNameSimple}__{metadata.Name}__Serde";

            var detail = new JsonEventTypeDetail(
                underlyingClassNameFull,
                optionalUnderlyingProvided,
                delegateClassNameFull,
                deserializerClassNameFull,
                serializerClassNameFull,
                serdeClassNameFull,
                fieldDescriptors,
                dynamic,
                numFieldsSuperType);
            var getterFactoryJson = new EventTypeNestableGetterFactoryJson(detail);

            var isStandIn = optionalUnderlyingProvided == null;
            var standIn   = isStandIn
                                ? services.CompilerServices.CompileStandInClass(CodegenClassType.JSONEVENT, underlyingClassNameSimple, services.Services)
                                : optionalUnderlyingProvided;

            var eventType = new JsonEventType(
                metadata,
                properties,
                superTypes == null ? new EventType[0] : superTypes.First,
                superTypes == null ? EmptySet <EventType> .Instance : superTypes.Second,
                config?.StartTimestampPropertyName,
                config?.EndTimestampPropertyName,
                getterFactoryJson,
                services.BeanEventTypeFactoryPrivate,
                detail,
                standIn,
                isStandIn);

            var additionalForgeables = new List <StmtClassForgeableFactory>();

            // generate serializer, deserializer, and delegate forgeables for application classes
            GenerateApplicationClassForgables(
                optionalUnderlyingProvided, deepClasses, additionalForgeables, raw.Annotations, services);

            if (optionalUnderlyingProvided == null)
            {
                additionalForgeables.Add(underlying);
            }

            additionalForgeables.Add(@delegate);
            additionalForgeables.Add(deserializer);
            additionalForgeables.Add(serializer);

            return(new EventTypeForgeablesPair(eventType, additionalForgeables));
        }