Commit 4b398a6d authored by Ashley Gau's avatar Ashley Gau

generate mocked methods with context as the first arg, because golint

parent b7fe9fe0
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -352,27 +352,27 @@ type {{.MockWrapType}} struct { ...@@ -352,27 +352,27 @@ type {{.MockWrapType}} struct {
// execution flow of the mock. Return (false, nil, nil) to continue with // execution flow of the mock. Return (false, nil, nil) to continue with
// normal mock behavior/ after the hook function executes. // normal mock behavior/ after the hook function executes.
{{- if .GenerateGet}} {{- if .GenerateGet}}
GetHook func(m *{{.MockWrapType}}, ctx context.Context, key *meta.Key) (bool, *{{.FQObjectType}}, error) GetHook func(ctx context.Context, key *meta.Key, m *{{.MockWrapType}}) (bool, *{{.FQObjectType}}, error)
{{- end -}} {{- end -}}
{{- if .GenerateList}} {{- if .GenerateList}}
{{- if .KeyIsGlobal}} {{- if .KeyIsGlobal}}
ListHook func(m *{{.MockWrapType}}, ctx context.Context, fl *filter.F) (bool, []*{{.FQObjectType}}, error) ListHook func(ctx context.Context, fl *filter.F, m *{{.MockWrapType}}) (bool, []*{{.FQObjectType}}, error)
{{- end -}} {{- end -}}
{{- if .KeyIsRegional}} {{- if .KeyIsRegional}}
ListHook func(m *{{.MockWrapType}}, ctx context.Context, region string, fl *filter.F) (bool, []*{{.FQObjectType}}, error) ListHook func(ctx context.Context, region string, fl *filter.F, m *{{.MockWrapType}}) (bool, []*{{.FQObjectType}}, error)
{{- end -}} {{- end -}}
{{- if .KeyIsZonal}} {{- if .KeyIsZonal}}
ListHook func(m *{{.MockWrapType}}, ctx context.Context, zone string, fl *filter.F) (bool, []*{{.FQObjectType}}, error) ListHook func(ctx context.Context, zone string, fl *filter.F, m *{{.MockWrapType}}) (bool, []*{{.FQObjectType}}, error)
{{- end}} {{- end}}
{{- end -}} {{- end -}}
{{- if .GenerateInsert}} {{- if .GenerateInsert}}
InsertHook func(m *{{.MockWrapType}}, ctx context.Context, key *meta.Key, obj *{{.FQObjectType}}) (bool, error) InsertHook func(ctx context.Context, key *meta.Key, obj *{{.FQObjectType}}, m *{{.MockWrapType}}) (bool, error)
{{- end -}} {{- end -}}
{{- if .GenerateDelete}} {{- if .GenerateDelete}}
DeleteHook func(m *{{.MockWrapType}}, ctx context.Context, key *meta.Key) (bool, error) DeleteHook func(ctx context.Context, key *meta.Key, m *{{.MockWrapType}}) (bool, error)
{{- end -}} {{- end -}}
{{- if .AggregatedList}} {{- if .AggregatedList}}
AggregatedListHook func(m *{{.MockWrapType}}, ctx context.Context, fl *filter.F) (bool, map[string][]*{{.FQObjectType}}, error) AggregatedListHook func(ctx context.Context, fl *filter.F, m *{{.MockWrapType}}) (bool, map[string][]*{{.FQObjectType}}, error)
{{- end}} {{- end}}
{{- with .Methods -}} {{- with .Methods -}}
...@@ -390,7 +390,7 @@ type {{.MockWrapType}} struct { ...@@ -390,7 +390,7 @@ type {{.MockWrapType}} struct {
// Get returns the object from the mock. // Get returns the object from the mock.
func (m *{{.MockWrapType}}) Get(ctx context.Context, key *meta.Key) (*{{.FQObjectType}}, error) { func (m *{{.MockWrapType}}) Get(ctx context.Context, key *meta.Key) (*{{.FQObjectType}}, error) {
if m.GetHook != nil { if m.GetHook != nil {
if intercept, obj, err := m.GetHook(m, ctx, key); intercept { if intercept, obj, err := m.GetHook(ctx, key, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.Get(%v, %s) = %+v, %v", ctx, key, obj ,err) glog.V(5).Infof("{{.MockWrapType}}.Get(%v, %s) = %+v, %v", ctx, key, obj ,err)
return obj, err return obj, err
} }
...@@ -436,15 +436,15 @@ func (m *{{.MockWrapType}}) List(ctx context.Context, zone string, fl *filter.F) ...@@ -436,15 +436,15 @@ func (m *{{.MockWrapType}}) List(ctx context.Context, zone string, fl *filter.F)
{{- end}} {{- end}}
if m.ListHook != nil { if m.ListHook != nil {
{{if .KeyIsGlobal -}} {{if .KeyIsGlobal -}}
if intercept, objs, err := m.ListHook(m, ctx, fl); intercept { if intercept, objs, err := m.ListHook(ctx, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.List(%v, %v) = [%v items], %v", ctx, fl, len(objs), err) glog.V(5).Infof("{{.MockWrapType}}.List(%v, %v) = [%v items], %v", ctx, fl, len(objs), err)
{{- end -}} {{- end -}}
{{- if .KeyIsRegional -}} {{- if .KeyIsRegional -}}
if intercept, objs, err := m.ListHook(m, ctx, region, fl); intercept { if intercept, objs, err := m.ListHook(ctx, region, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.List(%v, %q, %v) = [%v items], %v", ctx, region, fl, len(objs), err) glog.V(5).Infof("{{.MockWrapType}}.List(%v, %q, %v) = [%v items], %v", ctx, region, fl, len(objs), err)
{{- end -}} {{- end -}}
{{- if .KeyIsZonal -}} {{- if .KeyIsZonal -}}
if intercept, objs, err := m.ListHook(m, ctx, zone, fl); intercept { if intercept, objs, err := m.ListHook(ctx, zone, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.List(%v, %q, %v) = [%v items], %v", ctx, zone, fl, len(objs), err) glog.V(5).Infof("{{.MockWrapType}}.List(%v, %q, %v) = [%v items], %v", ctx, zone, fl, len(objs), err)
{{- end}} {{- end}}
return objs, err return objs, err
...@@ -508,7 +508,7 @@ func (m *{{.MockWrapType}}) List(ctx context.Context, zone string, fl *filter.F) ...@@ -508,7 +508,7 @@ func (m *{{.MockWrapType}}) List(ctx context.Context, zone string, fl *filter.F)
// Insert is a mock for inserting/creating a new object. // Insert is a mock for inserting/creating a new object.
func (m *{{.MockWrapType}}) Insert(ctx context.Context, key *meta.Key, obj *{{.FQObjectType}}) error { func (m *{{.MockWrapType}}) Insert(ctx context.Context, key *meta.Key, obj *{{.FQObjectType}}) error {
if m.InsertHook != nil { if m.InsertHook != nil {
if intercept, err := m.InsertHook(m, ctx, key, obj); intercept { if intercept, err := m.InsertHook(ctx, key, obj, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.Insert(%v, %v, %+v) = %v", ctx, key, obj, err) glog.V(5).Infof("{{.MockWrapType}}.Insert(%v, %v, %+v) = %v", ctx, key, obj, err)
return err return err
} }
...@@ -548,7 +548,7 @@ func (m *{{.MockWrapType}}) Insert(ctx context.Context, key *meta.Key, obj *{{.F ...@@ -548,7 +548,7 @@ func (m *{{.MockWrapType}}) Insert(ctx context.Context, key *meta.Key, obj *{{.F
// Delete is a mock for deleting the object. // Delete is a mock for deleting the object.
func (m *{{.MockWrapType}}) Delete(ctx context.Context, key *meta.Key) error { func (m *{{.MockWrapType}}) Delete(ctx context.Context, key *meta.Key) error {
if m.DeleteHook != nil { if m.DeleteHook != nil {
if intercept, err := m.DeleteHook(m, ctx, key); intercept { if intercept, err := m.DeleteHook(ctx, key, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.Delete(%v, %v) = %v", ctx, key, err) glog.V(5).Infof("{{.MockWrapType}}.Delete(%v, %v) = %v", ctx, key, err)
return err return err
} }
...@@ -583,7 +583,7 @@ func (m *{{.MockWrapType}}) Delete(ctx context.Context, key *meta.Key) error { ...@@ -583,7 +583,7 @@ func (m *{{.MockWrapType}}) Delete(ctx context.Context, key *meta.Key) error {
// AggregatedList is a mock for AggregatedList. // AggregatedList is a mock for AggregatedList.
func (m *{{.MockWrapType}}) AggregatedList(ctx context.Context, fl *filter.F) (map[string][]*{{.FQObjectType}}, error) { func (m *{{.MockWrapType}}) AggregatedList(ctx context.Context, fl *filter.F) (map[string][]*{{.FQObjectType}}, error) {
if m.AggregatedListHook != nil { if m.AggregatedListHook != nil {
if intercept, objs, err := m.AggregatedListHook(m, ctx, fl); intercept { if intercept, objs, err := m.AggregatedListHook(ctx, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.AggregatedList(%v, %v) = [%v items], %v", ctx, fl, len(objs), err) glog.V(5).Infof("{{.MockWrapType}}.AggregatedList(%v, %v) = [%v items], %v", ctx, fl, len(objs), err)
return objs, err return objs, err
} }
...@@ -632,17 +632,17 @@ func (m *{{.MockWrapType}}) Obj(o *{{.FQObjectType}}) *Mock{{.Service}}Obj { ...@@ -632,17 +632,17 @@ func (m *{{.MockWrapType}}) Obj(o *{{.FQObjectType}}) *Mock{{.Service}}Obj {
func (m *{{.MockWrapType}}) {{.FcnArgs}} { func (m *{{.MockWrapType}}) {{.FcnArgs}} {
{{- if .IsOperation }} {{- if .IsOperation }}
if m.{{.MockHookName}} != nil { if m.{{.MockHookName}} != nil {
return m.{{.MockHookName}}(m, ctx, key {{.CallArgs}}) return m.{{.MockHookName}}(ctx, key {{.CallArgs}}, m)
} }
return nil return nil
{{- else if .IsGet}} {{- else if .IsGet}}
if m.{{.MockHookName}} != nil { if m.{{.MockHookName}} != nil {
return m.{{.MockHookName}}(m, ctx, key {{.CallArgs}}) return m.{{.MockHookName}}(ctx, key {{.CallArgs}}, m)
} }
return nil, fmt.Errorf("{{.MockHookName}} must be set") return nil, fmt.Errorf("{{.MockHookName}} must be set")
{{- else if .IsPaged}} {{- else if .IsPaged}}
if m.{{.MockHookName}} != nil { if m.{{.MockHookName}} != nil {
return m.{{.MockHookName}}(m, ctx, key {{.CallArgs}}, fl) return m.{{.MockHookName}}(ctx, key {{.CallArgs}}, fl, m)
} }
return nil, nil return nil, nil
{{- end}} {{- end}}
......
...@@ -270,7 +270,6 @@ func (m *Method) MockHookName() string { ...@@ -270,7 +270,6 @@ func (m *Method) MockHookName() string {
// MockHook is the definition of the hook function. // MockHook is the definition of the hook function.
func (m *Method) MockHook() string { func (m *Method) MockHook() string {
args := m.args(m.argsSkip(), false, []string{ args := m.args(m.argsSkip(), false, []string{
fmt.Sprintf("*%s", m.MockWrapType()),
"context.Context", "context.Context",
"*meta.Key", "*meta.Key",
}) })
...@@ -278,6 +277,8 @@ func (m *Method) MockHook() string { ...@@ -278,6 +277,8 @@ func (m *Method) MockHook() string {
args = append(args, "*filter.F") args = append(args, "*filter.F")
} }
args = append(args, fmt.Sprintf("*%s", m.MockWrapType()))
switch m.kind { switch m.kind {
case MethodOperation: case MethodOperation:
return fmt.Sprintf("%v func(%v) error", m.MockHookName(), strings.Join(args, ", ")) return fmt.Sprintf("%v func(%v) error", m.MockHookName(), strings.Join(args, ", "))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment