Esempio n. 1
0
 public void UpdateType(RegisteredType registeredType)
 {
     foreach (ITypeReference typeReference in registeredType.References)
     {
         _registered[typeReference] = registeredType;
     }
 }
Esempio n. 2
0
        public void Register(NameString typeName, RegisteredType registeredType)
        {
            if (registeredType is null)
            {
                throw new ArgumentNullException(nameof(registeredType));
            }

            typeName.EnsureNotEmpty(nameof(typeName));

            if (registeredType.IsExtension)
            {
                return;
            }

            if (!registeredType.IsNamedType && !registeredType.IsDirectiveType)
            {
                return;
            }

            if (TryGetTypeRef(typeName, out ITypeReference? typeRef) &&
                TryGetType(typeRef, out RegisteredType? type) &&
                !ReferenceEquals(type, registeredType))
            {
                throw TypeInitializer_DuplicateTypeName(registeredType.Type, type.Type);
            }

            _nameRefs[typeName] = registeredType.References[0];
        }
Esempio n. 3
0
        public void Register(RegisteredType registeredType)
        {
            if (registeredType is null)
            {
                throw new ArgumentNullException(nameof(registeredType));
            }

            bool addToTypes = !_typeRegister.ContainsValue(registeredType);

            foreach (ITypeReference typeReference in registeredType.References)
            {
                if (_typeRegister.TryGetValue(typeReference, out RegisteredType? current) &&
                    !ReferenceEquals(current, registeredType))
                {
                    if (current.IsInferred && !registeredType.IsInferred)
                    {
                        _typeRegister[typeReference] = registeredType;
                        if (!_typeRegister.ContainsValue(current))
                        {
                            _types.Remove(current);
                        }
                    }
                }
                else
                {
                    _typeRegister[typeReference] = registeredType;
                }
            }

            if (addToTypes)
            {
                _types.Add(registeredType);
            }
        }
Esempio n. 4
0
        public void Register(TypeSystemObjectBase typeSystemObject, bool isInferred = false)
        {
            RegisteredType registeredType = InitializeType(typeSystemObject, isInferred);

            if (registeredType.References.Count > 0)
            {
                foreach (ITypeReference typeReference in registeredType.References)
                {
                    _registered[typeReference] = registeredType;
                    _unresolved.Remove(typeReference);
                }

                if (typeSystemObject is IHasClrType hasClrType &&
                    hasClrType.ClrType != typeof(object))
                {
                    var clrRef = new ClrTypeReference(
                        hasClrType.ClrType,
                        SchemaTypeReference.InferTypeContext(typeSystemObject));
                    _unresolved.Remove(clrRef);

                    if (!_clrTypeReferences.ContainsKey(clrRef))
                    {
                        _clrTypeReferences.Add(clrRef, registeredType.References[0]);
                    }
                }
            }
        }
Esempio n. 5
0
        public void Register(TypeSystemObjectBase typeSystemObject, bool isInferred = false)
        {
            RegisteredType registeredType = InitializeType(typeSystemObject, isInferred);

            if (registeredType.References.Count > 0)
            {
                foreach (ITypeReference typeReference in registeredType.References)
                {
                    _registered[typeReference] = registeredType;
                    _unresolved.Remove(typeReference);
                }

                if (typeSystemObject is IHasClrType hasClrType &&
                    hasClrType.ClrType != typeof(object))
                {
                    var clrRef = new ClrTypeReference(
                        hasClrType.ClrType,
                        SchemaTypeReference.InferTypeContext(typeSystemObject));
                    _unresolved.Remove(clrRef);

                    bool autoBind = true;

                    if (typeSystemObject is ScalarType scalar &&
                        scalar.Bind == BindingBehavior.Explicit)
                    {
                        autoBind = false;
                    }

                    if (autoBind && !_clrTypeReferences.ContainsKey(clrRef))
                    {
                        _clrTypeReferences.Add(clrRef, registeredType.References[0]);
                    }
                }
            }
        }
Esempio n. 6
0
 private void ResolveReferences(RegisteredType registeredType)
 {
     foreach (ITypeReference typeReference in registeredType.References)
     {
         _registered[typeReference] = registeredType;
         MarkResolved(typeReference);
     }
 }
Esempio n. 7
0
        private void ResolveReferences(RegisteredType registeredType)
        {
            _typeRegistry.Register(registeredType);

            foreach (ITypeReference typeReference in registeredType.References)
            {
                MarkResolved(typeReference);
            }
        }
Esempio n. 8
0
        private RegisteredType InitializeType(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred)
        {
            try
            {
                var discoveryContext = new TypeDiscoveryContext(
                    typeSystemObject,
                    scope,
                    _serviceFactory.Services,
                    _descriptorContext,
                    _interceptor);

                typeSystemObject.Initialize(discoveryContext);

                var references = new List <ITypeReference>();

                if (!isInferred)
                {
                    references.Add(TypeReference.Create(
                                       typeSystemObject,
                                       scope: scope));
                }

                if (!BaseTypes.IsNonGenericBaseType(typeSystemObject.GetType()))
                {
                    references.Add(TypeReference.Create(
                                       typeSystemObject.GetType(),
                                       SchemaTypeReference.InferTypeContext(typeSystemObject),
                                       scope: scope));
                }

                if (typeSystemObject is IHasTypeIdentity hasTypeIdentity &&
                    hasTypeIdentity.TypeIdentity is { })
                {
                    var reference = TypeReference.Create(
                        hasTypeIdentity.TypeIdentity,
                        SchemaTypeReference.InferTypeContext(typeSystemObject),
                        scope: scope);

                    if (!references.Contains(reference))
                    {
                        references.Add(reference);
                    }
                }

                var registeredType = new RegisteredType(
                    references,
                    typeSystemObject,
                    discoveryContext,
                    CollectDependencies(discoveryContext),
                    isInferred);

                return(registeredType);
            }
Esempio n. 9
0
        private RegisteredType InitializeType(
            TypeSystemObjectBase typeSystemObject,
            bool isInferred)
        {
            try
            {
                var initializationContext = new InitializationContext(
                    typeSystemObject,
                    _serviceFactory.Services,
                    _descriptorContext,
                    _contextData,
                    _interceptor);

                typeSystemObject.Initialize(initializationContext);

                var references = new List <ITypeReference>();

                if (!isInferred)
                {
                    references.Add(new SchemaTypeReference(typeSystemObject));
                }

                if (!BaseTypes.IsNonGenericBaseType(typeSystemObject.GetType()))
                {
                    references.Add(new ClrTypeReference(
                                       typeSystemObject.GetType(),
                                       SchemaTypeReference.InferTypeContext(typeSystemObject)));
                }

                if (typeSystemObject is IHasTypeIdentity hasTypeIdentity &&
                    hasTypeIdentity.TypeIdentity is { })
                {
                    var reference = new ClrTypeReference(
                        hasTypeIdentity.TypeIdentity,
                        SchemaTypeReference.InferTypeContext(typeSystemObject));

                    if (!references.Contains(reference))
                    {
                        references.Add(reference);
                    }
                }

                var registeredType = new RegisteredType(
                    references,
                    typeSystemObject,
                    initializationContext,
                    initializationContext.TypeDependencies,
                    isInferred);

                return(registeredType);
            }
Esempio n. 10
0
        public bool TryGetType(
            ITypeReference typeReference,
            out RegisteredType registeredType)
        {
            ITypeReference typeRef = typeReference;

            if (typeRef is IClrTypeReference clrTypeRef &&
                _clrTypeReferences.TryGetValue(clrTypeRef, out ITypeReference t))
            {
                typeRef = t;
            }

            return(_registered.TryGetValue(typeRef, out registeredType));
        }
Esempio n. 11
0
        private bool TryNormalizeDependencies(
            RegisteredType registeredType,
            IEnumerable <ITypeReference> dependencies,
            out IReadOnlyList <ITypeReference> normalized)
        {
            var n = new List <ITypeReference>();

            foreach (ITypeReference reference in dependencies)
            {
                if (!TryNormalizeReference(reference, out ITypeReference nr))
                {
                    normalized = null;
                    return(false);
                }
                _depsLup[reference] = nr;
                n.Add(nr);
            }

            normalized = n;
            return(true);
        }
Esempio n. 12
0
        private void MergeTypeExtension(
            IEnumerable <RegisteredType> extensions,
            RegisteredType registeredType,
            INamedType namedType)
        {
            foreach (RegisteredType extension in extensions)
            {
                if (extension.Type is INamedTypeExtensionMerger m)
                {
                    if (m.Kind != namedType.Kind)
                    {
                        throw new SchemaException(SchemaErrorBuilder.New()
                                                  .SetMessage(string.Format(
                                                                  CultureInfo.InvariantCulture,
                                                                  TypeInitializer_Merge_KindDoesNotMatch,
                                                                  namedType.Name))
                                                  .SetTypeSystemObject((ITypeSystemObject)namedType)
                                                  .Build());
                    }

                    TypeDiscoveryContext initContext = extension.DiscoveryContext;
                    foreach (FieldReference reference in initContext.Resolvers.Keys)
                    {
                        _resolvers[reference]
                            = initContext.Resolvers[reference].WithSourceType(registeredType.RuntimeType);
                    }

                    // merge
                    TypeCompletionContext context = extension.CompletionContext;
                    context.Status = TypeStatus.Named;
                    m.Merge(context, namedType);

                    // update dependencies
                    context = registeredType.CompletionContext;
                    registeredType.AddDependencies(extension.Dependencies);
                    _typeRegistry.Register(registeredType);
                    CopyAlternateNames(extension.CompletionContext, context);
                }
            }
        }
Esempio n. 13
0
        private void MergeTypeExtensions()
        {
            var extensions = _types.Values
                             .Where(t => t.Type is INamedTypeExtensionMerger)
                             .Distinct()
                             .ToList();

            if (extensions.Count > 0)
            {
                var types = _types.Values
                            .Where(t => t.Type is INamedType)
                            .Distinct()
                            .ToList();

                foreach (RegisteredType extension in extensions)
                {
                    RegisteredType type = types.FirstOrDefault(t =>
                                                               t.Type.Name.Equals(extension.Type.Name));

                    if (type != null &&
                        extension.Type is INamedTypeExtensionMerger m &&
                        type.Type is INamedType n)
                    {
                        // merge
                        CompletionContext context = _cmpCtx[extension];
                        context.Status = TypeStatus.Named;
                        MergeTypeExtension(context, m, n);

                        // update dependencies
                        context = _cmpCtx[type];
                        type    = type.AddDependencies(extension.Dependencies);
                        type.Update(_types);
                        _cmpCtx[type] = context;
                        CopyAlternateNames(_cmpCtx[extension], context);
                    }
                }
            }
        }
Esempio n. 14
0
        public bool TryGetRegisteredType(
            ITypeReference reference,
            out RegisteredType registeredType)
        {
            if (reference == null)
            {
                throw new ArgumentNullException(nameof(reference));
            }

            if (_types.TryGetValue(reference, out registeredType))
            {
                return(true);
            }

            if (TryNormalizeReference(reference, out ITypeReference nr) &&
                _types.TryGetValue(nr, out registeredType))
            {
                return(true);
            }

            registeredType = null;
            return(false);
        }
Esempio n. 15
0
        public void Register(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred = false)
        {
            RegisteredType registeredType = InitializeType(
                typeSystemObject,
                scope,
                isInferred);

            if (registeredType.References.Count > 0)
            {
                ResolveReferences(registeredType);

                if (typeSystemObject is IHasRuntimeType hasClrType &&
                    hasClrType.RuntimeType != typeof(object))
                {
                    var clrRef = TypeReference.Create(
                        hasClrType.RuntimeType,
                        SchemaTypeReference.InferTypeContext(typeSystemObject),
                        scope: scope);

                    var explicitBind = typeSystemObject is ScalarType scalar &&
                                       scalar.Bind == BindingBehavior.Explicit;

                    if (!explicitBind)
                    {
                        MarkResolved(clrRef);

                        if (!_clrTypeReferences.ContainsKey(clrRef))
                        {
                            _clrTypeReferences.Add(clrRef, registeredType.References[0]);
                        }
                    }
                }
            }
        }
Esempio n. 16
0
        public void Register(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred = false)
        {
            if (typeSystemObject is null)
            {
                throw new ArgumentNullException(nameof(typeSystemObject));
            }

            RegisteredType registeredType = InitializeType(typeSystemObject, scope, isInferred);

            if (registeredType.References.Count > 0)
            {
                ResolveReferences(registeredType);

                if (typeSystemObject is IHasRuntimeType hasRuntimeType &&
                    hasRuntimeType.RuntimeType != typeof(object))
                {
                    ExtendedTypeReference runtimeTypeRef =
                        _context.TypeInspector.GetTypeRef(
                            hasRuntimeType.RuntimeType,
                            SchemaTypeReference.InferTypeContext(typeSystemObject),
                            scope: scope);

                    var explicitBind = typeSystemObject is ScalarType scalar &&
                                       scalar.Bind == BindingBehavior.Explicit;

                    if (!explicitBind)
                    {
                        MarkResolved(runtimeTypeRef);
                        _typeRegistry.TryRegister(runtimeTypeRef, registeredType.References[0]);
                    }
                }
            }
        }
Esempio n. 17
0
        private RegisteredType InitializeType(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred)
        {
            try
            {
                var discoveryContext = new TypeDiscoveryContext(
                    typeSystemObject,
                    _typeRegistry,
                    _typeLookup,
                    _context,
                    _interceptor,
                    scope);

                typeSystemObject.Initialize(discoveryContext);

                var references = new List <ITypeReference>();

                if (!isInferred)
                {
                    references.Add(TypeReference.Create(typeSystemObject, scope));
                }

                if (!ExtendedType.Tools.IsNonGenericBaseType(typeSystemObject.GetType()))
                {
                    references.Add(_context.TypeInspector.GetTypeRef(
                                       typeSystemObject.GetType(),
                                       SchemaTypeReference.InferTypeContext(typeSystemObject),
                                       scope));
                }

                if (typeSystemObject is IHasTypeIdentity hasTypeIdentity &&
                    hasTypeIdentity.TypeIdentity is not null)
                {
                    ExtendedTypeReference reference =
                        _context.TypeInspector.GetTypeRef(
                            hasTypeIdentity.TypeIdentity,
                            SchemaTypeReference.InferTypeContext(typeSystemObject),
                            scope);

                    if (!references.Contains(reference))
                    {
                        references.Add(reference);
                    }
                }

                var registeredType = new RegisteredType(
                    typeSystemObject,
                    references,
                    CollectDependencies(discoveryContext),
                    discoveryContext,
                    isInferred);

                return(registeredType);
            }
            catch (Exception ex)
            {
                throw new SchemaException(
                          SchemaErrorBuilder.New()
                          .SetMessage(ex.Message)
                          .SetException(ex)
                          .SetTypeSystemObject(typeSystemObject)
                          .Build());
            }
        }