protected IEnumerable <ParameterInjection> GetParameterInjections(Type structType, ParameterInfo[] parametersList) { List <ParameterInjection> parameters = new List <ParameterInjection>(parametersList.Length); for (int parameterIndex = 0; parameterIndex < parametersList.Length; parameterIndex++) { var p = parametersList[parameterIndex]; ThriftFieldAttribute thriftField = p.GetCustomAttribute <ThriftFieldAttribute>(); /* * 这里以属性上的 ThriftFieldAttribute 优先,否则将双写 ThriftFieldAttribute 上的各种属性,例如 Required, Name,这将非常麻烦。 * 或许,最合理的方案应该是定义一个 ThriftParameterAttribute,只给一个 id 属性 ? * JAVA 字段名和构造函数参数均是小驼峰,不存在此问题,但依然存在 Required 必须一致的问题,也很麻烦, 优先使用字段配置可以缓解这个问题。 */ string extractedName = p.Name; if (thriftField != null) { var field = _fields.FirstOrDefault(f => f.Id == thriftField.Id); thriftField = field?.Annotation ?? thriftField; extractedName = field?.ExtractName() ?? p.Name; } ParameterInjection parameterInjection = new ParameterInjection( structType, parameterIndex, thriftField, extractedName, p.ParameterType ); parameters.Add(parameterInjection); } return(parameters); }
public FieldExtractor(PropertyInfo field, ThriftFieldAttribute annotation, FieldKind fieldKind) : base(annotation, fieldKind) { Guard.ArgumentNotNull(field, nameof(field)); this.CSharpType = field.PropertyType; this.Field = field; }
internal ParameterInjection(Type thriftStructType, int parameterIndex, ThriftFieldAttribute annotation, String extractedName, Type parameterType) : base(annotation, FieldKind.ThriftField) { Guard.ArgumentNotNull(parameterType, nameof(parameterType)); this.CSharpType = parameterType; this.thriftStructType = thriftStructType; this.ParameterIndex = parameterIndex; this.extractedName = extractedName; if (typeof(void).Equals(parameterType)) { throw new ArgumentException($"pararmeter type not allow void"); } Guard.ArgumentCondition(!String.IsNullOrWhiteSpace(this.Name) || !String.IsNullOrWhiteSpace(extractedName), "Parameter must have an explicit name or an extractedName"); }
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); }
public MethodExtractor(Type thriftStructType, MethodInfo method, ThriftFieldAttribute annotation, FieldKind fieldKind) : base(annotation, fieldKind) { this.CSharpType = thriftStructType; this.Method = method; }
protected Extractor(ThriftFieldAttribute annotation, FieldKind fieldKind) : base(annotation, fieldKind) { }
public FieldInjection(PropertyInfo field, ThriftFieldAttribute annotation, FieldKind fieldKind) : base(annotation, fieldKind) { this.Field = field; this.CSharpType = field.PropertyType; }
protected Injection(ThriftFieldAttribute annotation, FieldKind fieldKind) : base(annotation, fieldKind) { }
private FieldMetadata(ThriftFieldAttribute annotation, FieldKind type, IEnumerable <ThriftIdlAttribute> idlCollection) { this.Annotation = annotation; this.Id = short.MinValue; this._type = type; idlCollection = idlCollection ?? Enumerable.Empty <ThriftIdlAttribute>(); switch (type) { case FieldKind.ThriftField: if (annotation != null) { if (annotation.Id != short.MinValue) { this.Id = annotation.Id; } //isLegacyId = annotation.isLegacyId(); if (!String.IsNullOrWhiteSpace(annotation.Name)) { this.Name = annotation.Name; } this.Requiredness = annotation.Required; Dictionary <String, String> idlAnnotations = new Dictionary <String, String>(); foreach (var idlAnnotation in idlCollection) { idlAnnotations[idlAnnotation.Key] = idlAnnotation.Value; } if (annotation.Recursive != ThriftFieldAttribute.Recursiveness.Unspecified) { switch (annotation.Recursive) { case ThriftFieldAttribute.Recursiveness.True: this.IsRecursiveReference = true; break; case ThriftFieldAttribute.Recursiveness.False: this.IsRecursiveReference = false; break; default: throw new ArgumentException("Unexpected get for isRecursive field"); } } else if (idlAnnotations.ContainsKey(RecursiveReferenceAnnotationName)) { String v = null; idlAnnotations.TryGetValue(RecursiveReferenceAnnotationName, out v); if (v == null || String.IsNullOrWhiteSpace(v)) { v = "false"; } this.IsRecursiveReference = (String.Compare(bool.TrueString, v, true) == 0); } } break; case FieldKind.ThriftUnionId: System.Diagnostics.Contracts.Contract.Assert(annotation == null, "ThriftStruct annotation shouldn't be present for THRIFT_UNION_ID"); this.Id = short.MinValue; //isLegacyId = true; // preserve `negative field ID <=> isLegacyId` this.Name = "_union_id"; break; default: throw new ArgumentException($"Encountered field metadata type {type}."); } }
protected FieldMetadata(ThriftFieldAttribute annotation, FieldKind type) : this(annotation, type, null) { }