Пример #1
0
        public static bool IsDefaultBase(this Google.Protobuf.Reflection.FieldDescriptor from)
        {
            if (from.IsMessage())
            {
                if (from.MessageType.Name.EndsWith("_nullable"))
                {
                    return(false);
                }
                if (from.MessageType.Name.EndsWith("_nullable_enum"))
                {
                    return(false);
                }
            }
            var doc = JsonDoc.Files[from.File.Name].Messages[from.MessageType.Name];

            if (string.IsNullOrWhiteSpace(doc.BaseClass) ||
                doc.BaseClass.Contains("ConfigObjectVmBase") ||
                doc.BaseClass.Contains("ConfigObjectCommonBase") ||
                doc.BaseClass.Contains("ConfigObjectVmGenSettings")
                )
            {
                return(true);
            }
            return(false);
        }
Пример #2
0
 public static bool IsMap(this Google.Protobuf.Reflection.FieldDescriptor from)
 {
     if (from.IsMap)
     {
         return(true);
     }
     return(false);
 }
Пример #3
0
        public static bool IsCsSimple(this Google.Protobuf.Reflection.FieldDescriptor from)
        {
            switch (from.FieldType)
            {
            case Google.Protobuf.Reflection.FieldType.Bool:
            case Google.Protobuf.Reflection.FieldType.Bytes:
            case Google.Protobuf.Reflection.FieldType.Double:
            case Google.Protobuf.Reflection.FieldType.Enum:
            case Google.Protobuf.Reflection.FieldType.Fixed32:
            case Google.Protobuf.Reflection.FieldType.Fixed64:
            case Google.Protobuf.Reflection.FieldType.Float:
            case Google.Protobuf.Reflection.FieldType.Int32:
            case Google.Protobuf.Reflection.FieldType.Int64:
            case Google.Protobuf.Reflection.FieldType.SFixed32:
            case Google.Protobuf.Reflection.FieldType.SFixed64:
            case Google.Protobuf.Reflection.FieldType.SInt32:
            case Google.Protobuf.Reflection.FieldType.SInt64:
            case Google.Protobuf.Reflection.FieldType.String:
            case Google.Protobuf.Reflection.FieldType.UInt32:
            case Google.Protobuf.Reflection.FieldType.UInt64:
                return(true);

            case Google.Protobuf.Reflection.FieldType.Message:
                switch (from.MessageType.Name)
                {
                case "BoolValue":
                case "DoubleValue":
                case "UInt32Value":
                case "UInt64Value":
                case "FloatValue":
                case "Int32Value":
                case "Int64Value":
                case "StringValue":
                case "bool_nullable":
                case "double_nullable":
                case "uint_nullable":
                case "ulong_nullable":
                case "float_nullable":
                case "int_nullable":
                case "long_nullable":
                case "string_nullable":
                case "Timestamp":
                case "Duration":
                    return(true);

                default:
                    if (from.MessageType.Name.EndsWith("_nullable_enum"))
                    {
                        return(true);
                    }
                    return(false);
                }

            default:
                throw new NotSupportedException();
            }
        }
Пример #4
0
        public static bool IsBytes(this Google.Protobuf.Reflection.FieldDescriptor from)
        {
            switch (from.FieldType)
            {
            case Google.Protobuf.Reflection.FieldType.Bytes:
                return(true);

            default:
                return(false);
            }
        }
Пример #5
0
 public static bool IsNullable(this Google.Protobuf.Reflection.FieldDescriptor from)
 {
     if (from.MessageType.Name.EndsWith("_nullable"))
     {
         return(true);
     }
     if (from.MessageType.Name.EndsWith("_nullable_enum"))
     {
         return(true);
     }
     return(false);
 }
Пример #6
0
        public static bool IsAny(this Google.Protobuf.Reflection.FieldDescriptor from)
        {
            switch (from.FieldType)
            {
            case Google.Protobuf.Reflection.FieldType.Message:
                switch (from.MessageType.FullName)
                {
                case "google.protobuf.Any":
                    return(true);

                default:
                    return(false);
                }

            default:
                return(false);
            }
        }
Пример #7
0
        public static bool IsMessage(this Google.Protobuf.Reflection.FieldDescriptor from)
        {
            switch (from.FieldType)
            {
            case Google.Protobuf.Reflection.FieldType.Message:
                switch (from.MessageType.Name)
                {
                case "BoolValue":
                case "DoubleValue":
                case "UInt32Value":
                case "UInt64Value":
                case "FloatValue":
                case "Int32Value":
                case "Int64Value":
                case "StringValue":
                    return(false);
                }
                return(true);

            default:
                return(false);
            }
        }
Пример #8
0
        internal SingleFieldAccessor(
            [DynamicallyAccessedMembers(GeneratedClrTypeInfo.MessageAccessibility)]
            Type messageType, PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
        {
            if (!property.CanWrite)
            {
                throw new ArgumentException("Not all required properties/methods available");
            }
            setValueDelegate = ReflectionUtil.CreateActionIMessageObject(property.GetSetMethod());

            // Note: this looks worrying in that we access the containing oneof, which isn't valid until cross-linking
            // is complete... but field accessors aren't created until after cross-linking.
            // The oneof itself won't be cross-linked yet, but that's okay: the oneof accessor is created
            // earlier.

            // Message fields always support presence, via null checks.
            if (descriptor.FieldType == FieldType.Message)
            {
                hasDelegate   = message => GetValue(message) != null;
                clearDelegate = message => SetValue(message, null);
            }
            // Oneof fields always support presence, via case checks.
            // Note that clearing the field is a no-op unless that specific field is the current "case".
            else if (descriptor.RealContainingOneof != null)
            {
                var oneofAccessor = descriptor.RealContainingOneof.Accessor;
                hasDelegate   = message => oneofAccessor.GetCaseFieldDescriptor(message) == descriptor;
                clearDelegate = message =>
                {
                    // Clear on a field only affects the oneof itself if the current case is the field we're accessing.
                    if (oneofAccessor.GetCaseFieldDescriptor(message) == descriptor)
                    {
                        oneofAccessor.Clear(message);
                    }
                };
            }
            // Primitive fields always support presence in proto2, and support presence in proto3 for optional fields.
            else if (descriptor.File.Syntax == Syntax.Proto2 || descriptor.Proto.Proto3Optional)
            {
                MethodInfo hasMethod = messageType.GetRuntimeProperty("Has" + property.Name).GetMethod;
                if (hasMethod == null)
                {
                    throw new ArgumentException("Not all required properties/methods are available");
                }
                hasDelegate = ReflectionUtil.CreateFuncIMessageBool(hasMethod);
                MethodInfo clearMethod = messageType.GetRuntimeMethod("Clear" + property.Name, ReflectionUtil.EmptyTypes);
                if (clearMethod == null)
                {
                    throw new ArgumentException("Not all required properties/methods are available");
                }
                clearDelegate = ReflectionUtil.CreateActionIMessage(clearMethod);
            }
            // What's left?
            // Primitive proto3 fields without the optional keyword, which aren't in oneofs.
            else
            {
                hasDelegate = message => { throw new InvalidOperationException("Presence is not implemented for this field"); };

                // While presence isn't supported, clearing still is; it's just setting to a default value.
                object defaultValue = GetDefaultValue(descriptor);
                clearDelegate = message => SetValue(message, defaultValue);
            }
        }
Пример #9
0
 internal FieldAccessorBase(PropertyInfo property, FieldDescriptor descriptor)
 {
     this.descriptor  = descriptor;
     getValueDelegate = ReflectionUtil.CreateFuncIMessageObject(property.GetGetMethod());
 }
 internal MapFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
 {
 }
Пример #11
0
        internal void CrossLink()
        {
            List <FieldDescriptor>        list       = new List <FieldDescriptor>();
            IEnumerator <FieldDescriptor> enumerator = this.ContainingType.Fields.InDeclarationOrder().GetEnumerator();

            try
            {
                while (true)
                {
IL_A4:
                    uint arg_78_0 = OneofDescriptor.smethod_0(enumerator) ? 645945303u : 1779362706u;
                    while (true)
                    {
                        uint num;
                        switch ((num = (arg_78_0 ^ 1929289758u)) % 5u)
                        {
                        case 0u:
                        {
                            FieldDescriptor current;
                            list.Add(current);
                            arg_78_0 = (num * 2160487366u ^ 3013554972u);
                            continue;
                        }

                        case 1u:
                            goto IL_A4;

                        case 2u:
                            arg_78_0 = 645945303u;
                            continue;

                        case 3u:
                        {
                            FieldDescriptor current = enumerator.Current;
                            arg_78_0 = ((current.ContainingOneof != this) ? 607704728u : 324701640u);
                            continue;
                        }
                        }
                        goto Block_4;
                    }
                }
                Block_4 :;
            }
            finally
            {
                if (enumerator != null)
                {
                    while (true)
                    {
                        IL_E8 :
                        uint arg_D0_0 = 327623526u;
                        while (true)
                        {
                            uint num;
                            switch ((num = (arg_D0_0 ^ 1929289758u)) % 3u)
                            {
                            case 1u:
                                OneofDescriptor.smethod_1(enumerator);
                                arg_D0_0 = (num * 1913406752u ^ 3932505543u);
                                continue;

                            case 2u:
                                goto IL_E8;
                            }
                            goto Block_8;
                        }
                    }
                    Block_8 :;
                }
            }
            this.fields = new ReadOnlyCollection <FieldDescriptor>(list);
        }
Пример #12
0
        public static string ConvertToVm(this Google.Protobuf.Reflection.FieldDescriptor field, string from_proto)
        {
            StringBuilder sb = new StringBuilder();

            // https://developers.google.com/protocol-buffers/docs/reference/google.protobuf
            if (field.FieldType == Google.Protobuf.Reflection.FieldType.Message)
            {
                if (field.IsNullable())
                {
                    sb.Append(from_proto);
                    sb.Append(".");
                    sb.Append(field.Name.ToNameCs());
                    sb.Append(".HasValue ? (");
                    sb.Append(field.ToTypeCs());
                    sb.Append(")");
                    sb.Append(from_proto);
                    sb.Append(".");
                    sb.Append(field.Name.ToNameCs());
                    switch (field.MessageType.Name)
                    {
                    //case "Any":
                    //    return "Google.Protobuf.WellKnownTypes.Any";
                    case "time_span_nullable":
                        sb.Append(".Value.ToTimeSpan() : (");
                        break;

                    case "date_time_nullable":
                        sb.Append(".Value.ToDateTime() : (");
                        break;

                    default:
                        sb.Append(".Value : (");
                        //sb.Append("/*");
                        //sb.Append(field.MessageType.Name);
                        //sb.Append("*/");
                        break;
                    }
                    sb.Append(field.ToTypeCs());
                    sb.Append(")null");
                }
                else
                {
                    switch (field.MessageType.Name)
                    {
                    case "Any":
                        sb.Append("Google.Protobuf.WellKnownTypes.Any");
                        break;

                    default:
                        sb.Append(from_proto);
                        sb.Append(".");
                        sb.Append(field.Name.ToNameCs());
                        break;
                    }
                }
            }
            else if (field.FieldType == Google.Protobuf.Reflection.FieldType.Enum)
            {
                sb.Append("(");
                sb.Append(field.EnumType.Name.ToNameCs());
                sb.Append(")");
                sb.Append(from_proto);
                sb.Append(".");
                sb.Append(field.Name.ToNameCs());
            }
            else
            {
                sb.Append(from_proto);
                sb.Append(".");
                sb.Append(field.Name.ToNameCs());
            }
            return(sb.ToString());
        }
Пример #13
0
        public static string ToTypeCs(this Google.Protobuf.Reflection.FieldDescriptor from)
        {
            // https://developers.google.com/protocol-buffers/docs/reference/google.protobuf
            if (from.FieldType == Google.Protobuf.Reflection.FieldType.Message)
            {
                switch (from.MessageType.Name)
                {
                case "bool_nullable":
                case "BoolValue":
                    return("bool?");

                case "double_nullable":
                case "DoubleValue":
                    return("double?");

                case "uint_nullable":
                case "UInt32Value":
                    return("uint?");

                case "ulong_nullable":
                case "UInt64Value":
                    return("ulong?");

                case "float_nullable":
                case "FloatValue":
                    return("float?");

                case "int_nullable":
                case "Int32Value":
                    return("int?");

                case "long_nullable":
                case "Int64Value":
                    return("long?");

                case "string_nullable":
                case "StringValue":
                    return("string");

                case "Any":
                    return("Google.Protobuf.WellKnownTypes.Any");

                case "Duration":
                    return("Google.Protobuf.WellKnownTypes.Duration");

                case "Timestamp":
                    return("Google.Protobuf.WellKnownTypes.Timestamp");

                default:
                    if (from.MessageType.Name.EndsWith("_nullable"))
                    {
                        return(from.MessageType.Name.Replace("_nullable", "").ToNameCs() + "?");
                    }
                    if (from.MessageType.Name.EndsWith("_nullable_enum"))
                    {
                        return(from.MessageType.Name.Replace("_nullable_enum", "").ToNameCs() + "?");
                    }
                    return(from.MessageType.Name.ToNameCs());
                }
            }
            else if (from.FieldType == Google.Protobuf.Reflection.FieldType.Enum)
            {
                return(from.EnumType.Name.ToNameCs());
            }
            return(FieldTypeSimpleToTypeCs(from.FieldType));
        }
Пример #14
0
 private static object GetDefaultValue(FieldDescriptor descriptor) =>
 descriptor.FieldType switch
 {
Пример #15
0
 internal string ctor > b__4_5(FieldDescriptor field)
 {
     return(JsonFormatter.ToCamelCase(field.Name));
 }
Пример #16
0
 internal int ctor > b__4_4(FieldDescriptor field)
 {
     return(field.FieldNumber);
 }
Пример #17
0
 public RepeatedFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
 {
 }