-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] An ORM Implementation based on Protocol Buffer 3 definition #1
Comments
This is the list of type conversion from proto 3 scaler types to go types. From these go types, we can find sql types. We can follow this. |
Question: If I want to create a table using proto message where one column type can be Shall we use sql types as proto field types? Like this one?
From this we can generate a go type with tag of sql types.
Now we can use this generated struct for both. Am I missing something here? |
@aerokite thanks for asking this questions. This implementation also have some potential. But my initial thinking was not to maintain two different version of struct if you are going to use API layer with storage layer. message Test {
option (ormpb.table).name = "tests";
string label = 1 [(ormpb.column).type = "blob"];
int64 type = 2 [(ormpb.column).type = "bigint"];
} That would eventually be generating something like the following - func (t *Test) TableName() string {
return "tests"
}
func (t *Test) Tag(field, name string) string {
m := map[string]map[string]string{
"Label": map[string]string{
"type": "blob",
},
"Type": map[string]string{
"type": "bigint",
},
}
return m[field][name]
} In That way user should have more flexibility to user to even have same or even different layer upon API and storage. But i am still open for discussion. cc: @sunkuet02 |
I did say so. But instead of using |
Your idea is better I think. because, we can add more information in proto field. |
Found a library protoc-go-inject-tag it may help to add custom tag. Here is how it works,
command type Hello struct {
// @inject_tag: ormpb:"primary_key"
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
} and then if execute command type Hello struct {
// @inject_tag: ormpb:"primary_key"
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty" ormpb:"primary_key"`
} Hence we can work on these tags to do other orm related work. |
We need more methods that protoc will not generate. So we need our modified generator. We can take help from these repos. Also check this one : https://github.com/dsymonds/gotoc This library parses proto file and generate codes. Didn't take a look in code base. |
Yeah, agreed with @aerokite, still we need to generate some things as of - @s4kibs4mi thanks for the library. This looks interesting. will look more into that. |
I am not experienced with protobuf. I think protoc doesn't generate go code. protoc-gen-go does. protoc only parses .proto files and writes a JSON data which protoc-gen-go uses to generate code. In our case, we need custom parser to parse tag like this Lets see, if it is possible to use protoc for custom tag. |
yes possible. protoc can parse the custom tag. But if you need to generate something you need your custom generator. thats why we are going to write protoc-gen-orm. also take a look at: https://github.com/uber/prototool |
This library intend to design a ORM for go based on the protobuf definitions. This issue intend to have discussion about design and implementation, that needed to be asked and answered.
Most of the ORM design in go is based on struct tags. But generating from protobuf we needed to implement a way to get the tags from the field mappings, with out tagging the structure.
In short the orm should have two basic part -
Generator
Generates code definition, sql statements from protobuf definition. For reference This could follow the pattern of grpc-gateway, that generates go http proxy for gRPC services.
This also includes mapping between protobuf types and sql types.
Generator can heavily use protobuf Message Option and Field Option to take targeted inputs from user specifically indicating sql behaviours for fields indicating column and message indicating table.
For further flexibility generator can also generate sql statements and/or migrations, based on previous definitions and fields serials.
In future that may also generates helper methods to use with library.
Library
Actual implementations of the ORM that uses generated data to integrate with. Can follow pattern based on gorm or xorm two of most popular go orm libraries.
Comments and Suggestion wanted.
The text was updated successfully, but these errors were encountered: