id
int32 0
167k
| repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
listlengths 21
1.41k
| docstring
stringlengths 6
2.61k
| docstring_tokens
listlengths 3
215
| sha
stringlengths 40
40
| url
stringlengths 85
252
|
|---|---|---|---|---|---|---|---|---|---|---|---|
21,900
|
golang/mock
|
sample/concurrent/mock/concurrent_mock.go
|
NewMockMath
|
func NewMockMath(ctrl *gomock.Controller) *MockMath {
mock := &MockMath{ctrl: ctrl}
mock.recorder = &MockMathMockRecorder{mock}
return mock
}
|
go
|
func NewMockMath(ctrl *gomock.Controller) *MockMath {
mock := &MockMath{ctrl: ctrl}
mock.recorder = &MockMathMockRecorder{mock}
return mock
}
|
[
"func",
"NewMockMath",
"(",
"ctrl",
"*",
"gomock",
".",
"Controller",
")",
"*",
"MockMath",
"{",
"mock",
":=",
"&",
"MockMath",
"{",
"ctrl",
":",
"ctrl",
"}",
"\n",
"mock",
".",
"recorder",
"=",
"&",
"MockMathMockRecorder",
"{",
"mock",
"}",
"\n",
"return",
"mock",
"\n",
"}"
] |
// NewMockMath creates a new mock instance
|
[
"NewMockMath",
"creates",
"a",
"new",
"mock",
"instance"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/concurrent/mock/concurrent_mock.go#L24-L28
|
21,901
|
golang/mock
|
sample/concurrent/mock/concurrent_mock.go
|
Sum
|
func (m *MockMath) Sum(arg0, arg1 int) int {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Sum", arg0, arg1)
ret0, _ := ret[0].(int)
return ret0
}
|
go
|
func (m *MockMath) Sum(arg0, arg1 int) int {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Sum", arg0, arg1)
ret0, _ := ret[0].(int)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockMath",
")",
"Sum",
"(",
"arg0",
",",
"arg1",
"int",
")",
"int",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
",",
"arg1",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"int",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// Sum mocks base method
|
[
"Sum",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/concurrent/mock/concurrent_mock.go#L36-L41
|
21,902
|
golang/mock
|
sample/concurrent/mock/concurrent_mock.go
|
Sum
|
func (mr *MockMathMockRecorder) Sum(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sum", reflect.TypeOf((*MockMath)(nil).Sum), arg0, arg1)
}
|
go
|
func (mr *MockMathMockRecorder) Sum(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sum", reflect.TypeOf((*MockMath)(nil).Sum), arg0, arg1)
}
|
[
"func",
"(",
"mr",
"*",
"MockMathMockRecorder",
")",
"Sum",
"(",
"arg0",
",",
"arg1",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"mr",
".",
"mock",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockMath",
")",
"(",
"nil",
")",
".",
"Sum",
")",
",",
"arg0",
",",
"arg1",
")",
"\n",
"}"
] |
// Sum indicates an expected call of Sum
|
[
"Sum",
"indicates",
"an",
"expected",
"call",
"of",
"Sum"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/concurrent/mock/concurrent_mock.go#L44-L47
|
21,903
|
golang/mock
|
mockgen/reflect.go
|
run
|
func run(program string) (*model.Package, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
filename := f.Name()
defer os.Remove(filename)
if err := f.Close(); err != nil {
return nil, err
}
// Run the program.
cmd := exec.Command(program, "-output", filename)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
f, err = os.Open(filename)
if err != nil {
return nil, err
}
// Process output.
var pkg model.Package
if err := gob.NewDecoder(f).Decode(&pkg); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
return &pkg, nil
}
|
go
|
func run(program string) (*model.Package, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
filename := f.Name()
defer os.Remove(filename)
if err := f.Close(); err != nil {
return nil, err
}
// Run the program.
cmd := exec.Command(program, "-output", filename)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
f, err = os.Open(filename)
if err != nil {
return nil, err
}
// Process output.
var pkg model.Package
if err := gob.NewDecoder(f).Decode(&pkg); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
return &pkg, nil
}
|
[
"func",
"run",
"(",
"program",
"string",
")",
"(",
"*",
"model",
".",
"Package",
",",
"error",
")",
"{",
"f",
",",
"err",
":=",
"ioutil",
".",
"TempFile",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"filename",
":=",
"f",
".",
"Name",
"(",
")",
"\n",
"defer",
"os",
".",
"Remove",
"(",
"filename",
")",
"\n",
"if",
"err",
":=",
"f",
".",
"Close",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"// Run the program.",
"cmd",
":=",
"exec",
".",
"Command",
"(",
"program",
",",
"\"",
"\"",
",",
"filename",
")",
"\n",
"cmd",
".",
"Stdout",
"=",
"os",
".",
"Stdout",
"\n",
"cmd",
".",
"Stderr",
"=",
"os",
".",
"Stderr",
"\n",
"if",
"err",
":=",
"cmd",
".",
"Run",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"f",
",",
"err",
"=",
"os",
".",
"Open",
"(",
"filename",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"// Process output.",
"var",
"pkg",
"model",
".",
"Package",
"\n",
"if",
"err",
":=",
"gob",
".",
"NewDecoder",
"(",
"f",
")",
".",
"Decode",
"(",
"&",
"pkg",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"f",
".",
"Close",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"pkg",
",",
"nil",
"\n",
"}"
] |
// run the given program and parse the output as a model.Package.
|
[
"run",
"the",
"given",
"program",
"and",
"parse",
"the",
"output",
"as",
"a",
"model",
".",
"Package",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/reflect.go#L54-L90
|
21,904
|
golang/mock
|
mockgen/reflect.go
|
runInDir
|
func runInDir(program []byte, dir string) (*model.Package, error) {
// We use TempDir instead of TempFile so we can control the filename.
tmpDir, err := ioutil.TempDir(dir, "gomock_reflect_")
if err != nil {
return nil, err
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
log.Printf("failed to remove temp directory: %s", err)
}
}()
const progSource = "prog.go"
var progBinary = "prog.bin"
if runtime.GOOS == "windows" {
// Windows won't execute a program unless it has a ".exe" suffix.
progBinary += ".exe"
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, progSource), program, 0600); err != nil {
return nil, err
}
cmdArgs := []string{}
cmdArgs = append(cmdArgs, "build")
if *buildFlags != "" {
cmdArgs = append(cmdArgs, *buildFlags)
}
cmdArgs = append(cmdArgs, "-o", progBinary, progSource)
// Build the program.
cmd := exec.Command("go", cmdArgs...)
cmd.Dir = tmpDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
return run(filepath.Join(tmpDir, progBinary))
}
|
go
|
func runInDir(program []byte, dir string) (*model.Package, error) {
// We use TempDir instead of TempFile so we can control the filename.
tmpDir, err := ioutil.TempDir(dir, "gomock_reflect_")
if err != nil {
return nil, err
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
log.Printf("failed to remove temp directory: %s", err)
}
}()
const progSource = "prog.go"
var progBinary = "prog.bin"
if runtime.GOOS == "windows" {
// Windows won't execute a program unless it has a ".exe" suffix.
progBinary += ".exe"
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, progSource), program, 0600); err != nil {
return nil, err
}
cmdArgs := []string{}
cmdArgs = append(cmdArgs, "build")
if *buildFlags != "" {
cmdArgs = append(cmdArgs, *buildFlags)
}
cmdArgs = append(cmdArgs, "-o", progBinary, progSource)
// Build the program.
cmd := exec.Command("go", cmdArgs...)
cmd.Dir = tmpDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
return run(filepath.Join(tmpDir, progBinary))
}
|
[
"func",
"runInDir",
"(",
"program",
"[",
"]",
"byte",
",",
"dir",
"string",
")",
"(",
"*",
"model",
".",
"Package",
",",
"error",
")",
"{",
"// We use TempDir instead of TempFile so we can control the filename.",
"tmpDir",
",",
"err",
":=",
"ioutil",
".",
"TempDir",
"(",
"dir",
",",
"\"",
"\"",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"defer",
"func",
"(",
")",
"{",
"if",
"err",
":=",
"os",
".",
"RemoveAll",
"(",
"tmpDir",
")",
";",
"err",
"!=",
"nil",
"{",
"log",
".",
"Printf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n",
"const",
"progSource",
"=",
"\"",
"\"",
"\n",
"var",
"progBinary",
"=",
"\"",
"\"",
"\n",
"if",
"runtime",
".",
"GOOS",
"==",
"\"",
"\"",
"{",
"// Windows won't execute a program unless it has a \".exe\" suffix.",
"progBinary",
"+=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"ioutil",
".",
"WriteFile",
"(",
"filepath",
".",
"Join",
"(",
"tmpDir",
",",
"progSource",
")",
",",
"program",
",",
"0600",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"cmdArgs",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"cmdArgs",
"=",
"append",
"(",
"cmdArgs",
",",
"\"",
"\"",
")",
"\n",
"if",
"*",
"buildFlags",
"!=",
"\"",
"\"",
"{",
"cmdArgs",
"=",
"append",
"(",
"cmdArgs",
",",
"*",
"buildFlags",
")",
"\n",
"}",
"\n",
"cmdArgs",
"=",
"append",
"(",
"cmdArgs",
",",
"\"",
"\"",
",",
"progBinary",
",",
"progSource",
")",
"\n\n",
"// Build the program.",
"cmd",
":=",
"exec",
".",
"Command",
"(",
"\"",
"\"",
",",
"cmdArgs",
"...",
")",
"\n",
"cmd",
".",
"Dir",
"=",
"tmpDir",
"\n",
"cmd",
".",
"Stdout",
"=",
"os",
".",
"Stdout",
"\n",
"cmd",
".",
"Stderr",
"=",
"os",
".",
"Stderr",
"\n",
"if",
"err",
":=",
"cmd",
".",
"Run",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"run",
"(",
"filepath",
".",
"Join",
"(",
"tmpDir",
",",
"progBinary",
")",
")",
"\n",
"}"
] |
// runInDir writes the given program into the given dir, runs it there, and
// parses the output as a model.Package.
|
[
"runInDir",
"writes",
"the",
"given",
"program",
"into",
"the",
"given",
"dir",
"runs",
"it",
"there",
"and",
"parses",
"the",
"output",
"as",
"a",
"model",
".",
"Package",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/reflect.go#L94-L132
|
21,905
|
golang/mock
|
sample/user.go
|
Remember
|
func Remember(index Index, keys []string, values []interface{}) {
for i, k := range keys {
index.Put(k, values[i])
}
err := index.NillableRet()
if err != nil {
log.Fatalf("Woah! %v", err)
}
if len(keys) > 0 && keys[0] == "a" {
index.Ellip("%d", 0, 1, 1, 2, 3)
index.Ellip("%d", 1, 3, 6, 10, 15)
index.EllipOnly("arg")
}
}
|
go
|
func Remember(index Index, keys []string, values []interface{}) {
for i, k := range keys {
index.Put(k, values[i])
}
err := index.NillableRet()
if err != nil {
log.Fatalf("Woah! %v", err)
}
if len(keys) > 0 && keys[0] == "a" {
index.Ellip("%d", 0, 1, 1, 2, 3)
index.Ellip("%d", 1, 3, 6, 10, 15)
index.EllipOnly("arg")
}
}
|
[
"func",
"Remember",
"(",
"index",
"Index",
",",
"keys",
"[",
"]",
"string",
",",
"values",
"[",
"]",
"interface",
"{",
"}",
")",
"{",
"for",
"i",
",",
"k",
":=",
"range",
"keys",
"{",
"index",
".",
"Put",
"(",
"k",
",",
"values",
"[",
"i",
"]",
")",
"\n",
"}",
"\n",
"err",
":=",
"index",
".",
"NillableRet",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"log",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"keys",
")",
">",
"0",
"&&",
"keys",
"[",
"0",
"]",
"==",
"\"",
"\"",
"{",
"index",
".",
"Ellip",
"(",
"\"",
"\"",
",",
"0",
",",
"1",
",",
"1",
",",
"2",
",",
"3",
")",
"\n",
"index",
".",
"Ellip",
"(",
"\"",
"\"",
",",
"1",
",",
"3",
",",
"6",
",",
"10",
",",
"15",
")",
"\n",
"index",
".",
"EllipOnly",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}"
] |
// A function that we will test that uses the above interface.
// It takes a list of keys and values, and puts them in the index.
|
[
"A",
"function",
"that",
"we",
"will",
"test",
"that",
"uses",
"the",
"above",
"interface",
".",
"It",
"takes",
"a",
"list",
"of",
"keys",
"and",
"values",
"and",
"puts",
"them",
"in",
"the",
"index",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/user.go#L95-L108
|
21,906
|
golang/mock
|
gomock/call.go
|
MinTimes
|
func (c *Call) MinTimes(n int) *Call {
c.minCalls = n
if c.maxCalls == 1 {
c.maxCalls = 1e8
}
return c
}
|
go
|
func (c *Call) MinTimes(n int) *Call {
c.minCalls = n
if c.maxCalls == 1 {
c.maxCalls = 1e8
}
return c
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"MinTimes",
"(",
"n",
"int",
")",
"*",
"Call",
"{",
"c",
".",
"minCalls",
"=",
"n",
"\n",
"if",
"c",
".",
"maxCalls",
"==",
"1",
"{",
"c",
".",
"maxCalls",
"=",
"1e8",
"\n",
"}",
"\n",
"return",
"c",
"\n",
"}"
] |
// MinTimes requires the call to occur at least n times. If AnyTimes or MaxTimes have not been called, MinTimes also
// sets the maximum number of calls to infinity.
|
[
"MinTimes",
"requires",
"the",
"call",
"to",
"occur",
"at",
"least",
"n",
"times",
".",
"If",
"AnyTimes",
"or",
"MaxTimes",
"have",
"not",
"been",
"called",
"MinTimes",
"also",
"sets",
"the",
"maximum",
"number",
"of",
"calls",
"to",
"infinity",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L87-L93
|
21,907
|
golang/mock
|
gomock/call.go
|
MaxTimes
|
func (c *Call) MaxTimes(n int) *Call {
c.maxCalls = n
if c.minCalls == 1 {
c.minCalls = 0
}
return c
}
|
go
|
func (c *Call) MaxTimes(n int) *Call {
c.maxCalls = n
if c.minCalls == 1 {
c.minCalls = 0
}
return c
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"MaxTimes",
"(",
"n",
"int",
")",
"*",
"Call",
"{",
"c",
".",
"maxCalls",
"=",
"n",
"\n",
"if",
"c",
".",
"minCalls",
"==",
"1",
"{",
"c",
".",
"minCalls",
"=",
"0",
"\n",
"}",
"\n",
"return",
"c",
"\n",
"}"
] |
// MaxTimes limits the number of calls to n times. If AnyTimes or MinTimes have not been called, MaxTimes also
// sets the minimum number of calls to 0.
|
[
"MaxTimes",
"limits",
"the",
"number",
"of",
"calls",
"to",
"n",
"times",
".",
"If",
"AnyTimes",
"or",
"MinTimes",
"have",
"not",
"been",
"called",
"MaxTimes",
"also",
"sets",
"the",
"minimum",
"number",
"of",
"calls",
"to",
"0",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L97-L103
|
21,908
|
golang/mock
|
gomock/call.go
|
Return
|
func (c *Call) Return(rets ...interface{}) *Call {
c.t.Helper()
mt := c.methodType
if len(rets) != mt.NumOut() {
c.t.Fatalf("wrong number of arguments to Return for %T.%v: got %d, want %d [%s]",
c.receiver, c.method, len(rets), mt.NumOut(), c.origin)
}
for i, ret := range rets {
if got, want := reflect.TypeOf(ret), mt.Out(i); got == want {
// Identical types; nothing to do.
} else if got == nil {
// Nil needs special handling.
switch want.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
// ok
default:
c.t.Fatalf("argument %d to Return for %T.%v is nil, but %v is not nillable [%s]",
i, c.receiver, c.method, want, c.origin)
}
} else if got.AssignableTo(want) {
// Assignable type relation. Make the assignment now so that the generated code
// can return the values with a type assertion.
v := reflect.New(want).Elem()
v.Set(reflect.ValueOf(ret))
rets[i] = v.Interface()
} else {
c.t.Fatalf("wrong type of argument %d to Return for %T.%v: %v is not assignable to %v [%s]",
i, c.receiver, c.method, got, want, c.origin)
}
}
c.addAction(func([]interface{}) []interface{} {
return rets
})
return c
}
|
go
|
func (c *Call) Return(rets ...interface{}) *Call {
c.t.Helper()
mt := c.methodType
if len(rets) != mt.NumOut() {
c.t.Fatalf("wrong number of arguments to Return for %T.%v: got %d, want %d [%s]",
c.receiver, c.method, len(rets), mt.NumOut(), c.origin)
}
for i, ret := range rets {
if got, want := reflect.TypeOf(ret), mt.Out(i); got == want {
// Identical types; nothing to do.
} else if got == nil {
// Nil needs special handling.
switch want.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
// ok
default:
c.t.Fatalf("argument %d to Return for %T.%v is nil, but %v is not nillable [%s]",
i, c.receiver, c.method, want, c.origin)
}
} else if got.AssignableTo(want) {
// Assignable type relation. Make the assignment now so that the generated code
// can return the values with a type assertion.
v := reflect.New(want).Elem()
v.Set(reflect.ValueOf(ret))
rets[i] = v.Interface()
} else {
c.t.Fatalf("wrong type of argument %d to Return for %T.%v: %v is not assignable to %v [%s]",
i, c.receiver, c.method, got, want, c.origin)
}
}
c.addAction(func([]interface{}) []interface{} {
return rets
})
return c
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"Return",
"(",
"rets",
"...",
"interface",
"{",
"}",
")",
"*",
"Call",
"{",
"c",
".",
"t",
".",
"Helper",
"(",
")",
"\n\n",
"mt",
":=",
"c",
".",
"methodType",
"\n",
"if",
"len",
"(",
"rets",
")",
"!=",
"mt",
".",
"NumOut",
"(",
")",
"{",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"c",
".",
"receiver",
",",
"c",
".",
"method",
",",
"len",
"(",
"rets",
")",
",",
"mt",
".",
"NumOut",
"(",
")",
",",
"c",
".",
"origin",
")",
"\n",
"}",
"\n",
"for",
"i",
",",
"ret",
":=",
"range",
"rets",
"{",
"if",
"got",
",",
"want",
":=",
"reflect",
".",
"TypeOf",
"(",
"ret",
")",
",",
"mt",
".",
"Out",
"(",
"i",
")",
";",
"got",
"==",
"want",
"{",
"// Identical types; nothing to do.",
"}",
"else",
"if",
"got",
"==",
"nil",
"{",
"// Nil needs special handling.",
"switch",
"want",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Chan",
",",
"reflect",
".",
"Func",
",",
"reflect",
".",
"Interface",
",",
"reflect",
".",
"Map",
",",
"reflect",
".",
"Ptr",
",",
"reflect",
".",
"Slice",
":",
"// ok",
"default",
":",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"i",
",",
"c",
".",
"receiver",
",",
"c",
".",
"method",
",",
"want",
",",
"c",
".",
"origin",
")",
"\n",
"}",
"\n",
"}",
"else",
"if",
"got",
".",
"AssignableTo",
"(",
"want",
")",
"{",
"// Assignable type relation. Make the assignment now so that the generated code",
"// can return the values with a type assertion.",
"v",
":=",
"reflect",
".",
"New",
"(",
"want",
")",
".",
"Elem",
"(",
")",
"\n",
"v",
".",
"Set",
"(",
"reflect",
".",
"ValueOf",
"(",
"ret",
")",
")",
"\n",
"rets",
"[",
"i",
"]",
"=",
"v",
".",
"Interface",
"(",
")",
"\n",
"}",
"else",
"{",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"i",
",",
"c",
".",
"receiver",
",",
"c",
".",
"method",
",",
"got",
",",
"want",
",",
"c",
".",
"origin",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"c",
".",
"addAction",
"(",
"func",
"(",
"[",
"]",
"interface",
"{",
"}",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"return",
"rets",
"\n",
"}",
")",
"\n\n",
"return",
"c",
"\n",
"}"
] |
// Return declares the values to be returned by the mocked function call.
|
[
"Return",
"declares",
"the",
"values",
"to",
"be",
"returned",
"by",
"the",
"mocked",
"function",
"call",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L159-L196
|
21,909
|
golang/mock
|
gomock/call.go
|
Times
|
func (c *Call) Times(n int) *Call {
c.minCalls, c.maxCalls = n, n
return c
}
|
go
|
func (c *Call) Times(n int) *Call {
c.minCalls, c.maxCalls = n, n
return c
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"Times",
"(",
"n",
"int",
")",
"*",
"Call",
"{",
"c",
".",
"minCalls",
",",
"c",
".",
"maxCalls",
"=",
"n",
",",
"n",
"\n",
"return",
"c",
"\n",
"}"
] |
// Times declares the exact number of times a function call is expected to be executed.
|
[
"Times",
"declares",
"the",
"exact",
"number",
"of",
"times",
"a",
"function",
"call",
"is",
"expected",
"to",
"be",
"executed",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L199-L202
|
21,910
|
golang/mock
|
gomock/call.go
|
SetArg
|
func (c *Call) SetArg(n int, value interface{}) *Call {
c.t.Helper()
mt := c.methodType
// TODO: This will break on variadic methods.
// We will need to check those at invocation time.
if n < 0 || n >= mt.NumIn() {
c.t.Fatalf("SetArg(%d, ...) called for a method with %d args [%s]",
n, mt.NumIn(), c.origin)
}
// Permit setting argument through an interface.
// In the interface case, we don't (nay, can't) check the type here.
at := mt.In(n)
switch at.Kind() {
case reflect.Ptr:
dt := at.Elem()
if vt := reflect.TypeOf(value); !vt.AssignableTo(dt) {
c.t.Fatalf("SetArg(%d, ...) argument is a %v, not assignable to %v [%s]",
n, vt, dt, c.origin)
}
case reflect.Interface:
// nothing to do
case reflect.Slice:
// nothing to do
default:
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v [%s]",
n, at, c.origin)
}
c.addAction(func(args []interface{}) []interface{} {
v := reflect.ValueOf(value)
switch reflect.TypeOf(args[n]).Kind() {
case reflect.Slice:
setSlice(args[n], v)
default:
reflect.ValueOf(args[n]).Elem().Set(v)
}
return nil
})
return c
}
|
go
|
func (c *Call) SetArg(n int, value interface{}) *Call {
c.t.Helper()
mt := c.methodType
// TODO: This will break on variadic methods.
// We will need to check those at invocation time.
if n < 0 || n >= mt.NumIn() {
c.t.Fatalf("SetArg(%d, ...) called for a method with %d args [%s]",
n, mt.NumIn(), c.origin)
}
// Permit setting argument through an interface.
// In the interface case, we don't (nay, can't) check the type here.
at := mt.In(n)
switch at.Kind() {
case reflect.Ptr:
dt := at.Elem()
if vt := reflect.TypeOf(value); !vt.AssignableTo(dt) {
c.t.Fatalf("SetArg(%d, ...) argument is a %v, not assignable to %v [%s]",
n, vt, dt, c.origin)
}
case reflect.Interface:
// nothing to do
case reflect.Slice:
// nothing to do
default:
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v [%s]",
n, at, c.origin)
}
c.addAction(func(args []interface{}) []interface{} {
v := reflect.ValueOf(value)
switch reflect.TypeOf(args[n]).Kind() {
case reflect.Slice:
setSlice(args[n], v)
default:
reflect.ValueOf(args[n]).Elem().Set(v)
}
return nil
})
return c
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"SetArg",
"(",
"n",
"int",
",",
"value",
"interface",
"{",
"}",
")",
"*",
"Call",
"{",
"c",
".",
"t",
".",
"Helper",
"(",
")",
"\n\n",
"mt",
":=",
"c",
".",
"methodType",
"\n",
"// TODO: This will break on variadic methods.",
"// We will need to check those at invocation time.",
"if",
"n",
"<",
"0",
"||",
"n",
">=",
"mt",
".",
"NumIn",
"(",
")",
"{",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"n",
",",
"mt",
".",
"NumIn",
"(",
")",
",",
"c",
".",
"origin",
")",
"\n",
"}",
"\n",
"// Permit setting argument through an interface.",
"// In the interface case, we don't (nay, can't) check the type here.",
"at",
":=",
"mt",
".",
"In",
"(",
"n",
")",
"\n",
"switch",
"at",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Ptr",
":",
"dt",
":=",
"at",
".",
"Elem",
"(",
")",
"\n",
"if",
"vt",
":=",
"reflect",
".",
"TypeOf",
"(",
"value",
")",
";",
"!",
"vt",
".",
"AssignableTo",
"(",
"dt",
")",
"{",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"n",
",",
"vt",
",",
"dt",
",",
"c",
".",
"origin",
")",
"\n",
"}",
"\n",
"case",
"reflect",
".",
"Interface",
":",
"// nothing to do",
"case",
"reflect",
".",
"Slice",
":",
"// nothing to do",
"default",
":",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"n",
",",
"at",
",",
"c",
".",
"origin",
")",
"\n",
"}",
"\n\n",
"c",
".",
"addAction",
"(",
"func",
"(",
"args",
"[",
"]",
"interface",
"{",
"}",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"v",
":=",
"reflect",
".",
"ValueOf",
"(",
"value",
")",
"\n",
"switch",
"reflect",
".",
"TypeOf",
"(",
"args",
"[",
"n",
"]",
")",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Slice",
":",
"setSlice",
"(",
"args",
"[",
"n",
"]",
",",
"v",
")",
"\n",
"default",
":",
"reflect",
".",
"ValueOf",
"(",
"args",
"[",
"n",
"]",
")",
".",
"Elem",
"(",
")",
".",
"Set",
"(",
"v",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"return",
"c",
"\n",
"}"
] |
// SetArg declares an action that will set the nth argument's value,
// indirected through a pointer. Or, in the case of a slice, SetArg
// will copy value's elements into the nth argument.
|
[
"SetArg",
"declares",
"an",
"action",
"that",
"will",
"set",
"the",
"nth",
"argument",
"s",
"value",
"indirected",
"through",
"a",
"pointer",
".",
"Or",
"in",
"the",
"case",
"of",
"a",
"slice",
"SetArg",
"will",
"copy",
"value",
"s",
"elements",
"into",
"the",
"nth",
"argument",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L207-L247
|
21,911
|
golang/mock
|
gomock/call.go
|
isPreReq
|
func (c *Call) isPreReq(other *Call) bool {
for _, preReq := range c.preReqs {
if other == preReq || preReq.isPreReq(other) {
return true
}
}
return false
}
|
go
|
func (c *Call) isPreReq(other *Call) bool {
for _, preReq := range c.preReqs {
if other == preReq || preReq.isPreReq(other) {
return true
}
}
return false
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"isPreReq",
"(",
"other",
"*",
"Call",
")",
"bool",
"{",
"for",
"_",
",",
"preReq",
":=",
"range",
"c",
".",
"preReqs",
"{",
"if",
"other",
"==",
"preReq",
"||",
"preReq",
".",
"isPreReq",
"(",
"other",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// isPreReq returns true if other is a direct or indirect prerequisite to c.
|
[
"isPreReq",
"returns",
"true",
"if",
"other",
"is",
"a",
"direct",
"or",
"indirect",
"prerequisite",
"to",
"c",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L250-L257
|
21,912
|
golang/mock
|
gomock/call.go
|
After
|
func (c *Call) After(preReq *Call) *Call {
c.t.Helper()
if c == preReq {
c.t.Fatalf("A call isn't allowed to be its own prerequisite")
}
if preReq.isPreReq(c) {
c.t.Fatalf("Loop in call order: %v is a prerequisite to %v (possibly indirectly).", c, preReq)
}
c.preReqs = append(c.preReqs, preReq)
return c
}
|
go
|
func (c *Call) After(preReq *Call) *Call {
c.t.Helper()
if c == preReq {
c.t.Fatalf("A call isn't allowed to be its own prerequisite")
}
if preReq.isPreReq(c) {
c.t.Fatalf("Loop in call order: %v is a prerequisite to %v (possibly indirectly).", c, preReq)
}
c.preReqs = append(c.preReqs, preReq)
return c
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"After",
"(",
"preReq",
"*",
"Call",
")",
"*",
"Call",
"{",
"c",
".",
"t",
".",
"Helper",
"(",
")",
"\n\n",
"if",
"c",
"==",
"preReq",
"{",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"preReq",
".",
"isPreReq",
"(",
"c",
")",
"{",
"c",
".",
"t",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"c",
",",
"preReq",
")",
"\n",
"}",
"\n\n",
"c",
".",
"preReqs",
"=",
"append",
"(",
"c",
".",
"preReqs",
",",
"preReq",
")",
"\n",
"return",
"c",
"\n",
"}"
] |
// After declares that the call may only match after preReq has been exhausted.
|
[
"After",
"declares",
"that",
"the",
"call",
"may",
"only",
"match",
"after",
"preReq",
"has",
"been",
"exhausted",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L260-L272
|
21,913
|
golang/mock
|
gomock/call.go
|
dropPrereqs
|
func (c *Call) dropPrereqs() (preReqs []*Call) {
preReqs = c.preReqs
c.preReqs = nil
return
}
|
go
|
func (c *Call) dropPrereqs() (preReqs []*Call) {
preReqs = c.preReqs
c.preReqs = nil
return
}
|
[
"func",
"(",
"c",
"*",
"Call",
")",
"dropPrereqs",
"(",
")",
"(",
"preReqs",
"[",
"]",
"*",
"Call",
")",
"{",
"preReqs",
"=",
"c",
".",
"preReqs",
"\n",
"c",
".",
"preReqs",
"=",
"nil",
"\n",
"return",
"\n",
"}"
] |
// dropPrereqs tells the expected Call to not re-check prerequisite calls any
// longer, and to return its current set.
|
[
"dropPrereqs",
"tells",
"the",
"expected",
"Call",
"to",
"not",
"re",
"-",
"check",
"prerequisite",
"calls",
"any",
"longer",
"and",
"to",
"return",
"its",
"current",
"set",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L393-L397
|
21,914
|
golang/mock
|
gomock/call.go
|
InOrder
|
func InOrder(calls ...*Call) {
for i := 1; i < len(calls); i++ {
calls[i].After(calls[i-1])
}
}
|
go
|
func InOrder(calls ...*Call) {
for i := 1; i < len(calls); i++ {
calls[i].After(calls[i-1])
}
}
|
[
"func",
"InOrder",
"(",
"calls",
"...",
"*",
"Call",
")",
"{",
"for",
"i",
":=",
"1",
";",
"i",
"<",
"len",
"(",
"calls",
")",
";",
"i",
"++",
"{",
"calls",
"[",
"i",
"]",
".",
"After",
"(",
"calls",
"[",
"i",
"-",
"1",
"]",
")",
"\n",
"}",
"\n",
"}"
] |
// InOrder declares that the given calls should occur in order.
|
[
"InOrder",
"declares",
"that",
"the",
"given",
"calls",
"should",
"occur",
"in",
"order",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/call.go#L405-L409
|
21,915
|
golang/mock
|
mockgen/mockgen.go
|
sanitize
|
func sanitize(s string) string {
t := ""
for _, r := range s {
if t == "" {
if unicode.IsLetter(r) || r == '_' {
t += string(r)
continue
}
} else {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
t += string(r)
continue
}
}
t += "_"
}
if t == "_" {
t = "x"
}
return t
}
|
go
|
func sanitize(s string) string {
t := ""
for _, r := range s {
if t == "" {
if unicode.IsLetter(r) || r == '_' {
t += string(r)
continue
}
} else {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
t += string(r)
continue
}
}
t += "_"
}
if t == "_" {
t = "x"
}
return t
}
|
[
"func",
"sanitize",
"(",
"s",
"string",
")",
"string",
"{",
"t",
":=",
"\"",
"\"",
"\n",
"for",
"_",
",",
"r",
":=",
"range",
"s",
"{",
"if",
"t",
"==",
"\"",
"\"",
"{",
"if",
"unicode",
".",
"IsLetter",
"(",
"r",
")",
"||",
"r",
"==",
"'_'",
"{",
"t",
"+=",
"string",
"(",
"r",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"}",
"else",
"{",
"if",
"unicode",
".",
"IsLetter",
"(",
"r",
")",
"||",
"unicode",
".",
"IsDigit",
"(",
"r",
")",
"||",
"r",
"==",
"'_'",
"{",
"t",
"+=",
"string",
"(",
"r",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"}",
"\n",
"t",
"+=",
"\"",
"\"",
"\n",
"}",
"\n",
"if",
"t",
"==",
"\"",
"\"",
"{",
"t",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"return",
"t",
"\n",
"}"
] |
// sanitize cleans up a string to make a suitable package name.
|
[
"sanitize",
"cleans",
"up",
"a",
"string",
"to",
"make",
"a",
"suitable",
"package",
"name",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/mockgen.go#L215-L235
|
21,916
|
golang/mock
|
mockgen/mockgen.go
|
mockName
|
func (g *generator) mockName(typeName string) string {
if mockName, ok := g.mockNames[typeName]; ok {
return mockName
}
return "Mock" + typeName
}
|
go
|
func (g *generator) mockName(typeName string) string {
if mockName, ok := g.mockNames[typeName]; ok {
return mockName
}
return "Mock" + typeName
}
|
[
"func",
"(",
"g",
"*",
"generator",
")",
"mockName",
"(",
"typeName",
"string",
")",
"string",
"{",
"if",
"mockName",
",",
"ok",
":=",
"g",
".",
"mockNames",
"[",
"typeName",
"]",
";",
"ok",
"{",
"return",
"mockName",
"\n",
"}",
"\n\n",
"return",
"\"",
"\"",
"+",
"typeName",
"\n",
"}"
] |
// The name of the mock type to use for the given interface identifier.
|
[
"The",
"name",
"of",
"the",
"mock",
"type",
"to",
"use",
"for",
"the",
"given",
"interface",
"identifier",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/mockgen.go#L330-L336
|
21,917
|
golang/mock
|
mockgen/mockgen.go
|
GenerateMockMethod
|
func (g *generator) GenerateMockMethod(mockType string, m *model.Method, pkgOverride string) error {
argNames := g.getArgNames(m)
argTypes := g.getArgTypes(m, pkgOverride)
argString := makeArgString(argNames, argTypes)
rets := make([]string, len(m.Out))
for i, p := range m.Out {
rets[i] = p.Type.String(g.packageMap, pkgOverride)
}
retString := strings.Join(rets, ", ")
if len(rets) > 1 {
retString = "(" + retString + ")"
}
if retString != "" {
retString = " " + retString
}
ia := newIdentifierAllocator(argNames)
idRecv := ia.allocateIdentifier("m")
g.p("// %v mocks base method", m.Name)
g.p("func (%v *%v) %v(%v)%v {", idRecv, mockType, m.Name, argString, retString)
g.in()
g.p("%s.ctrl.T.Helper()", idRecv)
var callArgs string
if m.Variadic == nil {
if len(argNames) > 0 {
callArgs = ", " + strings.Join(argNames, ", ")
}
} else {
// Non-trivial. The generated code must build a []interface{},
// but the variadic argument may be any type.
idVarArgs := ia.allocateIdentifier("varargs")
idVArg := ia.allocateIdentifier("a")
g.p("%s := []interface{}{%s}", idVarArgs, strings.Join(argNames[:len(argNames)-1], ", "))
g.p("for _, %s := range %s {", idVArg, argNames[len(argNames)-1])
g.in()
g.p("%s = append(%s, %s)", idVarArgs, idVarArgs, idVArg)
g.out()
g.p("}")
callArgs = ", " + idVarArgs + "..."
}
if len(m.Out) == 0 {
g.p(`%v.ctrl.Call(%v, %q%v)`, idRecv, idRecv, m.Name, callArgs)
} else {
idRet := ia.allocateIdentifier("ret")
g.p(`%v := %v.ctrl.Call(%v, %q%v)`, idRet, idRecv, idRecv, m.Name, callArgs)
// Go does not allow "naked" type assertions on nil values, so we use the two-value form here.
// The value of that is either (x.(T), true) or (Z, false), where Z is the zero value for T.
// Happily, this coincides with the semantics we want here.
retNames := make([]string, len(rets))
for i, t := range rets {
retNames[i] = ia.allocateIdentifier(fmt.Sprintf("ret%d", i))
g.p("%s, _ := %s[%d].(%s)", retNames[i], idRet, i, t)
}
g.p("return " + strings.Join(retNames, ", "))
}
g.out()
g.p("}")
return nil
}
|
go
|
func (g *generator) GenerateMockMethod(mockType string, m *model.Method, pkgOverride string) error {
argNames := g.getArgNames(m)
argTypes := g.getArgTypes(m, pkgOverride)
argString := makeArgString(argNames, argTypes)
rets := make([]string, len(m.Out))
for i, p := range m.Out {
rets[i] = p.Type.String(g.packageMap, pkgOverride)
}
retString := strings.Join(rets, ", ")
if len(rets) > 1 {
retString = "(" + retString + ")"
}
if retString != "" {
retString = " " + retString
}
ia := newIdentifierAllocator(argNames)
idRecv := ia.allocateIdentifier("m")
g.p("// %v mocks base method", m.Name)
g.p("func (%v *%v) %v(%v)%v {", idRecv, mockType, m.Name, argString, retString)
g.in()
g.p("%s.ctrl.T.Helper()", idRecv)
var callArgs string
if m.Variadic == nil {
if len(argNames) > 0 {
callArgs = ", " + strings.Join(argNames, ", ")
}
} else {
// Non-trivial. The generated code must build a []interface{},
// but the variadic argument may be any type.
idVarArgs := ia.allocateIdentifier("varargs")
idVArg := ia.allocateIdentifier("a")
g.p("%s := []interface{}{%s}", idVarArgs, strings.Join(argNames[:len(argNames)-1], ", "))
g.p("for _, %s := range %s {", idVArg, argNames[len(argNames)-1])
g.in()
g.p("%s = append(%s, %s)", idVarArgs, idVarArgs, idVArg)
g.out()
g.p("}")
callArgs = ", " + idVarArgs + "..."
}
if len(m.Out) == 0 {
g.p(`%v.ctrl.Call(%v, %q%v)`, idRecv, idRecv, m.Name, callArgs)
} else {
idRet := ia.allocateIdentifier("ret")
g.p(`%v := %v.ctrl.Call(%v, %q%v)`, idRet, idRecv, idRecv, m.Name, callArgs)
// Go does not allow "naked" type assertions on nil values, so we use the two-value form here.
// The value of that is either (x.(T), true) or (Z, false), where Z is the zero value for T.
// Happily, this coincides with the semantics we want here.
retNames := make([]string, len(rets))
for i, t := range rets {
retNames[i] = ia.allocateIdentifier(fmt.Sprintf("ret%d", i))
g.p("%s, _ := %s[%d].(%s)", retNames[i], idRet, i, t)
}
g.p("return " + strings.Join(retNames, ", "))
}
g.out()
g.p("}")
return nil
}
|
[
"func",
"(",
"g",
"*",
"generator",
")",
"GenerateMockMethod",
"(",
"mockType",
"string",
",",
"m",
"*",
"model",
".",
"Method",
",",
"pkgOverride",
"string",
")",
"error",
"{",
"argNames",
":=",
"g",
".",
"getArgNames",
"(",
"m",
")",
"\n",
"argTypes",
":=",
"g",
".",
"getArgTypes",
"(",
"m",
",",
"pkgOverride",
")",
"\n",
"argString",
":=",
"makeArgString",
"(",
"argNames",
",",
"argTypes",
")",
"\n\n",
"rets",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"m",
".",
"Out",
")",
")",
"\n",
"for",
"i",
",",
"p",
":=",
"range",
"m",
".",
"Out",
"{",
"rets",
"[",
"i",
"]",
"=",
"p",
".",
"Type",
".",
"String",
"(",
"g",
".",
"packageMap",
",",
"pkgOverride",
")",
"\n",
"}",
"\n",
"retString",
":=",
"strings",
".",
"Join",
"(",
"rets",
",",
"\"",
"\"",
")",
"\n",
"if",
"len",
"(",
"rets",
")",
">",
"1",
"{",
"retString",
"=",
"\"",
"\"",
"+",
"retString",
"+",
"\"",
"\"",
"\n",
"}",
"\n",
"if",
"retString",
"!=",
"\"",
"\"",
"{",
"retString",
"=",
"\"",
"\"",
"+",
"retString",
"\n",
"}",
"\n\n",
"ia",
":=",
"newIdentifierAllocator",
"(",
"argNames",
")",
"\n",
"idRecv",
":=",
"ia",
".",
"allocateIdentifier",
"(",
"\"",
"\"",
")",
"\n\n",
"g",
".",
"p",
"(",
"\"",
"\"",
",",
"m",
".",
"Name",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
",",
"idRecv",
",",
"mockType",
",",
"m",
".",
"Name",
",",
"argString",
",",
"retString",
")",
"\n",
"g",
".",
"in",
"(",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
",",
"idRecv",
")",
"\n\n",
"var",
"callArgs",
"string",
"\n",
"if",
"m",
".",
"Variadic",
"==",
"nil",
"{",
"if",
"len",
"(",
"argNames",
")",
">",
"0",
"{",
"callArgs",
"=",
"\"",
"\"",
"+",
"strings",
".",
"Join",
"(",
"argNames",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"// Non-trivial. The generated code must build a []interface{},",
"// but the variadic argument may be any type.",
"idVarArgs",
":=",
"ia",
".",
"allocateIdentifier",
"(",
"\"",
"\"",
")",
"\n",
"idVArg",
":=",
"ia",
".",
"allocateIdentifier",
"(",
"\"",
"\"",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
",",
"idVarArgs",
",",
"strings",
".",
"Join",
"(",
"argNames",
"[",
":",
"len",
"(",
"argNames",
")",
"-",
"1",
"]",
",",
"\"",
"\"",
")",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
",",
"idVArg",
",",
"argNames",
"[",
"len",
"(",
"argNames",
")",
"-",
"1",
"]",
")",
"\n",
"g",
".",
"in",
"(",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
",",
"idVarArgs",
",",
"idVarArgs",
",",
"idVArg",
")",
"\n",
"g",
".",
"out",
"(",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
")",
"\n",
"callArgs",
"=",
"\"",
"\"",
"+",
"idVarArgs",
"+",
"\"",
"\"",
"\n",
"}",
"\n",
"if",
"len",
"(",
"m",
".",
"Out",
")",
"==",
"0",
"{",
"g",
".",
"p",
"(",
"`%v.ctrl.Call(%v, %q%v)`",
",",
"idRecv",
",",
"idRecv",
",",
"m",
".",
"Name",
",",
"callArgs",
")",
"\n",
"}",
"else",
"{",
"idRet",
":=",
"ia",
".",
"allocateIdentifier",
"(",
"\"",
"\"",
")",
"\n",
"g",
".",
"p",
"(",
"`%v := %v.ctrl.Call(%v, %q%v)`",
",",
"idRet",
",",
"idRecv",
",",
"idRecv",
",",
"m",
".",
"Name",
",",
"callArgs",
")",
"\n\n",
"// Go does not allow \"naked\" type assertions on nil values, so we use the two-value form here.",
"// The value of that is either (x.(T), true) or (Z, false), where Z is the zero value for T.",
"// Happily, this coincides with the semantics we want here.",
"retNames",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"rets",
")",
")",
"\n",
"for",
"i",
",",
"t",
":=",
"range",
"rets",
"{",
"retNames",
"[",
"i",
"]",
"=",
"ia",
".",
"allocateIdentifier",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"i",
")",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
",",
"retNames",
"[",
"i",
"]",
",",
"idRet",
",",
"i",
",",
"t",
")",
"\n",
"}",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
"+",
"strings",
".",
"Join",
"(",
"retNames",
",",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n\n",
"g",
".",
"out",
"(",
")",
"\n",
"g",
".",
"p",
"(",
"\"",
"\"",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// GenerateMockMethod generates a mock method implementation.
// If non-empty, pkgOverride is the package in which unqualified types reside.
|
[
"GenerateMockMethod",
"generates",
"a",
"mock",
"method",
"implementation",
".",
"If",
"non",
"-",
"empty",
"pkgOverride",
"is",
"the",
"package",
"in",
"which",
"unqualified",
"types",
"reside",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/mockgen.go#L411-L474
|
21,918
|
golang/mock
|
mockgen/mockgen.go
|
Output
|
func (g *generator) Output() []byte {
src, err := format.Source(g.buf.Bytes())
if err != nil {
log.Fatalf("Failed to format generated source code: %s\n%s", err, g.buf.String())
}
return src
}
|
go
|
func (g *generator) Output() []byte {
src, err := format.Source(g.buf.Bytes())
if err != nil {
log.Fatalf("Failed to format generated source code: %s\n%s", err, g.buf.String())
}
return src
}
|
[
"func",
"(",
"g",
"*",
"generator",
")",
"Output",
"(",
")",
"[",
"]",
"byte",
"{",
"src",
",",
"err",
":=",
"format",
".",
"Source",
"(",
"g",
".",
"buf",
".",
"Bytes",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"log",
".",
"Fatalf",
"(",
"\"",
"\\n",
"\"",
",",
"err",
",",
"g",
".",
"buf",
".",
"String",
"(",
")",
")",
"\n",
"}",
"\n",
"return",
"src",
"\n",
"}"
] |
// Output returns the generator's output, formatted in the standard Go style.
|
[
"Output",
"returns",
"the",
"generator",
"s",
"output",
"formatted",
"in",
"the",
"standard",
"Go",
"style",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/mockgen.go#L582-L588
|
21,919
|
golang/mock
|
gomock/callset.go
|
Add
|
func (cs callSet) Add(call *Call) {
key := callSetKey{call.receiver, call.method}
m := cs.expected
if call.exhausted() {
m = cs.exhausted
}
m[key] = append(m[key], call)
}
|
go
|
func (cs callSet) Add(call *Call) {
key := callSetKey{call.receiver, call.method}
m := cs.expected
if call.exhausted() {
m = cs.exhausted
}
m[key] = append(m[key], call)
}
|
[
"func",
"(",
"cs",
"callSet",
")",
"Add",
"(",
"call",
"*",
"Call",
")",
"{",
"key",
":=",
"callSetKey",
"{",
"call",
".",
"receiver",
",",
"call",
".",
"method",
"}",
"\n",
"m",
":=",
"cs",
".",
"expected",
"\n",
"if",
"call",
".",
"exhausted",
"(",
")",
"{",
"m",
"=",
"cs",
".",
"exhausted",
"\n",
"}",
"\n",
"m",
"[",
"key",
"]",
"=",
"append",
"(",
"m",
"[",
"key",
"]",
",",
"call",
")",
"\n",
"}"
] |
// Add adds a new expected call.
|
[
"Add",
"adds",
"a",
"new",
"expected",
"call",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/callset.go#L42-L49
|
21,920
|
golang/mock
|
gomock/callset.go
|
Remove
|
func (cs callSet) Remove(call *Call) {
key := callSetKey{call.receiver, call.method}
calls := cs.expected[key]
for i, c := range calls {
if c == call {
// maintain order for remaining calls
cs.expected[key] = append(calls[:i], calls[i+1:]...)
cs.exhausted[key] = append(cs.exhausted[key], call)
break
}
}
}
|
go
|
func (cs callSet) Remove(call *Call) {
key := callSetKey{call.receiver, call.method}
calls := cs.expected[key]
for i, c := range calls {
if c == call {
// maintain order for remaining calls
cs.expected[key] = append(calls[:i], calls[i+1:]...)
cs.exhausted[key] = append(cs.exhausted[key], call)
break
}
}
}
|
[
"func",
"(",
"cs",
"callSet",
")",
"Remove",
"(",
"call",
"*",
"Call",
")",
"{",
"key",
":=",
"callSetKey",
"{",
"call",
".",
"receiver",
",",
"call",
".",
"method",
"}",
"\n",
"calls",
":=",
"cs",
".",
"expected",
"[",
"key",
"]",
"\n",
"for",
"i",
",",
"c",
":=",
"range",
"calls",
"{",
"if",
"c",
"==",
"call",
"{",
"// maintain order for remaining calls",
"cs",
".",
"expected",
"[",
"key",
"]",
"=",
"append",
"(",
"calls",
"[",
":",
"i",
"]",
",",
"calls",
"[",
"i",
"+",
"1",
":",
"]",
"...",
")",
"\n",
"cs",
".",
"exhausted",
"[",
"key",
"]",
"=",
"append",
"(",
"cs",
".",
"exhausted",
"[",
"key",
"]",
",",
"call",
")",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Remove removes an expected call.
|
[
"Remove",
"removes",
"an",
"expected",
"call",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/callset.go#L52-L63
|
21,921
|
golang/mock
|
gomock/callset.go
|
FindMatch
|
func (cs callSet) FindMatch(receiver interface{}, method string, args []interface{}) (*Call, error) {
key := callSetKey{receiver, method}
// Search through the expected calls.
expected := cs.expected[key]
var callsErrors bytes.Buffer
for _, call := range expected {
err := call.matches(args)
if err != nil {
fmt.Fprintf(&callsErrors, "\n%v", err)
} else {
return call, nil
}
}
// If we haven't found a match then search through the exhausted calls so we
// get useful error messages.
exhausted := cs.exhausted[key]
for _, call := range exhausted {
if err := call.matches(args); err != nil {
fmt.Fprintf(&callsErrors, "\n%v", err)
}
}
if len(expected)+len(exhausted) == 0 {
fmt.Fprintf(&callsErrors, "there are no expected calls of the method %q for that receiver", method)
}
return nil, fmt.Errorf(callsErrors.String())
}
|
go
|
func (cs callSet) FindMatch(receiver interface{}, method string, args []interface{}) (*Call, error) {
key := callSetKey{receiver, method}
// Search through the expected calls.
expected := cs.expected[key]
var callsErrors bytes.Buffer
for _, call := range expected {
err := call.matches(args)
if err != nil {
fmt.Fprintf(&callsErrors, "\n%v", err)
} else {
return call, nil
}
}
// If we haven't found a match then search through the exhausted calls so we
// get useful error messages.
exhausted := cs.exhausted[key]
for _, call := range exhausted {
if err := call.matches(args); err != nil {
fmt.Fprintf(&callsErrors, "\n%v", err)
}
}
if len(expected)+len(exhausted) == 0 {
fmt.Fprintf(&callsErrors, "there are no expected calls of the method %q for that receiver", method)
}
return nil, fmt.Errorf(callsErrors.String())
}
|
[
"func",
"(",
"cs",
"callSet",
")",
"FindMatch",
"(",
"receiver",
"interface",
"{",
"}",
",",
"method",
"string",
",",
"args",
"[",
"]",
"interface",
"{",
"}",
")",
"(",
"*",
"Call",
",",
"error",
")",
"{",
"key",
":=",
"callSetKey",
"{",
"receiver",
",",
"method",
"}",
"\n\n",
"// Search through the expected calls.",
"expected",
":=",
"cs",
".",
"expected",
"[",
"key",
"]",
"\n",
"var",
"callsErrors",
"bytes",
".",
"Buffer",
"\n",
"for",
"_",
",",
"call",
":=",
"range",
"expected",
"{",
"err",
":=",
"call",
".",
"matches",
"(",
"args",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"fmt",
".",
"Fprintf",
"(",
"&",
"callsErrors",
",",
"\"",
"\\n",
"\"",
",",
"err",
")",
"\n",
"}",
"else",
"{",
"return",
"call",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"// If we haven't found a match then search through the exhausted calls so we",
"// get useful error messages.",
"exhausted",
":=",
"cs",
".",
"exhausted",
"[",
"key",
"]",
"\n",
"for",
"_",
",",
"call",
":=",
"range",
"exhausted",
"{",
"if",
"err",
":=",
"call",
".",
"matches",
"(",
"args",
")",
";",
"err",
"!=",
"nil",
"{",
"fmt",
".",
"Fprintf",
"(",
"&",
"callsErrors",
",",
"\"",
"\\n",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"expected",
")",
"+",
"len",
"(",
"exhausted",
")",
"==",
"0",
"{",
"fmt",
".",
"Fprintf",
"(",
"&",
"callsErrors",
",",
"\"",
"\"",
",",
"method",
")",
"\n",
"}",
"\n\n",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"callsErrors",
".",
"String",
"(",
")",
")",
"\n",
"}"
] |
// FindMatch searches for a matching call. Returns error with explanation message if no call matched.
|
[
"FindMatch",
"searches",
"for",
"a",
"matching",
"call",
".",
"Returns",
"error",
"with",
"explanation",
"message",
"if",
"no",
"call",
"matched",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/callset.go#L66-L95
|
21,922
|
golang/mock
|
gomock/callset.go
|
Failures
|
func (cs callSet) Failures() []*Call {
failures := make([]*Call, 0, len(cs.expected))
for _, calls := range cs.expected {
for _, call := range calls {
if !call.satisfied() {
failures = append(failures, call)
}
}
}
return failures
}
|
go
|
func (cs callSet) Failures() []*Call {
failures := make([]*Call, 0, len(cs.expected))
for _, calls := range cs.expected {
for _, call := range calls {
if !call.satisfied() {
failures = append(failures, call)
}
}
}
return failures
}
|
[
"func",
"(",
"cs",
"callSet",
")",
"Failures",
"(",
")",
"[",
"]",
"*",
"Call",
"{",
"failures",
":=",
"make",
"(",
"[",
"]",
"*",
"Call",
",",
"0",
",",
"len",
"(",
"cs",
".",
"expected",
")",
")",
"\n",
"for",
"_",
",",
"calls",
":=",
"range",
"cs",
".",
"expected",
"{",
"for",
"_",
",",
"call",
":=",
"range",
"calls",
"{",
"if",
"!",
"call",
".",
"satisfied",
"(",
")",
"{",
"failures",
"=",
"append",
"(",
"failures",
",",
"call",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"failures",
"\n",
"}"
] |
// Failures returns the calls that are not satisfied.
|
[
"Failures",
"returns",
"the",
"calls",
"that",
"are",
"not",
"satisfied",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/callset.go#L98-L108
|
21,923
|
golang/mock
|
sample/mock_user/mock_user.go
|
NewMockIndex
|
func NewMockIndex(ctrl *gomock.Controller) *MockIndex {
mock := &MockIndex{ctrl: ctrl}
mock.recorder = &MockIndexMockRecorder{mock}
return mock
}
|
go
|
func NewMockIndex(ctrl *gomock.Controller) *MockIndex {
mock := &MockIndex{ctrl: ctrl}
mock.recorder = &MockIndexMockRecorder{mock}
return mock
}
|
[
"func",
"NewMockIndex",
"(",
"ctrl",
"*",
"gomock",
".",
"Controller",
")",
"*",
"MockIndex",
"{",
"mock",
":=",
"&",
"MockIndex",
"{",
"ctrl",
":",
"ctrl",
"}",
"\n",
"mock",
".",
"recorder",
"=",
"&",
"MockIndexMockRecorder",
"{",
"mock",
"}",
"\n",
"return",
"mock",
"\n",
"}"
] |
// NewMockIndex creates a new mock instance
|
[
"NewMockIndex",
"creates",
"a",
"new",
"mock",
"instance"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L35-L39
|
21,924
|
golang/mock
|
sample/mock_user/mock_user.go
|
Anon
|
func (m *MockIndex) Anon(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Anon", arg0)
}
|
go
|
func (m *MockIndex) Anon(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Anon", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Anon",
"(",
"arg0",
"string",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// Anon mocks base method
|
[
"Anon",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L47-L50
|
21,925
|
golang/mock
|
sample/mock_user/mock_user.go
|
Anon
|
func (mr *MockIndexMockRecorder) Anon(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Anon", reflect.TypeOf((*MockIndex)(nil).Anon), arg0)
}
|
go
|
func (mr *MockIndexMockRecorder) Anon(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Anon", reflect.TypeOf((*MockIndex)(nil).Anon), arg0)
}
|
[
"func",
"(",
"mr",
"*",
"MockIndexMockRecorder",
")",
"Anon",
"(",
"arg0",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"mr",
".",
"mock",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockIndex",
")",
"(",
"nil",
")",
".",
"Anon",
")",
",",
"arg0",
")",
"\n",
"}"
] |
// Anon indicates an expected call of Anon
|
[
"Anon",
"indicates",
"an",
"expected",
"call",
"of",
"Anon"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L53-L56
|
21,926
|
golang/mock
|
sample/mock_user/mock_user.go
|
Chan
|
func (m *MockIndex) Chan(arg0 chan int, arg1 chan<- hash.Hash) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Chan", arg0, arg1)
}
|
go
|
func (m *MockIndex) Chan(arg0 chan int, arg1 chan<- hash.Hash) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Chan", arg0, arg1)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Chan",
"(",
"arg0",
"chan",
"int",
",",
"arg1",
"chan",
"<-",
"hash",
".",
"Hash",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
",",
"arg1",
")",
"\n",
"}"
] |
// Chan mocks base method
|
[
"Chan",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L59-L62
|
21,927
|
golang/mock
|
sample/mock_user/mock_user.go
|
ConcreteRet
|
func (m *MockIndex) ConcreteRet() chan<- bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ConcreteRet")
ret0, _ := ret[0].(chan<- bool)
return ret0
}
|
go
|
func (m *MockIndex) ConcreteRet() chan<- bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ConcreteRet")
ret0, _ := ret[0].(chan<- bool)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"ConcreteRet",
"(",
")",
"chan",
"<-",
"bool",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"chan",
"<-",
"bool",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// ConcreteRet mocks base method
|
[
"ConcreteRet",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L71-L76
|
21,928
|
golang/mock
|
sample/mock_user/mock_user.go
|
Ellip
|
func (mr *MockIndexMockRecorder) Ellip(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
varargs := append([]interface{}{arg0}, arg1...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ellip", reflect.TypeOf((*MockIndex)(nil).Ellip), varargs...)
}
|
go
|
func (mr *MockIndexMockRecorder) Ellip(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
varargs := append([]interface{}{arg0}, arg1...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ellip", reflect.TypeOf((*MockIndex)(nil).Ellip), varargs...)
}
|
[
"func",
"(",
"mr",
"*",
"MockIndexMockRecorder",
")",
"Ellip",
"(",
"arg0",
"interface",
"{",
"}",
",",
"arg1",
"...",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"mr",
".",
"mock",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"varargs",
":=",
"append",
"(",
"[",
"]",
"interface",
"{",
"}",
"{",
"arg0",
"}",
",",
"arg1",
"...",
")",
"\n",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockIndex",
")",
"(",
"nil",
")",
".",
"Ellip",
")",
",",
"varargs",
"...",
")",
"\n",
"}"
] |
// Ellip indicates an expected call of Ellip
|
[
"Ellip",
"indicates",
"an",
"expected",
"call",
"of",
"Ellip"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L95-L99
|
21,929
|
golang/mock
|
sample/mock_user/mock_user.go
|
EllipOnly
|
func (m *MockIndex) EllipOnly(arg0 ...string) {
m.ctrl.T.Helper()
varargs := []interface{}{}
for _, a := range arg0 {
varargs = append(varargs, a)
}
m.ctrl.Call(m, "EllipOnly", varargs...)
}
|
go
|
func (m *MockIndex) EllipOnly(arg0 ...string) {
m.ctrl.T.Helper()
varargs := []interface{}{}
for _, a := range arg0 {
varargs = append(varargs, a)
}
m.ctrl.Call(m, "EllipOnly", varargs...)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"EllipOnly",
"(",
"arg0",
"...",
"string",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"varargs",
":=",
"[",
"]",
"interface",
"{",
"}",
"{",
"}",
"\n",
"for",
"_",
",",
"a",
":=",
"range",
"arg0",
"{",
"varargs",
"=",
"append",
"(",
"varargs",
",",
"a",
")",
"\n",
"}",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"varargs",
"...",
")",
"\n",
"}"
] |
// EllipOnly mocks base method
|
[
"EllipOnly",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L102-L109
|
21,930
|
golang/mock
|
sample/mock_user/mock_user.go
|
ForeignFour
|
func (m *MockIndex) ForeignFour(arg0 imp4.Imp4) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignFour", arg0)
}
|
go
|
func (m *MockIndex) ForeignFour(arg0 imp4.Imp4) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignFour", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"ForeignFour",
"(",
"arg0",
"imp4",
".",
"Imp4",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// ForeignFour mocks base method
|
[
"ForeignFour",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L118-L121
|
21,931
|
golang/mock
|
sample/mock_user/mock_user.go
|
ForeignOne
|
func (m *MockIndex) ForeignOne(arg0 imp1.Imp1) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignOne", arg0)
}
|
go
|
func (m *MockIndex) ForeignOne(arg0 imp1.Imp1) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignOne", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"ForeignOne",
"(",
"arg0",
"imp1",
".",
"Imp1",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// ForeignOne mocks base method
|
[
"ForeignOne",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L130-L133
|
21,932
|
golang/mock
|
sample/mock_user/mock_user.go
|
ForeignThree
|
func (m *MockIndex) ForeignThree(arg0 imp3.Imp3) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignThree", arg0)
}
|
go
|
func (m *MockIndex) ForeignThree(arg0 imp3.Imp3) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignThree", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"ForeignThree",
"(",
"arg0",
"imp3",
".",
"Imp3",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// ForeignThree mocks base method
|
[
"ForeignThree",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L142-L145
|
21,933
|
golang/mock
|
sample/mock_user/mock_user.go
|
ForeignTwo
|
func (m *MockIndex) ForeignTwo(arg0 imp2.Imp2) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignTwo", arg0)
}
|
go
|
func (m *MockIndex) ForeignTwo(arg0 imp2.Imp2) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ForeignTwo", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"ForeignTwo",
"(",
"arg0",
"imp2",
".",
"Imp2",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// ForeignTwo mocks base method
|
[
"ForeignTwo",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L154-L157
|
21,934
|
golang/mock
|
sample/mock_user/mock_user.go
|
Func
|
func (m *MockIndex) Func(arg0 func(http.Request) (int, bool)) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Func", arg0)
}
|
go
|
func (m *MockIndex) Func(arg0 func(http.Request) (int, bool)) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Func", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Func",
"(",
"arg0",
"func",
"(",
"http",
".",
"Request",
")",
"(",
"int",
",",
"bool",
")",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// Func mocks base method
|
[
"Func",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L166-L169
|
21,935
|
golang/mock
|
sample/mock_user/mock_user.go
|
GetTwo
|
func (m *MockIndex) GetTwo(arg0, arg1 string) (interface{}, interface{}) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetTwo", arg0, arg1)
ret0, _ := ret[0].(interface{})
ret1, _ := ret[1].(interface{})
return ret0, ret1
}
|
go
|
func (m *MockIndex) GetTwo(arg0, arg1 string) (interface{}, interface{}) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetTwo", arg0, arg1)
ret0, _ := ret[0].(interface{})
ret1, _ := ret[1].(interface{})
return ret0, ret1
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"GetTwo",
"(",
"arg0",
",",
"arg1",
"string",
")",
"(",
"interface",
"{",
"}",
",",
"interface",
"{",
"}",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
",",
"arg1",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"interface",
"{",
"}",
")",
"\n",
"ret1",
",",
"_",
":=",
"ret",
"[",
"1",
"]",
".",
"(",
"interface",
"{",
"}",
")",
"\n",
"return",
"ret0",
",",
"ret1",
"\n",
"}"
] |
// GetTwo mocks base method
|
[
"GetTwo",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L192-L198
|
21,936
|
golang/mock
|
sample/mock_user/mock_user.go
|
Map
|
func (m *MockIndex) Map(arg0 map[int]hash.Hash) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Map", arg0)
}
|
go
|
func (m *MockIndex) Map(arg0 map[int]hash.Hash) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Map", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Map",
"(",
"arg0",
"map",
"[",
"int",
"]",
"hash",
".",
"Hash",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// Map mocks base method
|
[
"Map",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L207-L210
|
21,937
|
golang/mock
|
sample/mock_user/mock_user.go
|
NillableRet
|
func (m *MockIndex) NillableRet() error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "NillableRet")
ret0, _ := ret[0].(error)
return ret0
}
|
go
|
func (m *MockIndex) NillableRet() error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "NillableRet")
ret0, _ := ret[0].(error)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"NillableRet",
"(",
")",
"error",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"error",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// NillableRet mocks base method
|
[
"NillableRet",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L219-L224
|
21,938
|
golang/mock
|
sample/mock_user/mock_user.go
|
Other
|
func (m *MockIndex) Other() hash.Hash {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Other")
ret0, _ := ret[0].(hash.Hash)
return ret0
}
|
go
|
func (m *MockIndex) Other() hash.Hash {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Other")
ret0, _ := ret[0].(hash.Hash)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Other",
"(",
")",
"hash",
".",
"Hash",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"hash",
".",
"Hash",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// Other mocks base method
|
[
"Other",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L233-L238
|
21,939
|
golang/mock
|
sample/mock_user/mock_user.go
|
Ptr
|
func (m *MockIndex) Ptr(arg0 *int) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Ptr", arg0)
}
|
go
|
func (m *MockIndex) Ptr(arg0 *int) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Ptr", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Ptr",
"(",
"arg0",
"*",
"int",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// Ptr mocks base method
|
[
"Ptr",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L247-L250
|
21,940
|
golang/mock
|
sample/mock_user/mock_user.go
|
Slice
|
func (m *MockIndex) Slice(arg0 []int, arg1 []byte) [3]int {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Slice", arg0, arg1)
ret0, _ := ret[0].([3]int)
return ret0
}
|
go
|
func (m *MockIndex) Slice(arg0 []int, arg1 []byte) [3]int {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Slice", arg0, arg1)
ret0, _ := ret[0].([3]int)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Slice",
"(",
"arg0",
"[",
"]",
"int",
",",
"arg1",
"[",
"]",
"byte",
")",
"[",
"3",
"]",
"int",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
",",
"arg1",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"[",
"3",
"]",
"int",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// Slice mocks base method
|
[
"Slice",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L271-L276
|
21,941
|
golang/mock
|
sample/mock_user/mock_user.go
|
Struct
|
func (m *MockIndex) Struct(arg0 struct{}) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Struct", arg0)
}
|
go
|
func (m *MockIndex) Struct(arg0 struct{}) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Struct", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Struct",
"(",
"arg0",
"struct",
"{",
"}",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// Struct mocks base method
|
[
"Struct",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L285-L288
|
21,942
|
golang/mock
|
sample/mock_user/mock_user.go
|
StructChan
|
func (m *MockIndex) StructChan(arg0 chan struct{}) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "StructChan", arg0)
}
|
go
|
func (m *MockIndex) StructChan(arg0 chan struct{}) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "StructChan", arg0)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"StructChan",
"(",
"arg0",
"chan",
"struct",
"{",
"}",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"}"
] |
// StructChan mocks base method
|
[
"StructChan",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L297-L300
|
21,943
|
golang/mock
|
sample/mock_user/mock_user.go
|
Summary
|
func (m *MockIndex) Summary(arg0 *bytes.Buffer, arg1 io.Writer) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Summary", arg0, arg1)
}
|
go
|
func (m *MockIndex) Summary(arg0 *bytes.Buffer, arg1 io.Writer) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Summary", arg0, arg1)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Summary",
"(",
"arg0",
"*",
"bytes",
".",
"Buffer",
",",
"arg1",
"io",
".",
"Writer",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
",",
"arg1",
")",
"\n",
"}"
] |
// Summary mocks base method
|
[
"Summary",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L309-L312
|
21,944
|
golang/mock
|
sample/mock_user/mock_user.go
|
Templates
|
func (m *MockIndex) Templates(arg0 template.CSS, arg1 template0.FuncMap) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Templates", arg0, arg1)
}
|
go
|
func (m *MockIndex) Templates(arg0 template.CSS, arg1 template0.FuncMap) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Templates", arg0, arg1)
}
|
[
"func",
"(",
"m",
"*",
"MockIndex",
")",
"Templates",
"(",
"arg0",
"template",
".",
"CSS",
",",
"arg1",
"template0",
".",
"FuncMap",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
",",
"arg1",
")",
"\n",
"}"
] |
// Templates mocks base method
|
[
"Templates",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L321-L324
|
21,945
|
golang/mock
|
sample/mock_user/mock_user.go
|
NewMockEmbed
|
func NewMockEmbed(ctrl *gomock.Controller) *MockEmbed {
mock := &MockEmbed{ctrl: ctrl}
mock.recorder = &MockEmbedMockRecorder{mock}
return mock
}
|
go
|
func NewMockEmbed(ctrl *gomock.Controller) *MockEmbed {
mock := &MockEmbed{ctrl: ctrl}
mock.recorder = &MockEmbedMockRecorder{mock}
return mock
}
|
[
"func",
"NewMockEmbed",
"(",
"ctrl",
"*",
"gomock",
".",
"Controller",
")",
"*",
"MockEmbed",
"{",
"mock",
":=",
"&",
"MockEmbed",
"{",
"ctrl",
":",
"ctrl",
"}",
"\n",
"mock",
".",
"recorder",
"=",
"&",
"MockEmbedMockRecorder",
"{",
"mock",
"}",
"\n",
"return",
"mock",
"\n",
"}"
] |
// NewMockEmbed creates a new mock instance
|
[
"NewMockEmbed",
"creates",
"a",
"new",
"mock",
"instance"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L344-L348
|
21,946
|
golang/mock
|
sample/mock_user/mock_user.go
|
ForeignEmbeddedMethod
|
func (m *MockEmbed) ForeignEmbeddedMethod() *bufio.Reader {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ForeignEmbeddedMethod")
ret0, _ := ret[0].(*bufio.Reader)
return ret0
}
|
go
|
func (m *MockEmbed) ForeignEmbeddedMethod() *bufio.Reader {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ForeignEmbeddedMethod")
ret0, _ := ret[0].(*bufio.Reader)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockEmbed",
")",
"ForeignEmbeddedMethod",
"(",
")",
"*",
"bufio",
".",
"Reader",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"*",
"bufio",
".",
"Reader",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// ForeignEmbeddedMethod mocks base method
|
[
"ForeignEmbeddedMethod",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L368-L373
|
21,947
|
golang/mock
|
sample/mock_user/mock_user.go
|
ForeignEmbeddedMethod
|
func (mr *MockEmbedMockRecorder) ForeignEmbeddedMethod() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForeignEmbeddedMethod", reflect.TypeOf((*MockEmbed)(nil).ForeignEmbeddedMethod))
}
|
go
|
func (mr *MockEmbedMockRecorder) ForeignEmbeddedMethod() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForeignEmbeddedMethod", reflect.TypeOf((*MockEmbed)(nil).ForeignEmbeddedMethod))
}
|
[
"func",
"(",
"mr",
"*",
"MockEmbedMockRecorder",
")",
"ForeignEmbeddedMethod",
"(",
")",
"*",
"gomock",
".",
"Call",
"{",
"mr",
".",
"mock",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockEmbed",
")",
"(",
"nil",
")",
".",
"ForeignEmbeddedMethod",
")",
")",
"\n",
"}"
] |
// ForeignEmbeddedMethod indicates an expected call of ForeignEmbeddedMethod
|
[
"ForeignEmbeddedMethod",
"indicates",
"an",
"expected",
"call",
"of",
"ForeignEmbeddedMethod"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L376-L379
|
21,948
|
golang/mock
|
sample/mock_user/mock_user.go
|
ImplicitPackage
|
func (m *MockEmbed) ImplicitPackage(arg0 string, arg1 imp1.ImpT, arg2 []imp1.ImpT, arg3 *imp1.ImpT, arg4 chan imp1.ImpT) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ImplicitPackage", arg0, arg1, arg2, arg3, arg4)
}
|
go
|
func (m *MockEmbed) ImplicitPackage(arg0 string, arg1 imp1.ImpT, arg2 []imp1.ImpT, arg3 *imp1.ImpT, arg4 chan imp1.ImpT) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "ImplicitPackage", arg0, arg1, arg2, arg3, arg4)
}
|
[
"func",
"(",
"m",
"*",
"MockEmbed",
")",
"ImplicitPackage",
"(",
"arg0",
"string",
",",
"arg1",
"imp1",
".",
"ImpT",
",",
"arg2",
"[",
"]",
"imp1",
".",
"ImpT",
",",
"arg3",
"*",
"imp1",
".",
"ImpT",
",",
"arg4",
"chan",
"imp1",
".",
"ImpT",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
",",
"arg1",
",",
"arg2",
",",
"arg3",
",",
"arg4",
")",
"\n",
"}"
] |
// ImplicitPackage mocks base method
|
[
"ImplicitPackage",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L382-L385
|
21,949
|
golang/mock
|
sample/mock_user/mock_user.go
|
ImplicitPackage
|
func (mr *MockEmbedMockRecorder) ImplicitPackage(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ImplicitPackage", reflect.TypeOf((*MockEmbed)(nil).ImplicitPackage), arg0, arg1, arg2, arg3, arg4)
}
|
go
|
func (mr *MockEmbedMockRecorder) ImplicitPackage(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ImplicitPackage", reflect.TypeOf((*MockEmbed)(nil).ImplicitPackage), arg0, arg1, arg2, arg3, arg4)
}
|
[
"func",
"(",
"mr",
"*",
"MockEmbedMockRecorder",
")",
"ImplicitPackage",
"(",
"arg0",
",",
"arg1",
",",
"arg2",
",",
"arg3",
",",
"arg4",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"mr",
".",
"mock",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockEmbed",
")",
"(",
"nil",
")",
".",
"ImplicitPackage",
")",
",",
"arg0",
",",
"arg1",
",",
"arg2",
",",
"arg3",
",",
"arg4",
")",
"\n",
"}"
] |
// ImplicitPackage indicates an expected call of ImplicitPackage
|
[
"ImplicitPackage",
"indicates",
"an",
"expected",
"call",
"of",
"ImplicitPackage"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L388-L391
|
21,950
|
golang/mock
|
sample/mock_user/mock_user.go
|
RegularMethod
|
func (m *MockEmbed) RegularMethod() {
m.ctrl.T.Helper()
m.ctrl.Call(m, "RegularMethod")
}
|
go
|
func (m *MockEmbed) RegularMethod() {
m.ctrl.T.Helper()
m.ctrl.Call(m, "RegularMethod")
}
|
[
"func",
"(",
"m",
"*",
"MockEmbed",
")",
"RegularMethod",
"(",
")",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
")",
"\n",
"}"
] |
// RegularMethod mocks base method
|
[
"RegularMethod",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L394-L397
|
21,951
|
golang/mock
|
sample/mock_user/mock_user.go
|
NewMockEmbedded
|
func NewMockEmbedded(ctrl *gomock.Controller) *MockEmbedded {
mock := &MockEmbedded{ctrl: ctrl}
mock.recorder = &MockEmbeddedMockRecorder{mock}
return mock
}
|
go
|
func NewMockEmbedded(ctrl *gomock.Controller) *MockEmbedded {
mock := &MockEmbedded{ctrl: ctrl}
mock.recorder = &MockEmbeddedMockRecorder{mock}
return mock
}
|
[
"func",
"NewMockEmbedded",
"(",
"ctrl",
"*",
"gomock",
".",
"Controller",
")",
"*",
"MockEmbedded",
"{",
"mock",
":=",
"&",
"MockEmbedded",
"{",
"ctrl",
":",
"ctrl",
"}",
"\n",
"mock",
".",
"recorder",
"=",
"&",
"MockEmbeddedMockRecorder",
"{",
"mock",
"}",
"\n",
"return",
"mock",
"\n",
"}"
] |
// NewMockEmbedded creates a new mock instance
|
[
"NewMockEmbedded",
"creates",
"a",
"new",
"mock",
"instance"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L417-L421
|
21,952
|
golang/mock
|
sample/mock_user/mock_user.go
|
EmbeddedMethod
|
func (mr *MockEmbeddedMockRecorder) EmbeddedMethod() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EmbeddedMethod", reflect.TypeOf((*MockEmbedded)(nil).EmbeddedMethod))
}
|
go
|
func (mr *MockEmbeddedMockRecorder) EmbeddedMethod() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EmbeddedMethod", reflect.TypeOf((*MockEmbedded)(nil).EmbeddedMethod))
}
|
[
"func",
"(",
"mr",
"*",
"MockEmbeddedMockRecorder",
")",
"EmbeddedMethod",
"(",
")",
"*",
"gomock",
".",
"Call",
"{",
"mr",
".",
"mock",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockEmbedded",
")",
"(",
"nil",
")",
".",
"EmbeddedMethod",
")",
")",
"\n",
"}"
] |
// EmbeddedMethod indicates an expected call of EmbeddedMethod
|
[
"EmbeddedMethod",
"indicates",
"an",
"expected",
"call",
"of",
"EmbeddedMethod"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/sample/mock_user/mock_user.go#L435-L438
|
21,953
|
golang/mock
|
gomock/internal/mock_gomock/mock_matcher.go
|
NewMockMatcher
|
func NewMockMatcher(ctrl *gomock.Controller) *MockMatcher {
mock := &MockMatcher{ctrl: ctrl}
mock.recorder = &MockMatcherMockRecorder{mock}
return mock
}
|
go
|
func NewMockMatcher(ctrl *gomock.Controller) *MockMatcher {
mock := &MockMatcher{ctrl: ctrl}
mock.recorder = &MockMatcherMockRecorder{mock}
return mock
}
|
[
"func",
"NewMockMatcher",
"(",
"ctrl",
"*",
"gomock",
".",
"Controller",
")",
"*",
"MockMatcher",
"{",
"mock",
":=",
"&",
"MockMatcher",
"{",
"ctrl",
":",
"ctrl",
"}",
"\n",
"mock",
".",
"recorder",
"=",
"&",
"MockMatcherMockRecorder",
"{",
"mock",
"}",
"\n",
"return",
"mock",
"\n",
"}"
] |
// NewMockMatcher creates a new mock instance
|
[
"NewMockMatcher",
"creates",
"a",
"new",
"mock",
"instance"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/internal/mock_gomock/mock_matcher.go#L24-L28
|
21,954
|
golang/mock
|
gomock/internal/mock_gomock/mock_matcher.go
|
Matches
|
func (m *MockMatcher) Matches(arg0 interface{}) bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Matches", arg0)
ret0, _ := ret[0].(bool)
return ret0
}
|
go
|
func (m *MockMatcher) Matches(arg0 interface{}) bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Matches", arg0)
ret0, _ := ret[0].(bool)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockMatcher",
")",
"Matches",
"(",
"arg0",
"interface",
"{",
"}",
")",
"bool",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
",",
"arg0",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"bool",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// Matches mocks base method
|
[
"Matches",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/internal/mock_gomock/mock_matcher.go#L36-L41
|
21,955
|
golang/mock
|
gomock/internal/mock_gomock/mock_matcher.go
|
Matches
|
func (mr *MockMatcherMockRecorder) Matches(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Matches", reflect.TypeOf((*MockMatcher)(nil).Matches), arg0)
}
|
go
|
func (mr *MockMatcherMockRecorder) Matches(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Matches", reflect.TypeOf((*MockMatcher)(nil).Matches), arg0)
}
|
[
"func",
"(",
"mr",
"*",
"MockMatcherMockRecorder",
")",
"Matches",
"(",
"arg0",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"mr",
".",
"mock",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockMatcher",
")",
"(",
"nil",
")",
".",
"Matches",
")",
",",
"arg0",
")",
"\n",
"}"
] |
// Matches indicates an expected call of Matches
|
[
"Matches",
"indicates",
"an",
"expected",
"call",
"of",
"Matches"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/internal/mock_gomock/mock_matcher.go#L44-L47
|
21,956
|
golang/mock
|
gomock/internal/mock_gomock/mock_matcher.go
|
String
|
func (m *MockMatcher) String() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "String")
ret0, _ := ret[0].(string)
return ret0
}
|
go
|
func (m *MockMatcher) String() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "String")
ret0, _ := ret[0].(string)
return ret0
}
|
[
"func",
"(",
"m",
"*",
"MockMatcher",
")",
"String",
"(",
")",
"string",
"{",
"m",
".",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"",
"\"",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"string",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] |
// String mocks base method
|
[
"String",
"mocks",
"base",
"method"
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/internal/mock_gomock/mock_matcher.go#L50-L55
|
21,957
|
golang/mock
|
mockgen/parse.go
|
parseFile
|
func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Package, error) {
allImports, dotImports := importsOfFile(file)
// Don't stomp imports provided by -imports. Those should take precedence.
for pkg, path := range allImports {
if _, ok := p.imports[pkg]; !ok {
p.imports[pkg] = path
}
}
// Add imports from auxiliary files, which might be needed for embedded interfaces.
// Don't stomp any other imports.
for _, f := range p.auxFiles {
auxImports, _ := importsOfFile(f)
for pkg, path := range auxImports {
if _, ok := p.imports[pkg]; !ok {
p.imports[pkg] = path
}
}
}
var is []*model.Interface
for ni := range iterInterfaces(file) {
i, err := p.parseInterface(ni.name.String(), importPath, ni.it)
if err != nil {
return nil, err
}
is = append(is, i)
}
return &model.Package{
Name: file.Name.String(),
Interfaces: is,
DotImports: dotImports,
}, nil
}
|
go
|
func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Package, error) {
allImports, dotImports := importsOfFile(file)
// Don't stomp imports provided by -imports. Those should take precedence.
for pkg, path := range allImports {
if _, ok := p.imports[pkg]; !ok {
p.imports[pkg] = path
}
}
// Add imports from auxiliary files, which might be needed for embedded interfaces.
// Don't stomp any other imports.
for _, f := range p.auxFiles {
auxImports, _ := importsOfFile(f)
for pkg, path := range auxImports {
if _, ok := p.imports[pkg]; !ok {
p.imports[pkg] = path
}
}
}
var is []*model.Interface
for ni := range iterInterfaces(file) {
i, err := p.parseInterface(ni.name.String(), importPath, ni.it)
if err != nil {
return nil, err
}
is = append(is, i)
}
return &model.Package{
Name: file.Name.String(),
Interfaces: is,
DotImports: dotImports,
}, nil
}
|
[
"func",
"(",
"p",
"*",
"fileParser",
")",
"parseFile",
"(",
"importPath",
"string",
",",
"file",
"*",
"ast",
".",
"File",
")",
"(",
"*",
"model",
".",
"Package",
",",
"error",
")",
"{",
"allImports",
",",
"dotImports",
":=",
"importsOfFile",
"(",
"file",
")",
"\n",
"// Don't stomp imports provided by -imports. Those should take precedence.",
"for",
"pkg",
",",
"path",
":=",
"range",
"allImports",
"{",
"if",
"_",
",",
"ok",
":=",
"p",
".",
"imports",
"[",
"pkg",
"]",
";",
"!",
"ok",
"{",
"p",
".",
"imports",
"[",
"pkg",
"]",
"=",
"path",
"\n",
"}",
"\n",
"}",
"\n",
"// Add imports from auxiliary files, which might be needed for embedded interfaces.",
"// Don't stomp any other imports.",
"for",
"_",
",",
"f",
":=",
"range",
"p",
".",
"auxFiles",
"{",
"auxImports",
",",
"_",
":=",
"importsOfFile",
"(",
"f",
")",
"\n",
"for",
"pkg",
",",
"path",
":=",
"range",
"auxImports",
"{",
"if",
"_",
",",
"ok",
":=",
"p",
".",
"imports",
"[",
"pkg",
"]",
";",
"!",
"ok",
"{",
"p",
".",
"imports",
"[",
"pkg",
"]",
"=",
"path",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"var",
"is",
"[",
"]",
"*",
"model",
".",
"Interface",
"\n",
"for",
"ni",
":=",
"range",
"iterInterfaces",
"(",
"file",
")",
"{",
"i",
",",
"err",
":=",
"p",
".",
"parseInterface",
"(",
"ni",
".",
"name",
".",
"String",
"(",
")",
",",
"importPath",
",",
"ni",
".",
"it",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"is",
"=",
"append",
"(",
"is",
",",
"i",
")",
"\n",
"}",
"\n",
"return",
"&",
"model",
".",
"Package",
"{",
"Name",
":",
"file",
".",
"Name",
".",
"String",
"(",
")",
",",
"Interfaces",
":",
"is",
",",
"DotImports",
":",
"dotImports",
",",
"}",
",",
"nil",
"\n",
"}"
] |
// parseFile loads all file imports and auxiliary files import into the
// fileParser, parses all file interfaces and returns package model.
|
[
"parseFile",
"loads",
"all",
"file",
"imports",
"and",
"auxiliary",
"files",
"import",
"into",
"the",
"fileParser",
"parses",
"all",
"file",
"interfaces",
"and",
"returns",
"package",
"model",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/parse.go#L161-L193
|
21,958
|
golang/mock
|
mockgen/parse.go
|
parsePackage
|
func (p *fileParser) parsePackage(path string) error {
var pkgs map[string]*ast.Package
if imp, err := build.Import(path, p.srcDir, build.FindOnly); err != nil {
return err
} else if pkgs, err = parser.ParseDir(p.fileSet, imp.Dir, nil, 0); err != nil {
return err
}
for _, pkg := range pkgs {
file := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates|ast.FilterUnassociatedComments|ast.FilterImportDuplicates)
if _, ok := p.importedInterfaces[path]; !ok {
p.importedInterfaces[path] = make(map[string]*ast.InterfaceType)
}
for ni := range iterInterfaces(file) {
p.importedInterfaces[path][ni.name.Name] = ni.it
}
imports, _ := importsOfFile(file)
for pkgName, pkgPath := range imports {
if _, ok := p.imports[pkgName]; !ok {
p.imports[pkgName] = pkgPath
}
}
}
return nil
}
|
go
|
func (p *fileParser) parsePackage(path string) error {
var pkgs map[string]*ast.Package
if imp, err := build.Import(path, p.srcDir, build.FindOnly); err != nil {
return err
} else if pkgs, err = parser.ParseDir(p.fileSet, imp.Dir, nil, 0); err != nil {
return err
}
for _, pkg := range pkgs {
file := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates|ast.FilterUnassociatedComments|ast.FilterImportDuplicates)
if _, ok := p.importedInterfaces[path]; !ok {
p.importedInterfaces[path] = make(map[string]*ast.InterfaceType)
}
for ni := range iterInterfaces(file) {
p.importedInterfaces[path][ni.name.Name] = ni.it
}
imports, _ := importsOfFile(file)
for pkgName, pkgPath := range imports {
if _, ok := p.imports[pkgName]; !ok {
p.imports[pkgName] = pkgPath
}
}
}
return nil
}
|
[
"func",
"(",
"p",
"*",
"fileParser",
")",
"parsePackage",
"(",
"path",
"string",
")",
"error",
"{",
"var",
"pkgs",
"map",
"[",
"string",
"]",
"*",
"ast",
".",
"Package",
"\n",
"if",
"imp",
",",
"err",
":=",
"build",
".",
"Import",
"(",
"path",
",",
"p",
".",
"srcDir",
",",
"build",
".",
"FindOnly",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"else",
"if",
"pkgs",
",",
"err",
"=",
"parser",
".",
"ParseDir",
"(",
"p",
".",
"fileSet",
",",
"imp",
".",
"Dir",
",",
"nil",
",",
"0",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"for",
"_",
",",
"pkg",
":=",
"range",
"pkgs",
"{",
"file",
":=",
"ast",
".",
"MergePackageFiles",
"(",
"pkg",
",",
"ast",
".",
"FilterFuncDuplicates",
"|",
"ast",
".",
"FilterUnassociatedComments",
"|",
"ast",
".",
"FilterImportDuplicates",
")",
"\n",
"if",
"_",
",",
"ok",
":=",
"p",
".",
"importedInterfaces",
"[",
"path",
"]",
";",
"!",
"ok",
"{",
"p",
".",
"importedInterfaces",
"[",
"path",
"]",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"ast",
".",
"InterfaceType",
")",
"\n",
"}",
"\n",
"for",
"ni",
":=",
"range",
"iterInterfaces",
"(",
"file",
")",
"{",
"p",
".",
"importedInterfaces",
"[",
"path",
"]",
"[",
"ni",
".",
"name",
".",
"Name",
"]",
"=",
"ni",
".",
"it",
"\n",
"}",
"\n",
"imports",
",",
"_",
":=",
"importsOfFile",
"(",
"file",
")",
"\n",
"for",
"pkgName",
",",
"pkgPath",
":=",
"range",
"imports",
"{",
"if",
"_",
",",
"ok",
":=",
"p",
".",
"imports",
"[",
"pkgName",
"]",
";",
"!",
"ok",
"{",
"p",
".",
"imports",
"[",
"pkgName",
"]",
"=",
"pkgPath",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// parsePackage loads package specified by path, parses it and populates
// corresponding imports and importedInterfaces into the fileParser.
|
[
"parsePackage",
"loads",
"package",
"specified",
"by",
"path",
"parses",
"it",
"and",
"populates",
"corresponding",
"imports",
"and",
"importedInterfaces",
"into",
"the",
"fileParser",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/parse.go#L197-L220
|
21,959
|
golang/mock
|
mockgen/parse.go
|
importsOfFile
|
func importsOfFile(file *ast.File) (normalImports map[string]string, dotImports []string) {
normalImports = make(map[string]string)
dotImports = make([]string, 0)
for _, is := range file.Imports {
var pkgName string
importPath := is.Path.Value[1 : len(is.Path.Value)-1] // remove quotes
if is.Name != nil {
// Named imports are always certain.
if is.Name.Name == "_" {
continue
}
pkgName = is.Name.Name
} else {
pkg, err := build.Import(importPath, "", 0)
if err != nil {
// Fallback to import path suffix. Note that this is uncertain.
_, last := path.Split(importPath)
// If the last path component has dots, the first dot-delimited
// field is used as the name.
pkgName = strings.SplitN(last, ".", 2)[0]
} else {
pkgName = pkg.Name
}
}
if pkgName == "." {
dotImports = append(dotImports, importPath)
} else {
if _, ok := normalImports[pkgName]; ok {
log.Fatalf("imported package collision: %q imported twice", pkgName)
}
normalImports[pkgName] = importPath
}
}
return
}
|
go
|
func importsOfFile(file *ast.File) (normalImports map[string]string, dotImports []string) {
normalImports = make(map[string]string)
dotImports = make([]string, 0)
for _, is := range file.Imports {
var pkgName string
importPath := is.Path.Value[1 : len(is.Path.Value)-1] // remove quotes
if is.Name != nil {
// Named imports are always certain.
if is.Name.Name == "_" {
continue
}
pkgName = is.Name.Name
} else {
pkg, err := build.Import(importPath, "", 0)
if err != nil {
// Fallback to import path suffix. Note that this is uncertain.
_, last := path.Split(importPath)
// If the last path component has dots, the first dot-delimited
// field is used as the name.
pkgName = strings.SplitN(last, ".", 2)[0]
} else {
pkgName = pkg.Name
}
}
if pkgName == "." {
dotImports = append(dotImports, importPath)
} else {
if _, ok := normalImports[pkgName]; ok {
log.Fatalf("imported package collision: %q imported twice", pkgName)
}
normalImports[pkgName] = importPath
}
}
return
}
|
[
"func",
"importsOfFile",
"(",
"file",
"*",
"ast",
".",
"File",
")",
"(",
"normalImports",
"map",
"[",
"string",
"]",
"string",
",",
"dotImports",
"[",
"]",
"string",
")",
"{",
"normalImports",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n",
"dotImports",
"=",
"make",
"(",
"[",
"]",
"string",
",",
"0",
")",
"\n",
"for",
"_",
",",
"is",
":=",
"range",
"file",
".",
"Imports",
"{",
"var",
"pkgName",
"string",
"\n",
"importPath",
":=",
"is",
".",
"Path",
".",
"Value",
"[",
"1",
":",
"len",
"(",
"is",
".",
"Path",
".",
"Value",
")",
"-",
"1",
"]",
"// remove quotes",
"\n\n",
"if",
"is",
".",
"Name",
"!=",
"nil",
"{",
"// Named imports are always certain.",
"if",
"is",
".",
"Name",
".",
"Name",
"==",
"\"",
"\"",
"{",
"continue",
"\n",
"}",
"\n",
"pkgName",
"=",
"is",
".",
"Name",
".",
"Name",
"\n",
"}",
"else",
"{",
"pkg",
",",
"err",
":=",
"build",
".",
"Import",
"(",
"importPath",
",",
"\"",
"\"",
",",
"0",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"// Fallback to import path suffix. Note that this is uncertain.",
"_",
",",
"last",
":=",
"path",
".",
"Split",
"(",
"importPath",
")",
"\n",
"// If the last path component has dots, the first dot-delimited",
"// field is used as the name.",
"pkgName",
"=",
"strings",
".",
"SplitN",
"(",
"last",
",",
"\"",
"\"",
",",
"2",
")",
"[",
"0",
"]",
"\n",
"}",
"else",
"{",
"pkgName",
"=",
"pkg",
".",
"Name",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"pkgName",
"==",
"\"",
"\"",
"{",
"dotImports",
"=",
"append",
"(",
"dotImports",
",",
"importPath",
")",
"\n",
"}",
"else",
"{",
"if",
"_",
",",
"ok",
":=",
"normalImports",
"[",
"pkgName",
"]",
";",
"ok",
"{",
"log",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"pkgName",
")",
"\n",
"}",
"\n",
"normalImports",
"[",
"pkgName",
"]",
"=",
"importPath",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// importsOfFile returns a map of package name to import path
// of the imports in file.
|
[
"importsOfFile",
"returns",
"a",
"map",
"of",
"package",
"name",
"to",
"import",
"path",
"of",
"the",
"imports",
"in",
"file",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/parse.go#L444-L481
|
21,960
|
golang/mock
|
mockgen/parse.go
|
iterInterfaces
|
func iterInterfaces(file *ast.File) <-chan namedInterface {
ch := make(chan namedInterface)
go func() {
for _, decl := range file.Decls {
gd, ok := decl.(*ast.GenDecl)
if !ok || gd.Tok != token.TYPE {
continue
}
for _, spec := range gd.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
it, ok := ts.Type.(*ast.InterfaceType)
if !ok {
continue
}
ch <- namedInterface{ts.Name, it}
}
}
close(ch)
}()
return ch
}
|
go
|
func iterInterfaces(file *ast.File) <-chan namedInterface {
ch := make(chan namedInterface)
go func() {
for _, decl := range file.Decls {
gd, ok := decl.(*ast.GenDecl)
if !ok || gd.Tok != token.TYPE {
continue
}
for _, spec := range gd.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
it, ok := ts.Type.(*ast.InterfaceType)
if !ok {
continue
}
ch <- namedInterface{ts.Name, it}
}
}
close(ch)
}()
return ch
}
|
[
"func",
"iterInterfaces",
"(",
"file",
"*",
"ast",
".",
"File",
")",
"<-",
"chan",
"namedInterface",
"{",
"ch",
":=",
"make",
"(",
"chan",
"namedInterface",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"for",
"_",
",",
"decl",
":=",
"range",
"file",
".",
"Decls",
"{",
"gd",
",",
"ok",
":=",
"decl",
".",
"(",
"*",
"ast",
".",
"GenDecl",
")",
"\n",
"if",
"!",
"ok",
"||",
"gd",
".",
"Tok",
"!=",
"token",
".",
"TYPE",
"{",
"continue",
"\n",
"}",
"\n",
"for",
"_",
",",
"spec",
":=",
"range",
"gd",
".",
"Specs",
"{",
"ts",
",",
"ok",
":=",
"spec",
".",
"(",
"*",
"ast",
".",
"TypeSpec",
")",
"\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n",
"it",
",",
"ok",
":=",
"ts",
".",
"Type",
".",
"(",
"*",
"ast",
".",
"InterfaceType",
")",
"\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"ch",
"<-",
"namedInterface",
"{",
"ts",
".",
"Name",
",",
"it",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"close",
"(",
"ch",
")",
"\n",
"}",
"(",
")",
"\n",
"return",
"ch",
"\n",
"}"
] |
// Create an iterator over all interfaces in file.
|
[
"Create",
"an",
"iterator",
"over",
"all",
"interfaces",
"in",
"file",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/parse.go#L489-L513
|
21,961
|
golang/mock
|
mockgen/parse.go
|
isVariadic
|
func isVariadic(f *ast.FuncType) bool {
nargs := len(f.Params.List)
if nargs == 0 {
return false
}
_, ok := f.Params.List[nargs-1].Type.(*ast.Ellipsis)
return ok
}
|
go
|
func isVariadic(f *ast.FuncType) bool {
nargs := len(f.Params.List)
if nargs == 0 {
return false
}
_, ok := f.Params.List[nargs-1].Type.(*ast.Ellipsis)
return ok
}
|
[
"func",
"isVariadic",
"(",
"f",
"*",
"ast",
".",
"FuncType",
")",
"bool",
"{",
"nargs",
":=",
"len",
"(",
"f",
".",
"Params",
".",
"List",
")",
"\n",
"if",
"nargs",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"_",
",",
"ok",
":=",
"f",
".",
"Params",
".",
"List",
"[",
"nargs",
"-",
"1",
"]",
".",
"Type",
".",
"(",
"*",
"ast",
".",
"Ellipsis",
")",
"\n",
"return",
"ok",
"\n",
"}"
] |
// isVariadic returns whether the function is variadic.
|
[
"isVariadic",
"returns",
"whether",
"the",
"function",
"is",
"variadic",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/parse.go#L516-L523
|
21,962
|
golang/mock
|
gomock/controller.go
|
NewController
|
func NewController(t TestReporter) *Controller {
h, ok := t.(TestHelper)
if !ok {
h = nopTestHelper{t}
}
return &Controller{
T: h,
expectedCalls: newCallSet(),
}
}
|
go
|
func NewController(t TestReporter) *Controller {
h, ok := t.(TestHelper)
if !ok {
h = nopTestHelper{t}
}
return &Controller{
T: h,
expectedCalls: newCallSet(),
}
}
|
[
"func",
"NewController",
"(",
"t",
"TestReporter",
")",
"*",
"Controller",
"{",
"h",
",",
"ok",
":=",
"t",
".",
"(",
"TestHelper",
")",
"\n",
"if",
"!",
"ok",
"{",
"h",
"=",
"nopTestHelper",
"{",
"t",
"}",
"\n",
"}",
"\n\n",
"return",
"&",
"Controller",
"{",
"T",
":",
"h",
",",
"expectedCalls",
":",
"newCallSet",
"(",
")",
",",
"}",
"\n",
"}"
] |
// NewController returns a new Controller. It is the preferred way to create a
// Controller.
|
[
"NewController",
"returns",
"a",
"new",
"Controller",
".",
"It",
"is",
"the",
"preferred",
"way",
"to",
"create",
"a",
"Controller",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/controller.go#L118-L128
|
21,963
|
golang/mock
|
gomock/controller.go
|
WithContext
|
func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context) {
h, ok := t.(TestHelper)
if !ok {
h = nopTestHelper{t}
}
ctx, cancel := context.WithCancel(ctx)
return NewController(&cancelReporter{h, cancel}), ctx
}
|
go
|
func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context) {
h, ok := t.(TestHelper)
if !ok {
h = nopTestHelper{t}
}
ctx, cancel := context.WithCancel(ctx)
return NewController(&cancelReporter{h, cancel}), ctx
}
|
[
"func",
"WithContext",
"(",
"ctx",
"context",
".",
"Context",
",",
"t",
"TestReporter",
")",
"(",
"*",
"Controller",
",",
"context",
".",
"Context",
")",
"{",
"h",
",",
"ok",
":=",
"t",
".",
"(",
"TestHelper",
")",
"\n",
"if",
"!",
"ok",
"{",
"h",
"=",
"nopTestHelper",
"{",
"t",
"}",
"\n",
"}",
"\n\n",
"ctx",
",",
"cancel",
":=",
"context",
".",
"WithCancel",
"(",
"ctx",
")",
"\n",
"return",
"NewController",
"(",
"&",
"cancelReporter",
"{",
"h",
",",
"cancel",
"}",
")",
",",
"ctx",
"\n",
"}"
] |
// WithContext returns a new Controller and a Context, which is cancelled on any
// fatal failure.
|
[
"WithContext",
"returns",
"a",
"new",
"Controller",
"and",
"a",
"Context",
"which",
"is",
"cancelled",
"on",
"any",
"fatal",
"failure",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/controller.go#L145-L153
|
21,964
|
golang/mock
|
gomock/controller.go
|
RecordCall
|
func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call {
ctrl.T.Helper()
recv := reflect.ValueOf(receiver)
for i := 0; i < recv.Type().NumMethod(); i++ {
if recv.Type().Method(i).Name == method {
return ctrl.RecordCallWithMethodType(receiver, method, recv.Method(i).Type(), args...)
}
}
ctrl.T.Fatalf("gomock: failed finding method %s on %T", method, receiver)
panic("unreachable")
}
|
go
|
func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call {
ctrl.T.Helper()
recv := reflect.ValueOf(receiver)
for i := 0; i < recv.Type().NumMethod(); i++ {
if recv.Type().Method(i).Name == method {
return ctrl.RecordCallWithMethodType(receiver, method, recv.Method(i).Type(), args...)
}
}
ctrl.T.Fatalf("gomock: failed finding method %s on %T", method, receiver)
panic("unreachable")
}
|
[
"func",
"(",
"ctrl",
"*",
"Controller",
")",
"RecordCall",
"(",
"receiver",
"interface",
"{",
"}",
",",
"method",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"*",
"Call",
"{",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n\n",
"recv",
":=",
"reflect",
".",
"ValueOf",
"(",
"receiver",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"recv",
".",
"Type",
"(",
")",
".",
"NumMethod",
"(",
")",
";",
"i",
"++",
"{",
"if",
"recv",
".",
"Type",
"(",
")",
".",
"Method",
"(",
"i",
")",
".",
"Name",
"==",
"method",
"{",
"return",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"receiver",
",",
"method",
",",
"recv",
".",
"Method",
"(",
"i",
")",
".",
"Type",
"(",
")",
",",
"args",
"...",
")",
"\n",
"}",
"\n",
"}",
"\n",
"ctrl",
".",
"T",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"method",
",",
"receiver",
")",
"\n",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}"
] |
// RecordCall is called by a mock. It should not be called by user code.
|
[
"RecordCall",
"is",
"called",
"by",
"a",
"mock",
".",
"It",
"should",
"not",
"be",
"called",
"by",
"user",
"code",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/controller.go#L162-L173
|
21,965
|
golang/mock
|
gomock/controller.go
|
RecordCallWithMethodType
|
func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call {
ctrl.T.Helper()
call := newCall(ctrl.T, receiver, method, methodType, args...)
ctrl.mu.Lock()
defer ctrl.mu.Unlock()
ctrl.expectedCalls.Add(call)
return call
}
|
go
|
func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call {
ctrl.T.Helper()
call := newCall(ctrl.T, receiver, method, methodType, args...)
ctrl.mu.Lock()
defer ctrl.mu.Unlock()
ctrl.expectedCalls.Add(call)
return call
}
|
[
"func",
"(",
"ctrl",
"*",
"Controller",
")",
"RecordCallWithMethodType",
"(",
"receiver",
"interface",
"{",
"}",
",",
"method",
"string",
",",
"methodType",
"reflect",
".",
"Type",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"*",
"Call",
"{",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n\n",
"call",
":=",
"newCall",
"(",
"ctrl",
".",
"T",
",",
"receiver",
",",
"method",
",",
"methodType",
",",
"args",
"...",
")",
"\n\n",
"ctrl",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"ctrl",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"ctrl",
".",
"expectedCalls",
".",
"Add",
"(",
"call",
")",
"\n\n",
"return",
"call",
"\n",
"}"
] |
// RecordCallWithMethodType is called by a mock. It should not be called by user code.
|
[
"RecordCallWithMethodType",
"is",
"called",
"by",
"a",
"mock",
".",
"It",
"should",
"not",
"be",
"called",
"by",
"user",
"code",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/controller.go#L176-L186
|
21,966
|
golang/mock
|
gomock/controller.go
|
Call
|
func (ctrl *Controller) Call(receiver interface{}, method string, args ...interface{}) []interface{} {
ctrl.T.Helper()
// Nest this code so we can use defer to make sure the lock is released.
actions := func() []func([]interface{}) []interface{} {
ctrl.T.Helper()
ctrl.mu.Lock()
defer ctrl.mu.Unlock()
expected, err := ctrl.expectedCalls.FindMatch(receiver, method, args)
if err != nil {
origin := callerInfo(2)
ctrl.T.Fatalf("Unexpected call to %T.%v(%v) at %s because: %s", receiver, method, args, origin, err)
}
// Two things happen here:
// * the matching call no longer needs to check prerequite calls,
// * and the prerequite calls are no longer expected, so remove them.
preReqCalls := expected.dropPrereqs()
for _, preReqCall := range preReqCalls {
ctrl.expectedCalls.Remove(preReqCall)
}
actions := expected.call(args)
if expected.exhausted() {
ctrl.expectedCalls.Remove(expected)
}
return actions
}()
var rets []interface{}
for _, action := range actions {
if r := action(args); r != nil {
rets = r
}
}
return rets
}
|
go
|
func (ctrl *Controller) Call(receiver interface{}, method string, args ...interface{}) []interface{} {
ctrl.T.Helper()
// Nest this code so we can use defer to make sure the lock is released.
actions := func() []func([]interface{}) []interface{} {
ctrl.T.Helper()
ctrl.mu.Lock()
defer ctrl.mu.Unlock()
expected, err := ctrl.expectedCalls.FindMatch(receiver, method, args)
if err != nil {
origin := callerInfo(2)
ctrl.T.Fatalf("Unexpected call to %T.%v(%v) at %s because: %s", receiver, method, args, origin, err)
}
// Two things happen here:
// * the matching call no longer needs to check prerequite calls,
// * and the prerequite calls are no longer expected, so remove them.
preReqCalls := expected.dropPrereqs()
for _, preReqCall := range preReqCalls {
ctrl.expectedCalls.Remove(preReqCall)
}
actions := expected.call(args)
if expected.exhausted() {
ctrl.expectedCalls.Remove(expected)
}
return actions
}()
var rets []interface{}
for _, action := range actions {
if r := action(args); r != nil {
rets = r
}
}
return rets
}
|
[
"func",
"(",
"ctrl",
"*",
"Controller",
")",
"Call",
"(",
"receiver",
"interface",
"{",
"}",
",",
"method",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n\n",
"// Nest this code so we can use defer to make sure the lock is released.",
"actions",
":=",
"func",
"(",
")",
"[",
"]",
"func",
"(",
"[",
"]",
"interface",
"{",
"}",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n",
"ctrl",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"ctrl",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"expected",
",",
"err",
":=",
"ctrl",
".",
"expectedCalls",
".",
"FindMatch",
"(",
"receiver",
",",
"method",
",",
"args",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"origin",
":=",
"callerInfo",
"(",
"2",
")",
"\n",
"ctrl",
".",
"T",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"receiver",
",",
"method",
",",
"args",
",",
"origin",
",",
"err",
")",
"\n",
"}",
"\n\n",
"// Two things happen here:",
"// * the matching call no longer needs to check prerequite calls,",
"// * and the prerequite calls are no longer expected, so remove them.",
"preReqCalls",
":=",
"expected",
".",
"dropPrereqs",
"(",
")",
"\n",
"for",
"_",
",",
"preReqCall",
":=",
"range",
"preReqCalls",
"{",
"ctrl",
".",
"expectedCalls",
".",
"Remove",
"(",
"preReqCall",
")",
"\n",
"}",
"\n\n",
"actions",
":=",
"expected",
".",
"call",
"(",
"args",
")",
"\n",
"if",
"expected",
".",
"exhausted",
"(",
")",
"{",
"ctrl",
".",
"expectedCalls",
".",
"Remove",
"(",
"expected",
")",
"\n",
"}",
"\n",
"return",
"actions",
"\n",
"}",
"(",
")",
"\n\n",
"var",
"rets",
"[",
"]",
"interface",
"{",
"}",
"\n",
"for",
"_",
",",
"action",
":=",
"range",
"actions",
"{",
"if",
"r",
":=",
"action",
"(",
"args",
")",
";",
"r",
"!=",
"nil",
"{",
"rets",
"=",
"r",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"rets",
"\n",
"}"
] |
// Call is called by a mock. It should not be called by user code.
|
[
"Call",
"is",
"called",
"by",
"a",
"mock",
".",
"It",
"should",
"not",
"be",
"called",
"by",
"user",
"code",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/controller.go#L189-L227
|
21,967
|
golang/mock
|
gomock/controller.go
|
Finish
|
func (ctrl *Controller) Finish() {
ctrl.T.Helper()
ctrl.mu.Lock()
defer ctrl.mu.Unlock()
if ctrl.finished {
ctrl.T.Fatalf("Controller.Finish was called more than once. It has to be called exactly once.")
}
ctrl.finished = true
// If we're currently panicking, probably because this is a deferred call,
// pass through the panic.
if err := recover(); err != nil {
panic(err)
}
// Check that all remaining expected calls are satisfied.
failures := ctrl.expectedCalls.Failures()
for _, call := range failures {
ctrl.T.Errorf("missing call(s) to %v", call)
}
if len(failures) != 0 {
ctrl.T.Fatalf("aborting test due to missing call(s)")
}
}
|
go
|
func (ctrl *Controller) Finish() {
ctrl.T.Helper()
ctrl.mu.Lock()
defer ctrl.mu.Unlock()
if ctrl.finished {
ctrl.T.Fatalf("Controller.Finish was called more than once. It has to be called exactly once.")
}
ctrl.finished = true
// If we're currently panicking, probably because this is a deferred call,
// pass through the panic.
if err := recover(); err != nil {
panic(err)
}
// Check that all remaining expected calls are satisfied.
failures := ctrl.expectedCalls.Failures()
for _, call := range failures {
ctrl.T.Errorf("missing call(s) to %v", call)
}
if len(failures) != 0 {
ctrl.T.Fatalf("aborting test due to missing call(s)")
}
}
|
[
"func",
"(",
"ctrl",
"*",
"Controller",
")",
"Finish",
"(",
")",
"{",
"ctrl",
".",
"T",
".",
"Helper",
"(",
")",
"\n\n",
"ctrl",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"ctrl",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"ctrl",
".",
"finished",
"{",
"ctrl",
".",
"T",
".",
"Fatalf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"ctrl",
".",
"finished",
"=",
"true",
"\n\n",
"// If we're currently panicking, probably because this is a deferred call,",
"// pass through the panic.",
"if",
"err",
":=",
"recover",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"// Check that all remaining expected calls are satisfied.",
"failures",
":=",
"ctrl",
".",
"expectedCalls",
".",
"Failures",
"(",
")",
"\n",
"for",
"_",
",",
"call",
":=",
"range",
"failures",
"{",
"ctrl",
".",
"T",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"call",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"failures",
")",
"!=",
"0",
"{",
"ctrl",
".",
"T",
".",
"Fatalf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}"
] |
// Finish checks to see if all the methods that were expected to be called
// were called. It should be invoked for each Controller. It is not idempotent
// and therefore can only be invoked once.
|
[
"Finish",
"checks",
"to",
"see",
"if",
"all",
"the",
"methods",
"that",
"were",
"expected",
"to",
"be",
"called",
"were",
"called",
".",
"It",
"should",
"be",
"invoked",
"for",
"each",
"Controller",
".",
"It",
"is",
"not",
"idempotent",
"and",
"therefore",
"can",
"only",
"be",
"invoked",
"once",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/gomock/controller.go#L232-L257
|
21,968
|
golang/mock
|
mockgen/model/model.go
|
Imports
|
func (pkg *Package) Imports() map[string]bool {
im := make(map[string]bool)
for _, intf := range pkg.Interfaces {
intf.addImports(im)
}
return im
}
|
go
|
func (pkg *Package) Imports() map[string]bool {
im := make(map[string]bool)
for _, intf := range pkg.Interfaces {
intf.addImports(im)
}
return im
}
|
[
"func",
"(",
"pkg",
"*",
"Package",
")",
"Imports",
"(",
")",
"map",
"[",
"string",
"]",
"bool",
"{",
"im",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"for",
"_",
",",
"intf",
":=",
"range",
"pkg",
".",
"Interfaces",
"{",
"intf",
".",
"addImports",
"(",
"im",
")",
"\n",
"}",
"\n",
"return",
"im",
"\n",
"}"
] |
// Imports returns the imports needed by the Package as a set of import paths.
|
[
"Imports",
"returns",
"the",
"imports",
"needed",
"by",
"the",
"Package",
"as",
"a",
"set",
"of",
"import",
"paths",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/model/model.go#L44-L50
|
21,969
|
golang/mock
|
mockgen/model/model.go
|
funcArgsFromType
|
func funcArgsFromType(t reflect.Type) (in []*Parameter, variadic *Parameter, out []*Parameter, err error) {
nin := t.NumIn()
if t.IsVariadic() {
nin--
}
var p *Parameter
for i := 0; i < nin; i++ {
p, err = parameterFromType(t.In(i))
if err != nil {
return
}
in = append(in, p)
}
if t.IsVariadic() {
p, err = parameterFromType(t.In(nin).Elem())
if err != nil {
return
}
variadic = p
}
for i := 0; i < t.NumOut(); i++ {
p, err = parameterFromType(t.Out(i))
if err != nil {
return
}
out = append(out, p)
}
return
}
|
go
|
func funcArgsFromType(t reflect.Type) (in []*Parameter, variadic *Parameter, out []*Parameter, err error) {
nin := t.NumIn()
if t.IsVariadic() {
nin--
}
var p *Parameter
for i := 0; i < nin; i++ {
p, err = parameterFromType(t.In(i))
if err != nil {
return
}
in = append(in, p)
}
if t.IsVariadic() {
p, err = parameterFromType(t.In(nin).Elem())
if err != nil {
return
}
variadic = p
}
for i := 0; i < t.NumOut(); i++ {
p, err = parameterFromType(t.Out(i))
if err != nil {
return
}
out = append(out, p)
}
return
}
|
[
"func",
"funcArgsFromType",
"(",
"t",
"reflect",
".",
"Type",
")",
"(",
"in",
"[",
"]",
"*",
"Parameter",
",",
"variadic",
"*",
"Parameter",
",",
"out",
"[",
"]",
"*",
"Parameter",
",",
"err",
"error",
")",
"{",
"nin",
":=",
"t",
".",
"NumIn",
"(",
")",
"\n",
"if",
"t",
".",
"IsVariadic",
"(",
")",
"{",
"nin",
"--",
"\n",
"}",
"\n",
"var",
"p",
"*",
"Parameter",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"nin",
";",
"i",
"++",
"{",
"p",
",",
"err",
"=",
"parameterFromType",
"(",
"t",
".",
"In",
"(",
"i",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"in",
"=",
"append",
"(",
"in",
",",
"p",
")",
"\n",
"}",
"\n",
"if",
"t",
".",
"IsVariadic",
"(",
")",
"{",
"p",
",",
"err",
"=",
"parameterFromType",
"(",
"t",
".",
"In",
"(",
"nin",
")",
".",
"Elem",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"variadic",
"=",
"p",
"\n",
"}",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"t",
".",
"NumOut",
"(",
")",
";",
"i",
"++",
"{",
"p",
",",
"err",
"=",
"parameterFromType",
"(",
"t",
".",
"Out",
"(",
"i",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"out",
"=",
"append",
"(",
"out",
",",
"p",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// t's Kind must be a reflect.Func.
|
[
"t",
"s",
"Kind",
"must",
"be",
"a",
"reflect",
".",
"Func",
"."
] |
937870445b8bddd7f05ea90e81c58ebe83681534
|
https://github.com/golang/mock/blob/937870445b8bddd7f05ea90e81c58ebe83681534/mockgen/model/model.go#L312-L340
|
21,970
|
anacrolix/torrent
|
metainfo/magnet.go
|
ParseMagnetURI
|
func ParseMagnetURI(uri string) (m Magnet, err error) {
u, err := url.Parse(uri)
if err != nil {
err = fmt.Errorf("error parsing uri: %s", err)
return
}
if u.Scheme != "magnet" {
err = fmt.Errorf("unexpected scheme: %q", u.Scheme)
return
}
xt := u.Query().Get("xt")
if !strings.HasPrefix(xt, xtPrefix) {
err = fmt.Errorf("bad xt parameter")
return
}
infoHash := xt[len(xtPrefix):]
// BTIH hash can be in HEX or BASE32 encoding
// will assign appropriate func judging from symbol length
var decode func(dst, src []byte) (int, error)
switch len(infoHash) {
case 40:
decode = hex.Decode
case 32:
decode = base32.StdEncoding.Decode
}
if decode == nil {
err = fmt.Errorf("unhandled xt parameter encoding: encoded length %d", len(infoHash))
return
}
n, err := decode(m.InfoHash[:], []byte(infoHash))
if err != nil {
err = fmt.Errorf("error decoding xt: %s", err)
return
}
if n != 20 {
panic(n)
}
m.DisplayName = u.Query().Get("dn")
m.Trackers = u.Query()["tr"]
return
}
|
go
|
func ParseMagnetURI(uri string) (m Magnet, err error) {
u, err := url.Parse(uri)
if err != nil {
err = fmt.Errorf("error parsing uri: %s", err)
return
}
if u.Scheme != "magnet" {
err = fmt.Errorf("unexpected scheme: %q", u.Scheme)
return
}
xt := u.Query().Get("xt")
if !strings.HasPrefix(xt, xtPrefix) {
err = fmt.Errorf("bad xt parameter")
return
}
infoHash := xt[len(xtPrefix):]
// BTIH hash can be in HEX or BASE32 encoding
// will assign appropriate func judging from symbol length
var decode func(dst, src []byte) (int, error)
switch len(infoHash) {
case 40:
decode = hex.Decode
case 32:
decode = base32.StdEncoding.Decode
}
if decode == nil {
err = fmt.Errorf("unhandled xt parameter encoding: encoded length %d", len(infoHash))
return
}
n, err := decode(m.InfoHash[:], []byte(infoHash))
if err != nil {
err = fmt.Errorf("error decoding xt: %s", err)
return
}
if n != 20 {
panic(n)
}
m.DisplayName = u.Query().Get("dn")
m.Trackers = u.Query()["tr"]
return
}
|
[
"func",
"ParseMagnetURI",
"(",
"uri",
"string",
")",
"(",
"m",
"Magnet",
",",
"err",
"error",
")",
"{",
"u",
",",
"err",
":=",
"url",
".",
"Parse",
"(",
"uri",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n",
"if",
"u",
".",
"Scheme",
"!=",
"\"",
"\"",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"u",
".",
"Scheme",
")",
"\n",
"return",
"\n",
"}",
"\n",
"xt",
":=",
"u",
".",
"Query",
"(",
")",
".",
"Get",
"(",
"\"",
"\"",
")",
"\n",
"if",
"!",
"strings",
".",
"HasPrefix",
"(",
"xt",
",",
"xtPrefix",
")",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"return",
"\n",
"}",
"\n",
"infoHash",
":=",
"xt",
"[",
"len",
"(",
"xtPrefix",
")",
":",
"]",
"\n\n",
"// BTIH hash can be in HEX or BASE32 encoding",
"// will assign appropriate func judging from symbol length",
"var",
"decode",
"func",
"(",
"dst",
",",
"src",
"[",
"]",
"byte",
")",
"(",
"int",
",",
"error",
")",
"\n",
"switch",
"len",
"(",
"infoHash",
")",
"{",
"case",
"40",
":",
"decode",
"=",
"hex",
".",
"Decode",
"\n",
"case",
"32",
":",
"decode",
"=",
"base32",
".",
"StdEncoding",
".",
"Decode",
"\n",
"}",
"\n\n",
"if",
"decode",
"==",
"nil",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"len",
"(",
"infoHash",
")",
")",
"\n",
"return",
"\n",
"}",
"\n",
"n",
",",
"err",
":=",
"decode",
"(",
"m",
".",
"InfoHash",
"[",
":",
"]",
",",
"[",
"]",
"byte",
"(",
"infoHash",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n",
"if",
"n",
"!=",
"20",
"{",
"panic",
"(",
"n",
")",
"\n",
"}",
"\n",
"m",
".",
"DisplayName",
"=",
"u",
".",
"Query",
"(",
")",
".",
"Get",
"(",
"\"",
"\"",
")",
"\n",
"m",
".",
"Trackers",
"=",
"u",
".",
"Query",
"(",
")",
"[",
"\"",
"\"",
"]",
"\n",
"return",
"\n",
"}"
] |
// ParseMagnetURI parses Magnet-formatted URIs into a Magnet instance
|
[
"ParseMagnetURI",
"parses",
"Magnet",
"-",
"formatted",
"URIs",
"into",
"a",
"Magnet",
"instance"
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/metainfo/magnet.go#L35-L77
|
21,971
|
anacrolix/torrent
|
metainfo/metainfo.go
|
Load
|
func Load(r io.Reader) (*MetaInfo, error) {
var mi MetaInfo
d := bencode.NewDecoder(r)
err := d.Decode(&mi)
if err != nil {
return nil, err
}
return &mi, nil
}
|
go
|
func Load(r io.Reader) (*MetaInfo, error) {
var mi MetaInfo
d := bencode.NewDecoder(r)
err := d.Decode(&mi)
if err != nil {
return nil, err
}
return &mi, nil
}
|
[
"func",
"Load",
"(",
"r",
"io",
".",
"Reader",
")",
"(",
"*",
"MetaInfo",
",",
"error",
")",
"{",
"var",
"mi",
"MetaInfo",
"\n",
"d",
":=",
"bencode",
".",
"NewDecoder",
"(",
"r",
")",
"\n",
"err",
":=",
"d",
".",
"Decode",
"(",
"&",
"mi",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"&",
"mi",
",",
"nil",
"\n",
"}"
] |
// Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
// failure.
|
[
"Load",
"a",
"MetaInfo",
"from",
"an",
"io",
".",
"Reader",
".",
"Returns",
"a",
"non",
"-",
"nil",
"error",
"in",
"case",
"of",
"failure",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/metainfo/metainfo.go#L25-L33
|
21,972
|
anacrolix/torrent
|
metainfo/metainfo.go
|
LoadFromFile
|
func LoadFromFile(filename string) (*MetaInfo, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return Load(f)
}
|
go
|
func LoadFromFile(filename string) (*MetaInfo, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return Load(f)
}
|
[
"func",
"LoadFromFile",
"(",
"filename",
"string",
")",
"(",
"*",
"MetaInfo",
",",
"error",
")",
"{",
"f",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"filename",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"defer",
"f",
".",
"Close",
"(",
")",
"\n",
"return",
"Load",
"(",
"f",
")",
"\n",
"}"
] |
// Convenience function for loading a MetaInfo from a file.
|
[
"Convenience",
"function",
"for",
"loading",
"a",
"MetaInfo",
"from",
"a",
"file",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/metainfo/metainfo.go#L36-L43
|
21,973
|
anacrolix/torrent
|
metainfo/metainfo.go
|
Write
|
func (mi MetaInfo) Write(w io.Writer) error {
return bencode.NewEncoder(w).Encode(mi)
}
|
go
|
func (mi MetaInfo) Write(w io.Writer) error {
return bencode.NewEncoder(w).Encode(mi)
}
|
[
"func",
"(",
"mi",
"MetaInfo",
")",
"Write",
"(",
"w",
"io",
".",
"Writer",
")",
"error",
"{",
"return",
"bencode",
".",
"NewEncoder",
"(",
"w",
")",
".",
"Encode",
"(",
"mi",
")",
"\n",
"}"
] |
// Encode to bencoded form.
|
[
"Encode",
"to",
"bencoded",
"form",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/metainfo/metainfo.go#L55-L57
|
21,974
|
anacrolix/torrent
|
metainfo/metainfo.go
|
SetDefaults
|
func (mi *MetaInfo) SetDefaults() {
mi.Comment = "yoloham"
mi.CreatedBy = "github.com/anacrolix/torrent"
mi.CreationDate = time.Now().Unix()
// mi.Info.PieceLength = 256 * 1024
}
|
go
|
func (mi *MetaInfo) SetDefaults() {
mi.Comment = "yoloham"
mi.CreatedBy = "github.com/anacrolix/torrent"
mi.CreationDate = time.Now().Unix()
// mi.Info.PieceLength = 256 * 1024
}
|
[
"func",
"(",
"mi",
"*",
"MetaInfo",
")",
"SetDefaults",
"(",
")",
"{",
"mi",
".",
"Comment",
"=",
"\"",
"\"",
"\n",
"mi",
".",
"CreatedBy",
"=",
"\"",
"\"",
"\n",
"mi",
".",
"CreationDate",
"=",
"time",
".",
"Now",
"(",
")",
".",
"Unix",
"(",
")",
"\n",
"// mi.Info.PieceLength = 256 * 1024",
"}"
] |
// Set good default values in preparation for creating a new MetaInfo file.
|
[
"Set",
"good",
"default",
"values",
"in",
"preparation",
"for",
"creating",
"a",
"new",
"MetaInfo",
"file",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/metainfo/metainfo.go#L60-L65
|
21,975
|
anacrolix/torrent
|
metainfo/metainfo.go
|
Magnet
|
func (mi *MetaInfo) Magnet(displayName string, infoHash Hash) (m Magnet) {
for t := range mi.UpvertedAnnounceList().DistinctValues() {
m.Trackers = append(m.Trackers, t)
}
m.DisplayName = displayName
m.InfoHash = infoHash
return
}
|
go
|
func (mi *MetaInfo) Magnet(displayName string, infoHash Hash) (m Magnet) {
for t := range mi.UpvertedAnnounceList().DistinctValues() {
m.Trackers = append(m.Trackers, t)
}
m.DisplayName = displayName
m.InfoHash = infoHash
return
}
|
[
"func",
"(",
"mi",
"*",
"MetaInfo",
")",
"Magnet",
"(",
"displayName",
"string",
",",
"infoHash",
"Hash",
")",
"(",
"m",
"Magnet",
")",
"{",
"for",
"t",
":=",
"range",
"mi",
".",
"UpvertedAnnounceList",
"(",
")",
".",
"DistinctValues",
"(",
")",
"{",
"m",
".",
"Trackers",
"=",
"append",
"(",
"m",
".",
"Trackers",
",",
"t",
")",
"\n",
"}",
"\n",
"m",
".",
"DisplayName",
"=",
"displayName",
"\n",
"m",
".",
"InfoHash",
"=",
"infoHash",
"\n",
"return",
"\n",
"}"
] |
// Creates a Magnet from a MetaInfo.
|
[
"Creates",
"a",
"Magnet",
"from",
"a",
"MetaInfo",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/metainfo/metainfo.go#L68-L75
|
21,976
|
anacrolix/torrent
|
metainfo/metainfo.go
|
UpvertedAnnounceList
|
func (mi *MetaInfo) UpvertedAnnounceList() AnnounceList {
if mi.AnnounceList.OverridesAnnounce(mi.Announce) {
return mi.AnnounceList
}
if mi.Announce != "" {
return [][]string{[]string{mi.Announce}}
}
return nil
}
|
go
|
func (mi *MetaInfo) UpvertedAnnounceList() AnnounceList {
if mi.AnnounceList.OverridesAnnounce(mi.Announce) {
return mi.AnnounceList
}
if mi.Announce != "" {
return [][]string{[]string{mi.Announce}}
}
return nil
}
|
[
"func",
"(",
"mi",
"*",
"MetaInfo",
")",
"UpvertedAnnounceList",
"(",
")",
"AnnounceList",
"{",
"if",
"mi",
".",
"AnnounceList",
".",
"OverridesAnnounce",
"(",
"mi",
".",
"Announce",
")",
"{",
"return",
"mi",
".",
"AnnounceList",
"\n",
"}",
"\n",
"if",
"mi",
".",
"Announce",
"!=",
"\"",
"\"",
"{",
"return",
"[",
"]",
"[",
"]",
"string",
"{",
"[",
"]",
"string",
"{",
"mi",
".",
"Announce",
"}",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Returns the announce list converted from the old single announce field if
// necessary.
|
[
"Returns",
"the",
"announce",
"list",
"converted",
"from",
"the",
"old",
"single",
"announce",
"field",
"if",
"necessary",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/metainfo/metainfo.go#L79-L87
|
21,977
|
anacrolix/torrent
|
cmd/torrent-pick/main.go
|
totalBytesEstimate
|
func totalBytesEstimate(tc *torrent.Client) (ret int64) {
var noInfo, hadInfo int64
for _, t := range tc.Torrents() {
info := t.Info()
if info == nil {
noInfo++
continue
}
ret += info.TotalLength()
hadInfo++
}
if hadInfo != 0 {
// Treat each torrent without info as the average of those with,
// rounded up.
ret += (noInfo*ret + hadInfo - 1) / hadInfo
}
return
}
|
go
|
func totalBytesEstimate(tc *torrent.Client) (ret int64) {
var noInfo, hadInfo int64
for _, t := range tc.Torrents() {
info := t.Info()
if info == nil {
noInfo++
continue
}
ret += info.TotalLength()
hadInfo++
}
if hadInfo != 0 {
// Treat each torrent without info as the average of those with,
// rounded up.
ret += (noInfo*ret + hadInfo - 1) / hadInfo
}
return
}
|
[
"func",
"totalBytesEstimate",
"(",
"tc",
"*",
"torrent",
".",
"Client",
")",
"(",
"ret",
"int64",
")",
"{",
"var",
"noInfo",
",",
"hadInfo",
"int64",
"\n",
"for",
"_",
",",
"t",
":=",
"range",
"tc",
".",
"Torrents",
"(",
")",
"{",
"info",
":=",
"t",
".",
"Info",
"(",
")",
"\n",
"if",
"info",
"==",
"nil",
"{",
"noInfo",
"++",
"\n",
"continue",
"\n",
"}",
"\n",
"ret",
"+=",
"info",
".",
"TotalLength",
"(",
")",
"\n",
"hadInfo",
"++",
"\n",
"}",
"\n",
"if",
"hadInfo",
"!=",
"0",
"{",
"// Treat each torrent without info as the average of those with,",
"// rounded up.",
"ret",
"+=",
"(",
"noInfo",
"*",
"ret",
"+",
"hadInfo",
"-",
"1",
")",
"/",
"hadInfo",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Returns an estimate of the total bytes for all torrents.
|
[
"Returns",
"an",
"estimate",
"of",
"the",
"total",
"bytes",
"for",
"all",
"torrents",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/cmd/torrent-pick/main.go#L51-L68
|
21,978
|
anacrolix/torrent
|
connection.go
|
ipv6
|
func (cn *connection) ipv6() bool {
ip := cn.remoteAddr.IP
if ip.To4() != nil {
return false
}
return len(ip) == net.IPv6len
}
|
go
|
func (cn *connection) ipv6() bool {
ip := cn.remoteAddr.IP
if ip.To4() != nil {
return false
}
return len(ip) == net.IPv6len
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"ipv6",
"(",
")",
"bool",
"{",
"ip",
":=",
"cn",
".",
"remoteAddr",
".",
"IP",
"\n",
"if",
"ip",
".",
"To4",
"(",
")",
"!=",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"len",
"(",
"ip",
")",
"==",
"net",
".",
"IPv6len",
"\n",
"}"
] |
// Returns true if the connection is over IPv6.
|
[
"Returns",
"true",
"if",
"the",
"connection",
"is",
"over",
"IPv6",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L137-L143
|
21,979
|
anacrolix/torrent
|
connection.go
|
hasPreferredNetworkOver
|
func (l *connection) hasPreferredNetworkOver(r *connection) (left, ok bool) {
var ml multiLess
ml.NextBool(l.isPreferredDirection(), r.isPreferredDirection())
ml.NextBool(!l.utp(), !r.utp())
ml.NextBool(l.ipv6(), r.ipv6())
return ml.FinalOk()
}
|
go
|
func (l *connection) hasPreferredNetworkOver(r *connection) (left, ok bool) {
var ml multiLess
ml.NextBool(l.isPreferredDirection(), r.isPreferredDirection())
ml.NextBool(!l.utp(), !r.utp())
ml.NextBool(l.ipv6(), r.ipv6())
return ml.FinalOk()
}
|
[
"func",
"(",
"l",
"*",
"connection",
")",
"hasPreferredNetworkOver",
"(",
"r",
"*",
"connection",
")",
"(",
"left",
",",
"ok",
"bool",
")",
"{",
"var",
"ml",
"multiLess",
"\n",
"ml",
".",
"NextBool",
"(",
"l",
".",
"isPreferredDirection",
"(",
")",
",",
"r",
".",
"isPreferredDirection",
"(",
")",
")",
"\n",
"ml",
".",
"NextBool",
"(",
"!",
"l",
".",
"utp",
"(",
")",
",",
"!",
"r",
".",
"utp",
"(",
")",
")",
"\n",
"ml",
".",
"NextBool",
"(",
"l",
".",
"ipv6",
"(",
")",
",",
"r",
".",
"ipv6",
"(",
")",
")",
"\n",
"return",
"ml",
".",
"FinalOk",
"(",
")",
"\n",
"}"
] |
// Returns whether the left connection should be preferred over the right one,
// considering only their networking properties. If ok is false, we can't
// decide.
|
[
"Returns",
"whether",
"the",
"left",
"connection",
"should",
"be",
"preferred",
"over",
"the",
"right",
"one",
"considering",
"only",
"their",
"networking",
"properties",
".",
"If",
"ok",
"is",
"false",
"we",
"can",
"t",
"decide",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L154-L160
|
21,980
|
anacrolix/torrent
|
connection.go
|
bestPeerNumPieces
|
func (cn *connection) bestPeerNumPieces() pieceIndex {
if cn.t.haveInfo() {
return cn.t.numPieces()
}
return cn.peerMinPieces
}
|
go
|
func (cn *connection) bestPeerNumPieces() pieceIndex {
if cn.t.haveInfo() {
return cn.t.numPieces()
}
return cn.peerMinPieces
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"bestPeerNumPieces",
"(",
")",
"pieceIndex",
"{",
"if",
"cn",
".",
"t",
".",
"haveInfo",
"(",
")",
"{",
"return",
"cn",
".",
"t",
".",
"numPieces",
"(",
")",
"\n",
"}",
"\n",
"return",
"cn",
".",
"peerMinPieces",
"\n",
"}"
] |
// The best guess at number of pieces in the torrent for this peer.
|
[
"The",
"best",
"guess",
"at",
"number",
"of",
"pieces",
"in",
"the",
"torrent",
"for",
"this",
"peer",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L194-L199
|
21,981
|
anacrolix/torrent
|
connection.go
|
setNumPieces
|
func (cn *connection) setNumPieces(num pieceIndex) error {
cn.peerPieces.RemoveRange(bitmap.BitIndex(num), bitmap.ToEnd)
cn.peerPiecesChanged()
return nil
}
|
go
|
func (cn *connection) setNumPieces(num pieceIndex) error {
cn.peerPieces.RemoveRange(bitmap.BitIndex(num), bitmap.ToEnd)
cn.peerPiecesChanged()
return nil
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"setNumPieces",
"(",
"num",
"pieceIndex",
")",
"error",
"{",
"cn",
".",
"peerPieces",
".",
"RemoveRange",
"(",
"bitmap",
".",
"BitIndex",
"(",
"num",
")",
",",
"bitmap",
".",
"ToEnd",
")",
"\n",
"cn",
".",
"peerPiecesChanged",
"(",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Correct the PeerPieces slice length. Return false if the existing slice is
// invalid, such as by receiving badly sized BITFIELD, or invalid HAVE
// messages.
|
[
"Correct",
"the",
"PeerPieces",
"slice",
"length",
".",
"Return",
"false",
"if",
"the",
"existing",
"slice",
"is",
"invalid",
"such",
"as",
"by",
"receiving",
"badly",
"sized",
"BITFIELD",
"or",
"invalid",
"HAVE",
"messages",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L212-L216
|
21,982
|
anacrolix/torrent
|
connection.go
|
Post
|
func (cn *connection) Post(msg pp.Message) {
torrent.Add(fmt.Sprintf("messages posted of type %s", msg.Type.String()), 1)
// We don't need to track bytes here because a connection.w Writer wrapper
// takes care of that (although there's some delay between us recording
// the message, and the connection writer flushing it out.).
cn.writeBuffer.Write(msg.MustMarshalBinary())
// Last I checked only Piece messages affect stats, and we don't post
// those.
cn.wroteMsg(&msg)
cn.tickleWriter()
}
|
go
|
func (cn *connection) Post(msg pp.Message) {
torrent.Add(fmt.Sprintf("messages posted of type %s", msg.Type.String()), 1)
// We don't need to track bytes here because a connection.w Writer wrapper
// takes care of that (although there's some delay between us recording
// the message, and the connection writer flushing it out.).
cn.writeBuffer.Write(msg.MustMarshalBinary())
// Last I checked only Piece messages affect stats, and we don't post
// those.
cn.wroteMsg(&msg)
cn.tickleWriter()
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"Post",
"(",
"msg",
"pp",
".",
"Message",
")",
"{",
"torrent",
".",
"Add",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"msg",
".",
"Type",
".",
"String",
"(",
")",
")",
",",
"1",
")",
"\n",
"// We don't need to track bytes here because a connection.w Writer wrapper",
"// takes care of that (although there's some delay between us recording",
"// the message, and the connection writer flushing it out.).",
"cn",
".",
"writeBuffer",
".",
"Write",
"(",
"msg",
".",
"MustMarshalBinary",
"(",
")",
")",
"\n",
"// Last I checked only Piece messages affect stats, and we don't post",
"// those.",
"cn",
".",
"wroteMsg",
"(",
"&",
"msg",
")",
"\n",
"cn",
".",
"tickleWriter",
"(",
")",
"\n",
"}"
] |
// Writes a message into the write buffer.
|
[
"Writes",
"a",
"message",
"into",
"the",
"write",
"buffer",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L330-L340
|
21,983
|
anacrolix/torrent
|
connection.go
|
nominalMaxRequests
|
func (cn *connection) nominalMaxRequests() (ret int) {
if cn.t.requestStrategy == 3 {
expectingTime := int64(cn.totalExpectingTime())
if expectingTime == 0 {
expectingTime = math.MaxInt64
} else {
expectingTime *= 2
}
return int(clamp(
1,
int64(cn.PeerMaxRequests),
max(
// It makes sense to always pipeline at least one connection,
// since latency must be non-zero.
2,
// Request only as many as we expect to receive in the
// dupliateRequestTimeout window. We are trying to avoid having to
// duplicate requests.
cn.chunksReceivedWhileExpecting*int64(cn.t.duplicateRequestTimeout)/expectingTime,
),
))
}
return int(clamp(
1,
int64(cn.PeerMaxRequests),
max(64,
cn.stats.ChunksReadUseful.Int64()-(cn.stats.ChunksRead.Int64()-cn.stats.ChunksReadUseful.Int64()))))
}
|
go
|
func (cn *connection) nominalMaxRequests() (ret int) {
if cn.t.requestStrategy == 3 {
expectingTime := int64(cn.totalExpectingTime())
if expectingTime == 0 {
expectingTime = math.MaxInt64
} else {
expectingTime *= 2
}
return int(clamp(
1,
int64(cn.PeerMaxRequests),
max(
// It makes sense to always pipeline at least one connection,
// since latency must be non-zero.
2,
// Request only as many as we expect to receive in the
// dupliateRequestTimeout window. We are trying to avoid having to
// duplicate requests.
cn.chunksReceivedWhileExpecting*int64(cn.t.duplicateRequestTimeout)/expectingTime,
),
))
}
return int(clamp(
1,
int64(cn.PeerMaxRequests),
max(64,
cn.stats.ChunksReadUseful.Int64()-(cn.stats.ChunksRead.Int64()-cn.stats.ChunksReadUseful.Int64()))))
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"nominalMaxRequests",
"(",
")",
"(",
"ret",
"int",
")",
"{",
"if",
"cn",
".",
"t",
".",
"requestStrategy",
"==",
"3",
"{",
"expectingTime",
":=",
"int64",
"(",
"cn",
".",
"totalExpectingTime",
"(",
")",
")",
"\n",
"if",
"expectingTime",
"==",
"0",
"{",
"expectingTime",
"=",
"math",
".",
"MaxInt64",
"\n",
"}",
"else",
"{",
"expectingTime",
"*=",
"2",
"\n",
"}",
"\n",
"return",
"int",
"(",
"clamp",
"(",
"1",
",",
"int64",
"(",
"cn",
".",
"PeerMaxRequests",
")",
",",
"max",
"(",
"// It makes sense to always pipeline at least one connection,",
"// since latency must be non-zero.",
"2",
",",
"// Request only as many as we expect to receive in the",
"// dupliateRequestTimeout window. We are trying to avoid having to",
"// duplicate requests.",
"cn",
".",
"chunksReceivedWhileExpecting",
"*",
"int64",
"(",
"cn",
".",
"t",
".",
"duplicateRequestTimeout",
")",
"/",
"expectingTime",
",",
")",
",",
")",
")",
"\n",
"}",
"\n",
"return",
"int",
"(",
"clamp",
"(",
"1",
",",
"int64",
"(",
"cn",
".",
"PeerMaxRequests",
")",
",",
"max",
"(",
"64",
",",
"cn",
".",
"stats",
".",
"ChunksReadUseful",
".",
"Int64",
"(",
")",
"-",
"(",
"cn",
".",
"stats",
".",
"ChunksRead",
".",
"Int64",
"(",
")",
"-",
"cn",
".",
"stats",
".",
"ChunksReadUseful",
".",
"Int64",
"(",
")",
")",
")",
")",
")",
"\n",
"}"
] |
// The actual value to use as the maximum outbound requests.
|
[
"The",
"actual",
"value",
"to",
"use",
"as",
"the",
"maximum",
"outbound",
"requests",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L375-L402
|
21,984
|
anacrolix/torrent
|
connection.go
|
request
|
func (cn *connection) request(r request, mw messageWriter) bool {
if _, ok := cn.requests[r]; ok {
panic("chunk already requested")
}
if !cn.PeerHasPiece(pieceIndex(r.Index)) {
panic("requesting piece peer doesn't have")
}
if _, ok := cn.t.conns[cn]; !ok {
panic("requesting but not in active conns")
}
if cn.closed.IsSet() {
panic("requesting when connection is closed")
}
if cn.PeerChoked {
if cn.peerAllowedFast.Get(int(r.Index)) {
torrent.Add("allowed fast requests sent", 1)
} else {
panic("requesting while choked and not allowed fast")
}
}
if cn.t.hashingPiece(pieceIndex(r.Index)) {
panic("piece is being hashed")
}
if cn.t.pieceQueuedForHash(pieceIndex(r.Index)) {
panic("piece is queued for hash")
}
if cn.requests == nil {
cn.requests = make(map[request]struct{})
}
cn.requests[r] = struct{}{}
if cn.validReceiveChunks == nil {
cn.validReceiveChunks = make(map[request]struct{})
}
cn.validReceiveChunks[r] = struct{}{}
cn.t.pendingRequests[r]++
cn.t.lastRequested[r] = time.AfterFunc(cn.t.duplicateRequestTimeout, func() {
torrent.Add("duplicate request timeouts", 1)
cn.mu().Lock()
defer cn.mu().Unlock()
delete(cn.t.lastRequested, r)
for cn := range cn.t.conns {
if cn.PeerHasPiece(pieceIndex(r.Index)) {
cn.updateRequests()
}
}
})
cn.updateExpectingChunks()
return mw(pp.Message{
Type: pp.Request,
Index: r.Index,
Begin: r.Begin,
Length: r.Length,
})
}
|
go
|
func (cn *connection) request(r request, mw messageWriter) bool {
if _, ok := cn.requests[r]; ok {
panic("chunk already requested")
}
if !cn.PeerHasPiece(pieceIndex(r.Index)) {
panic("requesting piece peer doesn't have")
}
if _, ok := cn.t.conns[cn]; !ok {
panic("requesting but not in active conns")
}
if cn.closed.IsSet() {
panic("requesting when connection is closed")
}
if cn.PeerChoked {
if cn.peerAllowedFast.Get(int(r.Index)) {
torrent.Add("allowed fast requests sent", 1)
} else {
panic("requesting while choked and not allowed fast")
}
}
if cn.t.hashingPiece(pieceIndex(r.Index)) {
panic("piece is being hashed")
}
if cn.t.pieceQueuedForHash(pieceIndex(r.Index)) {
panic("piece is queued for hash")
}
if cn.requests == nil {
cn.requests = make(map[request]struct{})
}
cn.requests[r] = struct{}{}
if cn.validReceiveChunks == nil {
cn.validReceiveChunks = make(map[request]struct{})
}
cn.validReceiveChunks[r] = struct{}{}
cn.t.pendingRequests[r]++
cn.t.lastRequested[r] = time.AfterFunc(cn.t.duplicateRequestTimeout, func() {
torrent.Add("duplicate request timeouts", 1)
cn.mu().Lock()
defer cn.mu().Unlock()
delete(cn.t.lastRequested, r)
for cn := range cn.t.conns {
if cn.PeerHasPiece(pieceIndex(r.Index)) {
cn.updateRequests()
}
}
})
cn.updateExpectingChunks()
return mw(pp.Message{
Type: pp.Request,
Index: r.Index,
Begin: r.Begin,
Length: r.Length,
})
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"request",
"(",
"r",
"request",
",",
"mw",
"messageWriter",
")",
"bool",
"{",
"if",
"_",
",",
"ok",
":=",
"cn",
".",
"requests",
"[",
"r",
"]",
";",
"ok",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"!",
"cn",
".",
"PeerHasPiece",
"(",
"pieceIndex",
"(",
"r",
".",
"Index",
")",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"_",
",",
"ok",
":=",
"cn",
".",
"t",
".",
"conns",
"[",
"cn",
"]",
";",
"!",
"ok",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"cn",
".",
"closed",
".",
"IsSet",
"(",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"cn",
".",
"PeerChoked",
"{",
"if",
"cn",
".",
"peerAllowedFast",
".",
"Get",
"(",
"int",
"(",
"r",
".",
"Index",
")",
")",
"{",
"torrent",
".",
"Add",
"(",
"\"",
"\"",
",",
"1",
")",
"\n",
"}",
"else",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"cn",
".",
"t",
".",
"hashingPiece",
"(",
"pieceIndex",
"(",
"r",
".",
"Index",
")",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"cn",
".",
"t",
".",
"pieceQueuedForHash",
"(",
"pieceIndex",
"(",
"r",
".",
"Index",
")",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"cn",
".",
"requests",
"==",
"nil",
"{",
"cn",
".",
"requests",
"=",
"make",
"(",
"map",
"[",
"request",
"]",
"struct",
"{",
"}",
")",
"\n",
"}",
"\n",
"cn",
".",
"requests",
"[",
"r",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"if",
"cn",
".",
"validReceiveChunks",
"==",
"nil",
"{",
"cn",
".",
"validReceiveChunks",
"=",
"make",
"(",
"map",
"[",
"request",
"]",
"struct",
"{",
"}",
")",
"\n",
"}",
"\n",
"cn",
".",
"validReceiveChunks",
"[",
"r",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"cn",
".",
"t",
".",
"pendingRequests",
"[",
"r",
"]",
"++",
"\n",
"cn",
".",
"t",
".",
"lastRequested",
"[",
"r",
"]",
"=",
"time",
".",
"AfterFunc",
"(",
"cn",
".",
"t",
".",
"duplicateRequestTimeout",
",",
"func",
"(",
")",
"{",
"torrent",
".",
"Add",
"(",
"\"",
"\"",
",",
"1",
")",
"\n",
"cn",
".",
"mu",
"(",
")",
".",
"Lock",
"(",
")",
"\n",
"defer",
"cn",
".",
"mu",
"(",
")",
".",
"Unlock",
"(",
")",
"\n",
"delete",
"(",
"cn",
".",
"t",
".",
"lastRequested",
",",
"r",
")",
"\n",
"for",
"cn",
":=",
"range",
"cn",
".",
"t",
".",
"conns",
"{",
"if",
"cn",
".",
"PeerHasPiece",
"(",
"pieceIndex",
"(",
"r",
".",
"Index",
")",
")",
"{",
"cn",
".",
"updateRequests",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
")",
"\n",
"cn",
".",
"updateExpectingChunks",
"(",
")",
"\n",
"return",
"mw",
"(",
"pp",
".",
"Message",
"{",
"Type",
":",
"pp",
".",
"Request",
",",
"Index",
":",
"r",
".",
"Index",
",",
"Begin",
":",
"r",
".",
"Begin",
",",
"Length",
":",
"r",
".",
"Length",
",",
"}",
")",
"\n",
"}"
] |
// Proxies the messageWriter's response.
|
[
"Proxies",
"the",
"messageWriter",
"s",
"response",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L482-L535
|
21,985
|
anacrolix/torrent
|
connection.go
|
writer
|
func (cn *connection) writer(keepAliveTimeout time.Duration) {
var (
lastWrite time.Time = time.Now()
keepAliveTimer *time.Timer
)
keepAliveTimer = time.AfterFunc(keepAliveTimeout, func() {
cn.mu().Lock()
defer cn.mu().Unlock()
if time.Since(lastWrite) >= keepAliveTimeout {
cn.tickleWriter()
}
keepAliveTimer.Reset(keepAliveTimeout)
})
cn.mu().Lock()
defer cn.mu().Unlock()
defer cn.Close()
defer keepAliveTimer.Stop()
frontBuf := new(bytes.Buffer)
for {
if cn.closed.IsSet() {
return
}
if cn.writeBuffer.Len() == 0 {
cn.fillWriteBuffer(func(msg pp.Message) bool {
cn.wroteMsg(&msg)
cn.writeBuffer.Write(msg.MustMarshalBinary())
torrent.Add(fmt.Sprintf("messages filled of type %s", msg.Type.String()), 1)
return cn.writeBuffer.Len() < 1<<16 // 64KiB
})
}
if cn.writeBuffer.Len() == 0 && time.Since(lastWrite) >= keepAliveTimeout {
cn.writeBuffer.Write(pp.Message{Keepalive: true}.MustMarshalBinary())
postedKeepalives.Add(1)
}
if cn.writeBuffer.Len() == 0 {
// TODO: Minimize wakeups....
cn.writerCond.Wait()
continue
}
// Flip the buffers.
frontBuf, cn.writeBuffer = cn.writeBuffer, frontBuf
cn.mu().Unlock()
n, err := cn.w.Write(frontBuf.Bytes())
cn.mu().Lock()
if n != 0 {
lastWrite = time.Now()
keepAliveTimer.Reset(keepAliveTimeout)
}
if err != nil {
return
}
if n != frontBuf.Len() {
panic("short write")
}
frontBuf.Reset()
}
}
|
go
|
func (cn *connection) writer(keepAliveTimeout time.Duration) {
var (
lastWrite time.Time = time.Now()
keepAliveTimer *time.Timer
)
keepAliveTimer = time.AfterFunc(keepAliveTimeout, func() {
cn.mu().Lock()
defer cn.mu().Unlock()
if time.Since(lastWrite) >= keepAliveTimeout {
cn.tickleWriter()
}
keepAliveTimer.Reset(keepAliveTimeout)
})
cn.mu().Lock()
defer cn.mu().Unlock()
defer cn.Close()
defer keepAliveTimer.Stop()
frontBuf := new(bytes.Buffer)
for {
if cn.closed.IsSet() {
return
}
if cn.writeBuffer.Len() == 0 {
cn.fillWriteBuffer(func(msg pp.Message) bool {
cn.wroteMsg(&msg)
cn.writeBuffer.Write(msg.MustMarshalBinary())
torrent.Add(fmt.Sprintf("messages filled of type %s", msg.Type.String()), 1)
return cn.writeBuffer.Len() < 1<<16 // 64KiB
})
}
if cn.writeBuffer.Len() == 0 && time.Since(lastWrite) >= keepAliveTimeout {
cn.writeBuffer.Write(pp.Message{Keepalive: true}.MustMarshalBinary())
postedKeepalives.Add(1)
}
if cn.writeBuffer.Len() == 0 {
// TODO: Minimize wakeups....
cn.writerCond.Wait()
continue
}
// Flip the buffers.
frontBuf, cn.writeBuffer = cn.writeBuffer, frontBuf
cn.mu().Unlock()
n, err := cn.w.Write(frontBuf.Bytes())
cn.mu().Lock()
if n != 0 {
lastWrite = time.Now()
keepAliveTimer.Reset(keepAliveTimeout)
}
if err != nil {
return
}
if n != frontBuf.Len() {
panic("short write")
}
frontBuf.Reset()
}
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"writer",
"(",
"keepAliveTimeout",
"time",
".",
"Duration",
")",
"{",
"var",
"(",
"lastWrite",
"time",
".",
"Time",
"=",
"time",
".",
"Now",
"(",
")",
"\n",
"keepAliveTimer",
"*",
"time",
".",
"Timer",
"\n",
")",
"\n",
"keepAliveTimer",
"=",
"time",
".",
"AfterFunc",
"(",
"keepAliveTimeout",
",",
"func",
"(",
")",
"{",
"cn",
".",
"mu",
"(",
")",
".",
"Lock",
"(",
")",
"\n",
"defer",
"cn",
".",
"mu",
"(",
")",
".",
"Unlock",
"(",
")",
"\n",
"if",
"time",
".",
"Since",
"(",
"lastWrite",
")",
">=",
"keepAliveTimeout",
"{",
"cn",
".",
"tickleWriter",
"(",
")",
"\n",
"}",
"\n",
"keepAliveTimer",
".",
"Reset",
"(",
"keepAliveTimeout",
")",
"\n",
"}",
")",
"\n",
"cn",
".",
"mu",
"(",
")",
".",
"Lock",
"(",
")",
"\n",
"defer",
"cn",
".",
"mu",
"(",
")",
".",
"Unlock",
"(",
")",
"\n",
"defer",
"cn",
".",
"Close",
"(",
")",
"\n",
"defer",
"keepAliveTimer",
".",
"Stop",
"(",
")",
"\n",
"frontBuf",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n",
"for",
"{",
"if",
"cn",
".",
"closed",
".",
"IsSet",
"(",
")",
"{",
"return",
"\n",
"}",
"\n",
"if",
"cn",
".",
"writeBuffer",
".",
"Len",
"(",
")",
"==",
"0",
"{",
"cn",
".",
"fillWriteBuffer",
"(",
"func",
"(",
"msg",
"pp",
".",
"Message",
")",
"bool",
"{",
"cn",
".",
"wroteMsg",
"(",
"&",
"msg",
")",
"\n",
"cn",
".",
"writeBuffer",
".",
"Write",
"(",
"msg",
".",
"MustMarshalBinary",
"(",
")",
")",
"\n",
"torrent",
".",
"Add",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"msg",
".",
"Type",
".",
"String",
"(",
")",
")",
",",
"1",
")",
"\n",
"return",
"cn",
".",
"writeBuffer",
".",
"Len",
"(",
")",
"<",
"1",
"<<",
"16",
"// 64KiB",
"\n",
"}",
")",
"\n",
"}",
"\n",
"if",
"cn",
".",
"writeBuffer",
".",
"Len",
"(",
")",
"==",
"0",
"&&",
"time",
".",
"Since",
"(",
"lastWrite",
")",
">=",
"keepAliveTimeout",
"{",
"cn",
".",
"writeBuffer",
".",
"Write",
"(",
"pp",
".",
"Message",
"{",
"Keepalive",
":",
"true",
"}",
".",
"MustMarshalBinary",
"(",
")",
")",
"\n",
"postedKeepalives",
".",
"Add",
"(",
"1",
")",
"\n",
"}",
"\n",
"if",
"cn",
".",
"writeBuffer",
".",
"Len",
"(",
")",
"==",
"0",
"{",
"// TODO: Minimize wakeups....",
"cn",
".",
"writerCond",
".",
"Wait",
"(",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"// Flip the buffers.",
"frontBuf",
",",
"cn",
".",
"writeBuffer",
"=",
"cn",
".",
"writeBuffer",
",",
"frontBuf",
"\n",
"cn",
".",
"mu",
"(",
")",
".",
"Unlock",
"(",
")",
"\n",
"n",
",",
"err",
":=",
"cn",
".",
"w",
".",
"Write",
"(",
"frontBuf",
".",
"Bytes",
"(",
")",
")",
"\n",
"cn",
".",
"mu",
"(",
")",
".",
"Lock",
"(",
")",
"\n",
"if",
"n",
"!=",
"0",
"{",
"lastWrite",
"=",
"time",
".",
"Now",
"(",
")",
"\n",
"keepAliveTimer",
".",
"Reset",
"(",
"keepAliveTimeout",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"if",
"n",
"!=",
"frontBuf",
".",
"Len",
"(",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"frontBuf",
".",
"Reset",
"(",
")",
"\n",
"}",
"\n",
"}"
] |
// Routine that writes to the peer. Some of what to write is buffered by
// activity elsewhere in the Client, and some is determined locally when the
// connection is writable.
|
[
"Routine",
"that",
"writes",
"to",
"the",
"peer",
".",
"Some",
"of",
"what",
"to",
"write",
"is",
"buffered",
"by",
"activity",
"elsewhere",
"in",
"the",
"Client",
"and",
"some",
"is",
"determined",
"locally",
"when",
"the",
"connection",
"is",
"writable",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L593-L649
|
21,986
|
anacrolix/torrent
|
connection.go
|
iterBitmapsDistinct
|
func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
return func(cb iter.Callback) {
for _, bm := range bms {
if !iter.All(func(i interface{}) bool {
skip.Add(i.(int))
return cb(i)
}, bitmap.Sub(bm, *skip).Iter) {
return
}
}
}
}
|
go
|
func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
return func(cb iter.Callback) {
for _, bm := range bms {
if !iter.All(func(i interface{}) bool {
skip.Add(i.(int))
return cb(i)
}, bitmap.Sub(bm, *skip).Iter) {
return
}
}
}
}
|
[
"func",
"iterBitmapsDistinct",
"(",
"skip",
"*",
"bitmap",
".",
"Bitmap",
",",
"bms",
"...",
"bitmap",
".",
"Bitmap",
")",
"iter",
".",
"Func",
"{",
"return",
"func",
"(",
"cb",
"iter",
".",
"Callback",
")",
"{",
"for",
"_",
",",
"bm",
":=",
"range",
"bms",
"{",
"if",
"!",
"iter",
".",
"All",
"(",
"func",
"(",
"i",
"interface",
"{",
"}",
")",
"bool",
"{",
"skip",
".",
"Add",
"(",
"i",
".",
"(",
"int",
")",
")",
"\n",
"return",
"cb",
"(",
"i",
")",
"\n",
"}",
",",
"bitmap",
".",
"Sub",
"(",
"bm",
",",
"*",
"skip",
")",
".",
"Iter",
")",
"{",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Emits the indices in the Bitmaps bms in order, never repeating any index.
// skip is mutated during execution, and its initial values will never be
// emitted.
|
[
"Emits",
"the",
"indices",
"in",
"the",
"Bitmaps",
"bms",
"in",
"order",
"never",
"repeating",
"any",
"index",
".",
"skip",
"is",
"mutated",
"during",
"execution",
"and",
"its",
"initial",
"values",
"will",
"never",
"be",
"emitted",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L684-L695
|
21,987
|
anacrolix/torrent
|
connection.go
|
shouldRequestWithoutBias
|
func (cn *connection) shouldRequestWithoutBias() bool {
if cn.t.requestStrategy != 2 {
return false
}
if len(cn.t.readers) == 0 {
return false
}
if len(cn.t.conns) == 1 {
return true
}
if cn == cn.t.fastestConn {
return true
}
return false
}
|
go
|
func (cn *connection) shouldRequestWithoutBias() bool {
if cn.t.requestStrategy != 2 {
return false
}
if len(cn.t.readers) == 0 {
return false
}
if len(cn.t.conns) == 1 {
return true
}
if cn == cn.t.fastestConn {
return true
}
return false
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"shouldRequestWithoutBias",
"(",
")",
"bool",
"{",
"if",
"cn",
".",
"t",
".",
"requestStrategy",
"!=",
"2",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"len",
"(",
"cn",
".",
"t",
".",
"readers",
")",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"len",
"(",
"cn",
".",
"t",
".",
"conns",
")",
"==",
"1",
"{",
"return",
"true",
"\n",
"}",
"\n",
"if",
"cn",
"==",
"cn",
".",
"t",
".",
"fastestConn",
"{",
"return",
"true",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// The connection should download highest priority pieces first, without any
// inclination toward avoiding wastage. Generally we might do this if there's
// a single connection, or this is the fastest connection, and we have active
// readers that signal an ordering preference. It's conceivable that the best
// connection should do this, since it's least likely to waste our time if
// assigned to the highest priority pieces, and assigning more than one this
// role would cause significant wasted bandwidth.
|
[
"The",
"connection",
"should",
"download",
"highest",
"priority",
"pieces",
"first",
"without",
"any",
"inclination",
"toward",
"avoiding",
"wastage",
".",
"Generally",
"we",
"might",
"do",
"this",
"if",
"there",
"s",
"a",
"single",
"connection",
"or",
"this",
"is",
"the",
"fastest",
"connection",
"and",
"we",
"have",
"active",
"readers",
"that",
"signal",
"an",
"ordering",
"preference",
".",
"It",
"s",
"conceivable",
"that",
"the",
"best",
"connection",
"should",
"do",
"this",
"since",
"it",
"s",
"least",
"likely",
"to",
"waste",
"our",
"time",
"if",
"assigned",
"to",
"the",
"highest",
"priority",
"pieces",
"and",
"assigning",
"more",
"than",
"one",
"this",
"role",
"would",
"cause",
"significant",
"wasted",
"bandwidth",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L738-L752
|
21,988
|
anacrolix/torrent
|
connection.go
|
stopRequestingPiece
|
func (cn *connection) stopRequestingPiece(piece pieceIndex) bool {
return cn.pieceRequestOrder.Remove(bitmap.BitIndex(piece))
}
|
go
|
func (cn *connection) stopRequestingPiece(piece pieceIndex) bool {
return cn.pieceRequestOrder.Remove(bitmap.BitIndex(piece))
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"stopRequestingPiece",
"(",
"piece",
"pieceIndex",
")",
"bool",
"{",
"return",
"cn",
".",
"pieceRequestOrder",
".",
"Remove",
"(",
"bitmap",
".",
"BitIndex",
"(",
"piece",
")",
")",
"\n",
"}"
] |
// check callers updaterequests
|
[
"check",
"callers",
"updaterequests"
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L811-L813
|
21,989
|
anacrolix/torrent
|
connection.go
|
updatePiecePriority
|
func (cn *connection) updatePiecePriority(piece pieceIndex) bool {
tpp := cn.t.piecePriority(piece)
if !cn.PeerHasPiece(piece) {
tpp = PiecePriorityNone
}
if tpp == PiecePriorityNone {
return cn.stopRequestingPiece(piece)
}
prio := cn.getPieceInclination()[piece]
switch cn.t.requestStrategy {
case 1:
switch tpp {
case PiecePriorityNormal:
case PiecePriorityReadahead:
prio -= int(cn.t.numPieces())
case PiecePriorityNext, PiecePriorityNow:
prio -= 2 * int(cn.t.numPieces())
default:
panic(tpp)
}
prio += int(piece / 3)
default:
}
return cn.pieceRequestOrder.Set(bitmap.BitIndex(piece), prio) || cn.shouldRequestWithoutBias()
}
|
go
|
func (cn *connection) updatePiecePriority(piece pieceIndex) bool {
tpp := cn.t.piecePriority(piece)
if !cn.PeerHasPiece(piece) {
tpp = PiecePriorityNone
}
if tpp == PiecePriorityNone {
return cn.stopRequestingPiece(piece)
}
prio := cn.getPieceInclination()[piece]
switch cn.t.requestStrategy {
case 1:
switch tpp {
case PiecePriorityNormal:
case PiecePriorityReadahead:
prio -= int(cn.t.numPieces())
case PiecePriorityNext, PiecePriorityNow:
prio -= 2 * int(cn.t.numPieces())
default:
panic(tpp)
}
prio += int(piece / 3)
default:
}
return cn.pieceRequestOrder.Set(bitmap.BitIndex(piece), prio) || cn.shouldRequestWithoutBias()
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"updatePiecePriority",
"(",
"piece",
"pieceIndex",
")",
"bool",
"{",
"tpp",
":=",
"cn",
".",
"t",
".",
"piecePriority",
"(",
"piece",
")",
"\n",
"if",
"!",
"cn",
".",
"PeerHasPiece",
"(",
"piece",
")",
"{",
"tpp",
"=",
"PiecePriorityNone",
"\n",
"}",
"\n",
"if",
"tpp",
"==",
"PiecePriorityNone",
"{",
"return",
"cn",
".",
"stopRequestingPiece",
"(",
"piece",
")",
"\n",
"}",
"\n",
"prio",
":=",
"cn",
".",
"getPieceInclination",
"(",
")",
"[",
"piece",
"]",
"\n",
"switch",
"cn",
".",
"t",
".",
"requestStrategy",
"{",
"case",
"1",
":",
"switch",
"tpp",
"{",
"case",
"PiecePriorityNormal",
":",
"case",
"PiecePriorityReadahead",
":",
"prio",
"-=",
"int",
"(",
"cn",
".",
"t",
".",
"numPieces",
"(",
")",
")",
"\n",
"case",
"PiecePriorityNext",
",",
"PiecePriorityNow",
":",
"prio",
"-=",
"2",
"*",
"int",
"(",
"cn",
".",
"t",
".",
"numPieces",
"(",
")",
")",
"\n",
"default",
":",
"panic",
"(",
"tpp",
")",
"\n",
"}",
"\n",
"prio",
"+=",
"int",
"(",
"piece",
"/",
"3",
")",
"\n",
"default",
":",
"}",
"\n",
"return",
"cn",
".",
"pieceRequestOrder",
".",
"Set",
"(",
"bitmap",
".",
"BitIndex",
"(",
"piece",
")",
",",
"prio",
")",
"||",
"cn",
".",
"shouldRequestWithoutBias",
"(",
")",
"\n",
"}"
] |
// This is distinct from Torrent piece priority, which is the user's
// preference. Connection piece priority is specific to a connection and is
// used to pseudorandomly avoid connections always requesting the same pieces
// and thus wasting effort.
|
[
"This",
"is",
"distinct",
"from",
"Torrent",
"piece",
"priority",
"which",
"is",
"the",
"user",
"s",
"preference",
".",
"Connection",
"piece",
"priority",
"is",
"specific",
"to",
"a",
"connection",
"and",
"is",
"used",
"to",
"pseudorandomly",
"avoid",
"connections",
"always",
"requesting",
"the",
"same",
"pieces",
"and",
"thus",
"wasting",
"effort",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L819-L843
|
21,990
|
anacrolix/torrent
|
connection.go
|
postHandshakeStats
|
func (cn *connection) postHandshakeStats(f func(*ConnStats)) {
t := cn.t
f(&t.stats)
f(&t.cl.stats)
}
|
go
|
func (cn *connection) postHandshakeStats(f func(*ConnStats)) {
t := cn.t
f(&t.stats)
f(&t.cl.stats)
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"postHandshakeStats",
"(",
"f",
"func",
"(",
"*",
"ConnStats",
")",
")",
"{",
"t",
":=",
"cn",
".",
"t",
"\n",
"f",
"(",
"&",
"t",
".",
"stats",
")",
"\n",
"f",
"(",
"&",
"t",
".",
"cl",
".",
"stats",
")",
"\n",
"}"
] |
// After handshake, we know what Torrent and Client stats to include for a
// connection.
|
[
"After",
"handshake",
"we",
"know",
"what",
"Torrent",
"and",
"Client",
"stats",
"to",
"include",
"for",
"a",
"connection",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L962-L966
|
21,991
|
anacrolix/torrent
|
connection.go
|
allStats
|
func (cn *connection) allStats(f func(*ConnStats)) {
f(&cn.stats)
if cn.reconciledHandshakeStats {
cn.postHandshakeStats(f)
}
}
|
go
|
func (cn *connection) allStats(f func(*ConnStats)) {
f(&cn.stats)
if cn.reconciledHandshakeStats {
cn.postHandshakeStats(f)
}
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"allStats",
"(",
"f",
"func",
"(",
"*",
"ConnStats",
")",
")",
"{",
"f",
"(",
"&",
"cn",
".",
"stats",
")",
"\n",
"if",
"cn",
".",
"reconciledHandshakeStats",
"{",
"cn",
".",
"postHandshakeStats",
"(",
"f",
")",
"\n",
"}",
"\n",
"}"
] |
// All ConnStats that include this connection. Some objects are not known
// until the handshake is complete, after which it's expected to reconcile the
// differences.
|
[
"All",
"ConnStats",
"that",
"include",
"this",
"connection",
".",
"Some",
"objects",
"are",
"not",
"known",
"until",
"the",
"handshake",
"is",
"complete",
"after",
"which",
"it",
"s",
"expected",
"to",
"reconcile",
"the",
"differences",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L971-L976
|
21,992
|
anacrolix/torrent
|
connection.go
|
useful
|
func (c *connection) useful() bool {
t := c.t
if c.closed.IsSet() {
return false
}
if !t.haveInfo() {
return c.supportsExtension("ut_metadata")
}
if t.seeding() && c.PeerInterested {
return true
}
if c.peerHasWantedPieces() {
return true
}
return false
}
|
go
|
func (c *connection) useful() bool {
t := c.t
if c.closed.IsSet() {
return false
}
if !t.haveInfo() {
return c.supportsExtension("ut_metadata")
}
if t.seeding() && c.PeerInterested {
return true
}
if c.peerHasWantedPieces() {
return true
}
return false
}
|
[
"func",
"(",
"c",
"*",
"connection",
")",
"useful",
"(",
")",
"bool",
"{",
"t",
":=",
"c",
".",
"t",
"\n",
"if",
"c",
".",
"closed",
".",
"IsSet",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"!",
"t",
".",
"haveInfo",
"(",
")",
"{",
"return",
"c",
".",
"supportsExtension",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"t",
".",
"seeding",
"(",
")",
"&&",
"c",
".",
"PeerInterested",
"{",
"return",
"true",
"\n",
"}",
"\n",
"if",
"c",
".",
"peerHasWantedPieces",
"(",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// Returns whether the connection could be useful to us. We're seeding and
// they want data, we don't have metainfo and they can provide it, etc.
|
[
"Returns",
"whether",
"the",
"connection",
"could",
"be",
"useful",
"to",
"us",
".",
"We",
"re",
"seeding",
"and",
"they",
"want",
"data",
"we",
"don",
"t",
"have",
"metainfo",
"and",
"they",
"can",
"provide",
"it",
"etc",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L988-L1003
|
21,993
|
anacrolix/torrent
|
connection.go
|
setRW
|
func (cn *connection) setRW(rw io.ReadWriter) {
cn.r = rw
cn.w = rw
}
|
go
|
func (cn *connection) setRW(rw io.ReadWriter) {
cn.r = rw
cn.w = rw
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"setRW",
"(",
"rw",
"io",
".",
"ReadWriter",
")",
"{",
"cn",
".",
"r",
"=",
"rw",
"\n",
"cn",
".",
"w",
"=",
"rw",
"\n",
"}"
] |
// Set both the Reader and Writer for the connection from a single ReadWriter.
|
[
"Set",
"both",
"the",
"Reader",
"and",
"Writer",
"for",
"the",
"connection",
"from",
"a",
"single",
"ReadWriter",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L1253-L1256
|
21,994
|
anacrolix/torrent
|
connection.go
|
rw
|
func (cn *connection) rw() io.ReadWriter {
return struct {
io.Reader
io.Writer
}{cn.r, cn.w}
}
|
go
|
func (cn *connection) rw() io.ReadWriter {
return struct {
io.Reader
io.Writer
}{cn.r, cn.w}
}
|
[
"func",
"(",
"cn",
"*",
"connection",
")",
"rw",
"(",
")",
"io",
".",
"ReadWriter",
"{",
"return",
"struct",
"{",
"io",
".",
"Reader",
"\n",
"io",
".",
"Writer",
"\n",
"}",
"{",
"cn",
".",
"r",
",",
"cn",
".",
"w",
"}",
"\n",
"}"
] |
// Returns the Reader and Writer as a combined ReadWriter.
|
[
"Returns",
"the",
"Reader",
"and",
"Writer",
"as",
"a",
"combined",
"ReadWriter",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L1259-L1264
|
21,995
|
anacrolix/torrent
|
connection.go
|
upload
|
func (c *connection) upload(msg func(pp.Message) bool) bool {
// Breaking or completing this loop means we don't want to upload to the
// peer anymore, and we choke them.
another:
for c.uploadAllowed() {
// We want to upload to the peer.
if !c.Unchoke(msg) {
return false
}
for r := range c.PeerRequests {
res := c.t.cl.config.UploadRateLimiter.ReserveN(time.Now(), int(r.Length))
if !res.OK() {
panic(fmt.Sprintf("upload rate limiter burst size < %d", r.Length))
}
delay := res.Delay()
if delay > 0 {
res.Cancel()
c.setRetryUploadTimer(delay)
// Hard to say what to return here.
return true
}
more, err := c.sendChunk(r, msg)
if err != nil {
i := pieceIndex(r.Index)
if c.t.pieceComplete(i) {
c.t.updatePieceCompletion(i)
if !c.t.pieceComplete(i) {
// We had the piece, but not anymore.
break another
}
}
log.Str("error sending chunk to peer").AddValues(c, r, err).Log(c.t.logger)
// If we failed to send a chunk, choke the peer to ensure they
// flush all their requests. We've probably dropped a piece,
// but there's no way to communicate this to the peer. If they
// ask for it again, we'll kick them to allow us to send them
// an updated bitfield.
break another
}
delete(c.PeerRequests, r)
if !more {
return false
}
goto another
}
return true
}
return c.Choke(msg)
}
|
go
|
func (c *connection) upload(msg func(pp.Message) bool) bool {
// Breaking or completing this loop means we don't want to upload to the
// peer anymore, and we choke them.
another:
for c.uploadAllowed() {
// We want to upload to the peer.
if !c.Unchoke(msg) {
return false
}
for r := range c.PeerRequests {
res := c.t.cl.config.UploadRateLimiter.ReserveN(time.Now(), int(r.Length))
if !res.OK() {
panic(fmt.Sprintf("upload rate limiter burst size < %d", r.Length))
}
delay := res.Delay()
if delay > 0 {
res.Cancel()
c.setRetryUploadTimer(delay)
// Hard to say what to return here.
return true
}
more, err := c.sendChunk(r, msg)
if err != nil {
i := pieceIndex(r.Index)
if c.t.pieceComplete(i) {
c.t.updatePieceCompletion(i)
if !c.t.pieceComplete(i) {
// We had the piece, but not anymore.
break another
}
}
log.Str("error sending chunk to peer").AddValues(c, r, err).Log(c.t.logger)
// If we failed to send a chunk, choke the peer to ensure they
// flush all their requests. We've probably dropped a piece,
// but there's no way to communicate this to the peer. If they
// ask for it again, we'll kick them to allow us to send them
// an updated bitfield.
break another
}
delete(c.PeerRequests, r)
if !more {
return false
}
goto another
}
return true
}
return c.Choke(msg)
}
|
[
"func",
"(",
"c",
"*",
"connection",
")",
"upload",
"(",
"msg",
"func",
"(",
"pp",
".",
"Message",
")",
"bool",
")",
"bool",
"{",
"// Breaking or completing this loop means we don't want to upload to the",
"// peer anymore, and we choke them.",
"another",
":",
"for",
"c",
".",
"uploadAllowed",
"(",
")",
"{",
"// We want to upload to the peer.",
"if",
"!",
"c",
".",
"Unchoke",
"(",
"msg",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"for",
"r",
":=",
"range",
"c",
".",
"PeerRequests",
"{",
"res",
":=",
"c",
".",
"t",
".",
"cl",
".",
"config",
".",
"UploadRateLimiter",
".",
"ReserveN",
"(",
"time",
".",
"Now",
"(",
")",
",",
"int",
"(",
"r",
".",
"Length",
")",
")",
"\n",
"if",
"!",
"res",
".",
"OK",
"(",
")",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"r",
".",
"Length",
")",
")",
"\n",
"}",
"\n",
"delay",
":=",
"res",
".",
"Delay",
"(",
")",
"\n",
"if",
"delay",
">",
"0",
"{",
"res",
".",
"Cancel",
"(",
")",
"\n",
"c",
".",
"setRetryUploadTimer",
"(",
"delay",
")",
"\n",
"// Hard to say what to return here.",
"return",
"true",
"\n",
"}",
"\n",
"more",
",",
"err",
":=",
"c",
".",
"sendChunk",
"(",
"r",
",",
"msg",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"i",
":=",
"pieceIndex",
"(",
"r",
".",
"Index",
")",
"\n",
"if",
"c",
".",
"t",
".",
"pieceComplete",
"(",
"i",
")",
"{",
"c",
".",
"t",
".",
"updatePieceCompletion",
"(",
"i",
")",
"\n",
"if",
"!",
"c",
".",
"t",
".",
"pieceComplete",
"(",
"i",
")",
"{",
"// We had the piece, but not anymore.",
"break",
"another",
"\n",
"}",
"\n",
"}",
"\n",
"log",
".",
"Str",
"(",
"\"",
"\"",
")",
".",
"AddValues",
"(",
"c",
",",
"r",
",",
"err",
")",
".",
"Log",
"(",
"c",
".",
"t",
".",
"logger",
")",
"\n",
"// If we failed to send a chunk, choke the peer to ensure they",
"// flush all their requests. We've probably dropped a piece,",
"// but there's no way to communicate this to the peer. If they",
"// ask for it again, we'll kick them to allow us to send them",
"// an updated bitfield.",
"break",
"another",
"\n",
"}",
"\n",
"delete",
"(",
"c",
".",
"PeerRequests",
",",
"r",
")",
"\n",
"if",
"!",
"more",
"{",
"return",
"false",
"\n",
"}",
"\n",
"goto",
"another",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}",
"\n",
"return",
"c",
".",
"Choke",
"(",
"msg",
")",
"\n",
"}"
] |
// Also handles choking and unchoking of the remote peer.
|
[
"Also",
"handles",
"choking",
"and",
"unchoking",
"of",
"the",
"remote",
"peer",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/connection.go#L1400-L1448
|
21,996
|
anacrolix/torrent
|
iplist/iplist.go
|
Lookup
|
func (ipl *IPList) Lookup(ip net.IP) (r Range, ok bool) {
if ipl == nil {
return
}
// TODO: Perhaps all addresses should be converted to IPv6, if the future
// of IP is to always be backwards compatible. But this will cost 4x the
// memory for IPv4 addresses?
v4 := ip.To4()
if v4 != nil {
r, ok = ipl.lookup(v4)
if ok {
return
}
}
v6 := ip.To16()
if v6 != nil {
return ipl.lookup(v6)
}
if v4 == nil && v6 == nil {
r = Range{
Description: "bad IP",
}
ok = true
}
return
}
|
go
|
func (ipl *IPList) Lookup(ip net.IP) (r Range, ok bool) {
if ipl == nil {
return
}
// TODO: Perhaps all addresses should be converted to IPv6, if the future
// of IP is to always be backwards compatible. But this will cost 4x the
// memory for IPv4 addresses?
v4 := ip.To4()
if v4 != nil {
r, ok = ipl.lookup(v4)
if ok {
return
}
}
v6 := ip.To16()
if v6 != nil {
return ipl.lookup(v6)
}
if v4 == nil && v6 == nil {
r = Range{
Description: "bad IP",
}
ok = true
}
return
}
|
[
"func",
"(",
"ipl",
"*",
"IPList",
")",
"Lookup",
"(",
"ip",
"net",
".",
"IP",
")",
"(",
"r",
"Range",
",",
"ok",
"bool",
")",
"{",
"if",
"ipl",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"// TODO: Perhaps all addresses should be converted to IPv6, if the future",
"// of IP is to always be backwards compatible. But this will cost 4x the",
"// memory for IPv4 addresses?",
"v4",
":=",
"ip",
".",
"To4",
"(",
")",
"\n",
"if",
"v4",
"!=",
"nil",
"{",
"r",
",",
"ok",
"=",
"ipl",
".",
"lookup",
"(",
"v4",
")",
"\n",
"if",
"ok",
"{",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"v6",
":=",
"ip",
".",
"To16",
"(",
")",
"\n",
"if",
"v6",
"!=",
"nil",
"{",
"return",
"ipl",
".",
"lookup",
"(",
"v6",
")",
"\n",
"}",
"\n",
"if",
"v4",
"==",
"nil",
"&&",
"v6",
"==",
"nil",
"{",
"r",
"=",
"Range",
"{",
"Description",
":",
"\"",
"\"",
",",
"}",
"\n",
"ok",
"=",
"true",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Return the range the given IP is in. ok if false if no range is found.
|
[
"Return",
"the",
"range",
"the",
"given",
"IP",
"is",
"in",
".",
"ok",
"if",
"false",
"if",
"no",
"range",
"is",
"found",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/iplist/iplist.go#L53-L78
|
21,997
|
anacrolix/torrent
|
iplist/iplist.go
|
lookup
|
func lookup(
first func(i int) net.IP,
full func(i int) Range,
n int,
ip net.IP,
) (
r Range, ok bool,
) {
// Find the index of the first range for which the following range exceeds
// it.
i := sort.Search(n, func(i int) bool {
if i+1 >= n {
return true
}
return bytes.Compare(ip, first(i+1)) < 0
})
if i == n {
return
}
r = full(i)
ok = bytes.Compare(r.First, ip) <= 0 && bytes.Compare(ip, r.Last) <= 0
return
}
|
go
|
func lookup(
first func(i int) net.IP,
full func(i int) Range,
n int,
ip net.IP,
) (
r Range, ok bool,
) {
// Find the index of the first range for which the following range exceeds
// it.
i := sort.Search(n, func(i int) bool {
if i+1 >= n {
return true
}
return bytes.Compare(ip, first(i+1)) < 0
})
if i == n {
return
}
r = full(i)
ok = bytes.Compare(r.First, ip) <= 0 && bytes.Compare(ip, r.Last) <= 0
return
}
|
[
"func",
"lookup",
"(",
"first",
"func",
"(",
"i",
"int",
")",
"net",
".",
"IP",
",",
"full",
"func",
"(",
"i",
"int",
")",
"Range",
",",
"n",
"int",
",",
"ip",
"net",
".",
"IP",
",",
")",
"(",
"r",
"Range",
",",
"ok",
"bool",
",",
")",
"{",
"// Find the index of the first range for which the following range exceeds",
"// it.",
"i",
":=",
"sort",
".",
"Search",
"(",
"n",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"if",
"i",
"+",
"1",
">=",
"n",
"{",
"return",
"true",
"\n",
"}",
"\n",
"return",
"bytes",
".",
"Compare",
"(",
"ip",
",",
"first",
"(",
"i",
"+",
"1",
")",
")",
"<",
"0",
"\n",
"}",
")",
"\n",
"if",
"i",
"==",
"n",
"{",
"return",
"\n",
"}",
"\n",
"r",
"=",
"full",
"(",
"i",
")",
"\n",
"ok",
"=",
"bytes",
".",
"Compare",
"(",
"r",
".",
"First",
",",
"ip",
")",
"<=",
"0",
"&&",
"bytes",
".",
"Compare",
"(",
"ip",
",",
"r",
".",
"Last",
")",
"<=",
"0",
"\n",
"return",
"\n",
"}"
] |
// Return a range that contains ip, or nil.
|
[
"Return",
"a",
"range",
"that",
"contains",
"ip",
"or",
"nil",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/iplist/iplist.go#L81-L103
|
21,998
|
anacrolix/torrent
|
iplist/iplist.go
|
lookup
|
func (ipl *IPList) lookup(ip net.IP) (Range, bool) {
return lookup(func(i int) net.IP {
return ipl.ranges[i].First
}, func(i int) Range {
return ipl.ranges[i]
}, len(ipl.ranges), ip)
}
|
go
|
func (ipl *IPList) lookup(ip net.IP) (Range, bool) {
return lookup(func(i int) net.IP {
return ipl.ranges[i].First
}, func(i int) Range {
return ipl.ranges[i]
}, len(ipl.ranges), ip)
}
|
[
"func",
"(",
"ipl",
"*",
"IPList",
")",
"lookup",
"(",
"ip",
"net",
".",
"IP",
")",
"(",
"Range",
",",
"bool",
")",
"{",
"return",
"lookup",
"(",
"func",
"(",
"i",
"int",
")",
"net",
".",
"IP",
"{",
"return",
"ipl",
".",
"ranges",
"[",
"i",
"]",
".",
"First",
"\n",
"}",
",",
"func",
"(",
"i",
"int",
")",
"Range",
"{",
"return",
"ipl",
".",
"ranges",
"[",
"i",
"]",
"\n",
"}",
",",
"len",
"(",
"ipl",
".",
"ranges",
")",
",",
"ip",
")",
"\n",
"}"
] |
// Return the range the given IP is in. Returns nil if no range is found.
|
[
"Return",
"the",
"range",
"the",
"given",
"IP",
"is",
"in",
".",
"Returns",
"nil",
"if",
"no",
"range",
"is",
"found",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/iplist/iplist.go#L106-L112
|
21,999
|
anacrolix/torrent
|
iplist/iplist.go
|
NewFromReader
|
func NewFromReader(f io.Reader) (ret *IPList, err error) {
var ranges []Range
// There's a lot of similar descriptions, so we maintain a pool and reuse
// them to reduce memory overhead.
uniqStrs := make(map[string]string)
scanner := bufio.NewScanner(f)
lineNum := 1
for scanner.Scan() {
r, ok, lineErr := ParseBlocklistP2PLine(scanner.Bytes())
if lineErr != nil {
err = fmt.Errorf("error parsing line %d: %s", lineNum, lineErr)
return
}
lineNum++
if !ok {
continue
}
if s, ok := uniqStrs[r.Description]; ok {
r.Description = s
} else {
uniqStrs[r.Description] = r.Description
}
ranges = append(ranges, r)
}
err = scanner.Err()
if err != nil {
return
}
ret = New(ranges)
return
}
|
go
|
func NewFromReader(f io.Reader) (ret *IPList, err error) {
var ranges []Range
// There's a lot of similar descriptions, so we maintain a pool and reuse
// them to reduce memory overhead.
uniqStrs := make(map[string]string)
scanner := bufio.NewScanner(f)
lineNum := 1
for scanner.Scan() {
r, ok, lineErr := ParseBlocklistP2PLine(scanner.Bytes())
if lineErr != nil {
err = fmt.Errorf("error parsing line %d: %s", lineNum, lineErr)
return
}
lineNum++
if !ok {
continue
}
if s, ok := uniqStrs[r.Description]; ok {
r.Description = s
} else {
uniqStrs[r.Description] = r.Description
}
ranges = append(ranges, r)
}
err = scanner.Err()
if err != nil {
return
}
ret = New(ranges)
return
}
|
[
"func",
"NewFromReader",
"(",
"f",
"io",
".",
"Reader",
")",
"(",
"ret",
"*",
"IPList",
",",
"err",
"error",
")",
"{",
"var",
"ranges",
"[",
"]",
"Range",
"\n",
"// There's a lot of similar descriptions, so we maintain a pool and reuse",
"// them to reduce memory overhead.",
"uniqStrs",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n",
"scanner",
":=",
"bufio",
".",
"NewScanner",
"(",
"f",
")",
"\n",
"lineNum",
":=",
"1",
"\n",
"for",
"scanner",
".",
"Scan",
"(",
")",
"{",
"r",
",",
"ok",
",",
"lineErr",
":=",
"ParseBlocklistP2PLine",
"(",
"scanner",
".",
"Bytes",
"(",
")",
")",
"\n",
"if",
"lineErr",
"!=",
"nil",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"lineNum",
",",
"lineErr",
")",
"\n",
"return",
"\n",
"}",
"\n",
"lineNum",
"++",
"\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"s",
",",
"ok",
":=",
"uniqStrs",
"[",
"r",
".",
"Description",
"]",
";",
"ok",
"{",
"r",
".",
"Description",
"=",
"s",
"\n",
"}",
"else",
"{",
"uniqStrs",
"[",
"r",
".",
"Description",
"]",
"=",
"r",
".",
"Description",
"\n",
"}",
"\n",
"ranges",
"=",
"append",
"(",
"ranges",
",",
"r",
")",
"\n",
"}",
"\n",
"err",
"=",
"scanner",
".",
"Err",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"ret",
"=",
"New",
"(",
"ranges",
")",
"\n",
"return",
"\n",
"}"
] |
// Creates an IPList from a line-delimited P2P Plaintext file.
|
[
"Creates",
"an",
"IPList",
"from",
"a",
"line",
"-",
"delimited",
"P2P",
"Plaintext",
"file",
"."
] |
d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb
|
https://github.com/anacrolix/torrent/blob/d2400968fc54c3d8a6482d4ca5ff16cc5e5139cb/iplist/iplist.go#L155-L185
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.