What are the available datatypes in C# bond?

2.9k Views Asked by At

I'm wondering what is the best way to represent a table that should contain those fields in C# bond format?

  • string FirstName
  • string LastName
  • string Email
  • bool Registered
  • DateTime DateJoined
  • char Gender
  • List<string> Favorites
  • string City
  • string State
  • unit16 Zip
  • string Country
  • List<string> FrequentPagesURLs

And I want to have something similar to that format

namespace MyProject
{
    struct Key
    {
        0: required string Email;
    }

    struct Value
    {
        0: required string FirstName; 
        1: optional char Gender;
        .
        .
        .
    }
}

I'm not sure what is the best way to represent char, DateTime, and List<string> in C# bond format to use them in creating table in Object store.

2

There are 2 best solutions below

2
Cutler Cox On

According to the official documentation for Bond, there are the following types:

Basic type: bool, uint8, uint16, uint32, uint64, int8, int16, int32, int64, float, double, string, wstring.

Container: blob, list, vector, set, map, nullable.

User-defined type: enum, struct or bonded where T is a struct.

However, the documentation also explains how to generate the DateTime, char, etc. if you are using bond to generate C# code. That is to use the following in your CLI command:

gbc c# --using="DateTime=System.DateTime" date_time.bond

The using parameter is where you put type aliases, such as "char=System.Char;DateTime=System.DateTime".

I don't know if this adequately helps you, please let me know if you need anything else.

Sources:

https://microsoft.github.io/bond/manual/compiler.html

https://microsoft.github.io/bond/manual/bond_cs.html

0
chwarr On

I would model the gender field as an enum, as this is more explicit than a char; the DateTime field as a uint64, but use a type converter to turn this into a DateTime struct in C#; and the List<string> field as a vector<string>:

namespace MyProject;

using DateTime=uint64;

enum Gender
{
    Unspecified;
    ...
}

struct Favorite { ... }
struct FrequentPagesURL { ... }

struct SomeType
{
    ...
    7: DateTime DateJoined;
    8: Gender Gender = Unspecified;
    9: vector<Favorite> Favorites;
    ...
    17: vector<FrequentPagesURL> FrequentPagesURLs;
    ...
}

You may want to consider modeling the DateJoined field as a string/blob and using a type converter to turn it into a DateTimeOffset struct in C#, depending on your needs.