void AnnotateTypes(IMetaModelRepository repository, ResourceModel model)
 {
     if (model.Hydra().JsonLdType == null)
     {
         model.Hydra().JsonLdType = model.GetJsonLdTypeName();
     }
 }
        void AddImplicitCollectionRegistrations(IMetaModelRepository repository, ResourceModel model)
        {
            var hydra = model.Hydra();

            if (hydra.Collection.IsCollection || hydra.Collection.IsHydraCollectionType)
            {
                return;
            }

            var collectionRm = new ResourceModel
            {
                ResourceKey = HydraTypes.Collection.MakeGenericType(model.ResourceType)
            };


            var collectionHydra = collectionRm.Hydra();

            collectionHydra.Vocabulary     = Vocabularies.Hydra;
            collectionHydra.JsonLdTypeFunc = _ => "hydra:Collection";

            collectionHydra.Collection.IsCollection          = true;
            collectionHydra.Collection.IsFrameworkCollection = false;

            repository.ResourceRegistrations.Add(collectionRm);
        }
        static void CreateClass(IMetaModelRepository _, ResourceModel model)
        {
            var hydraModel = model.Hydra();
            var hydraClass = hydraModel.Class ?? (hydraModel.Class = new HydraCore.Class());

            var vocabPrefix = hydraModel.Vocabulary.DefaultPrefix;
            var className   = model.ResourceType.Name;
            var identifier  = vocabPrefix != null ? $"{vocabPrefix}:{className}" : className;


            hydraModel.Class = new HydraCore.Class
            {
                Identifier          = identifier,
                SupportedProperties = hydraModel.ResourceProperties
                                      .Select(p =>
                                              new HydraCore.SupportedProperty
                {
                    Property = new Rdf.Property
                    {
                        Identifier = $"{identifier}/{p.Name}",
                        Range      = p.RdfRange
                    }
                }
                                              ).ToList(),
                SupportedOperations = hydraModel.SupportedOperations
            };
        }
        public static string GetHydraTypeName(ResourceModel model)
        {
            var hydraResourceModel = model.Hydra();

            return((hydraResourceModel.Vocabulary?.DefaultPrefix == null
               ? String.Empty
               : $"{hydraResourceModel.Vocabulary.DefaultPrefix}:") +
                   model.ResourceType.Name);
        }
Example #5
0
        static string GetTypeName(IMetaModelRepository models, ResourceModel model)
        {
            var opts = models.CustomRegistrations.OfType <HydraOptions>().Single();
            var hydraResourceModel = model.Hydra();

            return((hydraResourceModel.Vocabulary?.DefaultPrefix == null
               ? string.Empty
               : $"{hydraResourceModel.Vocabulary.DefaultPrefix}:") +
                   model.ResourceType.Name);
        }
        void AnnotateCollectionTypes(IMetaModelRepository repository, ResourceModel model)
        {
            var enumerableTypes = model.ResourceType.EnumerableItemTypes().ToList();

            var enumerableType = enumerableTypes.FirstOrDefault();

            if (enumerableType != null && repository.TryGetResourceModel(enumerableType, out var itemModel))
            {
                var hydraResourceModel = model.Hydra();
                hydraResourceModel.Collection.IsCollection          = true;
                hydraResourceModel.Collection.ItemModel             = itemModel;
                hydraResourceModel.Collection.IsFrameworkCollection =
                    enumerableType == model.ResourceType || (model.ResourceType.IsGenericType &&
                                                             model.ResourceType.GetGenericTypeDefinition() == typeof(List <>));
            }
        }
        void PrepareProperties(IMetaModelRepository repository, ResourceModel model)
        {
            if (model.ResourceType == null)
            {
                return;
            }

            var hydra         = model.Hydra();
            var clrProperties = model.ResourceType
                                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                .Where(HydraTextExtensions.IsNotIgnored)
                                .Where(pi => pi.GetIndexParameters().Any() == false)
                                .Select(pi => new ResourceProperty
            {
                Member      = pi,
                Name        = pi.GetJsonPropertyName(),
                IsValueNode = pi.PropertyType.IsValueType && Nullable.GetUnderlyingType(pi.PropertyType) == null,
                RdfRange    = pi.PropertyType.GetRdfRange()
            })
                                .ToList();

            hydra.ResourceProperties.AddRange(clrProperties);
        }
Example #8
0
        static IEnumerable <NodeProperty> GetNodeProperties(
            Variable <JsonWriter> jsonWriter,
            TypedExpression <string> baseUri,
            ResourceModel model,
            Expression resource,
            MemberAccess <Func <object, string> > uriGenerator,
            MemberAccess <Func <object, string> > typeGenerator,
            IMetaModelRepository models,
            Stack <ResourceModel> recursionDefender,
            Variable <HydraJsonFormatterResolver> jsonFormatterResolver,
            Type resourceType,
            TypedExpression <string> resourceUri,
            string resourceRegistrationHydraType)
        {
            var publicProperties = resourceType
                                   .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                   .Where(HydraTextExtensions.IsNotIgnored)
                                   .ToList();

            var propNames     = publicProperties.Select(HydraTextExtensions.GetJsonPropertyName);
            var overridesId   = propNames.Any(name => name == "@id");
            var overridesType = propNames.Any(name => name == "@type");

            if (overridesId == false && model.Uris.Any())
            {
                yield return(WriteId(jsonWriter, resourceUri));
            }

            if (overridesType == false)
            {
                var typePropertyFactory = model.Hydra().TypeFunc;
                if (typePropertyFactory == null)
                {
                    yield return(WriteType(jsonWriter, resourceRegistrationHydraType));
                }
                else
                {
                    yield return(WriteType(jsonWriter, StringMethods.Concat(baseUri, typeGenerator.Invoke(resource))));
                }
            }


            foreach (var pi in publicProperties)
            {
                if (pi.GetIndexParameters().Any())
                {
                    continue;
                }

                if (pi.PropertyType == typeof(string) ||
                    (pi.PropertyType.IsValueType && Nullable.GetUnderlyingType(pi.PropertyType) == null))
                {
                    var nodePropertyValue = WriteNodePropertyValue(
                        jsonWriter,
                        pi,
                        jsonFormatterResolver,
                        resource);

                    yield return(nodePropertyValue);

                    continue;
                }

                yield return(WriteNodeProperty(
                                 jsonWriter, baseUri, resource, uriGenerator, typeGenerator, models, recursionDefender, pi,
                                 jsonFormatterResolver));
            }

            foreach (var link in model.Links)
            {
                yield return(WriteNodeLink(jsonWriter, link.Relationship, link.Uri, resourceUri, link));
            }
        }