Skip to content
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

書き込みモデルを作成した #12

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,21 @@ import "google/protobuf/timestamp.proto";

message View1 {
option (protobq.materialized_view) = {
is_materialized_view: true
base_table: "example"
enable_refresh: true
};

google.protobuf.Timestamp timestamp = 1 [(protobq.materialized_view_field) = {
source: {
table: "example"
field: "timestamp"
}
origin_path: "timestamp"
is_partitioned: true
}];

string message = 3 [(protobq.materialized_view_field) = {
source: {
table: "example"
field: "message"
}
}];

string message = 2;
}
```

### 2. Apply schema
#### 2. Apply schema

```shell
protobq apply -i example/view1.proto --project-id {YOUR_PROJECT_ID}
protobq apply -i example/view1.proto --project-id {YOUR_PROJECT_ID} --dataset-id {YOUR_DATASET_ID}
```
12 changes: 11 additions & 1 deletion cmd/protobq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ func newCliApp() *cli.App {
Usage: "google cloud project id",
Required: true,
},
&cli.StringFlag{
Name: "dataset-id",
Usage: "bigquery dataset id",
Required: true,
},
},
Action: func(c *cli.Context) error {
err := internal.Apply(context.Background(), c.String("project-id"))
err := internal.Apply(context.Background(), c.String("project-id"), c.String("dataset-id"))
if err != nil {
return err
}
Expand All @@ -59,6 +64,11 @@ func newCliApp() *cli.App {
Usage: "google cloud project id",
Required: true,
},
&cli.StringFlag{
Name: "dataset-id",
Usage: "bigquery dataset id",
Required: true,
},
},
Action: func(c *cli.Context) error {
// TODO: implements me
Expand Down
2 changes: 1 addition & 1 deletion internal/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"cloud.google.com/go/bigquery"
)

func Apply(ctx context.Context, projectID string) error {
func Apply(ctx context.Context, projectID, datasetID string) error {
cli, err := bigquery.NewClient(ctx, projectID)
if err != nil {
return err
Expand Down
44 changes: 44 additions & 0 deletions internal/bigquery_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package internal

type InsertDTOImpl struct {
tableName string
fields []BQField
}

func NewInsertDTOImpl(tableName string, fields []BQField) *InsertDTOImpl {
return &InsertDTOImpl{
tableName: tableName,
fields: fields,
}
}

func (r InsertDTOImpl) TableName() string {
return r.tableName
}

func (r InsertDTOImpl) Value() map[string]any {
res := make(map[string]any)
for _, f := range r.fields {
currentMap := res
for i, key := range f.Path {
if i == len(f.Path)-1 {
currentMap[key] = f.Value
} else {
if _, ok := currentMap[key]; !ok {
currentMap[key] = make(map[string]any)
}
currentMap = currentMap[key].(map[string]any) //nolint:forcetypeassert
}
}
}
return res
}

func (r *InsertDTOImpl) AddField(f BQField) {
r.fields = append(r.fields, f)
}

type BQField struct {
Path []string
Value any
}
59 changes: 59 additions & 0 deletions internal/bigquery_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package internal

import (
"reflect"
"testing"
)

func TestInsertDTOImpl_Value(t *testing.T) {
type fields struct {
tableName string
fields []BQField
}
tests := []struct {
name string
fields fields
want map[string]any
}{
{
fields: fields{
fields: []BQField{
{
Path: []string{"a", "b", "c"},
Value: 1,
},
{
Path: []string{"a", "d"},
Value: 2,
},
{
Path: []string{"a", "e", "f"},
Value: 3,
},
},
},
want: map[string]any{
"a": map[string]any{
"b": map[string]any{
"c": 1,
},
"d": 2,
"e": map[string]any{
"f": 3,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := InsertDTOImpl{
tableName: tt.fields.tableName,
fields: tt.fields.fields,
}
if got := r.Value(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Value() = %v, want %v", got, tt.want)
}
})
}
}
90 changes: 68 additions & 22 deletions internal/codegen.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
package internal

import (
"fmt"
"runtime/debug"
"slices"
"strings"

"github.com/averak/protobq/internal/protobuf/protobq"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)

//goland:noinspection GoSnakeCaseUsage
var (
timeIdents = struct {
Duration protogen.GoIdent
Minute protogen.GoIdent
}{
Duration: protogen.GoImportPath("time").Ident("Duration"),
}
protoIdents = struct {
GetExtension protogen.GoIdent
}{
GetExtension: protogen.GoImportPath("google.golang.org/protobuf/proto").Ident("GetExtension"),
}
internalIdents = struct {
MaterializedView protogen.GoIdent
E_MaterializedView protogen.GoIdent
}{
MaterializedView: protogen.GoImportPath("github.com/averak/protobq/internal/protobuf/protobq").Ident("MaterializedView"),
E_MaterializedView: protogen.GoImportPath("github.com/averak/protobq/internal/protobuf/protobq").Ident("E_MaterializedView"),
Minute: protogen.GoImportPath("time").Ident("Minute"),
}
protobqIdents = struct {
MaterializedView protogen.GoIdent
MaterializedViewOptions protogen.GoIdent
InsertDTO protogen.GoIdent
internal struct {
NewInsertDTOImpl protogen.GoIdent
BQField protogen.GoIdent
}
}{
MaterializedView: protogen.GoImportPath("github.com/averak/protobq").Ident("MaterializedView"),
MaterializedViewOptions: protogen.GoImportPath("github.com/averak/protobq").Ident("MaterializedViewOptions"),
InsertDTO: protogen.GoImportPath("github.com/averak/protobq").Ident("InsertDTO"),
internal: struct {
NewInsertDTOImpl protogen.GoIdent
BQField protogen.GoIdent
}{
NewInsertDTOImpl: protogen.GoImportPath("github.com/averak/protobq/internal").Ident("NewInsertDTOImpl"),
BQField: protogen.GoImportPath("github.com/averak/protobq/internal").Ident("BQField"),
},
}
)

Expand Down Expand Up @@ -69,17 +72,38 @@ func (g CodeGenerator) Gen() error {
if !g.isMaterializedViewSchema(msg) {
continue
}
ext, _ := proto.GetExtension(msg.Desc.Options(), protobq.E_MaterializedView).(*protobq.MaterializedView)

gf.P("var _ ", protobqIdents.MaterializedView, " = (*", msg.GoIdent.GoName, ")(nil)")
gf.P()

gf.P("func (mv *", msg.GoIdent.GoName, ") Name() string {")
gf.P(" return \"", msg.Desc.Name(), "\"")
gf.P("}")
gf.P()

gf.P("func (mv *", msg.GoIdent.GoName, ") Options() ", protobqIdents.MaterializedViewOptions, " {")
gf.P(" ext, _ := ", protoIdents.GetExtension, "(mv.ProtoReflect().Descriptor().Options(), ", internalIdents.E_MaterializedView, ").(*", internalIdents.MaterializedView, ")")
gf.P(" return ", protobqIdents.MaterializedViewOptions, "{")
gf.P(" EnableRefresh: ext.GetEnableRefresh(),")
gf.P(" RefreshInterval: ", timeIdents.Duration, "(ext.GetRefreshIntervalMinutes()) * time.Minute,")
gf.P(" EnableRefresh: ", ext.GetEnableRefresh(), ",")
gf.P(" RefreshInterval: ", ext.GetRefreshIntervalMinutes(), " * ", timeIdents.Minute, ",")
gf.P(" }")
gf.P("}")
gf.P()

gf.P("func (mv *", msg.GoIdent.GoName, ") InsertDTO() ", protobqIdents.InsertDTO, " {")
gf.P(" res := ", protobqIdents.internal.NewInsertDTOImpl, "(\"", ext.GetBaseTable(), "\", nil)")
for _, field := range msg.Fields {
g.generateAddField(gf, field, nil, "res", "mv")
}
gf.P(" return res")
gf.P("}")
gf.P()

//for _, field := range msg.Fields {
//fieldExt, _ := proto.GetExtension(field.Desc.Options(), protobq.E_MaterializedViewField).(*protobq.MaterializedViewField)
//if fieldExt != nil {
// gf.P(" res[\"", field.Desc.Name(), "\"] = mv.", field.GoName)
//}
}
}
return nil
Expand All @@ -100,9 +124,31 @@ func (g CodeGenerator) isMaterializedViewSchema(msg *protogen.Message) bool {
return false
}

ext, ok := proto.GetExtension(opts, protobq.E_MaterializedView).(*protobq.MaterializedView)
if !ok {
return false
ext, _ := proto.GetExtension(opts, protobq.E_MaterializedView).(*protobq.MaterializedView)
return ext != nil
}

func (g CodeGenerator) generateAddField(gf *protogen.GeneratedFile, field *protogen.Field, parentPaths []string, result string, receiver string) {
ext, _ := proto.GetExtension(field.Desc.Options(), protobq.E_MaterializedViewField).(*protobq.MaterializedViewField)
if ext == nil {
ext = &protobq.MaterializedViewField{}
}

paths := parentPaths
if len(ext.GetOriginPath()) > 0 {
paths = append(paths, ext.GetOriginPath()...)
} else {
paths = append(paths, string(field.Desc.Name()))
}

blacklist := []string{
"google.protobuf.Timestamp",
}
if field.Message != nil && !slices.Contains(blacklist, string(field.Message.Desc.FullName())) {
for _, nestedField := range field.Message.Fields {
g.generateAddField(gf, nestedField, paths, result, receiver+".Get"+field.GoName+"()")
}
} else {
gf.P(result, ".AddField(", protobqIdents.internal.BQField, "{[]string{", fmt.Sprintf(`"%s"`, strings.Join(paths, `", "`)), "}, ", receiver, ".Get", field.GoName, "()})")
}
return ext.GetIsMaterializedView()
}
Loading
Loading