public Field(ServiceDetails svc, MessageDescriptor msg, string fieldName)
                {
                    Descs = fieldName.Split('.').Aggregate((msg, result: ImmutableList <FieldDescriptor> .Empty), (acc, part) =>
                    {
                        // TODO: Check nested fields aren't repeated.
                        var fieldDesc = acc.msg.FindFieldByName(part);
                        if (fieldDesc == null)
                        {
                            throw new InvalidOperationException($"Field '{part}' does not exist in message: {acc.msg.FullName}");
                        }
                        return(fieldDesc.FieldType == FieldType.Message ? fieldDesc.MessageType : null, acc.result.Add(fieldDesc));
                    }, acc => acc.result);
                    var lastDesc = Descs.Last();

                    Typ            = ProtoTyp.Of(lastDesc);
                    IsMap          = lastDesc.IsMap;
                    IsRepeated     = lastDesc.IsRepeated;
                    IsWrapperType  = ProtoTyp.IsWrapperType(lastDesc);
                    IsRequired     = lastDesc.GetExtension(FieldBehaviorExtensions.FieldBehavior).Any(x => x == FieldBehavior.Required);
                    ParameterName  = lastDesc.CSharpFieldName();
                    PropertyName   = lastDesc.CSharpPropertyName();
                    IsDeprecated   = Descs.Any(x => x.IsDeprecated());
                    DocLines       = lastDesc.Declaration.DocLines();
                    FieldResources = svc.Catalog.GetResourceDetailsByField(lastDesc);
                }
예제 #2
0
        //注意fields是共用的,要确保在下次load之后不使用
        public static bool Load(string path, out List <Dictionary <string, string> > l, out List <string> fields, out List <string> descs)
        {
            l = new List <Dictionary <string, string> >();//用反射设置每个字段
            Fields.Clear();
            Descs.Clear();
            fields = Fields;
            descs  = Descs;
            CsvReader r = LoadReader(path);

            if (r == null)
            {
                return(false);
            }
            Dictionary <string, string> d = null;
            bool endOfRow = true;


            //第一行为描述,不解析
            while (r.Read())
            {
                Descs.Add(r.Value);
                if (r.IsEndOfRow)
                {
                    break;
                }
            }

            //第二行为字段名
            while (r.Read())
            {
                Fields.Add(r.Value);
                if (r.IsEndOfRow)
                {
                    break;
                }
            }
            if (Fields.Count == 0)
            {
                Debuger.LogError(string.Format("{0}.csv解析不到列名", path));
                return(false);
            }

            //剩下的行数据
            while (r.Read())
            {
                if (endOfRow)
                {
                    d = new Dictionary <string, string>();
                    l.Add(d);
                }

                endOfRow = r.IsEndOfRow;

                if (r.Col >= Fields.Count || string.IsNullOrEmpty(Fields[r.Col]))
                {
                    continue;
                }

                if (d.ContainsKey(Fields[r.Col]))
                {
                    Debuger.LogError(string.Format("{0}.csv解析出错 字段名重复", path, Fields[r.Col]));
                    continue;
                }
                d[Fields[r.Col]] = r.Value;
            }

            if (r.IsError)
            {
                Debuger.LogError(string.Format("{0}.csv解析出错 行:{1} 列:{2}", path, r.Row, r.Col));
            }

            return(!r.IsError);
        }