示例#1
0
        public void ToJsonSchema()
        {
            var schema = DataTypeUtility.ToTableFieldSchema(typeof(MyClass), pi =>
            {
                if (pi == typeof(MyClass).GetProperty("MyProperty"))
                {
                    return(new TableFieldSchema {
                        Name = "MyProperty", Type = DataType.String.ToIdentifier()
                    });
                }
                return(null);
            });

            schema.ToJsonSchema().Is(@"[
  {
    ""name"": ""Hoge"",
    ""mode"": ""REQUIRED"",
    ""type"": ""INTEGER""
  },
  {
    ""name"": ""Nano"",
    ""mode"": ""NULLABLE"",
    ""type"": ""TIMESTAMP""
  },
  {
    ""name"": ""MyProperty"",
    ""type"": ""STRING""
  },
  {
    ""name"": ""Next"",
    ""mode"": ""NULLABLE"",
    ""type"": ""RECORD"",
    ""fields"": [
      {
        ""name"": ""BBB"",
        ""mode"": ""NULLABLE"",
        ""type"": ""STRING""
      },
      {
        ""name"": ""ZZZ"",
        ""mode"": ""NULLABLE"",
        ""type"": ""FLOAT""
      }
    ]
  },
  {
    ""name"": ""Xs"",
    ""mode"": ""REPEATED"",
    ""type"": ""INTEGER""
  }
]");
        }
示例#2
0
        public void CustomSerialization()
        {
            var schema = DataTypeUtility.ToTableFieldSchema(typeof(MyClass), pi =>
            {
                if (pi == typeof(MyClass).GetProperty("MyProperty"))
                {
                    return(new TableFieldSchema {
                        Name = "MyProperty", Type = DataType.String.ToIdentifier()
                    });
                }
                return(null);
            });

            schema[0].Is(x => x.Name == "Hoge" && x.Type == "INTEGER");
            schema[1].Is(x => x.Name == "Nano" && x.Type == "TIMESTAMP");
            schema[2].Is(x => x.Name == "MyProperty" && x.Type == "STRING");
            schema[3].Is(x => x.Name == "Next" && x.Type == "RECORD");
            schema[4].Is(x => x.Name == "Xs" && x.Type == "INTEGER" && x.Mode == "REPEATED");

            // deserialize

            var value = new Deserializer <MyClass>(new TableSchema {
                Fields = schema
            }, (PropertyInfo pi, object v, out object r) =>
            {
                if (pi == typeof(MyClass).GetProperty("MyProperty"))
                {
                    r = JsonConvert.DeserializeObject <Dictionary <string, object> >((string)v);
                    return(true);
                }
                r = null;
                return(false);
            })
                        .Deserialize(new TableRow()
            {
                F = new[]
                {
                    new TableCell {
                        V = "100"
                    },
                    new TableCell {
                        V = null
                    },
                    new TableCell {
                        V = "{\"a\": 1}"
                    },
                }
            }, true);

            value.Hoge.Is(100);
            value.MyProperty["a"].Is(1L);
        }
示例#3
0
        static void CreateTable()
        {
            var context = Account.GetContext();
            var service = context.BigQueryService;
            var schema  = CreateTwitterStatusSchemas();

            new MetaTable(context.ProjectId, "twitter", "sample")
            .CreateTable(service, schema, "Twitter Streaming Timeline:Sample");

            new MetaTable(context.ProjectId, "twitter", "user")
            .CreateTable(service, schema, "Twitter Streaming Timeline:User");

            new MetaTable(context.ProjectId, "twitter", "error")
            .CreateTable(service, DataTypeUtility.ToTableFieldSchema <ErrorTable>());

            new MetaTable(context.ProjectId, "twitter", "response")
            .CreateTable(service, DataTypeUtility.ToTableFieldSchema <ResponseTable>());
        }
示例#4
0
        static TableFieldSchema[] CreateTwitterStatusSchemas()
        {
            var schemas = DataTypeUtility.ToTableFieldSchema(typeof(Status), (pi) =>
            {
                // avoid circular reference
                if (pi == typeof(Place).GetProperty("ContainedWithin"))
                {
                    return(new TableFieldSchema
                    {
                        Name = "contained_within_id",
                        Type = DataType.String.ToIdentifier(),
                        Mode = "REPEATED"
                    });
                }
                if (pi == typeof(Status).GetProperty("RetweetedStatus"))
                {
                    return(new TableFieldSchema
                    {
                        Name = "retweeted_status_id",
                        Type = DataType.Integer.ToIdentifier(),
                    });
                }
                if (pi == typeof(User).GetProperty("Status"))
                {
                    return(new TableFieldSchema
                    {
                        Name = "status_id",
                        Type = DataType.Integer.ToIdentifier()
                    });
                }

                // avoid complex type

                if (pi.PropertyType == typeof(double[][][])) // Place/BoundingBox/Coordinates
                {
                    return(new TableFieldSchema
                    {
                        Name = pi.GetCustomAttribute <JsonPropertyAttribute>().PropertyName,
                        Type = DataType.String.ToIdentifier()
                    });
                }

                // Type mapping
                if (pi.PropertyType == typeof(Uri))
                {
                    return(new TableFieldSchema
                    {
                        Name = pi.GetCustomAttribute <JsonPropertyAttribute>().PropertyName,
                        Type = DataType.String.ToIdentifier()
                    });
                }
                if (pi.PropertyType == typeof(Dictionary <string, object>))
                {
                    return(new TableFieldSchema
                    {
                        Name = pi.GetCustomAttribute <JsonPropertyAttribute>().PropertyName,
                        Type = DataType.String.ToIdentifier()
                    });
                }

                return(null);
            });

            return(schemas);
        }
示例#5
0
        public void ToTableFieldSchema()
        {
            DataTypeUtility.ToTableFieldSchema <wikipedia>().IsStructuralEqual(new[]
            {
                new TableFieldSchema()
                {
                    Name = "title", Type = "STRING", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "id", Type = "INTEGER", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "language", Type = "STRING", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "wp_namespace", Type = "INTEGER", Mode = "REQUIRED"
                },
                new TableFieldSchema()
                {
                    Name = "is_redirect", Type = "BOOLEAN", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "revision_id", Type = "INTEGER", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "contributor_ip", Type = "STRING", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "contributor_id", Type = "INTEGER", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "contributor_username", Type = "STRING", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "timestamp", Type = "INTEGER", Mode = "REQUIRED"
                },
                new TableFieldSchema()
                {
                    Name = "is_minor", Type = "BOOLEAN", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "is_bot", Type = "BOOLEAN", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "reversion_id", Type = "INTEGER", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "comment", Type = "STRING", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "num_characters", Type = "INTEGER", Mode = "REQUIRED"
                },
            });


            DataTypeUtility.ToTableFieldSchema(new
            {
                Hoge = default(DateTime),
                Huga = default(DateTimeOffset?),
                Tako = default(bool),
                Nano = 1.0,
                K    = default(int?),
                C    = MyEnum.Apple
            }).IsStructuralEqual(new[]
            {
                new TableFieldSchema()
                {
                    Name = "Hoge", Type = "TIMESTAMP", Mode = "REQUIRED"
                },
                new TableFieldSchema()
                {
                    Name = "Huga", Type = "TIMESTAMP", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "Tako", Type = "BOOLEAN", Mode = "REQUIRED"
                },
                new TableFieldSchema()
                {
                    Name = "Nano", Type = "FLOAT", Mode = "REQUIRED"
                },
                new TableFieldSchema()
                {
                    Name = "K", Type = "INTEGER", Mode = "NULLABLE"
                },
                new TableFieldSchema()
                {
                    Name = "C", Type = "INTEGER", Mode = "REQUIRED"
                },
            });
        }