コード例 #1
0
        public IThriftCodec GetCodec(ThriftType type)
        {
            // The loading function pushes types before they are loaded and pops them afterwards in
            // order to detect recursive loading (which will would otherwise fail in the LoadingCache).
            // In this case, to avoid the cycle, we return a DelegateCodec that points back to this
            // ThriftCodecManager and references the type. When used, the DelegateCodec will require
            // that our cache contain an actual ThriftCodec, but this should not be a problem as
            // it won't be used while we are loading types, and by the time we're done loading the
            // type at the top of the stack, *all* types on the stack should have been loaded and
            // cached.
            if (_stack.Value.Contains(type))
            {
                //性能需要改进?
                var codecType = typeof(DelegateCodec <>).MakeGenericType(type.CSharpType);
                return((IThriftCodec)System.Activator.CreateInstance(codecType, this));
            }
            var thriftCodec = _typeCodecs.GetOrAdd(type, Load);

            while (_deferredTypesWorkList.Value.Count > 0)
            {
                var first = _deferredTypesWorkList.Value.Pop();
                GetCodec(first);
            }

            return(thriftCodec);
        }
コード例 #2
0
        public SetThriftCodec(ThriftType type, IThriftCodec <T> elementCodec)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(elementCodec, nameof(elementCodec));

            this._type         = type;
            this._elementCodec = elementCodec;
        }
コード例 #3
0
        public ListThriftCodec(ThriftType type, IThriftCodec <T> elementCodec)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(elementCodec, nameof(elementCodec));

            this.type         = type;
            this.elementCodec = elementCodec;
            this._isArray     = type.CSharpType.IsArray;
        }
コード例 #4
0
        public Object GetCodec(Type csharpType)
        {
            ThriftType thriftType = this.Catalog.GetThriftType(csharpType);

            if (thriftType == null)
            {
                throw new ThriftyException($"Unsupported csharp type {csharpType.FullName}");
            }
            return(GetCodec(thriftType));
        }
コード例 #5
0
        public MapThriftCodec(ThriftType type, IThriftCodec <K> keyCodec, IThriftCodec <V> valueCodec)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(keyCodec, nameof(keyCodec));
            Guard.ArgumentNotNull(valueCodec, nameof(valueCodec));

            this._thriftType = type;
            this._keyCodec   = keyCodec;
            this._valueCodec = valueCodec;
        }
コード例 #6
0
        internal IThriftCodec GetCachedCodecIfPresent(Type csharpType)
        {
            ThriftType thriftType = Catalog.GetThriftType(csharpType);

            if (thriftType == null)
            {
                throw new ThriftyException($"Unsupported csharp type {csharpType.FullName}");
            }

            return(this.GetCachedCodecIfPresent(thriftType));
        }
コード例 #7
0
        private static ThriftFieldMetadata CreateFieldMetadata(ThriftCatalog catalog, int index, ParameterInfo parameterInfo)
        {
            ThriftFieldAttribute thriftField   = parameterInfo.GetCustomAttribute <ThriftFieldAttribute>();
            short        parameterId           = short.MinValue;
            String       parameterName         = parameterInfo.Name;
            Requiredness parameterRequiredness = Requiredness.Unspecified;

            if (thriftField != null)
            {
                parameterId           = thriftField.Id;
                parameterRequiredness = thriftField.Required;
                if (!String.IsNullOrWhiteSpace(thriftField.Name))
                {
                    parameterName = thriftField.Name.Trim();
                }
            }
            if (parameterId == short.MinValue)
            {
                parameterId = (short)(index + 1);
            }
            ThriftType thriftType         = catalog.GetThriftType(parameterInfo.ParameterType);
            var        parameterInjection = new ThriftParameterInjection(parameterId, parameterName, index, parameterInfo.ParameterType);

            if (parameterRequiredness == Requiredness.Unspecified)
            {
                // There is only one field injection used to build metadata for method parameters, and if a
                // single injection point has UNSPECIFIED requiredness, that resolves to NONE.
                parameterRequiredness = Requiredness.None;
            }
            ThriftFieldMetadata fieldMetadata = new ThriftFieldMetadata(
                parameterId,
                false /* recursiveness */,
                parameterRequiredness,
                new DefaultThriftTypeReference(thriftType),
                parameterName,
                FieldKind.ThriftField,
                new IThriftInjection[] { parameterInjection });

            return(fieldMetadata);
        }
コード例 #8
0
        public IThriftCodec Load(ThriftType type)
        {
            try
            {
                // When we need to load a codec for a type the first time, we push it on the
                // thread-local stack before starting the load, and pop it off afterwards,
                // so that we can detect recursive loads.
                _stack.Value.Push(type);

                switch (type.ProtocolType)
                {
                case ThriftProtocolType.Struct:
                {
                    return(_factory.GenerateThriftTypeCodec(this, type.StructMetadata));
                }

                case ThriftProtocolType.Map:
                {
                    var codecType = typeof(MapThriftCodec <,>).MakeGenericType(type.KeyTypeReference.CSharpType, type.ValueTypeReference.CSharpType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type, GetElementCodec(type.KeyTypeReference), GetElementCodec(type.ValueTypeReference)));
                }

                case ThriftProtocolType.Set:
                {
                    var codecType = typeof(SetThriftCodec <>).MakeGenericType(type.ValueTypeReference.CSharpType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type, GetElementCodec(type.ValueTypeReference)));
                }

                case ThriftProtocolType.List:
                {
                    var codecType = typeof(ListThriftCodec <>).MakeGenericType(type.ValueTypeReference.CSharpType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type, GetElementCodec(type.ValueTypeReference)));
                }

                case ThriftProtocolType.Enum:
                {
                    var codecType = typeof(EnumThriftCodec <>).MakeGenericType(type.EnumMetadata.EnumType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type));
                }

                default:
                    if (type.IsCoerced)
                    {
                        var          codec    = GetCodec(type.UncoercedType);
                        TypeCoercion coercion = this.Catalog.GetDefaultCoercion(type.CSharpType);
                        var          coercionThriftCodecType = typeof(CoercionThriftCodec <>).MakeGenericType(type.UncoercedType.CSharpType);
                        return((IThriftCodec)Activator.CreateInstance(coercionThriftCodecType, codec, coercion));
                    }
                    else
                    {
                        return(GetCodec(type.UncoercedType));
                    }
                    throw new ThriftyException("Unsupported Thrift type " + type);
                }
            }
            finally
            {
                if (_stack.Value.Count > 0)
                {
                    ThriftType top = _stack.Value.Pop();
                    if (!type.Equals(top))
                    {
                        throw new ThriftyException(
                                  $"ThriftCatalog circularity detection stack is corrupt: expected {type}, but got {top}");
                    }
                }
            }
        }
コード例 #9
0
        public void Write <T>(ThriftType type, T value, TProtocol protocol)
        {
            IThriftCodec <T> codec = GetCodec(type) as IThriftCodec <T>;

            codec.Write(value, protocol);
        }
コード例 #10
0
        public T Read <T>(ThriftType type, TProtocol protocol)
        {
            var codec = GetCodec(type) as IThriftCodec <T>;

            return(codec.Read(protocol));
        }
コード例 #11
0
 public IThriftCodec GetCachedCodecIfPresent(ThriftType type)
 {
     return(_typeCodecs.GetOrAdd(type, Load));
 }
コード例 #12
0
ファイル: EnumThriftCodec.cs プロジェクト: ysj123688/Thrifty
 public EnumThriftCodec(ThriftType type)
 {
     this.Type = type;
     _metadata = type.EnumMetadata;
 }