From c9d438f7068394aaac34cf5a8ade58edc9fb9016 Mon Sep 17 00:00:00 2001 From: yukatayu Date: Sun, 23 Aug 2020 16:10:09 +0900 Subject: [PATCH 1/2] fix: 'var... type' -> 'var ...type' --- templates/gen.go.tmpl | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/templates/gen.go.tmpl b/templates/gen.go.tmpl index d7eb166..b8d6440 100644 --- a/templates/gen.go.tmpl +++ b/templates/gen.go.tmpl @@ -25,29 +25,29 @@ import ( // {{ .RepositoryInterfaceName }} Repository of {{ .StructName }} type {{ .RepositoryInterfaceName }} interface { // Single - Get(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... GetOption) (*{{ .StructName }}, error) + Get(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...GetOption) (*{{ .StructName }}, error) Insert(ctx context.Context, subject *{{ .StructName }}) error Update(ctx context.Context, subject *{{ .StructName }}) error ForceUpdate(ctx context.Context, subject *{{ .StructName }}) error - Delete(ctx context.Context, subject *{{ .StructName }}, options... DeleteOption) error - DeleteBy{{ FuncNameByValue }}(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... DeleteOption) error + Delete(ctx context.Context, subject *{{ .StructName }}, options ...DeleteOption) error + DeleteBy{{ FuncNameByValue }}(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...DeleteOption) error // Multiple - GetMulti(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options... GetOption) ([]*{{ .StructName }}, error) + GetMulti(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options ...GetOption) ([]*{{ .StructName }}, error) InsertMulti(ctx context.Context, subjects []*{{ .StructName }}) error UpdateMulti(ctx context.Context, subjects []*{{ .StructName }}) error - DeleteMulti(ctx context.Context, subjects []*{{ .StructName }}, options... DeleteOption) error - DeleteMultiBy{{ PluralForm FuncNameByValue }}(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options... DeleteOption) error + DeleteMulti(ctx context.Context, subjects []*{{ .StructName }}, options ...DeleteOption) error + DeleteMultiBy{{ PluralForm FuncNameByValue }}(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options ...DeleteOption) error // Query List(name string, value interface{}) *dynamo.Query Scan() *dynamo.Scan // Transaction RunInTransaction(ctx context.Context, fn func(tx *dynamo.WriteTx) error) error - GetWithTx(tx *dynamo.GetTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... GetOption) (*{{ .StructName }}, error) + GetWithTx(tx *dynamo.GetTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...GetOption) (*{{ .StructName }}, error) InsertWithTx(ctx context.Context, tx *dynamo.WriteTx, subject *{{ .StructName }}) error UpdateWithTx(ctx context.Context, tx *dynamo.WriteTx, subject *{{ .StructName }}) error ForceUpdateWithTx(ctx context.Context, tx *dynamo.WriteTx, subject *{{ .StructName }}) error - DeleteWithTx(ctx context.Context, tx *dynamo.WriteTx, subject *{{ .StructName }}, options... DeleteOption) error - DeleteBy{{ FuncNameByValue }}WithTx(ctx context.Context, tx *dynamo.WriteTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... DeleteOption) error + DeleteWithTx(ctx context.Context, tx *dynamo.WriteTx, subject *{{ .StructName }}, options ...DeleteOption) error + DeleteBy{{ FuncNameByValue }}WithTx(ctx context.Context, tx *dynamo.WriteTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...DeleteOption) error // misc GetTable() dynamo.Table GetTableName() string @@ -58,7 +58,7 @@ type {{ .RepositoryInterfaceName }} interface { type {{ $middlewareName }} interface { BeforeInsert(ctx context.Context, subject *{{ .StructName }}) (bool, error) BeforeUpdate(ctx context.Context, subject *{{ .StructName }}) (bool, error) - BeforeDelete(ctx context.Context, subject *{{ .StructName }}, options... DeleteOption) (bool, error) + BeforeDelete(ctx context.Context, subject *{{ .StructName }}, options ...DeleteOption) (bool, error) } type {{ .RepositoryStructName }} struct { @@ -96,7 +96,7 @@ func Create{{ .StructName }}DepsTable(ctx context.Context, db *dynamo.DB) error } {{ end }} // New{{ .RepositoryInterfaceName }} constructor -func New{{ .RepositoryInterfaceName }}(db *dynamo.DB, middleware... {{ $middlewareName }}) {{ .RepositoryInterfaceName }} { +func New{{ .RepositoryInterfaceName }}(db *dynamo.DB, middleware ...{{ $middlewareName }}) {{ .RepositoryInterfaceName }} { return &{{ .RepositoryStructName }}{ db: db, table: db.Table("{{ .TableName }}"), @@ -166,7 +166,7 @@ func (repo *lockRepository) validateUnique(ctx context.Context, subject *{{ .Str } {{ end }} // Get 処理中の {{ .StructName }} の取得処理一切の責任を持ち、これを行う -func (repo *{{ .RepositoryStructName }}) Get(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... GetOption) (*{{ .StructName }}, error) { +func (repo *{{ .RepositoryStructName }}) Get(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...GetOption) (*{{ .StructName }}, error) { subject := new({{ .StructName }}) q := repo.table.Get("{{ .HashKeyFieldTagName }}", {{ .HashKeyValueName }}){{ if ne .RangeKeyFieldName "" }}.Range("{{ .RangeKeyFieldTagName }}", dynamo.Equal, {{ .RangeKeyValueName }}){{ end }} @@ -184,7 +184,7 @@ func (repo *{{ .RepositoryStructName }}) Get(ctx context.Context, {{ .HashKeyVal } // GetWithTx トランザクション中の {{ .StructName }} の取得処理を行う -func (repo *{{ .RepositoryStructName }}) GetWithTx(tx *dynamo.GetTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... GetOption) (*{{ .StructName }}, error) { +func (repo *{{ .RepositoryStructName }}) GetWithTx(tx *dynamo.GetTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...GetOption) (*{{ .StructName }}, error) { subject := new({{ .StructName }}) query := repo.table.Get("{{ .HashKeyFieldTagName }}", {{ .HashKeyValueName }}){{ if ne .RangeKeyFieldName "" }}.Range("{{ .RangeKeyFieldTagName }}", dynamo.Equal, {{ .RangeKeyValueName }}){{ end }} @@ -473,7 +473,7 @@ func (repo *{{ .RepositoryStructName }}) ForceUpdateWithTx(ctx context.Context, } // Delete 処理中の {{ .StructName }} の削除処理一切の責任を持ち、これを行う -func (repo *{{ .RepositoryStructName }}) Delete(ctx context.Context, subject *{{ .StructName }}, options... DeleteOption) error { +func (repo *{{ .RepositoryStructName }}) Delete(ctx context.Context, subject *{{ .StructName }}, options ...DeleteOption) error { for _, m := range repo.middleware { c, err := m.BeforeDelete(ctx, subject, options...) if err != nil { @@ -503,7 +503,7 @@ func (repo *{{ .RepositoryStructName }}) Delete(ctx context.Context, subject *{{ } // DeleteBy{{ .HashKeyFieldName }} 処理中の {{ .StructName }} の{{ .HashKeyFieldName }}から削除処理一切の責任を持ち、これを行う -func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... DeleteOption) error { +func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...DeleteOption) error { subject, err := repo.Get(ctx, {{ .HashKeyValueName }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }}{{ end }}) if err != nil { return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}.Get error: %w", err) @@ -537,7 +537,7 @@ func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}(ctx conte } // DeleteWithTx トランザクション中の {{ .StructName }} の削除処理を行う -func (repo *{{ .RepositoryStructName }}) DeleteWithTx(ctx context.Context, tx *dynamo.WriteTx, subject *{{ .StructName }}, options... DeleteOption) error { +func (repo *{{ .RepositoryStructName }}) DeleteWithTx(ctx context.Context, tx *dynamo.WriteTx, subject *{{ .StructName }}, options ...DeleteOption) error { for _, m := range repo.middleware { c, err := m.BeforeDelete(ctx, subject, options...) if err != nil { @@ -569,7 +569,7 @@ func (repo *{{ .RepositoryStructName }}) DeleteWithTx(ctx context.Context, tx *d } // DeleteBy{{ .HashKeyFieldName }}WithTx トランザクション中の {{ .StructName }} の{{ .HashKeyFieldName }}から削除処理を行う -func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}WithTx(ctx context.Context, tx *dynamo.WriteTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options... DeleteOption) error { +func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}WithTx(ctx context.Context, tx *dynamo.WriteTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...DeleteOption) error { subject, err := repo.Get(ctx, {{ .HashKeyValueName }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }}{{ end }}) if err != nil { return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}WithTx.Get error: %w", err) @@ -606,7 +606,7 @@ func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}WithTx(ctx } // GetMulti 処理中の {{ .StructName }} の一括取得処理一切の責任を持ち、これを行う -func (repo *{{ .RepositoryStructName }}) GetMulti(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options... GetOption) (subjects []*{{ .StructName }}, err error) { +func (repo *{{ .RepositoryStructName }}) GetMulti(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options ...GetOption) (subjects []*{{ .StructName }}, err error) { for {{ RangeKeyForTerms }} { subject, err := repo.Get(ctx, {{ if ne .RangeKeyFieldName "" }}key, value{{ else }}{{ .HashKeyFieldTagName }}{{ end }}, options...) if err != nil { @@ -641,7 +641,7 @@ func (repo *{{ .RepositoryStructName }}) UpdateMulti(ctx context.Context, subjec } // DeleteMulti 処理中の {{ .StructName }} の一括削除処理一切の責任を持ち、これを行う -func (repo *{{ .RepositoryStructName }}) DeleteMulti(ctx context.Context, subjects []*{{ .StructName }}, options... DeleteOption) error { +func (repo *{{ .RepositoryStructName }}) DeleteMulti(ctx context.Context, subjects []*{{ .StructName }}, options ...DeleteOption) error { tx := repo.db.WriteTx() for _, subject := range subjects { err := repo.DeleteWithTx(ctx, tx, subject, options...) @@ -653,10 +653,10 @@ func (repo *{{ .RepositoryStructName }}) DeleteMulti(ctx context.Context, subjec } // DeleteMultiBy{{ PluralForm FuncNameByValue }} 処理中の {{ .StructName }} の{{ .HashKeyFieldName }}群を元に一括削除処理一切の責任を持ち、これを行う -func (repo *{{ .RepositoryStructName }}) DeleteMultiBy{{ PluralForm FuncNameByValue }}(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options... DeleteOption) error { +func (repo *{{ .RepositoryStructName }}) DeleteMultiBy{{ PluralForm FuncNameByValue }}(ctx context.Context, {{ RangeKeyArgCheckPairs }}, options ...DeleteOption) error { tx := repo.db.WriteTx() for {{ RangeKeyForTerms }} { - err := repo.DeleteBy{{ FuncNameByValue }}WithTx(ctx , tx, {{ if ne .RangeKeyFieldName "" }}key, value{{ else }}{{ .HashKeyFieldTagName }}{{ end }}, options...) + err := repo.DeleteBy{{ FuncNameByValue }}WithTx(ctx, tx, {{ if ne .RangeKeyFieldName "" }}key, value{{ else }}{{ .HashKeyFieldTagName }}{{ end }}, options...) if err != nil { return xerrors.Errorf("DeleteMultiBy{{ PluralForm FuncNameByValue }}.DeleteBy{{ FuncNameByValue }}WithTx error: %w", err) } From e8a6c45b286c6141a599bbcc0a24d211880eace7 Mon Sep 17 00:00:00 2001 From: yukatayu Date: Sun, 23 Aug 2020 16:12:00 +0900 Subject: [PATCH 2/2] fix: replace indent 4-spaces with tab --- templates/gen.go.tmpl | 128 +++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/templates/gen.go.tmpl b/templates/gen.go.tmpl index b8d6440..0fd3e4f 100644 --- a/templates/gen.go.tmpl +++ b/templates/gen.go.tmpl @@ -74,22 +74,22 @@ type {{ $uniqueField.StructName }} struct { {{ end }} // Create{{ .StructName }}DepsTable Create a dependency table for {{ .TableName }} func Create{{ .StructName }}DepsTable(ctx context.Context, db *dynamo.DB) error { - structs := map[string]interface{}{ + structs := map[string]interface{}{ {{- range $uniqueField := .UniqueFields }} - "{{ $uniqueField.StructName }}": new({{ $uniqueField.StructName }}), + "{{ $uniqueField.StructName }}": new({{ $uniqueField.StructName }}), {{- end }} - } + } - for name, s := range structs { - _, err := db.Table(name).Describe().RunWithContext(ctx) - if err != nil && xerrors.Is(err, dynamo.ErrNotFound) { - return err - } + for name, s := range structs { + _, err := db.Table(name).Describe().RunWithContext(ctx) + if err != nil && xerrors.Is(err, dynamo.ErrNotFound) { + return err + } - createTable := db.CreateTable(name, s) - if err := createTable.RunWithContext(ctx); err != nil { - return xerrors.Errorf("failed to Create{{ .StructName }}DepsTable by {{ .RepositoryInterfaceName }} err: %w", err) - } + createTable := db.CreateTable(name, s) + if err := createTable.RunWithContext(ctx); err != nil { + return xerrors.Errorf("failed to Create{{ .StructName }}DepsTable by {{ .RepositoryInterfaceName }} err: %w", err) + } } return nil @@ -152,17 +152,17 @@ func (repo *{{ .RepositoryStructName }}) Scan() *dynamo.Scan { // validateUnique unique check func (repo *lockRepository) validateUnique(ctx context.Context, subject *{{ .StructName }}) error { {{- range $uniqueField := .UniqueFields }} - var {{ $uniqueField.SubjectName }} *{{ $uniqueField.StructName }} = nil - {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") - if err := {{ printf "%sTable" $uniqueField.VarName }}.Get("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}).OneWithContext(ctx, {{ $uniqueField.SubjectName }}); - err != nil && !xerrors.Is(err, dynamo.ErrNotFound) { - return xerrors.Errorf("validateUnique.Get error: %w", err) - } - if {{ $uniqueField.SubjectName }} != nil { - return xerrors.New("{{ $uniqueField.Name }} has to be unique, but it already exists.") - } + var {{ $uniqueField.SubjectName }} *{{ $uniqueField.StructName }} = nil + {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") + if err := {{ printf "%sTable" $uniqueField.VarName }}.Get("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}).OneWithContext(ctx, {{ $uniqueField.SubjectName }}); + err != nil && !xerrors.Is(err, dynamo.ErrNotFound) { + return xerrors.Errorf("validateUnique.Get error: %w", err) + } + if {{ $uniqueField.SubjectName }} != nil { + return xerrors.New("{{ $uniqueField.Name }} has to be unique, but it already exists.") + } {{ end }} - return nil + return nil } {{ end }} // Get 処理中の {{ .StructName }} の取得処理一切の責任を持ち、これを行う @@ -259,13 +259,13 @@ func (repo *{{ .RepositoryStructName }}) Insert(ctx context.Context, subject *{{ } {{- end }} {{- if $uniqueField := .UniqueFields }} - if err := repo.validateUnique(ctx, subject); err != nil { - return xerrors.Errorf("Insert.validateUnique error: %w", err) - } + if err := repo.validateUnique(ctx, subject); err != nil { + return xerrors.Errorf("Insert.validateUnique error: %w", err) + } {{- end }} {{- range $uniqueField := .UniqueFields }} - {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") - {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) + {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") + {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) {{ $uniqueField.SubjectName }}.{{ $uniqueField.Name }} = subject.{{ $uniqueField.Name }} if err := {{ printf "%sTable" $uniqueField.VarName }}.Put({{ $uniqueField.SubjectName }}).RunWithContext(ctx); err != nil { return xerrors.Errorf("Insert.{{ printf "%sTable" $uniqueField.VarName }}.Put error: %w", err) @@ -292,13 +292,13 @@ func (repo *{{ .RepositoryStructName }}) InsertWithTx(ctx context.Context, tx *d } {{- end }} {{- if $uniqueField := .UniqueFields }} - if err := repo.validateUnique(ctx, subject); err != nil { - return xerrors.Errorf("Insert.validateUnique error: %w", err) - } + if err := repo.validateUnique(ctx, subject); err != nil { + return xerrors.Errorf("Insert.validateUnique error: %w", err) + } {{- end }} {{- range $uniqueField := .UniqueFields }} - {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") - {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) + {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") + {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) {{ $uniqueField.SubjectName }}.{{ $uniqueField.Name }} = subject.{{ $uniqueField.Name }} if err := {{ printf "%sTable" $uniqueField.VarName }}.Put({{ $uniqueField.SubjectName }}).RunWithContext(ctx); err != nil { return xerrors.Errorf("InsertWithTx.{{ printf "%sTable" $uniqueField.VarName }}.Put error: %w", err) @@ -315,12 +315,12 @@ func (repo *{{ .RepositoryStructName }}) InsertWithTx(ctx context.Context, tx *d func (repo *{{ .RepositoryStructName }}) updateQuery(ctx context.Context, old, subject *{{ .StructName }}) (*dynamo.Update, error) { {{- if .MetaFields }} if subject.{{ GetMetaKeyWithPath .MetaFields "DeletedAt" }} != nil { - return nil, xerrors.Errorf("Updating 'DeletedAt' in Update is prohibited.: %+v", subject) + return nil, xerrors.Errorf("Updating 'DeletedAt' in Update is prohibited.: %+v", subject) } if old.{{ GetMetaKeyWithPath .MetaFields "Version" }} > subject.{{ GetMetaKeyWithPath .MetaFields "Version" }} { - return nil, xerrors.Errorf("The data in the database is newer: (db {{ GetMetaKeyWithPath .MetaFields "Version" }}: %d, target {{ GetMetaKeyWithPath .MetaFields "Version" }}: %d) %+v", - old.{{ GetMetaKeyWithPath .MetaFields "Version" }}, subject.{{ GetMetaKeyWithPath .MetaFields "Version" }}, subject) + return nil, xerrors.Errorf("The data in the database is newer: (db {{ GetMetaKeyWithPath .MetaFields "Version" }}: %d, target {{ GetMetaKeyWithPath .MetaFields "Version" }}: %d) %+v", + old.{{ GetMetaKeyWithPath .MetaFields "Version" }}, subject.{{ GetMetaKeyWithPath .MetaFields "Version" }}, subject) } repo.setMeta(subject, false) {{ end }} @@ -366,16 +366,16 @@ func (repo *{{ .RepositoryStructName }}) Update(ctx context.Context, subject *{{ return xerrors.Errorf("error in Get method: %w", err) } {{- range $uniqueField := .UniqueFields }} - if subject.{{ $uniqueField.Name }} != old.{{ $uniqueField.Name }} { - {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") - if err := {{ printf "%sTable" $uniqueField.VarName }}.Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}).RunWithContext(ctx); err != nil { - return xerrors.Errorf("Update.{{ printf "%sTable" $uniqueField.VarName }}.Delete error: %w", err) - } - {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) - {{ $uniqueField.SubjectName }}.{{ $uniqueField.Name }} = subject.{{ $uniqueField.Name }} - if err := {{ printf "%sTable" $uniqueField.VarName }}.Put({{ $uniqueField.SubjectName }}).RunWithContext(ctx); err != nil { - return xerrors.Errorf("Update.{{ printf "%sTable" $uniqueField.VarName }}.Put error: %w", err) - } + if subject.{{ $uniqueField.Name }} != old.{{ $uniqueField.Name }} { + {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") + if err := {{ printf "%sTable" $uniqueField.VarName }}.Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}).RunWithContext(ctx); err != nil { + return xerrors.Errorf("Update.{{ printf "%sTable" $uniqueField.VarName }}.Delete error: %w", err) + } + {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) + {{ $uniqueField.SubjectName }}.{{ $uniqueField.Name }} = subject.{{ $uniqueField.Name }} + if err := {{ printf "%sTable" $uniqueField.VarName }}.Put({{ $uniqueField.SubjectName }}).RunWithContext(ctx); err != nil { + return xerrors.Errorf("Update.{{ printf "%sTable" $uniqueField.VarName }}.Put error: %w", err) + } } {{ end }} query, err := repo.updateQuery(ctx, old, subject) @@ -397,14 +397,14 @@ func (repo *{{ .RepositoryStructName }}) UpdateWithTx(ctx context.Context, tx *d return xerrors.Errorf("error in GetWithTx method: %w", err) } {{- range $uniqueField := .UniqueFields }} - if subject.{{ $uniqueField.Name }} != old.{{ $uniqueField.Name }} { - {{ printf "%s%sDeleteRequest" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Delete("{{ $uniqueField.Name }}", old.{{ $uniqueField.Name }}) - tx.Delete({{ printf "%s%sDeleteRequest" $tableName $uniqueField.Name }}) + if subject.{{ $uniqueField.Name }} != old.{{ $uniqueField.Name }} { + {{ printf "%s%sDeleteRequest" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Delete("{{ $uniqueField.Name }}", old.{{ $uniqueField.Name }}) + tx.Delete({{ printf "%s%sDeleteRequest" $tableName $uniqueField.Name }}) - {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) - {{ $uniqueField.SubjectName }}.{{ $uniqueField.Name }} = subject.{{ $uniqueField.Name }} - {{ printf "%s%sPutRequest" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Put({{ $uniqueField.SubjectName }}) - tx.Put({{ printf "%s%sPutRequest" $tableName $uniqueField.Name }}) + {{ $uniqueField.SubjectName }} := new({{ $uniqueField.StructName }}) + {{ $uniqueField.SubjectName }}.{{ $uniqueField.Name }} = subject.{{ $uniqueField.Name }} + {{ printf "%s%sPutRequest" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Put({{ $uniqueField.SubjectName }}) + tx.Put({{ printf "%s%sPutRequest" $tableName $uniqueField.Name }}) } {{ end }} query, err := repo.updateQuery(ctx, old, subject) @@ -484,7 +484,7 @@ func (repo *{{ .RepositoryStructName }}) Delete(ctx context.Context, subject *{{ } } {{- range $uniqueField := .UniqueFields }} - {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") + {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") if err := {{ printf "%sTable" $uniqueField.VarName }}.Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}).RunWithContext(ctx); err != nil { return xerrors.Errorf("Delete.{{ printf "%sTable" $uniqueField.VarName }}.Delete error: %w", err) } @@ -504,10 +504,10 @@ func (repo *{{ .RepositoryStructName }}) Delete(ctx context.Context, subject *{{ // DeleteBy{{ .HashKeyFieldName }} 処理中の {{ .StructName }} の{{ .HashKeyFieldName }}から削除処理一切の責任を持ち、これを行う func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}(ctx context.Context, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...DeleteOption) error { - subject, err := repo.Get(ctx, {{ .HashKeyValueName }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }}{{ end }}) - if err != nil { - return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}.Get error: %w", err) - } + subject, err := repo.Get(ctx, {{ .HashKeyValueName }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }}{{ end }}) + if err != nil { + return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}.Get error: %w", err) + } for _, m := range repo.middleware { c, err := m.BeforeDelete(ctx, subject, options...) if err != nil { @@ -519,7 +519,7 @@ func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}(ctx conte } {{- if .MetaFields }} {{- range $uniqueField := .UniqueFields }} - {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") + {{ printf "%sTable" $uniqueField.VarName }} := repo.db.Table("{{ $uniqueField.StructName }}") if err := {{ printf "%sTable" $uniqueField.VarName }}.Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}).RunWithContext(ctx); err != nil { return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}.{{ printf "%sTable" $uniqueField.VarName }}.Delete error: %w", err) } @@ -548,7 +548,7 @@ func (repo *{{ .RepositoryStructName }}) DeleteWithTx(ctx context.Context, tx *d } } {{- range $uniqueField := .UniqueFields }} - {{ printf "%s%sDeleteQuery" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}) + {{ printf "%s%sDeleteQuery" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}) if err := tx.Delete({{ printf "%s%sDeleteQuery" $tableName $uniqueField.Name }}).RunWithContext(ctx); err != nil { return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}WithTx.Delete error: %w", err) } @@ -570,10 +570,10 @@ func (repo *{{ .RepositoryStructName }}) DeleteWithTx(ctx context.Context, tx *d // DeleteBy{{ .HashKeyFieldName }}WithTx トランザクション中の {{ .StructName }} の{{ .HashKeyFieldName }}から削除処理を行う func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}WithTx(ctx context.Context, tx *dynamo.WriteTx, {{ .HashKeyValueName }} {{ .HashKeyFieldType }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }} {{ .RangeKeyFieldType }}{{ end }}, options ...DeleteOption) error { - subject, err := repo.Get(ctx, {{ .HashKeyValueName }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }}{{ end }}) - if err != nil { - return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}WithTx.Get error: %w", err) - } + subject, err := repo.Get(ctx, {{ .HashKeyValueName }}{{ if ne .RangeKeyFieldName "" }}, {{ .RangeKeyValueName }}{{ end }}) + if err != nil { + return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}WithTx.Get error: %w", err) + } for _, m := range repo.middleware { c, err := m.BeforeDelete(ctx, subject, options...) @@ -586,7 +586,7 @@ func (repo *{{ .RepositoryStructName }}) DeleteBy{{ FuncNameByValue }}WithTx(ctx } {{- if .MetaFields }} {{- range $uniqueField := .UniqueFields }} - {{ printf "%s%sDeleteQuery" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}) + {{ printf "%s%sDeleteQuery" $tableName $uniqueField.Name }} := repo.db.Table("{{ $uniqueField.StructName }}").Delete("{{ $uniqueField.Name }}", subject.{{ $uniqueField.Name }}) if err := tx.Delete({{ printf "%s%sDeleteQuery" $tableName $uniqueField.Name }}).RunWithContext(ctx); err != nil { return xerrors.Errorf("DeleteBy{{ FuncNameByValue }}WithTx.Delete error: %w", err) }