Esempio n. 1
0
 public ExportedTypeDetails(TransformedType type,
     HttpMethod allowedMethods,
     string pluralName,
     Action<object> onDeserialized,
     bool mappedAsValueObject,
     bool alwaysExpand)
 {
     this.type = type;
     this.allowedMethods = allowedMethods;
     this.pluralName = pluralName;
     this.onDeserialized = onDeserialized;
     this.mappedAsValueObject = mappedAsValueObject;
     this.type = type;
     this.alwaysExpand = alwaysExpand;
 }
Esempio n. 2
0
        private void AddRepositoryGetByIdMethod(TypeCodeGenInfo rti,
            MethodAttributes methodAttributes,
            bool isImplementation,
            TransformedType tt,
            TypeDefinition repoTypeDef,
            TypeDefinition baseTypeGenericDef,
            TypeReference[] baseTypeGenericArgs,
            string methodName)
        {
            var method = new MethodDefinition(methodName, methodAttributes, rti.InterfaceType);
            var idType = tt.PrimaryId.PropertyType;
            if (!(idType is TypeSpec))
                throw new NotSupportedException("Id needs to be a shared type.");
            var idTypeRef = Import(idType.Type);
            method.Parameters.Add(new ParameterDefinition(tt.PrimaryId.LowerCaseName,
                0,
                idTypeRef));
            repoTypeDef.Methods.Add(method);

            if (isImplementation)
            {
                var baseGetMethodRef =
                    Import(Import(typeof(ClientRepository<,>)).Resolve().Methods.First(x => x.Name == methodName))
                        .MakeHostInstanceGeneric(baseTypeGenericArgs);
                var ilproc = method.Body.GetILProcessor();

                ilproc.Emit(OpCodes.Ldarg_0);
                ilproc.Emit(OpCodes.Ldarg_1);
                if (idType.Type.IsValueType)
                    ilproc.Emit(OpCodes.Box, idTypeRef);
                else
                    ilproc.Emit(OpCodes.Castclass, idTypeRef);
                ilproc.Emit(OpCodes.Callvirt, baseGetMethodRef);
                ilproc.Emit(OpCodes.Ret);
            }
        }
Esempio n. 3
0
            public TypeCodeGenInfo(ClientLibGenerator parent, TransformedType transformedType)
            {
                var resourceType = transformedType as ResourceType;
                if (resourceType != null && resourceType.IsUriBaseType)
                {
                    if ((resourceType.IsRootResource && resourceType.IsExposedAsRepository)
                        || (resourceType.ParentToChildProperty != null
                            && resourceType.ParentToChildProperty.ExposedAsRepository))
                    {
                        this.customRepositoryInterface = new TypeDefinition(parent.assemblyName,
                            string.Format("I{0}Repository", transformedType.Name),
                            TypeAttributes.Interface | TypeAttributes.Public |
                            TypeAttributes.Abstract);
                        parent.module.Types.Add(this.customRepositoryInterface);
                    }
                }

                if (parent == null)
                    throw new ArgumentNullException("parent");
                this.parent = parent;
                this.transformedType = transformedType;

                this.postReturnTypeReference = new System.Lazy<TypeReference>(() =>
                {
                    if (resourceType == null || resourceType.PostReturnType == null)
                        return InterfaceType;
                    return parent.clientTypeInfoDict[resourceType.PostReturnType].InterfaceType;
                });

                this.customRepositoryBaseType = new System.Lazy<Type>(() =>
                {
                    if (resourceType != null)
                    {
                        if (resourceType.IsUriBaseType)
                        {
                            if (resourceType.IsRootResource && resourceType.IsExposedAsRepository)
                                return typeof(ClientRepository<,>);
                            if (resourceType.ParentToChildProperty != null
                                && resourceType.ParentToChildProperty.ExposedAsRepository)
                                return typeof(ChildResourceRepository<,>);
                        }
                    }
                    return null;
                });

                this.customRepositoryBaseTypeDefinition = new System.Lazy<TypeDefinition>(() =>
                {
                    if (this.customRepositoryBaseType.Value == null)
                        return null;

                    var typeRef = parent.Import(this.customRepositoryBaseType.Value);
                    return typeRef as TypeDefinition ?? typeRef.Resolve();
                });

                this.customRepositoryBaseTypeReference = new System.Lazy<TypeReference>(() =>
                {
                    if (this.customRepositoryBaseType.Value == null)
                        return null;

                    return
                        parent.Import(this.customRepositoryBaseType.Value).MakeGenericInstanceType(
                            InterfaceType,
                            PostReturnTypeReference);
                });

                this.interfaceType = new TypeDefinition(
                    parent.assemblyName,
                    "I" + transformedType.Name,
                    TypeAttributes.Interface | TypeAttributes.Public |
                    TypeAttributes.Abstract);
            }
 public abstract ExportedTypeDetails LoadExportedTypeDetails(TransformedType exportedType);
        public PomonaQuery TransformRequest(NancyContext nancyContext, TransformedType rootType)
        {
            var request = nancyContext.Request;
            if (request == null)
                throw new ArgumentNullException("request");
            if (nancyContext == null)
                throw new ArgumentNullException("nancyContext");
            if (rootType == null)
                throw new ArgumentNullException("rootType");

            TransformedType ofType = null;
            if (request.Query["$oftype"].HasValue)
            {
                ofType = (TransformedType) typeMapper.GetClassMapping((string) request.Query["$oftype"]);
            }

            var query = new PomonaQuery(rootType, ofType);

            if (request.Query["$debug"].HasValue)
            {
                query.DebugInfoKeys =
                    new HashSet<string>(((string) request.Query["$debug"]).ToLower().Split(',').Select(x => x.Trim()));
            }

            string filter = null;
            var top = 100;
            var skip = 0;

            if (request.Query["$totalcount"].HasValue && ((string) request.Query["$totalcount"]).ToLower() == "true")
                query.IncludeTotalCount = true;

            if (request.Query["$top"].HasValue)
                top = int.Parse(request.Query["$top"]);

            if (request.Query["$skip"].HasValue)
                skip = int.Parse(request.Query["$skip"]);

            if (request.Query["$filter"].HasValue)
                filter = (string) request.Query["$filter"];

            ParseFilterExpression(query, filter);
            var selectSourceType = query.OfType.Type;

            if (request.Query["$groupby"].HasValue)
            {
                var groupby = (string) request.Query["$groupby"];
                ParseGroupByExpression(query, groupby);
                selectSourceType =
                    typeof (IGrouping<,>).MakeGenericType(
                        query.GroupByExpression.ReturnType, selectSourceType);
            }

            if (request.Query["$projection"].HasValue)
            {
                var projectionString = (string) request.Query["$projection"];
                PomonaQuery.ProjectionType projection;
                if (!Enum.TryParse(projectionString, true, out projection))
                    throw new QueryParseException("\"" + projectionString +
                                                  "\" is not a valid value for query parameter $projection",
                        null,
                        QueryParseErrorReason.UnrecognizedProjection,
                        null);
                query.Projection = projection;
            }

            if (request.Query["$select"].HasValue)
            {
                var select = (string) request.Query["$select"];
                ParseSelect(query, select, selectSourceType);
            }

            if (request.Query["$orderby"].HasValue)
                ParseOrderBy(query, (string) request.Query["$orderby"]);

            query.Top = top;
            query.Skip = skip;

            if (request.Query["$expand"].HasValue)
            {
                // TODO: Translate expanded paths using TypeMapper
                query.ExpandedPaths = ((string) request.Query["$expand"]);
            }
            else
                query.ExpandedPaths = string.Empty;

            query.Url = request.Url.ToString();

            UpdateResultType(query);

            return query;
        }
Esempio n. 6
0
        private SchemaTypeEntry GenerateForType(TransformedType transformedType)
        {
            string extends = null;
            var resourceTypeSpec = transformedType as ResourceType;
            IEnumerable<PropertyMapping> properties = transformedType.Properties;
            if (resourceTypeSpec == null || !resourceTypeSpec.IsUriBaseType)
            {
                extends = transformedType.BaseType.Name;
                var propsOfBaseType = new HashSet<string>(transformedType.BaseType.Properties.Select(x => x.Name));
                properties = properties.Where(x => !propsOfBaseType.Contains(x.Name));
            }

            var typeName = transformedType.Name;

            var schemaTypeEntry = new SchemaTypeEntry
            {
                Extends = extends,
                Name = typeName,
                Properties = new SortedDictionary<string, SchemaPropertyEntry>(properties
                    .Select(GenerateForProperty)
                    .ToDictionary(x => x.Name, x => x)),
                // TODO: Expose IsAbstract on TypeSpec
                Abstract = transformedType.Type != null && transformedType.Type.IsAbstract,
                AllowedMethods = transformedType.AllowedMethods
            };

            if (resourceTypeSpec != null)
            {
                schemaTypeEntry.Uri = resourceTypeSpec.UriRelativePath;
            }

            return schemaTypeEntry;
        }
Esempio n. 7
0
 private object Deserialize(TransformedType expectedBaseType, Stream body, object patchedObject = null)
 {
     using (var textReader = new StreamReader(body))
     {
         return this.serializerFactory.GetDeserializer().Deserialize(textReader,
             new DeserializeOptions() { Target = patchedObject, ExpectedBaseType = expectedBaseType, TargetNode = Node });
     }
 }