Example #1
0
        private string getEnumLiteral(Enum item)
        {
            Type        type    = item.GetType();
            EnumMapping mapping = EnumMapping.Create(type);
            //todo: Chaching these mappings should probably optimize performance. But for now load seems managable.
            string literal = mapping.GetLiteral(item);

            return(literal);
        }
Example #2
0
        private static EnumMapping GetEnumMapping(Type enumType)
        {
            EnumMapping fieldInfo = null;

            lock ( _cacheLock )
            {
                if (!_cache.TryGetValue(enumType, out fieldInfo))
                {
                    fieldInfo = EnumMapping.Create(enumType);
                    _cache.Add(enumType, fieldInfo);
                }
            }

            return(fieldInfo);
        }
        public FhirModel(Dictionary <Type, string> csTypeToFhirTypeNameMapping, IEnumerable <SearchParamDefinition> searchParameters, List <Type> enums)
        {
            LoadSearchParameters(searchParameters);
            _csTypeToFhirTypeName = csTypeToFhirTypeNameMapping;
            _fhirTypeNameToCsType = _csTypeToFhirTypeName.ToLookup(pair => pair.Value, pair => pair.Key).ToDictionary(group => group.Key, group => group.FirstOrDefault());

            _enumMappings = new List <EnumMapping>();
            foreach (var enumType in enums)
            {
                if (EnumMapping.IsMappableEnum(enumType))
                {
                    _enumMappings.Add(EnumMapping.Create(enumType));
                }
            }
        }
        public void TestCreation()
        {
            EnumMapping mapping = EnumMapping.Create(typeof(TestEnum));

            Assert.AreEqual("Testee", mapping.Name);
            Assert.AreEqual(typeof(TestEnum), mapping.EnumType);
            Assert.IsTrue(mapping.ContainsLiteral("Item1"));
            Assert.IsFalse(mapping.ContainsLiteral("Item2"));
            Assert.IsFalse(mapping.ContainsLiteral("iTeM1"));
            Assert.IsTrue(mapping.ContainsLiteral("ItemTwo"));

            Assert.AreEqual(TestEnum.Item2, mapping.ParseLiteral("ItemTwo"));
            Assert.AreEqual(TestEnum.Item1, mapping.ParseLiteral("Item1"));
            Assert.AreEqual("ItemTwo", mapping.GetLiteral(TestEnum.Item2));
            Assert.AreEqual("Item1", mapping.GetLiteral(TestEnum.Item1));
        }
        public void LoadAssembly(Assembly fhirAssembly)
        {
            _csTypeToFhirTypeName = new Dictionary <Type, string>();
            _fhirTypeNameToCsType = new Dictionary <string, Type>();
            _enumMappings         = new List <EnumMapping>();

            foreach (Type fhirType in fhirAssembly.GetTypes())
            {
                if (typeof(Resource).IsAssignableFrom(fhirType)) //It is derived of Resource, so it is a Resource type.
                {
                    var fhirTypeAtt = fhirType.GetCustomAttribute <FhirTypeAttribute>();
                    var obsoleteAtt = fhirType.GetCustomAttribute <ObsoleteAttribute>();
                    //CK: Also check for obsolete. Example was Query, which was derived from Parameters, and therefore had the same name ("Parameters").
                    if (fhirTypeAtt != null && fhirTypeAtt.IsResource && (obsoleteAtt == null || !obsoleteAtt.IsError))
                    {
                        if (_csTypeToFhirTypeName.Keys.Contains(fhirType))
                        {
                            Debug.WriteLine("Double import: " + fhirType.FullName);
                        }
                        else
                        {
                            _csTypeToFhirTypeName.Add(fhirType, fhirTypeAtt.Name);
                        }
                        if (_fhirTypeNameToCsType.Keys.Contains(fhirTypeAtt.Name))
                        {
                            Debug.WriteLine("Double import: " + fhirType.FullName);
                        }
                        else
                        {
                            _fhirTypeNameToCsType.Add(fhirTypeAtt.Name, fhirType);
                        }
                    }
                }
                else if (EnumMapping.IsMappableEnum(fhirType))
                {
                    _enumMappings.Add(EnumMapping.Create(fhirType));
                }
            }
        }
Example #6
0
 private static EnumMapping getEnumMapping(Type enumType)
 => _cache.GetOrAdd(enumType, t => EnumMapping.Create(t));