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 list | docstring stringlengths 6 2.61k | docstring_tokens list | sha stringlengths 40 40 | url stringlengths 85 252 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
thoas/go-funk | typesafe.go | ContainsInt32 | func ContainsInt32(s []int32, v int32) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | go | func ContainsInt32(s []int32, v int32) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | [
"func",
"ContainsInt32",
"(",
"s",
"[",
"]",
"int32",
",",
"v",
"int32",
")",
"bool",
"{",
"for",
"_",
",",
"vv",
":=",
"range",
"s",
"{",
"if",
"vv",
"==",
"v",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // ContainsInt32 returns true if an int32 is present in a iteratee. | [
"ContainsInt32",
"returns",
"true",
"if",
"an",
"int32",
"is",
"present",
"in",
"a",
"iteratee",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L228-L235 | train |
thoas/go-funk | typesafe.go | ContainsInt64 | func ContainsInt64(s []int64, v int64) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | go | func ContainsInt64(s []int64, v int64) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | [
"func",
"ContainsInt64",
"(",
"s",
"[",
"]",
"int64",
",",
"v",
"int64",
")",
"bool",
"{",
"for",
"_",
",",
"vv",
":=",
"range",
"s",
"{",
"if",
"vv",
"==",
"v",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // ContainsInt64 returns true if an int64 is present in a iteratee. | [
"ContainsInt64",
"returns",
"true",
"if",
"an",
"int64",
"is",
"present",
"in",
"a",
"iteratee",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L238-L245 | train |
thoas/go-funk | typesafe.go | ContainsFloat32 | func ContainsFloat32(s []float32, v float32) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | go | func ContainsFloat32(s []float32, v float32) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | [
"func",
"ContainsFloat32",
"(",
"s",
"[",
"]",
"float32",
",",
"v",
"float32",
")",
"bool",
"{",
"for",
"_",
",",
"vv",
":=",
"range",
"s",
"{",
"if",
"vv",
"==",
"v",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // ContainsFloat32 returns true if a float32 is present in a iteratee. | [
"ContainsFloat32",
"returns",
"true",
"if",
"a",
"float32",
"is",
"present",
"in",
"a",
"iteratee",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L258-L265 | train |
thoas/go-funk | typesafe.go | ContainsFloat64 | func ContainsFloat64(s []float64, v float64) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | go | func ContainsFloat64(s []float64, v float64) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
} | [
"func",
"ContainsFloat64",
"(",
"s",
"[",
"]",
"float64",
",",
"v",
"float64",
")",
"bool",
"{",
"for",
"_",
",",
"vv",
":=",
"range",
"s",
"{",
"if",
"vv",
"==",
"v",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // ContainsFloat64 returns true if a float64 is present in a iteratee. | [
"ContainsFloat64",
"returns",
"true",
"if",
"a",
"float64",
"is",
"present",
"in",
"a",
"iteratee",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L268-L275 | train |
thoas/go-funk | typesafe.go | SumInt32 | func SumInt32(s []int32) (sum int32) {
for _, v := range s {
sum += v
}
return
} | go | func SumInt32(s []int32) (sum int32) {
for _, v := range s {
sum += v
}
return
} | [
"func",
"SumInt32",
"(",
"s",
"[",
"]",
"int32",
")",
"(",
"sum",
"int32",
")",
"{",
"for",
"_",
",",
"v",
":=",
"range",
"s",
"{",
"sum",
"+=",
"v",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] | // SumInt32 sums a int32 iteratee and returns the sum of all elements | [
"SumInt32",
"sums",
"a",
"int32",
"iteratee",
"and",
"returns",
"the",
"sum",
"of",
"all",
"elements"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L278-L283 | train |
thoas/go-funk | typesafe.go | SumInt64 | func SumInt64(s []int64) (sum int64) {
for _, v := range s {
sum += v
}
return
} | go | func SumInt64(s []int64) (sum int64) {
for _, v := range s {
sum += v
}
return
} | [
"func",
"SumInt64",
"(",
"s",
"[",
"]",
"int64",
")",
"(",
"sum",
"int64",
")",
"{",
"for",
"_",
",",
"v",
":=",
"range",
"s",
"{",
"sum",
"+=",
"v",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] | // SumInt64 sums a int64 iteratee and returns the sum of all elements | [
"SumInt64",
"sums",
"a",
"int64",
"iteratee",
"and",
"returns",
"the",
"sum",
"of",
"all",
"elements"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L286-L291 | train |
thoas/go-funk | typesafe.go | SumInt | func SumInt(s []int) (sum int) {
for _, v := range s {
sum += v
}
return
} | go | func SumInt(s []int) (sum int) {
for _, v := range s {
sum += v
}
return
} | [
"func",
"SumInt",
"(",
"s",
"[",
"]",
"int",
")",
"(",
"sum",
"int",
")",
"{",
"for",
"_",
",",
"v",
":=",
"range",
"s",
"{",
"sum",
"+=",
"v",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] | // SumInt sums a int iteratee and returns the sum of all elements | [
"SumInt",
"sums",
"a",
"int",
"iteratee",
"and",
"returns",
"the",
"sum",
"of",
"all",
"elements"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L294-L299 | train |
thoas/go-funk | typesafe.go | SumFloat64 | func SumFloat64(s []float64) (sum float64) {
for _, v := range s {
sum += v
}
return
} | go | func SumFloat64(s []float64) (sum float64) {
for _, v := range s {
sum += v
}
return
} | [
"func",
"SumFloat64",
"(",
"s",
"[",
"]",
"float64",
")",
"(",
"sum",
"float64",
")",
"{",
"for",
"_",
",",
"v",
":=",
"range",
"s",
"{",
"sum",
"+=",
"v",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] | // SumFloat64 sums a float64 iteratee and returns the sum of all elements | [
"SumFloat64",
"sums",
"a",
"float64",
"iteratee",
"and",
"returns",
"the",
"sum",
"of",
"all",
"elements"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L302-L307 | train |
thoas/go-funk | typesafe.go | SumFloat32 | func SumFloat32(s []float32) (sum float32) {
for _, v := range s {
sum += v
}
return
} | go | func SumFloat32(s []float32) (sum float32) {
for _, v := range s {
sum += v
}
return
} | [
"func",
"SumFloat32",
"(",
"s",
"[",
"]",
"float32",
")",
"(",
"sum",
"float32",
")",
"{",
"for",
"_",
",",
"v",
":=",
"range",
"s",
"{",
"sum",
"+=",
"v",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] | // SumFloat32 sums a float32 iteratee and returns the sum of all elements | [
"SumFloat32",
"sums",
"a",
"float32",
"iteratee",
"and",
"returns",
"the",
"sum",
"of",
"all",
"elements"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L310-L315 | train |
thoas/go-funk | typesafe.go | ReverseStrings | func ReverseStrings(s []string) []string {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | go | func ReverseStrings(s []string) []string {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | [
"func",
"ReverseStrings",
"(",
"s",
"[",
"]",
"string",
")",
"[",
"]",
"string",
"{",
"for",
"i",
",",
"j",
":=",
"0",
",",
"len",
"(",
"s",
")",
"-",
"1",
";",
"i",
"<",
"len",
"(",
"s",
")",
"/",
"2",
";",
"i",
",",
"j",
"=",
"i",
"+",
"1",
",",
"j",
"-",
"1",
"{",
"s",
"[",
"i",
"]",
",",
"s",
"[",
"j",
"]",
"=",
"s",
"[",
"j",
"]",
",",
"s",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // ReverseStrings reverses an array of string | [
"ReverseStrings",
"reverses",
"an",
"array",
"of",
"string"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L318-L323 | train |
thoas/go-funk | typesafe.go | ReverseInt | func ReverseInt(s []int) []int {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | go | func ReverseInt(s []int) []int {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | [
"func",
"ReverseInt",
"(",
"s",
"[",
"]",
"int",
")",
"[",
"]",
"int",
"{",
"for",
"i",
",",
"j",
":=",
"0",
",",
"len",
"(",
"s",
")",
"-",
"1",
";",
"i",
"<",
"len",
"(",
"s",
")",
"/",
"2",
";",
"i",
",",
"j",
"=",
"i",
"+",
"1",
",",
"j",
"-",
"1",
"{",
"s",
"[",
"i",
"]",
",",
"s",
"[",
"j",
"]",
"=",
"s",
"[",
"j",
"]",
",",
"s",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // ReverseInt reverses an array of int | [
"ReverseInt",
"reverses",
"an",
"array",
"of",
"int"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L326-L331 | train |
thoas/go-funk | typesafe.go | ReverseInt32 | func ReverseInt32(s []int32) []int32 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | go | func ReverseInt32(s []int32) []int32 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | [
"func",
"ReverseInt32",
"(",
"s",
"[",
"]",
"int32",
")",
"[",
"]",
"int32",
"{",
"for",
"i",
",",
"j",
":=",
"0",
",",
"len",
"(",
"s",
")",
"-",
"1",
";",
"i",
"<",
"len",
"(",
"s",
")",
"/",
"2",
";",
"i",
",",
"j",
"=",
"i",
"+",
"1",
",",
"j",
"-",
"1",
"{",
"s",
"[",
"i",
"]",
",",
"s",
"[",
"j",
"]",
"=",
"s",
"[",
"j",
"]",
",",
"s",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // ReverseInt32 reverses an array of int32 | [
"ReverseInt32",
"reverses",
"an",
"array",
"of",
"int32"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L334-L339 | train |
thoas/go-funk | typesafe.go | ReverseInt64 | func ReverseInt64(s []int64) []int64 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | go | func ReverseInt64(s []int64) []int64 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | [
"func",
"ReverseInt64",
"(",
"s",
"[",
"]",
"int64",
")",
"[",
"]",
"int64",
"{",
"for",
"i",
",",
"j",
":=",
"0",
",",
"len",
"(",
"s",
")",
"-",
"1",
";",
"i",
"<",
"len",
"(",
"s",
")",
"/",
"2",
";",
"i",
",",
"j",
"=",
"i",
"+",
"1",
",",
"j",
"-",
"1",
"{",
"s",
"[",
"i",
"]",
",",
"s",
"[",
"j",
"]",
"=",
"s",
"[",
"j",
"]",
",",
"s",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // ReverseInt64 reverses an array of int64 | [
"ReverseInt64",
"reverses",
"an",
"array",
"of",
"int64"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L342-L347 | train |
thoas/go-funk | typesafe.go | ReverseFloat64 | func ReverseFloat64(s []float64) []float64 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | go | func ReverseFloat64(s []float64) []float64 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | [
"func",
"ReverseFloat64",
"(",
"s",
"[",
"]",
"float64",
")",
"[",
"]",
"float64",
"{",
"for",
"i",
",",
"j",
":=",
"0",
",",
"len",
"(",
"s",
")",
"-",
"1",
";",
"i",
"<",
"len",
"(",
"s",
")",
"/",
"2",
";",
"i",
",",
"j",
"=",
"i",
"+",
"1",
",",
"j",
"-",
"1",
"{",
"s",
"[",
"i",
"]",
",",
"s",
"[",
"j",
"]",
"=",
"s",
"[",
"j",
"]",
",",
"s",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // ReverseFloat64 reverses an array of float64 | [
"ReverseFloat64",
"reverses",
"an",
"array",
"of",
"float64"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L350-L355 | train |
thoas/go-funk | typesafe.go | ReverseFloat32 | func ReverseFloat32(s []float32) []float32 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | go | func ReverseFloat32(s []float32) []float32 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
} | [
"func",
"ReverseFloat32",
"(",
"s",
"[",
"]",
"float32",
")",
"[",
"]",
"float32",
"{",
"for",
"i",
",",
"j",
":=",
"0",
",",
"len",
"(",
"s",
")",
"-",
"1",
";",
"i",
"<",
"len",
"(",
"s",
")",
"/",
"2",
";",
"i",
",",
"j",
"=",
"i",
"+",
"1",
",",
"j",
"-",
"1",
"{",
"s",
"[",
"i",
"]",
",",
"s",
"[",
"j",
"]",
"=",
"s",
"[",
"j",
"]",
",",
"s",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // ReverseFloat32 reverses an array of float32 | [
"ReverseFloat32",
"reverses",
"an",
"array",
"of",
"float32"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L358-L363 | train |
thoas/go-funk | typesafe.go | ReverseString | func ReverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
} | go | func ReverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
} | [
"func",
"ReverseString",
"(",
"s",
"string",
")",
"string",
"{",
"r",
":=",
"[",
"]",
"rune",
"(",
"s",
")",
"\n",
"for",
"i",
",",
"j",
":=",
"0",
",",
"len",
"(",
"r",
")",
"-",
"1",
";",
"i",
"<",
"len",
"(",
"r",
")",
"/",
"2",
";",
"i",
",",
"j",
"=",
"i",
"+",
"1",
",",
"j",
"-",
"1",
"{",
"r",
"[",
"i",
"]",
",",
"r",
"[",
"j",
"]",
"=",
"r",
"[",
"j",
"]",
",",
"r",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"string",
"(",
"r",
")",
"\n",
"}"
] | // ReverseString reverses a string | [
"ReverseString",
"reverses",
"a",
"string"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L366-L372 | train |
thoas/go-funk | typesafe.go | IndexOfInt | func IndexOfInt(a []int, x int) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func IndexOfInt(a []int, x int) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"IndexOfInt",
"(",
"a",
"[",
"]",
"int",
",",
"x",
"int",
")",
"int",
"{",
"return",
"indexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // IndexOfInt gets the index at which the first occurrence of an int value is found in array or return -1
// if the value cannot be found | [
"IndexOfInt",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"int",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L385-L387 | train |
thoas/go-funk | typesafe.go | IndexOfInt32 | func IndexOfInt32(a []int32, x int32) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func IndexOfInt32(a []int32, x int32) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"IndexOfInt32",
"(",
"a",
"[",
"]",
"int32",
",",
"x",
"int32",
")",
"int",
"{",
"return",
"indexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // IndexOfInt32 gets the index at which the first occurrence of an int32 value is found in array or return -1
// if the value cannot be found | [
"IndexOfInt32",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"int32",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L391-L393 | train |
thoas/go-funk | typesafe.go | IndexOfInt64 | func IndexOfInt64(a []int64, x int64) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func IndexOfInt64(a []int64, x int64) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"IndexOfInt64",
"(",
"a",
"[",
"]",
"int64",
",",
"x",
"int64",
")",
"int",
"{",
"return",
"indexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // IndexOfInt64 gets the index at which the first occurrence of an int64 value is found in array or return -1
// if the value cannot be found | [
"IndexOfInt64",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"int64",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L397-L399 | train |
thoas/go-funk | typesafe.go | IndexOfFloat64 | func IndexOfFloat64(a []float64, x float64) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func IndexOfFloat64(a []float64, x float64) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"IndexOfFloat64",
"(",
"a",
"[",
"]",
"float64",
",",
"x",
"float64",
")",
"int",
"{",
"return",
"indexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // IndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
// if the value cannot be found | [
"IndexOfFloat64",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"float64",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L403-L405 | train |
thoas/go-funk | typesafe.go | IndexOfString | func IndexOfString(a []string, x string) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func IndexOfString(a []string, x string) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"IndexOfString",
"(",
"a",
"[",
"]",
"string",
",",
"x",
"string",
")",
"int",
"{",
"return",
"indexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // IndexOfString gets the index at which the first occurrence of a string value is found in array or return -1
// if the value cannot be found | [
"IndexOfString",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"a",
"string",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L409-L411 | train |
thoas/go-funk | typesafe.go | LastIndexOfInt | func LastIndexOfInt(a []int, x int) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func LastIndexOfInt(a []int, x int) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"LastIndexOfInt",
"(",
"a",
"[",
"]",
"int",
",",
"x",
"int",
")",
"int",
"{",
"return",
"lastIndexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // LastIndexOfInt gets the index at which the first occurrence of an int value is found in array or return -1
// if the value cannot be found | [
"LastIndexOfInt",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"int",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L424-L426 | train |
thoas/go-funk | typesafe.go | LastIndexOfInt32 | func LastIndexOfInt32(a []int32, x int32) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func LastIndexOfInt32(a []int32, x int32) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"LastIndexOfInt32",
"(",
"a",
"[",
"]",
"int32",
",",
"x",
"int32",
")",
"int",
"{",
"return",
"lastIndexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // LastIndexOfInt32 gets the index at which the first occurrence of an int32 value is found in array or return -1
// if the value cannot be found | [
"LastIndexOfInt32",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"int32",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L430-L432 | train |
thoas/go-funk | typesafe.go | LastIndexOfInt64 | func LastIndexOfInt64(a []int64, x int64) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func LastIndexOfInt64(a []int64, x int64) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"LastIndexOfInt64",
"(",
"a",
"[",
"]",
"int64",
",",
"x",
"int64",
")",
"int",
"{",
"return",
"lastIndexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // LastIndexOfInt64 gets the index at which the first occurrence of an int64 value is found in array or return -1
// if the value cannot be found | [
"LastIndexOfInt64",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"int64",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L436-L438 | train |
thoas/go-funk | typesafe.go | LastIndexOfFloat64 | func LastIndexOfFloat64(a []float64, x float64) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func LastIndexOfFloat64(a []float64, x float64) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"LastIndexOfFloat64",
"(",
"a",
"[",
"]",
"float64",
",",
"x",
"float64",
")",
"int",
"{",
"return",
"lastIndexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // LastIndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
// if the value cannot be found | [
"LastIndexOfFloat64",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"float64",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L442-L444 | train |
thoas/go-funk | typesafe.go | LastIndexOfFloat32 | func LastIndexOfFloat32(a []float32, x float32) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func LastIndexOfFloat32(a []float32, x float32) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"LastIndexOfFloat32",
"(",
"a",
"[",
"]",
"float32",
",",
"x",
"float32",
")",
"int",
"{",
"return",
"lastIndexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // LastIndexOfFloat32 gets the index at which the first occurrence of an float32 value is found in array or return -1
// if the value cannot be found | [
"LastIndexOfFloat32",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"an",
"float32",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L448-L450 | train |
thoas/go-funk | typesafe.go | LastIndexOfString | func LastIndexOfString(a []string, x string) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | go | func LastIndexOfString(a []string, x string) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
} | [
"func",
"LastIndexOfString",
"(",
"a",
"[",
"]",
"string",
",",
"x",
"string",
")",
"int",
"{",
"return",
"lastIndexOf",
"(",
"len",
"(",
"a",
")",
",",
"func",
"(",
"i",
"int",
")",
"bool",
"{",
"return",
"a",
"[",
"i",
"]",
"==",
"x",
"}",
")",
"\n",
"}"
] | // LastIndexOfString gets the index at which the first occurrence of a string value is found in array or return -1
// if the value cannot be found | [
"LastIndexOfString",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"a",
"string",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L454-L456 | train |
thoas/go-funk | typesafe.go | UniqInt32 | func UniqInt32(a []int32) []int32 {
length := len(a)
seen := make(map[int32]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | go | func UniqInt32(a []int32) []int32 {
length := len(a)
seen := make(map[int32]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | [
"func",
"UniqInt32",
"(",
"a",
"[",
"]",
"int32",
")",
"[",
"]",
"int32",
"{",
"length",
":=",
"len",
"(",
"a",
")",
"\n\n",
"seen",
":=",
"make",
"(",
"map",
"[",
"int32",
"]",
"struct",
"{",
"}",
",",
"length",
")",
"\n",
"j",
":=",
"0",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"v",
":=",
"a",
"[",
"i",
"]",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"seen",
"[",
"v",
"]",
";",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"seen",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"a",
"[",
"j",
"]",
"=",
"v",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"a",
"[",
"0",
":",
"j",
"]",
"\n",
"}"
] | // UniqInt32 creates an array of int32 with unique values. | [
"UniqInt32",
"creates",
"an",
"array",
"of",
"int32",
"with",
"unique",
"values",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L459-L478 | train |
thoas/go-funk | typesafe.go | UniqInt64 | func UniqInt64(a []int64) []int64 {
length := len(a)
seen := make(map[int64]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | go | func UniqInt64(a []int64) []int64 {
length := len(a)
seen := make(map[int64]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | [
"func",
"UniqInt64",
"(",
"a",
"[",
"]",
"int64",
")",
"[",
"]",
"int64",
"{",
"length",
":=",
"len",
"(",
"a",
")",
"\n\n",
"seen",
":=",
"make",
"(",
"map",
"[",
"int64",
"]",
"struct",
"{",
"}",
",",
"length",
")",
"\n",
"j",
":=",
"0",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"v",
":=",
"a",
"[",
"i",
"]",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"seen",
"[",
"v",
"]",
";",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"seen",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"a",
"[",
"j",
"]",
"=",
"v",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"a",
"[",
"0",
":",
"j",
"]",
"\n",
"}"
] | // UniqInt64 creates an array of int64 with unique values. | [
"UniqInt64",
"creates",
"an",
"array",
"of",
"int64",
"with",
"unique",
"values",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L481-L500 | train |
thoas/go-funk | typesafe.go | UniqInt | func UniqInt(a []int) []int {
length := len(a)
seen := make(map[int]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | go | func UniqInt(a []int) []int {
length := len(a)
seen := make(map[int]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | [
"func",
"UniqInt",
"(",
"a",
"[",
"]",
"int",
")",
"[",
"]",
"int",
"{",
"length",
":=",
"len",
"(",
"a",
")",
"\n\n",
"seen",
":=",
"make",
"(",
"map",
"[",
"int",
"]",
"struct",
"{",
"}",
",",
"length",
")",
"\n",
"j",
":=",
"0",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"v",
":=",
"a",
"[",
"i",
"]",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"seen",
"[",
"v",
"]",
";",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"seen",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"a",
"[",
"j",
"]",
"=",
"v",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"a",
"[",
"0",
":",
"j",
"]",
"\n",
"}"
] | // UniqInt creates an array of int with unique values. | [
"UniqInt",
"creates",
"an",
"array",
"of",
"int",
"with",
"unique",
"values",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L503-L522 | train |
thoas/go-funk | typesafe.go | UniqString | func UniqString(a []string) []string {
length := len(a)
seen := make(map[string]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | go | func UniqString(a []string) []string {
length := len(a)
seen := make(map[string]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | [
"func",
"UniqString",
"(",
"a",
"[",
"]",
"string",
")",
"[",
"]",
"string",
"{",
"length",
":=",
"len",
"(",
"a",
")",
"\n\n",
"seen",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
",",
"length",
")",
"\n",
"j",
":=",
"0",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"v",
":=",
"a",
"[",
"i",
"]",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"seen",
"[",
"v",
"]",
";",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"seen",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"a",
"[",
"j",
"]",
"=",
"v",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"a",
"[",
"0",
":",
"j",
"]",
"\n",
"}"
] | // UniqString creates an array of string with unique values. | [
"UniqString",
"creates",
"an",
"array",
"of",
"string",
"with",
"unique",
"values",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L525-L544 | train |
thoas/go-funk | typesafe.go | UniqFloat64 | func UniqFloat64(a []float64) []float64 {
length := len(a)
seen := make(map[float64]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | go | func UniqFloat64(a []float64) []float64 {
length := len(a)
seen := make(map[float64]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | [
"func",
"UniqFloat64",
"(",
"a",
"[",
"]",
"float64",
")",
"[",
"]",
"float64",
"{",
"length",
":=",
"len",
"(",
"a",
")",
"\n\n",
"seen",
":=",
"make",
"(",
"map",
"[",
"float64",
"]",
"struct",
"{",
"}",
",",
"length",
")",
"\n",
"j",
":=",
"0",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"v",
":=",
"a",
"[",
"i",
"]",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"seen",
"[",
"v",
"]",
";",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"seen",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"a",
"[",
"j",
"]",
"=",
"v",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"a",
"[",
"0",
":",
"j",
"]",
"\n",
"}"
] | // UniqFloat64 creates an array of float64 with unique values. | [
"UniqFloat64",
"creates",
"an",
"array",
"of",
"float64",
"with",
"unique",
"values",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L547-L566 | train |
thoas/go-funk | typesafe.go | UniqFloat32 | func UniqFloat32(a []float32) []float32 {
length := len(a)
seen := make(map[float32]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | go | func UniqFloat32(a []float32) []float32 {
length := len(a)
seen := make(map[float32]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
} | [
"func",
"UniqFloat32",
"(",
"a",
"[",
"]",
"float32",
")",
"[",
"]",
"float32",
"{",
"length",
":=",
"len",
"(",
"a",
")",
"\n\n",
"seen",
":=",
"make",
"(",
"map",
"[",
"float32",
"]",
"struct",
"{",
"}",
",",
"length",
")",
"\n",
"j",
":=",
"0",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"v",
":=",
"a",
"[",
"i",
"]",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"seen",
"[",
"v",
"]",
";",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"seen",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"a",
"[",
"j",
"]",
"=",
"v",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"a",
"[",
"0",
":",
"j",
"]",
"\n",
"}"
] | // UniqFloat32 creates an array of float32 with unique values. | [
"UniqFloat32",
"creates",
"an",
"array",
"of",
"float32",
"with",
"unique",
"values",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/typesafe.go#L569-L588 | train |
thoas/go-funk | map.go | Keys | func Keys(out interface{}) interface{} {
value := redirectValue(reflect.ValueOf(out))
valueType := value.Type()
if value.Kind() == reflect.Map {
keys := value.MapKeys()
length := len(keys)
resultSlice := reflect.MakeSlice(reflect.SliceOf(valueType.Key()), length, length)
for i, key := range keys {
resultSlice.Index(i).Set(key)
}
return resultSlice.Interface()
}
if value.Kind() == reflect.Struct {
length := value.NumField()
resultSlice := make([]string, length)
for i := 0; i < length; i++ {
resultSlice[i] = valueType.Field(i).Name
}
return resultSlice
}
panic(fmt.Sprintf("Type %s is not supported by Keys", valueType.String()))
} | go | func Keys(out interface{}) interface{} {
value := redirectValue(reflect.ValueOf(out))
valueType := value.Type()
if value.Kind() == reflect.Map {
keys := value.MapKeys()
length := len(keys)
resultSlice := reflect.MakeSlice(reflect.SliceOf(valueType.Key()), length, length)
for i, key := range keys {
resultSlice.Index(i).Set(key)
}
return resultSlice.Interface()
}
if value.Kind() == reflect.Struct {
length := value.NumField()
resultSlice := make([]string, length)
for i := 0; i < length; i++ {
resultSlice[i] = valueType.Field(i).Name
}
return resultSlice
}
panic(fmt.Sprintf("Type %s is not supported by Keys", valueType.String()))
} | [
"func",
"Keys",
"(",
"out",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"value",
":=",
"redirectValue",
"(",
"reflect",
".",
"ValueOf",
"(",
"out",
")",
")",
"\n",
"valueType",
":=",
"value",
".",
"Type",
"(",
")",
"\n\n",
"if",
"value",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Map",
"{",
"keys",
":=",
"value",
".",
"MapKeys",
"(",
")",
"\n\n",
"length",
":=",
"len",
"(",
"keys",
")",
"\n\n",
"resultSlice",
":=",
"reflect",
".",
"MakeSlice",
"(",
"reflect",
".",
"SliceOf",
"(",
"valueType",
".",
"Key",
"(",
")",
")",
",",
"length",
",",
"length",
")",
"\n\n",
"for",
"i",
",",
"key",
":=",
"range",
"keys",
"{",
"resultSlice",
".",
"Index",
"(",
"i",
")",
".",
"Set",
"(",
"key",
")",
"\n",
"}",
"\n\n",
"return",
"resultSlice",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n\n",
"if",
"value",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Struct",
"{",
"length",
":=",
"value",
".",
"NumField",
"(",
")",
"\n\n",
"resultSlice",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"length",
")",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"resultSlice",
"[",
"i",
"]",
"=",
"valueType",
".",
"Field",
"(",
"i",
")",
".",
"Name",
"\n",
"}",
"\n\n",
"return",
"resultSlice",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Keys creates an array of the own enumerable map keys or struct field names. | [
"Keys",
"creates",
"an",
"array",
"of",
"the",
"own",
"enumerable",
"map",
"keys",
"or",
"struct",
"field",
"names",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/map.go#L9-L40 | train |
thoas/go-funk | scan.go | ForEach | func ForEach(arr interface{}, predicate interface{}) {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
var (
funcValue = reflect.ValueOf(predicate)
arrValue = reflect.ValueOf(arr)
arrType = arrValue.Type()
funcType = funcValue.Type()
)
if arrType.Kind() == reflect.Slice || arrType.Kind() == reflect.Array {
if !IsFunction(predicate, 1, 0) {
panic("Second argument must be a function with one parameter")
}
arrElemType := arrValue.Type().Elem()
// Checking whether element type is convertible to function's first argument's type.
if !arrElemType.ConvertibleTo(funcType.In(0)) {
panic("Map function's argument is not compatible with type of array.")
}
for i := 0; i < arrValue.Len(); i++ {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
}
}
if arrType.Kind() == reflect.Map {
if !IsFunction(predicate, 2, 0) {
panic("Second argument must be a function with two parameters")
}
// Type checking for Map<key, value> = (key, value)
keyType := arrType.Key()
valueType := arrType.Elem()
if !keyType.ConvertibleTo(funcType.In(0)) {
panic(fmt.Sprintf("function first argument is not compatible with %s", keyType.String()))
}
if !valueType.ConvertibleTo(funcType.In(1)) {
panic(fmt.Sprintf("function second argument is not compatible with %s", valueType.String()))
}
for _, key := range arrValue.MapKeys() {
funcValue.Call([]reflect.Value{key, arrValue.MapIndex(key)})
}
}
} | go | func ForEach(arr interface{}, predicate interface{}) {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
var (
funcValue = reflect.ValueOf(predicate)
arrValue = reflect.ValueOf(arr)
arrType = arrValue.Type()
funcType = funcValue.Type()
)
if arrType.Kind() == reflect.Slice || arrType.Kind() == reflect.Array {
if !IsFunction(predicate, 1, 0) {
panic("Second argument must be a function with one parameter")
}
arrElemType := arrValue.Type().Elem()
// Checking whether element type is convertible to function's first argument's type.
if !arrElemType.ConvertibleTo(funcType.In(0)) {
panic("Map function's argument is not compatible with type of array.")
}
for i := 0; i < arrValue.Len(); i++ {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
}
}
if arrType.Kind() == reflect.Map {
if !IsFunction(predicate, 2, 0) {
panic("Second argument must be a function with two parameters")
}
// Type checking for Map<key, value> = (key, value)
keyType := arrType.Key()
valueType := arrType.Elem()
if !keyType.ConvertibleTo(funcType.In(0)) {
panic(fmt.Sprintf("function first argument is not compatible with %s", keyType.String()))
}
if !valueType.ConvertibleTo(funcType.In(1)) {
panic(fmt.Sprintf("function second argument is not compatible with %s", valueType.String()))
}
for _, key := range arrValue.MapKeys() {
funcValue.Call([]reflect.Value{key, arrValue.MapIndex(key)})
}
}
} | [
"func",
"ForEach",
"(",
"arr",
"interface",
"{",
"}",
",",
"predicate",
"interface",
"{",
"}",
")",
"{",
"if",
"!",
"IsIteratee",
"(",
"arr",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"var",
"(",
"funcValue",
"=",
"reflect",
".",
"ValueOf",
"(",
"predicate",
")",
"\n",
"arrValue",
"=",
"reflect",
".",
"ValueOf",
"(",
"arr",
")",
"\n",
"arrType",
"=",
"arrValue",
".",
"Type",
"(",
")",
"\n",
"funcType",
"=",
"funcValue",
".",
"Type",
"(",
")",
"\n",
")",
"\n\n",
"if",
"arrType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Slice",
"||",
"arrType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Array",
"{",
"if",
"!",
"IsFunction",
"(",
"predicate",
",",
"1",
",",
"0",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"arrElemType",
":=",
"arrValue",
".",
"Type",
"(",
")",
".",
"Elem",
"(",
")",
"\n\n",
"// Checking whether element type is convertible to function's first argument's type.",
"if",
"!",
"arrElemType",
".",
"ConvertibleTo",
"(",
"funcType",
".",
"In",
"(",
"0",
")",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"arrValue",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"funcValue",
".",
"Call",
"(",
"[",
"]",
"reflect",
".",
"Value",
"{",
"arrValue",
".",
"Index",
"(",
"i",
")",
"}",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"arrType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Map",
"{",
"if",
"!",
"IsFunction",
"(",
"predicate",
",",
"2",
",",
"0",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// Type checking for Map<key, value> = (key, value)",
"keyType",
":=",
"arrType",
".",
"Key",
"(",
")",
"\n",
"valueType",
":=",
"arrType",
".",
"Elem",
"(",
")",
"\n\n",
"if",
"!",
"keyType",
".",
"ConvertibleTo",
"(",
"funcType",
".",
"In",
"(",
"0",
")",
")",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"keyType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"valueType",
".",
"ConvertibleTo",
"(",
"funcType",
".",
"In",
"(",
"1",
")",
")",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"key",
":=",
"range",
"arrValue",
".",
"MapKeys",
"(",
")",
"{",
"funcValue",
".",
"Call",
"(",
"[",
"]",
"reflect",
".",
"Value",
"{",
"key",
",",
"arrValue",
".",
"MapIndex",
"(",
"key",
")",
"}",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // ForEach iterates over elements of collection and invokes iteratee
// for each element. | [
"ForEach",
"iterates",
"over",
"elements",
"of",
"collection",
"and",
"invokes",
"iteratee",
"for",
"each",
"element",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/scan.go#L10-L60 | train |
thoas/go-funk | scan.go | Head | func Head(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
if value.Len() == 0 {
return nil
}
return value.Index(0).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Head", valueType.String()))
} | go | func Head(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
if value.Len() == 0 {
return nil
}
return value.Index(0).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Head", valueType.String()))
} | [
"func",
"Head",
"(",
"arr",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"value",
":=",
"redirectValue",
"(",
"reflect",
".",
"ValueOf",
"(",
"arr",
")",
")",
"\n",
"valueType",
":=",
"value",
".",
"Type",
"(",
")",
"\n\n",
"kind",
":=",
"value",
".",
"Kind",
"(",
")",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"Array",
"||",
"kind",
"==",
"reflect",
".",
"Slice",
"{",
"if",
"value",
".",
"Len",
"(",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"value",
".",
"Index",
"(",
"0",
")",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Head gets the first element of array. | [
"Head",
"gets",
"the",
"first",
"element",
"of",
"array",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/scan.go#L119-L134 | train |
thoas/go-funk | scan.go | Tail | func Tail(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
if length <= 1 {
return arr
}
return value.Slice(1, length).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Initial", valueType.String()))
} | go | func Tail(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
if length <= 1 {
return arr
}
return value.Slice(1, length).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Initial", valueType.String()))
} | [
"func",
"Tail",
"(",
"arr",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"value",
":=",
"redirectValue",
"(",
"reflect",
".",
"ValueOf",
"(",
"arr",
")",
")",
"\n",
"valueType",
":=",
"value",
".",
"Type",
"(",
")",
"\n\n",
"kind",
":=",
"value",
".",
"Kind",
"(",
")",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"Array",
"||",
"kind",
"==",
"reflect",
".",
"Slice",
"{",
"length",
":=",
"value",
".",
"Len",
"(",
")",
"\n\n",
"if",
"length",
"<=",
"1",
"{",
"return",
"arr",
"\n",
"}",
"\n\n",
"return",
"value",
".",
"Slice",
"(",
"1",
",",
"length",
")",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Tail gets all but the first element of array. | [
"Tail",
"gets",
"all",
"but",
"the",
"first",
"element",
"of",
"array",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/scan.go#L175-L192 | train |
thoas/go-funk | presence.go | Filter | func Filter(arr interface{}, predicate interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(predicate, 1, 1) {
panic("Second argument must be function")
}
funcValue := reflect.ValueOf(predicate)
funcType := funcValue.Type()
if funcType.Out(0).Kind() != reflect.Bool {
panic("Return argument should be a boolean")
}
arrValue := reflect.ValueOf(arr)
arrType := arrValue.Type()
// Get slice type corresponding to array type
resultSliceType := reflect.SliceOf(arrType.Elem())
// MakeSlice takes a slice kind type, and makes a slice.
resultSlice := reflect.MakeSlice(resultSliceType, 0, 0)
for i := 0; i < arrValue.Len(); i++ {
elem := arrValue.Index(i)
result := funcValue.Call([]reflect.Value{elem})[0].Interface().(bool)
if result {
resultSlice = reflect.Append(resultSlice, elem)
}
}
return resultSlice.Interface()
} | go | func Filter(arr interface{}, predicate interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(predicate, 1, 1) {
panic("Second argument must be function")
}
funcValue := reflect.ValueOf(predicate)
funcType := funcValue.Type()
if funcType.Out(0).Kind() != reflect.Bool {
panic("Return argument should be a boolean")
}
arrValue := reflect.ValueOf(arr)
arrType := arrValue.Type()
// Get slice type corresponding to array type
resultSliceType := reflect.SliceOf(arrType.Elem())
// MakeSlice takes a slice kind type, and makes a slice.
resultSlice := reflect.MakeSlice(resultSliceType, 0, 0)
for i := 0; i < arrValue.Len(); i++ {
elem := arrValue.Index(i)
result := funcValue.Call([]reflect.Value{elem})[0].Interface().(bool)
if result {
resultSlice = reflect.Append(resultSlice, elem)
}
}
return resultSlice.Interface()
} | [
"func",
"Filter",
"(",
"arr",
"interface",
"{",
"}",
",",
"predicate",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"if",
"!",
"IsIteratee",
"(",
"arr",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"IsFunction",
"(",
"predicate",
",",
"1",
",",
"1",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"funcValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"predicate",
")",
"\n\n",
"funcType",
":=",
"funcValue",
".",
"Type",
"(",
")",
"\n\n",
"if",
"funcType",
".",
"Out",
"(",
"0",
")",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Bool",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"arrValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"arr",
")",
"\n\n",
"arrType",
":=",
"arrValue",
".",
"Type",
"(",
")",
"\n\n",
"// Get slice type corresponding to array type",
"resultSliceType",
":=",
"reflect",
".",
"SliceOf",
"(",
"arrType",
".",
"Elem",
"(",
")",
")",
"\n\n",
"// MakeSlice takes a slice kind type, and makes a slice.",
"resultSlice",
":=",
"reflect",
".",
"MakeSlice",
"(",
"resultSliceType",
",",
"0",
",",
"0",
")",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"arrValue",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"elem",
":=",
"arrValue",
".",
"Index",
"(",
"i",
")",
"\n\n",
"result",
":=",
"funcValue",
".",
"Call",
"(",
"[",
"]",
"reflect",
".",
"Value",
"{",
"elem",
"}",
")",
"[",
"0",
"]",
".",
"Interface",
"(",
")",
".",
"(",
"bool",
")",
"\n\n",
"if",
"result",
"{",
"resultSlice",
"=",
"reflect",
".",
"Append",
"(",
"resultSlice",
",",
"elem",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"resultSlice",
".",
"Interface",
"(",
")",
"\n",
"}"
] | // Filter iterates over elements of collection, returning an array of
// all elements predicate returns truthy for. | [
"Filter",
"iterates",
"over",
"elements",
"of",
"collection",
"returning",
"an",
"array",
"of",
"all",
"elements",
"predicate",
"returns",
"truthy",
"for",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/presence.go#L11-L49 | train |
thoas/go-funk | presence.go | Find | func Find(arr interface{}, predicate interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(predicate, 1, 1) {
panic("Second argument must be function")
}
funcValue := reflect.ValueOf(predicate)
funcType := funcValue.Type()
if funcType.Out(0).Kind() != reflect.Bool {
panic("Return argument should be a boolean")
}
arrValue := reflect.ValueOf(arr)
for i := 0; i < arrValue.Len(); i++ {
elem := arrValue.Index(i)
result := funcValue.Call([]reflect.Value{elem})[0].Interface().(bool)
if result {
return elem.Interface()
}
}
return nil
} | go | func Find(arr interface{}, predicate interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(predicate, 1, 1) {
panic("Second argument must be function")
}
funcValue := reflect.ValueOf(predicate)
funcType := funcValue.Type()
if funcType.Out(0).Kind() != reflect.Bool {
panic("Return argument should be a boolean")
}
arrValue := reflect.ValueOf(arr)
for i := 0; i < arrValue.Len(); i++ {
elem := arrValue.Index(i)
result := funcValue.Call([]reflect.Value{elem})[0].Interface().(bool)
if result {
return elem.Interface()
}
}
return nil
} | [
"func",
"Find",
"(",
"arr",
"interface",
"{",
"}",
",",
"predicate",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"if",
"!",
"IsIteratee",
"(",
"arr",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"IsFunction",
"(",
"predicate",
",",
"1",
",",
"1",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"funcValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"predicate",
")",
"\n\n",
"funcType",
":=",
"funcValue",
".",
"Type",
"(",
")",
"\n\n",
"if",
"funcType",
".",
"Out",
"(",
"0",
")",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Bool",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"arrValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"arr",
")",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"arrValue",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"elem",
":=",
"arrValue",
".",
"Index",
"(",
"i",
")",
"\n\n",
"result",
":=",
"funcValue",
".",
"Call",
"(",
"[",
"]",
"reflect",
".",
"Value",
"{",
"elem",
"}",
")",
"[",
"0",
"]",
".",
"Interface",
"(",
")",
".",
"(",
"bool",
")",
"\n\n",
"if",
"result",
"{",
"return",
"elem",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // Find iterates over elements of collection, returning the first
// element predicate returns truthy for. | [
"Find",
"iterates",
"over",
"elements",
"of",
"collection",
"returning",
"the",
"first",
"element",
"predicate",
"returns",
"truthy",
"for",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/presence.go#L53-L83 | train |
thoas/go-funk | presence.go | IndexOf | func IndexOf(in interface{}, elem interface{}) int {
inValue := reflect.ValueOf(in)
elemValue := reflect.ValueOf(elem)
inType := inValue.Type()
if inType.Kind() == reflect.String {
return strings.Index(inValue.String(), elemValue.String())
}
if inType.Kind() == reflect.Slice {
for i := 0; i < inValue.Len(); i++ {
if equal(inValue.Index(i).Interface(), elem) {
return i
}
}
}
return -1
} | go | func IndexOf(in interface{}, elem interface{}) int {
inValue := reflect.ValueOf(in)
elemValue := reflect.ValueOf(elem)
inType := inValue.Type()
if inType.Kind() == reflect.String {
return strings.Index(inValue.String(), elemValue.String())
}
if inType.Kind() == reflect.Slice {
for i := 0; i < inValue.Len(); i++ {
if equal(inValue.Index(i).Interface(), elem) {
return i
}
}
}
return -1
} | [
"func",
"IndexOf",
"(",
"in",
"interface",
"{",
"}",
",",
"elem",
"interface",
"{",
"}",
")",
"int",
"{",
"inValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n\n",
"elemValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"elem",
")",
"\n\n",
"inType",
":=",
"inValue",
".",
"Type",
"(",
")",
"\n\n",
"if",
"inType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"String",
"{",
"return",
"strings",
".",
"Index",
"(",
"inValue",
".",
"String",
"(",
")",
",",
"elemValue",
".",
"String",
"(",
")",
")",
"\n",
"}",
"\n\n",
"if",
"inType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Slice",
"{",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"inValue",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"if",
"equal",
"(",
"inValue",
".",
"Index",
"(",
"i",
")",
".",
"Interface",
"(",
")",
",",
"elem",
")",
"{",
"return",
"i",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"-",
"1",
"\n",
"}"
] | // IndexOf gets the index at which the first occurrence of value is found in array or return -1
// if the value cannot be found | [
"IndexOf",
"gets",
"the",
"index",
"at",
"which",
"the",
"first",
"occurrence",
"of",
"value",
"is",
"found",
"in",
"array",
"or",
"return",
"-",
"1",
"if",
"the",
"value",
"cannot",
"be",
"found"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/presence.go#L87-L107 | train |
thoas/go-funk | presence.go | Contains | func Contains(in interface{}, elem interface{}) bool {
inValue := reflect.ValueOf(in)
elemValue := reflect.ValueOf(elem)
inType := inValue.Type()
switch inType.Kind() {
case reflect.String:
return strings.Contains(inValue.String(), elemValue.String())
case reflect.Map:
for _, key := range inValue.MapKeys() {
if equal(key.Interface(), elem) {
return true
}
}
case reflect.Slice, reflect.Array:
for i := 0; i < inValue.Len(); i++ {
if equal(inValue.Index(i).Interface(), elem) {
return true
}
}
default:
panic(fmt.Sprintf("Type %s is not supported by Contains, supported types are String, Map, Slice, Array", inType.String()))
}
return false
} | go | func Contains(in interface{}, elem interface{}) bool {
inValue := reflect.ValueOf(in)
elemValue := reflect.ValueOf(elem)
inType := inValue.Type()
switch inType.Kind() {
case reflect.String:
return strings.Contains(inValue.String(), elemValue.String())
case reflect.Map:
for _, key := range inValue.MapKeys() {
if equal(key.Interface(), elem) {
return true
}
}
case reflect.Slice, reflect.Array:
for i := 0; i < inValue.Len(); i++ {
if equal(inValue.Index(i).Interface(), elem) {
return true
}
}
default:
panic(fmt.Sprintf("Type %s is not supported by Contains, supported types are String, Map, Slice, Array", inType.String()))
}
return false
} | [
"func",
"Contains",
"(",
"in",
"interface",
"{",
"}",
",",
"elem",
"interface",
"{",
"}",
")",
"bool",
"{",
"inValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n",
"elemValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"elem",
")",
"\n",
"inType",
":=",
"inValue",
".",
"Type",
"(",
")",
"\n\n",
"switch",
"inType",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"String",
":",
"return",
"strings",
".",
"Contains",
"(",
"inValue",
".",
"String",
"(",
")",
",",
"elemValue",
".",
"String",
"(",
")",
")",
"\n",
"case",
"reflect",
".",
"Map",
":",
"for",
"_",
",",
"key",
":=",
"range",
"inValue",
".",
"MapKeys",
"(",
")",
"{",
"if",
"equal",
"(",
"key",
".",
"Interface",
"(",
")",
",",
"elem",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"case",
"reflect",
".",
"Slice",
",",
"reflect",
".",
"Array",
":",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"inValue",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"if",
"equal",
"(",
"inValue",
".",
"Index",
"(",
"i",
")",
".",
"Interface",
"(",
")",
",",
"elem",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"default",
":",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"inType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // Contains returns true if an element is present in a iteratee. | [
"Contains",
"returns",
"true",
"if",
"an",
"element",
"is",
"present",
"in",
"a",
"iteratee",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/presence.go#L136-L161 | train |
thoas/go-funk | presence.go | Every | func Every(in interface{}, elements ...interface{}) bool {
for _, elem := range elements {
if !Contains(in, elem) {
return false
}
}
return true
} | go | func Every(in interface{}, elements ...interface{}) bool {
for _, elem := range elements {
if !Contains(in, elem) {
return false
}
}
return true
} | [
"func",
"Every",
"(",
"in",
"interface",
"{",
"}",
",",
"elements",
"...",
"interface",
"{",
"}",
")",
"bool",
"{",
"for",
"_",
",",
"elem",
":=",
"range",
"elements",
"{",
"if",
"!",
"Contains",
"(",
"in",
",",
"elem",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // Every returns true if every element is present in a iteratee. | [
"Every",
"returns",
"true",
"if",
"every",
"element",
"is",
"present",
"in",
"a",
"iteratee",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/presence.go#L164-L171 | train |
thoas/go-funk | builder.go | Chain | func Chain(v interface{}) Builder {
isNotNil(v, "Chain")
valueType := reflect.TypeOf(v)
if isValidBuilderEntry(valueType) ||
(valueType.Kind() == reflect.Ptr && isValidBuilderEntry(valueType.Elem())) {
return &chainBuilder{v}
}
panic(fmt.Sprintf("Type %s is not supported by Chain", valueType.String()))
} | go | func Chain(v interface{}) Builder {
isNotNil(v, "Chain")
valueType := reflect.TypeOf(v)
if isValidBuilderEntry(valueType) ||
(valueType.Kind() == reflect.Ptr && isValidBuilderEntry(valueType.Elem())) {
return &chainBuilder{v}
}
panic(fmt.Sprintf("Type %s is not supported by Chain", valueType.String()))
} | [
"func",
"Chain",
"(",
"v",
"interface",
"{",
"}",
")",
"Builder",
"{",
"isNotNil",
"(",
"v",
",",
"\"",
"\"",
")",
"\n\n",
"valueType",
":=",
"reflect",
".",
"TypeOf",
"(",
"v",
")",
"\n",
"if",
"isValidBuilderEntry",
"(",
"valueType",
")",
"||",
"(",
"valueType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"&&",
"isValidBuilderEntry",
"(",
"valueType",
".",
"Elem",
"(",
")",
")",
")",
"{",
"return",
"&",
"chainBuilder",
"{",
"v",
"}",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Chain creates a simple new go-funk.Builder from a collection. Each method
// call generate a new builder containing the previous result. | [
"Chain",
"creates",
"a",
"simple",
"new",
"go",
"-",
"funk",
".",
"Builder",
"from",
"a",
"collection",
".",
"Each",
"method",
"call",
"generate",
"a",
"new",
"builder",
"containing",
"the",
"previous",
"result",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/builder.go#L47-L57 | train |
thoas/go-funk | builder.go | LazyChain | func LazyChain(v interface{}) Builder {
isNotNil(v, "LazyChain")
valueType := reflect.TypeOf(v)
if isValidBuilderEntry(valueType) ||
(valueType.Kind() == reflect.Ptr && isValidBuilderEntry(valueType.Elem())) {
return &lazyBuilder{func() interface{} { return v }}
}
panic(fmt.Sprintf("Type %s is not supported by LazyChain", valueType.String()))
} | go | func LazyChain(v interface{}) Builder {
isNotNil(v, "LazyChain")
valueType := reflect.TypeOf(v)
if isValidBuilderEntry(valueType) ||
(valueType.Kind() == reflect.Ptr && isValidBuilderEntry(valueType.Elem())) {
return &lazyBuilder{func() interface{} { return v }}
}
panic(fmt.Sprintf("Type %s is not supported by LazyChain", valueType.String()))
} | [
"func",
"LazyChain",
"(",
"v",
"interface",
"{",
"}",
")",
"Builder",
"{",
"isNotNil",
"(",
"v",
",",
"\"",
"\"",
")",
"\n\n",
"valueType",
":=",
"reflect",
".",
"TypeOf",
"(",
"v",
")",
"\n",
"if",
"isValidBuilderEntry",
"(",
"valueType",
")",
"||",
"(",
"valueType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"&&",
"isValidBuilderEntry",
"(",
"valueType",
".",
"Elem",
"(",
")",
")",
")",
"{",
"return",
"&",
"lazyBuilder",
"{",
"func",
"(",
")",
"interface",
"{",
"}",
"{",
"return",
"v",
"}",
"}",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n\n",
"}"
] | // LazyChain creates a lazy go-funk.Builder from a collection. Each method call
// generate a new builder containing a method generating the previous value.
// With that, all data are only generated when we call a tailling method like All or Find. | [
"LazyChain",
"creates",
"a",
"lazy",
"go",
"-",
"funk",
".",
"Builder",
"from",
"a",
"collection",
".",
"Each",
"method",
"call",
"generate",
"a",
"new",
"builder",
"containing",
"a",
"method",
"generating",
"the",
"previous",
"value",
".",
"With",
"that",
"all",
"data",
"are",
"only",
"generated",
"when",
"we",
"call",
"a",
"tailling",
"method",
"like",
"All",
"or",
"Find",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/builder.go#L62-L73 | train |
thoas/go-funk | fill.go | Fill | func Fill(in interface{}, fillValue interface{}) (interface{}, error) {
inValue := reflect.ValueOf(in)
inKind := inValue.Type().Kind()
if inKind != reflect.Slice && inKind != reflect.Array {
return nil, errors.New("Can only fill slices and arrays")
}
inType := reflect.TypeOf(in).Elem()
value := reflect.ValueOf(fillValue)
if inType != value.Type() {
return nil, fmt.Errorf(
"Cannot fill '%s' with '%s'", reflect.TypeOf(in), value.Type(),
)
}
length := inValue.Len()
newSlice := reflect.SliceOf(reflect.TypeOf(fillValue))
in = reflect.MakeSlice(newSlice, length, length).Interface()
inValue = reflect.ValueOf(in)
for i := 0; i < length; i++ {
inValue.Index(i).Set(value)
}
return in, nil
} | go | func Fill(in interface{}, fillValue interface{}) (interface{}, error) {
inValue := reflect.ValueOf(in)
inKind := inValue.Type().Kind()
if inKind != reflect.Slice && inKind != reflect.Array {
return nil, errors.New("Can only fill slices and arrays")
}
inType := reflect.TypeOf(in).Elem()
value := reflect.ValueOf(fillValue)
if inType != value.Type() {
return nil, fmt.Errorf(
"Cannot fill '%s' with '%s'", reflect.TypeOf(in), value.Type(),
)
}
length := inValue.Len()
newSlice := reflect.SliceOf(reflect.TypeOf(fillValue))
in = reflect.MakeSlice(newSlice, length, length).Interface()
inValue = reflect.ValueOf(in)
for i := 0; i < length; i++ {
inValue.Index(i).Set(value)
}
return in, nil
} | [
"func",
"Fill",
"(",
"in",
"interface",
"{",
"}",
",",
"fillValue",
"interface",
"{",
"}",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"inValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n",
"inKind",
":=",
"inValue",
".",
"Type",
"(",
")",
".",
"Kind",
"(",
")",
"\n",
"if",
"inKind",
"!=",
"reflect",
".",
"Slice",
"&&",
"inKind",
"!=",
"reflect",
".",
"Array",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"inType",
":=",
"reflect",
".",
"TypeOf",
"(",
"in",
")",
".",
"Elem",
"(",
")",
"\n",
"value",
":=",
"reflect",
".",
"ValueOf",
"(",
"fillValue",
")",
"\n",
"if",
"inType",
"!=",
"value",
".",
"Type",
"(",
")",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"reflect",
".",
"TypeOf",
"(",
"in",
")",
",",
"value",
".",
"Type",
"(",
")",
",",
")",
"\n",
"}",
"\n\n",
"length",
":=",
"inValue",
".",
"Len",
"(",
")",
"\n",
"newSlice",
":=",
"reflect",
".",
"SliceOf",
"(",
"reflect",
".",
"TypeOf",
"(",
"fillValue",
")",
")",
"\n",
"in",
"=",
"reflect",
".",
"MakeSlice",
"(",
"newSlice",
",",
"length",
",",
"length",
")",
".",
"Interface",
"(",
")",
"\n",
"inValue",
"=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"inValue",
".",
"Index",
"(",
"i",
")",
".",
"Set",
"(",
"value",
")",
"\n",
"}",
"\n",
"return",
"in",
",",
"nil",
"\n",
"}"
] | // Fill fills elements of array with value | [
"Fill",
"fills",
"elements",
"of",
"array",
"with",
"value"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/fill.go#L10-L34 | train |
thoas/go-funk | intersection.go | Intersect | func Intersect(x interface{}, y interface{}) interface{} {
if !IsCollection(x) {
panic("First parameter must be a collection")
}
if !IsCollection(y) {
panic("Second parameter must be a collection")
}
hash := map[interface{}]struct{}{}
xValue := reflect.ValueOf(x)
xType := xValue.Type()
yValue := reflect.ValueOf(y)
yType := yValue.Type()
if NotEqual(xType, yType) {
panic("Parameters must have the same type")
}
zType := reflect.SliceOf(xType.Elem())
zSlice := reflect.MakeSlice(zType, 0, 0)
for i := 0; i < xValue.Len(); i++ {
v := xValue.Index(i).Interface()
hash[v] = struct{}{}
}
for i := 0; i < yValue.Len(); i++ {
v := yValue.Index(i).Interface()
_, ok := hash[v]
if ok {
zSlice = reflect.Append(zSlice, yValue.Index(i))
}
}
return zSlice.Interface()
} | go | func Intersect(x interface{}, y interface{}) interface{} {
if !IsCollection(x) {
panic("First parameter must be a collection")
}
if !IsCollection(y) {
panic("Second parameter must be a collection")
}
hash := map[interface{}]struct{}{}
xValue := reflect.ValueOf(x)
xType := xValue.Type()
yValue := reflect.ValueOf(y)
yType := yValue.Type()
if NotEqual(xType, yType) {
panic("Parameters must have the same type")
}
zType := reflect.SliceOf(xType.Elem())
zSlice := reflect.MakeSlice(zType, 0, 0)
for i := 0; i < xValue.Len(); i++ {
v := xValue.Index(i).Interface()
hash[v] = struct{}{}
}
for i := 0; i < yValue.Len(); i++ {
v := yValue.Index(i).Interface()
_, ok := hash[v]
if ok {
zSlice = reflect.Append(zSlice, yValue.Index(i))
}
}
return zSlice.Interface()
} | [
"func",
"Intersect",
"(",
"x",
"interface",
"{",
"}",
",",
"y",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"if",
"!",
"IsCollection",
"(",
"x",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"!",
"IsCollection",
"(",
"y",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"hash",
":=",
"map",
"[",
"interface",
"{",
"}",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"xValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"x",
")",
"\n",
"xType",
":=",
"xValue",
".",
"Type",
"(",
")",
"\n\n",
"yValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"y",
")",
"\n",
"yType",
":=",
"yValue",
".",
"Type",
"(",
")",
"\n\n",
"if",
"NotEqual",
"(",
"xType",
",",
"yType",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"zType",
":=",
"reflect",
".",
"SliceOf",
"(",
"xType",
".",
"Elem",
"(",
")",
")",
"\n",
"zSlice",
":=",
"reflect",
".",
"MakeSlice",
"(",
"zType",
",",
"0",
",",
"0",
")",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"xValue",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"v",
":=",
"xValue",
".",
"Index",
"(",
"i",
")",
".",
"Interface",
"(",
")",
"\n",
"hash",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"yValue",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"v",
":=",
"yValue",
".",
"Index",
"(",
"i",
")",
".",
"Interface",
"(",
")",
"\n",
"_",
",",
"ok",
":=",
"hash",
"[",
"v",
"]",
"\n",
"if",
"ok",
"{",
"zSlice",
"=",
"reflect",
".",
"Append",
"(",
"zSlice",
",",
"yValue",
".",
"Index",
"(",
"i",
")",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"zSlice",
".",
"Interface",
"(",
")",
"\n",
"}"
] | // Intersect returns the intersection between two collections. | [
"Intersect",
"returns",
"the",
"intersection",
"between",
"two",
"collections",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/intersection.go#L8-L45 | train |
thoas/go-funk | intersection.go | IntersectString | func IntersectString(x []string, y []string) []string {
if len(x) == 0 || len(y) == 0 {
return []string{}
}
set := []string{}
hash := map[string]struct{}{}
for _, v := range x {
hash[v] = struct{}{}
}
for _, v := range y {
_, ok := hash[v]
if ok {
set = append(set, v)
}
}
return set
} | go | func IntersectString(x []string, y []string) []string {
if len(x) == 0 || len(y) == 0 {
return []string{}
}
set := []string{}
hash := map[string]struct{}{}
for _, v := range x {
hash[v] = struct{}{}
}
for _, v := range y {
_, ok := hash[v]
if ok {
set = append(set, v)
}
}
return set
} | [
"func",
"IntersectString",
"(",
"x",
"[",
"]",
"string",
",",
"y",
"[",
"]",
"string",
")",
"[",
"]",
"string",
"{",
"if",
"len",
"(",
"x",
")",
"==",
"0",
"||",
"len",
"(",
"y",
")",
"==",
"0",
"{",
"return",
"[",
"]",
"string",
"{",
"}",
"\n",
"}",
"\n\n",
"set",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"hash",
":=",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"for",
"_",
",",
"v",
":=",
"range",
"x",
"{",
"hash",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"v",
":=",
"range",
"y",
"{",
"_",
",",
"ok",
":=",
"hash",
"[",
"v",
"]",
"\n",
"if",
"ok",
"{",
"set",
"=",
"append",
"(",
"set",
",",
"v",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"set",
"\n",
"}"
] | // IntersectString returns the intersection between two collections of string. | [
"IntersectString",
"returns",
"the",
"intersection",
"between",
"two",
"collections",
"of",
"string",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/intersection.go#L48-L68 | train |
thoas/go-funk | helpers.go | ToFloat64 | func ToFloat64(x interface{}) (float64, bool) {
var xf float64
xok := true
switch xn := x.(type) {
case uint8:
xf = float64(xn)
case uint16:
xf = float64(xn)
case uint32:
xf = float64(xn)
case uint64:
xf = float64(xn)
case int:
xf = float64(xn)
case int8:
xf = float64(xn)
case int16:
xf = float64(xn)
case int32:
xf = float64(xn)
case int64:
xf = float64(xn)
case float32:
xf = float64(xn)
case float64:
xf = float64(xn)
default:
xok = false
}
return xf, xok
} | go | func ToFloat64(x interface{}) (float64, bool) {
var xf float64
xok := true
switch xn := x.(type) {
case uint8:
xf = float64(xn)
case uint16:
xf = float64(xn)
case uint32:
xf = float64(xn)
case uint64:
xf = float64(xn)
case int:
xf = float64(xn)
case int8:
xf = float64(xn)
case int16:
xf = float64(xn)
case int32:
xf = float64(xn)
case int64:
xf = float64(xn)
case float32:
xf = float64(xn)
case float64:
xf = float64(xn)
default:
xok = false
}
return xf, xok
} | [
"func",
"ToFloat64",
"(",
"x",
"interface",
"{",
"}",
")",
"(",
"float64",
",",
"bool",
")",
"{",
"var",
"xf",
"float64",
"\n",
"xok",
":=",
"true",
"\n\n",
"switch",
"xn",
":=",
"x",
".",
"(",
"type",
")",
"{",
"case",
"uint8",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"uint16",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"uint32",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"uint64",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"int",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"int8",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"int16",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"int32",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"int64",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"float32",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"case",
"float64",
":",
"xf",
"=",
"float64",
"(",
"xn",
")",
"\n",
"default",
":",
"xok",
"=",
"false",
"\n",
"}",
"\n\n",
"return",
"xf",
",",
"xok",
"\n",
"}"
] | // ToFloat64 converts any numeric value to float64. | [
"ToFloat64",
"converts",
"any",
"numeric",
"value",
"to",
"float64",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L25-L57 | train |
thoas/go-funk | helpers.go | PtrOf | func PtrOf(itf interface{}) interface{} {
t := reflect.TypeOf(itf)
cp := reflect.New(t)
cp.Elem().Set(reflect.ValueOf(itf))
// Avoid double pointers if itf is a pointer
if t.Kind() == reflect.Ptr {
return cp.Elem().Interface()
}
return cp.Interface()
} | go | func PtrOf(itf interface{}) interface{} {
t := reflect.TypeOf(itf)
cp := reflect.New(t)
cp.Elem().Set(reflect.ValueOf(itf))
// Avoid double pointers if itf is a pointer
if t.Kind() == reflect.Ptr {
return cp.Elem().Interface()
}
return cp.Interface()
} | [
"func",
"PtrOf",
"(",
"itf",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"t",
":=",
"reflect",
".",
"TypeOf",
"(",
"itf",
")",
"\n\n",
"cp",
":=",
"reflect",
".",
"New",
"(",
"t",
")",
"\n",
"cp",
".",
"Elem",
"(",
")",
".",
"Set",
"(",
"reflect",
".",
"ValueOf",
"(",
"itf",
")",
")",
"\n\n",
"// Avoid double pointers if itf is a pointer",
"if",
"t",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"return",
"cp",
".",
"Elem",
"(",
")",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"cp",
".",
"Interface",
"(",
")",
"\n",
"}"
] | // PtrOf makes a copy of the given interface and returns a pointer. | [
"PtrOf",
"makes",
"a",
"copy",
"of",
"the",
"given",
"interface",
"and",
"returns",
"a",
"pointer",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L60-L72 | train |
thoas/go-funk | helpers.go | IsFunction | func IsFunction(in interface{}, num ...int) bool {
funcType := reflect.TypeOf(in)
result := funcType.Kind() == reflect.Func
if len(num) >= 1 {
result = result && funcType.NumIn() == num[0]
}
if len(num) == 2 {
result = result && funcType.NumOut() == num[1]
}
return result
} | go | func IsFunction(in interface{}, num ...int) bool {
funcType := reflect.TypeOf(in)
result := funcType.Kind() == reflect.Func
if len(num) >= 1 {
result = result && funcType.NumIn() == num[0]
}
if len(num) == 2 {
result = result && funcType.NumOut() == num[1]
}
return result
} | [
"func",
"IsFunction",
"(",
"in",
"interface",
"{",
"}",
",",
"num",
"...",
"int",
")",
"bool",
"{",
"funcType",
":=",
"reflect",
".",
"TypeOf",
"(",
"in",
")",
"\n\n",
"result",
":=",
"funcType",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Func",
"\n\n",
"if",
"len",
"(",
"num",
")",
">=",
"1",
"{",
"result",
"=",
"result",
"&&",
"funcType",
".",
"NumIn",
"(",
")",
"==",
"num",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"num",
")",
"==",
"2",
"{",
"result",
"=",
"result",
"&&",
"funcType",
".",
"NumOut",
"(",
")",
"==",
"num",
"[",
"1",
"]",
"\n",
"}",
"\n\n",
"return",
"result",
"\n",
"}"
] | // IsFunction returns if the argument is a function. | [
"IsFunction",
"returns",
"if",
"the",
"argument",
"is",
"a",
"function",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L75-L89 | train |
thoas/go-funk | helpers.go | IsType | func IsType(expected interface{}, actual interface{}) bool {
return IsEqual(reflect.TypeOf(expected), reflect.TypeOf(actual))
} | go | func IsType(expected interface{}, actual interface{}) bool {
return IsEqual(reflect.TypeOf(expected), reflect.TypeOf(actual))
} | [
"func",
"IsType",
"(",
"expected",
"interface",
"{",
"}",
",",
"actual",
"interface",
"{",
"}",
")",
"bool",
"{",
"return",
"IsEqual",
"(",
"reflect",
".",
"TypeOf",
"(",
"expected",
")",
",",
"reflect",
".",
"TypeOf",
"(",
"actual",
")",
")",
"\n",
"}"
] | // IsType returns if the two objects are in the same type | [
"IsType",
"returns",
"if",
"the",
"two",
"objects",
"are",
"in",
"the",
"same",
"type"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L115-L117 | train |
thoas/go-funk | helpers.go | IsIteratee | func IsIteratee(in interface{}) bool {
if in == nil {
return false
}
arrType := reflect.TypeOf(in)
kind := arrType.Kind()
return kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map
} | go | func IsIteratee(in interface{}) bool {
if in == nil {
return false
}
arrType := reflect.TypeOf(in)
kind := arrType.Kind()
return kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map
} | [
"func",
"IsIteratee",
"(",
"in",
"interface",
"{",
"}",
")",
"bool",
"{",
"if",
"in",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"arrType",
":=",
"reflect",
".",
"TypeOf",
"(",
"in",
")",
"\n\n",
"kind",
":=",
"arrType",
".",
"Kind",
"(",
")",
"\n\n",
"return",
"kind",
"==",
"reflect",
".",
"Array",
"||",
"kind",
"==",
"reflect",
".",
"Slice",
"||",
"kind",
"==",
"reflect",
".",
"Map",
"\n",
"}"
] | // IsIteratee returns if the argument is an iteratee. | [
"IsIteratee",
"returns",
"if",
"the",
"argument",
"is",
"an",
"iteratee",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L130-L139 | train |
thoas/go-funk | helpers.go | IsCollection | func IsCollection(in interface{}) bool {
arrType := reflect.TypeOf(in)
kind := arrType.Kind()
return kind == reflect.Array || kind == reflect.Slice
} | go | func IsCollection(in interface{}) bool {
arrType := reflect.TypeOf(in)
kind := arrType.Kind()
return kind == reflect.Array || kind == reflect.Slice
} | [
"func",
"IsCollection",
"(",
"in",
"interface",
"{",
"}",
")",
"bool",
"{",
"arrType",
":=",
"reflect",
".",
"TypeOf",
"(",
"in",
")",
"\n\n",
"kind",
":=",
"arrType",
".",
"Kind",
"(",
")",
"\n\n",
"return",
"kind",
"==",
"reflect",
".",
"Array",
"||",
"kind",
"==",
"reflect",
".",
"Slice",
"\n",
"}"
] | // IsCollection returns if the argument is a collection. | [
"IsCollection",
"returns",
"if",
"the",
"argument",
"is",
"a",
"collection",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L142-L148 | train |
thoas/go-funk | helpers.go | SliceOf | func SliceOf(in interface{}) interface{} {
value := reflect.ValueOf(in)
sliceType := reflect.SliceOf(reflect.TypeOf(in))
slice := reflect.New(sliceType)
sliceValue := reflect.MakeSlice(sliceType, 0, 0)
sliceValue = reflect.Append(sliceValue, value)
slice.Elem().Set(sliceValue)
return slice.Elem().Interface()
} | go | func SliceOf(in interface{}) interface{} {
value := reflect.ValueOf(in)
sliceType := reflect.SliceOf(reflect.TypeOf(in))
slice := reflect.New(sliceType)
sliceValue := reflect.MakeSlice(sliceType, 0, 0)
sliceValue = reflect.Append(sliceValue, value)
slice.Elem().Set(sliceValue)
return slice.Elem().Interface()
} | [
"func",
"SliceOf",
"(",
"in",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"value",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n\n",
"sliceType",
":=",
"reflect",
".",
"SliceOf",
"(",
"reflect",
".",
"TypeOf",
"(",
"in",
")",
")",
"\n",
"slice",
":=",
"reflect",
".",
"New",
"(",
"sliceType",
")",
"\n",
"sliceValue",
":=",
"reflect",
".",
"MakeSlice",
"(",
"sliceType",
",",
"0",
",",
"0",
")",
"\n",
"sliceValue",
"=",
"reflect",
".",
"Append",
"(",
"sliceValue",
",",
"value",
")",
"\n",
"slice",
".",
"Elem",
"(",
")",
".",
"Set",
"(",
"sliceValue",
")",
"\n\n",
"return",
"slice",
".",
"Elem",
"(",
")",
".",
"Interface",
"(",
")",
"\n",
"}"
] | // SliceOf returns a slice which contains the element. | [
"SliceOf",
"returns",
"a",
"slice",
"which",
"contains",
"the",
"element",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L151-L161 | train |
thoas/go-funk | helpers.go | Any | func Any(objs ...interface{}) bool {
if len(objs) == 0 {
return false
}
for _, obj := range objs {
if !IsEmpty(obj) {
return true
}
}
return false
} | go | func Any(objs ...interface{}) bool {
if len(objs) == 0 {
return false
}
for _, obj := range objs {
if !IsEmpty(obj) {
return true
}
}
return false
} | [
"func",
"Any",
"(",
"objs",
"...",
"interface",
"{",
"}",
")",
"bool",
"{",
"if",
"len",
"(",
"objs",
")",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"obj",
":=",
"range",
"objs",
"{",
"if",
"!",
"IsEmpty",
"(",
"obj",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // Any returns true if any element of the iterable is not empty. If the iterable is empty, return False. | [
"Any",
"returns",
"true",
"if",
"any",
"element",
"of",
"the",
"iterable",
"is",
"not",
"empty",
".",
"If",
"the",
"iterable",
"is",
"empty",
"return",
"False",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L164-L176 | train |
thoas/go-funk | helpers.go | IsEmpty | func IsEmpty(obj interface{}) bool {
if obj == nil || obj == "" || obj == false {
return true
}
for _, v := range numericZeros {
if obj == v {
return true
}
}
objValue := reflect.ValueOf(obj)
switch objValue.Kind() {
case reflect.Map:
fallthrough
case reflect.Slice, reflect.Chan:
return objValue.Len() == 0
case reflect.Struct:
return reflect.DeepEqual(obj, ZeroOf(obj))
case reflect.Ptr:
if objValue.IsNil() {
return true
}
obj = redirectValue(objValue).Interface()
return reflect.DeepEqual(obj, ZeroOf(obj))
}
return false
} | go | func IsEmpty(obj interface{}) bool {
if obj == nil || obj == "" || obj == false {
return true
}
for _, v := range numericZeros {
if obj == v {
return true
}
}
objValue := reflect.ValueOf(obj)
switch objValue.Kind() {
case reflect.Map:
fallthrough
case reflect.Slice, reflect.Chan:
return objValue.Len() == 0
case reflect.Struct:
return reflect.DeepEqual(obj, ZeroOf(obj))
case reflect.Ptr:
if objValue.IsNil() {
return true
}
obj = redirectValue(objValue).Interface()
return reflect.DeepEqual(obj, ZeroOf(obj))
}
return false
} | [
"func",
"IsEmpty",
"(",
"obj",
"interface",
"{",
"}",
")",
"bool",
"{",
"if",
"obj",
"==",
"nil",
"||",
"obj",
"==",
"\"",
"\"",
"||",
"obj",
"==",
"false",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"v",
":=",
"range",
"numericZeros",
"{",
"if",
"obj",
"==",
"v",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"objValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"obj",
")",
"\n\n",
"switch",
"objValue",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Map",
":",
"fallthrough",
"\n",
"case",
"reflect",
".",
"Slice",
",",
"reflect",
".",
"Chan",
":",
"return",
"objValue",
".",
"Len",
"(",
")",
"==",
"0",
"\n",
"case",
"reflect",
".",
"Struct",
":",
"return",
"reflect",
".",
"DeepEqual",
"(",
"obj",
",",
"ZeroOf",
"(",
"obj",
")",
")",
"\n",
"case",
"reflect",
".",
"Ptr",
":",
"if",
"objValue",
".",
"IsNil",
"(",
")",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"obj",
"=",
"redirectValue",
"(",
"objValue",
")",
".",
"Interface",
"(",
")",
"\n\n",
"return",
"reflect",
".",
"DeepEqual",
"(",
"obj",
",",
"ZeroOf",
"(",
"obj",
")",
")",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // IsEmpty returns if the object is considered as empty or not. | [
"IsEmpty",
"returns",
"if",
"the",
"object",
"is",
"considered",
"as",
"empty",
"or",
"not",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L194-L225 | train |
thoas/go-funk | helpers.go | IsZero | func IsZero(obj interface{}) bool {
if obj == nil || obj == "" || obj == false {
return true
}
for _, v := range numericZeros {
if obj == v {
return true
}
}
return reflect.DeepEqual(obj, ZeroOf(obj))
} | go | func IsZero(obj interface{}) bool {
if obj == nil || obj == "" || obj == false {
return true
}
for _, v := range numericZeros {
if obj == v {
return true
}
}
return reflect.DeepEqual(obj, ZeroOf(obj))
} | [
"func",
"IsZero",
"(",
"obj",
"interface",
"{",
"}",
")",
"bool",
"{",
"if",
"obj",
"==",
"nil",
"||",
"obj",
"==",
"\"",
"\"",
"||",
"obj",
"==",
"false",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"v",
":=",
"range",
"numericZeros",
"{",
"if",
"obj",
"==",
"v",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"reflect",
".",
"DeepEqual",
"(",
"obj",
",",
"ZeroOf",
"(",
"obj",
")",
")",
"\n",
"}"
] | // IsZero returns if the object is considered as zero value | [
"IsZero",
"returns",
"if",
"the",
"object",
"is",
"considered",
"as",
"zero",
"value"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L228-L240 | train |
thoas/go-funk | helpers.go | ZeroOf | func ZeroOf(in interface{}) interface{} {
if in == nil {
return nil
}
return reflect.Zero(reflect.TypeOf(in)).Interface()
} | go | func ZeroOf(in interface{}) interface{} {
if in == nil {
return nil
}
return reflect.Zero(reflect.TypeOf(in)).Interface()
} | [
"func",
"ZeroOf",
"(",
"in",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"if",
"in",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"reflect",
".",
"Zero",
"(",
"reflect",
".",
"TypeOf",
"(",
"in",
")",
")",
".",
"Interface",
"(",
")",
"\n",
"}"
] | // ZeroOf returns a zero value of an element. | [
"ZeroOf",
"returns",
"a",
"zero",
"value",
"of",
"an",
"element",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L248-L254 | train |
thoas/go-funk | helpers.go | RandomInt | func RandomInt(min, max int) int {
return min + rand.Intn(max-min)
} | go | func RandomInt(min, max int) int {
return min + rand.Intn(max-min)
} | [
"func",
"RandomInt",
"(",
"min",
",",
"max",
"int",
")",
"int",
"{",
"return",
"min",
"+",
"rand",
".",
"Intn",
"(",
"max",
"-",
"min",
")",
"\n",
"}"
] | // RandomInt generates a random int, based on a min and max values | [
"RandomInt",
"generates",
"a",
"random",
"int",
"based",
"on",
"a",
"min",
"and",
"max",
"values"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L257-L259 | train |
thoas/go-funk | helpers.go | Shard | func Shard(str string, width int, depth int, restOnly bool) []string {
var results []string
for i := 0; i < depth; i++ {
results = append(results, str[(width*i):(width*(i+1))])
}
if restOnly {
results = append(results, str[(width*depth):])
} else {
results = append(results, str)
}
return results
} | go | func Shard(str string, width int, depth int, restOnly bool) []string {
var results []string
for i := 0; i < depth; i++ {
results = append(results, str[(width*i):(width*(i+1))])
}
if restOnly {
results = append(results, str[(width*depth):])
} else {
results = append(results, str)
}
return results
} | [
"func",
"Shard",
"(",
"str",
"string",
",",
"width",
"int",
",",
"depth",
"int",
",",
"restOnly",
"bool",
")",
"[",
"]",
"string",
"{",
"var",
"results",
"[",
"]",
"string",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"depth",
";",
"i",
"++",
"{",
"results",
"=",
"append",
"(",
"results",
",",
"str",
"[",
"(",
"width",
"*",
"i",
")",
":",
"(",
"width",
"*",
"(",
"i",
"+",
"1",
")",
")",
"]",
")",
"\n",
"}",
"\n\n",
"if",
"restOnly",
"{",
"results",
"=",
"append",
"(",
"results",
",",
"str",
"[",
"(",
"width",
"*",
"depth",
")",
":",
"]",
")",
"\n",
"}",
"else",
"{",
"results",
"=",
"append",
"(",
"results",
",",
"str",
")",
"\n",
"}",
"\n\n",
"return",
"results",
"\n",
"}"
] | // Shard will shard a string name | [
"Shard",
"will",
"shard",
"a",
"string",
"name"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L262-L276 | train |
thoas/go-funk | helpers.go | RandomString | func RandomString(n int, allowedChars ...[]rune) string {
var letters []rune
if len(allowedChars) == 0 {
letters = defaultLetters
} else {
letters = allowedChars[0]
}
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
} | go | func RandomString(n int, allowedChars ...[]rune) string {
var letters []rune
if len(allowedChars) == 0 {
letters = defaultLetters
} else {
letters = allowedChars[0]
}
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
} | [
"func",
"RandomString",
"(",
"n",
"int",
",",
"allowedChars",
"...",
"[",
"]",
"rune",
")",
"string",
"{",
"var",
"letters",
"[",
"]",
"rune",
"\n\n",
"if",
"len",
"(",
"allowedChars",
")",
"==",
"0",
"{",
"letters",
"=",
"defaultLetters",
"\n",
"}",
"else",
"{",
"letters",
"=",
"allowedChars",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"b",
":=",
"make",
"(",
"[",
"]",
"rune",
",",
"n",
")",
"\n",
"for",
"i",
":=",
"range",
"b",
"{",
"b",
"[",
"i",
"]",
"=",
"letters",
"[",
"rand",
".",
"Intn",
"(",
"len",
"(",
"letters",
")",
")",
"]",
"\n",
"}",
"\n\n",
"return",
"string",
"(",
"b",
")",
"\n",
"}"
] | // RandomString returns a random string with a fixed length | [
"RandomString",
"returns",
"a",
"random",
"string",
"with",
"a",
"fixed",
"length"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/helpers.go#L281-L296 | train |
thoas/go-funk | transform.go | Chunk | func Chunk(arr interface{}, size int) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be neither array nor slice")
}
arrValue := reflect.ValueOf(arr)
arrType := arrValue.Type()
resultSliceType := reflect.SliceOf(arrType)
// Initialize final result slice which will contains slice
resultSlice := reflect.MakeSlice(resultSliceType, 0, 0)
itemType := arrType.Elem()
var itemSlice reflect.Value
itemSliceType := reflect.SliceOf(itemType)
length := arrValue.Len()
for i := 0; i < length; i++ {
if i%size == 0 || i == 0 {
if itemSlice.Kind() != reflect.Invalid {
resultSlice = reflect.Append(resultSlice, itemSlice)
}
itemSlice = reflect.MakeSlice(itemSliceType, 0, 0)
}
itemSlice = reflect.Append(itemSlice, arrValue.Index(i))
if i == length-1 {
resultSlice = reflect.Append(resultSlice, itemSlice)
}
}
return resultSlice.Interface()
} | go | func Chunk(arr interface{}, size int) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be neither array nor slice")
}
arrValue := reflect.ValueOf(arr)
arrType := arrValue.Type()
resultSliceType := reflect.SliceOf(arrType)
// Initialize final result slice which will contains slice
resultSlice := reflect.MakeSlice(resultSliceType, 0, 0)
itemType := arrType.Elem()
var itemSlice reflect.Value
itemSliceType := reflect.SliceOf(itemType)
length := arrValue.Len()
for i := 0; i < length; i++ {
if i%size == 0 || i == 0 {
if itemSlice.Kind() != reflect.Invalid {
resultSlice = reflect.Append(resultSlice, itemSlice)
}
itemSlice = reflect.MakeSlice(itemSliceType, 0, 0)
}
itemSlice = reflect.Append(itemSlice, arrValue.Index(i))
if i == length-1 {
resultSlice = reflect.Append(resultSlice, itemSlice)
}
}
return resultSlice.Interface()
} | [
"func",
"Chunk",
"(",
"arr",
"interface",
"{",
"}",
",",
"size",
"int",
")",
"interface",
"{",
"}",
"{",
"if",
"!",
"IsIteratee",
"(",
"arr",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"arrValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"arr",
")",
"\n\n",
"arrType",
":=",
"arrValue",
".",
"Type",
"(",
")",
"\n\n",
"resultSliceType",
":=",
"reflect",
".",
"SliceOf",
"(",
"arrType",
")",
"\n\n",
"// Initialize final result slice which will contains slice",
"resultSlice",
":=",
"reflect",
".",
"MakeSlice",
"(",
"resultSliceType",
",",
"0",
",",
"0",
")",
"\n\n",
"itemType",
":=",
"arrType",
".",
"Elem",
"(",
")",
"\n\n",
"var",
"itemSlice",
"reflect",
".",
"Value",
"\n\n",
"itemSliceType",
":=",
"reflect",
".",
"SliceOf",
"(",
"itemType",
")",
"\n\n",
"length",
":=",
"arrValue",
".",
"Len",
"(",
")",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"if",
"i",
"%",
"size",
"==",
"0",
"||",
"i",
"==",
"0",
"{",
"if",
"itemSlice",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Invalid",
"{",
"resultSlice",
"=",
"reflect",
".",
"Append",
"(",
"resultSlice",
",",
"itemSlice",
")",
"\n",
"}",
"\n\n",
"itemSlice",
"=",
"reflect",
".",
"MakeSlice",
"(",
"itemSliceType",
",",
"0",
",",
"0",
")",
"\n",
"}",
"\n\n",
"itemSlice",
"=",
"reflect",
".",
"Append",
"(",
"itemSlice",
",",
"arrValue",
".",
"Index",
"(",
"i",
")",
")",
"\n\n",
"if",
"i",
"==",
"length",
"-",
"1",
"{",
"resultSlice",
"=",
"reflect",
".",
"Append",
"(",
"resultSlice",
",",
"itemSlice",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"resultSlice",
".",
"Interface",
"(",
")",
"\n",
"}"
] | // Chunk creates an array of elements split into groups with the length of size.
// If array can't be split evenly, the final chunk will be
// the remaining element. | [
"Chunk",
"creates",
"an",
"array",
"of",
"elements",
"split",
"into",
"groups",
"with",
"the",
"length",
"of",
"size",
".",
"If",
"array",
"can",
"t",
"be",
"split",
"evenly",
"the",
"final",
"chunk",
"will",
"be",
"the",
"remaining",
"element",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/transform.go#L12-L51 | train |
thoas/go-funk | transform.go | Map | func Map(arr interface{}, mapFunc interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(mapFunc) {
panic("Second argument must be function")
}
var (
funcValue = reflect.ValueOf(mapFunc)
arrValue = reflect.ValueOf(arr)
arrType = arrValue.Type()
)
kind := arrType.Kind()
if kind == reflect.Slice || kind == reflect.Array {
return mapSlice(arrValue, funcValue)
}
if kind == reflect.Map {
return mapMap(arrValue, funcValue)
}
panic(fmt.Sprintf("Type %s is not supported by Map", arrType.String()))
} | go | func Map(arr interface{}, mapFunc interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(mapFunc) {
panic("Second argument must be function")
}
var (
funcValue = reflect.ValueOf(mapFunc)
arrValue = reflect.ValueOf(arr)
arrType = arrValue.Type()
)
kind := arrType.Kind()
if kind == reflect.Slice || kind == reflect.Array {
return mapSlice(arrValue, funcValue)
}
if kind == reflect.Map {
return mapMap(arrValue, funcValue)
}
panic(fmt.Sprintf("Type %s is not supported by Map", arrType.String()))
} | [
"func",
"Map",
"(",
"arr",
"interface",
"{",
"}",
",",
"mapFunc",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"if",
"!",
"IsIteratee",
"(",
"arr",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"IsFunction",
"(",
"mapFunc",
")",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"var",
"(",
"funcValue",
"=",
"reflect",
".",
"ValueOf",
"(",
"mapFunc",
")",
"\n",
"arrValue",
"=",
"reflect",
".",
"ValueOf",
"(",
"arr",
")",
"\n",
"arrType",
"=",
"arrValue",
".",
"Type",
"(",
")",
"\n",
")",
"\n\n",
"kind",
":=",
"arrType",
".",
"Kind",
"(",
")",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"Slice",
"||",
"kind",
"==",
"reflect",
".",
"Array",
"{",
"return",
"mapSlice",
"(",
"arrValue",
",",
"funcValue",
")",
"\n",
"}",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"Map",
"{",
"return",
"mapMap",
"(",
"arrValue",
",",
"funcValue",
")",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"arrType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Map manipulates an iteratee and transforms it to another type. | [
"Map",
"manipulates",
"an",
"iteratee",
"and",
"transforms",
"it",
"to",
"another",
"type",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/transform.go#L193-L219 | train |
thoas/go-funk | transform.go | Shuffle | func Shuffle(in interface{}) interface{} {
value := reflect.ValueOf(in)
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
resultSlice := makeSlice(value, length)
for i, v := range rand.Perm(length) {
resultSlice.Index(i).Set(value.Index(v))
}
return resultSlice.Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Shuffle", valueType.String()))
} | go | func Shuffle(in interface{}) interface{} {
value := reflect.ValueOf(in)
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
resultSlice := makeSlice(value, length)
for i, v := range rand.Perm(length) {
resultSlice.Index(i).Set(value.Index(v))
}
return resultSlice.Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Shuffle", valueType.String()))
} | [
"func",
"Shuffle",
"(",
"in",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"value",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n",
"valueType",
":=",
"value",
".",
"Type",
"(",
")",
"\n\n",
"kind",
":=",
"value",
".",
"Kind",
"(",
")",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"Array",
"||",
"kind",
"==",
"reflect",
".",
"Slice",
"{",
"length",
":=",
"value",
".",
"Len",
"(",
")",
"\n\n",
"resultSlice",
":=",
"makeSlice",
"(",
"value",
",",
"length",
")",
"\n\n",
"for",
"i",
",",
"v",
":=",
"range",
"rand",
".",
"Perm",
"(",
"length",
")",
"{",
"resultSlice",
".",
"Index",
"(",
"i",
")",
".",
"Set",
"(",
"value",
".",
"Index",
"(",
"v",
")",
")",
"\n",
"}",
"\n\n",
"return",
"resultSlice",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Shuffle creates an array of shuffled values | [
"Shuffle",
"creates",
"an",
"array",
"of",
"shuffled",
"values"
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/transform.go#L252-L271 | train |
thoas/go-funk | transform.go | Reverse | func Reverse(in interface{}) interface{} {
value := reflect.ValueOf(in)
valueType := value.Type()
kind := value.Kind()
if kind == reflect.String {
return ReverseString(in.(string))
}
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
resultSlice := makeSlice(value, length)
j := 0
for i := length - 1; i >= 0; i-- {
resultSlice.Index(j).Set(value.Index(i))
j++
}
return resultSlice.Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Reverse", valueType.String()))
} | go | func Reverse(in interface{}) interface{} {
value := reflect.ValueOf(in)
valueType := value.Type()
kind := value.Kind()
if kind == reflect.String {
return ReverseString(in.(string))
}
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
resultSlice := makeSlice(value, length)
j := 0
for i := length - 1; i >= 0; i-- {
resultSlice.Index(j).Set(value.Index(i))
j++
}
return resultSlice.Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Reverse", valueType.String()))
} | [
"func",
"Reverse",
"(",
"in",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"value",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n",
"valueType",
":=",
"value",
".",
"Type",
"(",
")",
"\n\n",
"kind",
":=",
"value",
".",
"Kind",
"(",
")",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"String",
"{",
"return",
"ReverseString",
"(",
"in",
".",
"(",
"string",
")",
")",
"\n",
"}",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"Array",
"||",
"kind",
"==",
"reflect",
".",
"Slice",
"{",
"length",
":=",
"value",
".",
"Len",
"(",
")",
"\n\n",
"resultSlice",
":=",
"makeSlice",
"(",
"value",
",",
"length",
")",
"\n\n",
"j",
":=",
"0",
"\n",
"for",
"i",
":=",
"length",
"-",
"1",
";",
"i",
">=",
"0",
";",
"i",
"--",
"{",
"resultSlice",
".",
"Index",
"(",
"j",
")",
".",
"Set",
"(",
"value",
".",
"Index",
"(",
"i",
")",
")",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"resultSlice",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Reverse transforms an array the first element will become the last,
// the second element will become the second to last, etc. | [
"Reverse",
"transforms",
"an",
"array",
"the",
"first",
"element",
"will",
"become",
"the",
"last",
"the",
"second",
"element",
"will",
"become",
"the",
"second",
"to",
"last",
"etc",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/transform.go#L275-L300 | train |
thoas/go-funk | transform.go | Uniq | func Uniq(in interface{}) interface{} {
value := reflect.ValueOf(in)
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
seen := make(map[interface{}]bool, length)
j := 0
for i := 0; i < length; i++ {
val := value.Index(i)
v := val.Interface()
if _, ok := seen[v]; ok {
continue
}
seen[v] = true
value.Index(j).Set(val)
j++
}
return value.Slice(0, j).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Uniq", valueType.String()))
} | go | func Uniq(in interface{}) interface{} {
value := reflect.ValueOf(in)
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
seen := make(map[interface{}]bool, length)
j := 0
for i := 0; i < length; i++ {
val := value.Index(i)
v := val.Interface()
if _, ok := seen[v]; ok {
continue
}
seen[v] = true
value.Index(j).Set(val)
j++
}
return value.Slice(0, j).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Uniq", valueType.String()))
} | [
"func",
"Uniq",
"(",
"in",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"value",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n",
"valueType",
":=",
"value",
".",
"Type",
"(",
")",
"\n\n",
"kind",
":=",
"value",
".",
"Kind",
"(",
")",
"\n\n",
"if",
"kind",
"==",
"reflect",
".",
"Array",
"||",
"kind",
"==",
"reflect",
".",
"Slice",
"{",
"length",
":=",
"value",
".",
"Len",
"(",
")",
"\n\n",
"seen",
":=",
"make",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"bool",
",",
"length",
")",
"\n",
"j",
":=",
"0",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"val",
":=",
"value",
".",
"Index",
"(",
"i",
")",
"\n",
"v",
":=",
"val",
".",
"Interface",
"(",
")",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"seen",
"[",
"v",
"]",
";",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"seen",
"[",
"v",
"]",
"=",
"true",
"\n",
"value",
".",
"Index",
"(",
"j",
")",
".",
"Set",
"(",
"val",
")",
"\n",
"j",
"++",
"\n",
"}",
"\n\n",
"return",
"value",
".",
"Slice",
"(",
"0",
",",
"j",
")",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n\n",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"valueType",
".",
"String",
"(",
")",
")",
")",
"\n",
"}"
] | // Uniq creates an array with unique values. | [
"Uniq",
"creates",
"an",
"array",
"with",
"unique",
"values",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/transform.go#L303-L332 | train |
thoas/go-funk | transform.go | ConvertSlice | func ConvertSlice(in interface{}, out interface{}) {
srcValue := reflect.ValueOf(in)
dstValue := reflect.ValueOf(out)
if dstValue.Kind() != reflect.Ptr {
panic("Second argument must be a pointer")
}
dstValue = dstValue.Elem()
if srcValue.Kind() != reflect.Slice && srcValue.Kind() != reflect.Array {
panic("First argument must be an array or slice")
}
if dstValue.Kind() != reflect.Slice && dstValue.Kind() != reflect.Array {
panic("Second argument must be an array or slice")
}
// returns value that points to dstValue
direct := reflect.Indirect(dstValue)
length := srcValue.Len()
for i := 0; i < length; i++ {
dstValue = reflect.Append(dstValue, srcValue.Index(i))
}
direct.Set(dstValue)
} | go | func ConvertSlice(in interface{}, out interface{}) {
srcValue := reflect.ValueOf(in)
dstValue := reflect.ValueOf(out)
if dstValue.Kind() != reflect.Ptr {
panic("Second argument must be a pointer")
}
dstValue = dstValue.Elem()
if srcValue.Kind() != reflect.Slice && srcValue.Kind() != reflect.Array {
panic("First argument must be an array or slice")
}
if dstValue.Kind() != reflect.Slice && dstValue.Kind() != reflect.Array {
panic("Second argument must be an array or slice")
}
// returns value that points to dstValue
direct := reflect.Indirect(dstValue)
length := srcValue.Len()
for i := 0; i < length; i++ {
dstValue = reflect.Append(dstValue, srcValue.Index(i))
}
direct.Set(dstValue)
} | [
"func",
"ConvertSlice",
"(",
"in",
"interface",
"{",
"}",
",",
"out",
"interface",
"{",
"}",
")",
"{",
"srcValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"in",
")",
"\n\n",
"dstValue",
":=",
"reflect",
".",
"ValueOf",
"(",
"out",
")",
"\n\n",
"if",
"dstValue",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"dstValue",
"=",
"dstValue",
".",
"Elem",
"(",
")",
"\n\n",
"if",
"srcValue",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Slice",
"&&",
"srcValue",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Array",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"dstValue",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Slice",
"&&",
"dstValue",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Array",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// returns value that points to dstValue",
"direct",
":=",
"reflect",
".",
"Indirect",
"(",
"dstValue",
")",
"\n\n",
"length",
":=",
"srcValue",
".",
"Len",
"(",
")",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"length",
";",
"i",
"++",
"{",
"dstValue",
"=",
"reflect",
".",
"Append",
"(",
"dstValue",
",",
"srcValue",
".",
"Index",
"(",
"i",
")",
")",
"\n",
"}",
"\n\n",
"direct",
".",
"Set",
"(",
"dstValue",
")",
"\n",
"}"
] | // ConvertSlice converts a slice type to another,
// a perfect example would be to convert a slice of struct to a slice of interface. | [
"ConvertSlice",
"converts",
"a",
"slice",
"type",
"to",
"another",
"a",
"perfect",
"example",
"would",
"be",
"to",
"convert",
"a",
"slice",
"of",
"struct",
"to",
"a",
"slice",
"of",
"interface",
"."
] | a0a199e85d6d961670e46df4998f8868a3e3309b | https://github.com/thoas/go-funk/blob/a0a199e85d6d961670e46df4998f8868a3e3309b/transform.go#L336-L365 | train |
genuinetools/reg | registry/customtransport.go | RoundTrip | func (t *CustomTransport) RoundTrip(request *http.Request) (*http.Response, error) {
if len(t.Headers) != 0 {
for header, value := range t.Headers {
request.Header.Add(header, value)
}
}
resp, err := t.Transport.RoundTrip(request)
return resp, err
} | go | func (t *CustomTransport) RoundTrip(request *http.Request) (*http.Response, error) {
if len(t.Headers) != 0 {
for header, value := range t.Headers {
request.Header.Add(header, value)
}
}
resp, err := t.Transport.RoundTrip(request)
return resp, err
} | [
"func",
"(",
"t",
"*",
"CustomTransport",
")",
"RoundTrip",
"(",
"request",
"*",
"http",
".",
"Request",
")",
"(",
"*",
"http",
".",
"Response",
",",
"error",
")",
"{",
"if",
"len",
"(",
"t",
".",
"Headers",
")",
"!=",
"0",
"{",
"for",
"header",
",",
"value",
":=",
"range",
"t",
".",
"Headers",
"{",
"request",
".",
"Header",
".",
"Add",
"(",
"header",
",",
"value",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"resp",
",",
"err",
":=",
"t",
".",
"Transport",
".",
"RoundTrip",
"(",
"request",
")",
"\n\n",
"return",
"resp",
",",
"err",
"\n",
"}"
] | // RoundTrip defines the round tripper for the error transport. | [
"RoundTrip",
"defines",
"the",
"round",
"tripper",
"for",
"the",
"error",
"transport",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/customtransport.go#L14-L24 | train |
genuinetools/reg | registry/image.go | Reference | func (i Image) Reference() string {
if len(i.Digest.String()) > 1 {
return i.Digest.String()
}
return i.Tag
} | go | func (i Image) Reference() string {
if len(i.Digest.String()) > 1 {
return i.Digest.String()
}
return i.Tag
} | [
"func",
"(",
"i",
"Image",
")",
"Reference",
"(",
")",
"string",
"{",
"if",
"len",
"(",
"i",
".",
"Digest",
".",
"String",
"(",
")",
")",
">",
"1",
"{",
"return",
"i",
".",
"Digest",
".",
"String",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"i",
".",
"Tag",
"\n",
"}"
] | // Reference returns either the digest if it is non-empty or the tag for the image. | [
"Reference",
"returns",
"either",
"the",
"digest",
"if",
"it",
"is",
"non",
"-",
"empty",
"or",
"the",
"tag",
"for",
"the",
"image",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/image.go#L25-L31 | train |
genuinetools/reg | registry/image.go | WithDigest | func (i *Image) WithDigest(digest digest.Digest) (err error) {
i.Digest = digest
i.named, err = reference.WithDigest(i.named, digest)
return err
} | go | func (i *Image) WithDigest(digest digest.Digest) (err error) {
i.Digest = digest
i.named, err = reference.WithDigest(i.named, digest)
return err
} | [
"func",
"(",
"i",
"*",
"Image",
")",
"WithDigest",
"(",
"digest",
"digest",
".",
"Digest",
")",
"(",
"err",
"error",
")",
"{",
"i",
".",
"Digest",
"=",
"digest",
"\n",
"i",
".",
"named",
",",
"err",
"=",
"reference",
".",
"WithDigest",
"(",
"i",
".",
"named",
",",
"digest",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // WithDigest sets the digest for an image. | [
"WithDigest",
"sets",
"the",
"digest",
"for",
"an",
"image",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/image.go#L34-L38 | train |
genuinetools/reg | registry/image.go | ParseImage | func ParseImage(image string) (Image, error) {
// Parse the image name and tag.
named, err := reference.ParseNormalizedNamed(image)
if err != nil {
return Image{}, fmt.Errorf("parsing image %q failed: %v", image, err)
}
// Add the latest lag if they did not provide one.
named = reference.TagNameOnly(named)
i := Image{
named: named,
Domain: reference.Domain(named),
Path: reference.Path(named),
}
// Add the tag if there was one.
if tagged, ok := named.(reference.Tagged); ok {
i.Tag = tagged.Tag()
}
// Add the digest if there was one.
if canonical, ok := named.(reference.Canonical); ok {
i.Digest = canonical.Digest()
}
return i, nil
} | go | func ParseImage(image string) (Image, error) {
// Parse the image name and tag.
named, err := reference.ParseNormalizedNamed(image)
if err != nil {
return Image{}, fmt.Errorf("parsing image %q failed: %v", image, err)
}
// Add the latest lag if they did not provide one.
named = reference.TagNameOnly(named)
i := Image{
named: named,
Domain: reference.Domain(named),
Path: reference.Path(named),
}
// Add the tag if there was one.
if tagged, ok := named.(reference.Tagged); ok {
i.Tag = tagged.Tag()
}
// Add the digest if there was one.
if canonical, ok := named.(reference.Canonical); ok {
i.Digest = canonical.Digest()
}
return i, nil
} | [
"func",
"ParseImage",
"(",
"image",
"string",
")",
"(",
"Image",
",",
"error",
")",
"{",
"// Parse the image name and tag.",
"named",
",",
"err",
":=",
"reference",
".",
"ParseNormalizedNamed",
"(",
"image",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"Image",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"image",
",",
"err",
")",
"\n",
"}",
"\n",
"// Add the latest lag if they did not provide one.",
"named",
"=",
"reference",
".",
"TagNameOnly",
"(",
"named",
")",
"\n\n",
"i",
":=",
"Image",
"{",
"named",
":",
"named",
",",
"Domain",
":",
"reference",
".",
"Domain",
"(",
"named",
")",
",",
"Path",
":",
"reference",
".",
"Path",
"(",
"named",
")",
",",
"}",
"\n\n",
"// Add the tag if there was one.",
"if",
"tagged",
",",
"ok",
":=",
"named",
".",
"(",
"reference",
".",
"Tagged",
")",
";",
"ok",
"{",
"i",
".",
"Tag",
"=",
"tagged",
".",
"Tag",
"(",
")",
"\n",
"}",
"\n\n",
"// Add the digest if there was one.",
"if",
"canonical",
",",
"ok",
":=",
"named",
".",
"(",
"reference",
".",
"Canonical",
")",
";",
"ok",
"{",
"i",
".",
"Digest",
"=",
"canonical",
".",
"Digest",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"i",
",",
"nil",
"\n",
"}"
] | // ParseImage returns an Image struct with all the values filled in for a given image. | [
"ParseImage",
"returns",
"an",
"Image",
"struct",
"with",
"all",
"the",
"values",
"filled",
"in",
"for",
"a",
"given",
"image",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/image.go#L41-L67 | train |
genuinetools/reg | registry/tags.go | Tags | func (r *Registry) Tags(ctx context.Context, repository string) ([]string, error) {
url := r.url("/v2/%s/tags/list", repository)
r.Logf("registry.tags url=%s repository=%s", url, repository)
var response tagsResponse
if _, err := r.getJSON(ctx, url, &response); err != nil {
return nil, err
}
return response.Tags, nil
} | go | func (r *Registry) Tags(ctx context.Context, repository string) ([]string, error) {
url := r.url("/v2/%s/tags/list", repository)
r.Logf("registry.tags url=%s repository=%s", url, repository)
var response tagsResponse
if _, err := r.getJSON(ctx, url, &response); err != nil {
return nil, err
}
return response.Tags, nil
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"Tags",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
"string",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"url",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"repository",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
",",
"repository",
")",
"\n\n",
"var",
"response",
"tagsResponse",
"\n",
"if",
"_",
",",
"err",
":=",
"r",
".",
"getJSON",
"(",
"ctx",
",",
"url",
",",
"&",
"response",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"response",
".",
"Tags",
",",
"nil",
"\n",
"}"
] | // Tags returns the tags for a specific repository. | [
"Tags",
"returns",
"the",
"tags",
"for",
"a",
"specific",
"repository",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/tags.go#L10-L20 | train |
genuinetools/reg | registry/ping.go | Ping | func (r *Registry) Ping(ctx context.Context) error {
url := r.url("/v2/")
r.Logf("registry.ping url=%s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := r.Client.Do(req.WithContext(ctx))
if resp != nil {
defer resp.Body.Close()
}
return err
} | go | func (r *Registry) Ping(ctx context.Context) error {
url := r.url("/v2/")
r.Logf("registry.ping url=%s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := r.Client.Do(req.WithContext(ctx))
if resp != nil {
defer resp.Body.Close()
}
return err
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"Ping",
"(",
"ctx",
"context",
".",
"Context",
")",
"error",
"{",
"url",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
")",
"\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"url",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"resp",
",",
"err",
":=",
"r",
".",
"Client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"resp",
"!=",
"nil",
"{",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"}",
"\n",
"return",
"err",
"\n",
"}"
] | // Ping tries to contact a registry URL to make sure it is up and accessible. | [
"Ping",
"tries",
"to",
"contact",
"a",
"registry",
"URL",
"to",
"make",
"sure",
"it",
"is",
"up",
"and",
"accessible",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/ping.go#L9-L21 | train |
genuinetools/reg | clair/vulns.go | Vulnerabilities | func (c *Clair) Vulnerabilities(ctx context.Context, r *registry.Registry, repo, tag string) (VulnerabilityReport, error) {
report := VulnerabilityReport{
RegistryURL: r.Domain,
Repo: repo,
Tag: tag,
Date: time.Now().Local().Format(time.RFC1123),
VulnsBySeverity: make(map[string][]Vulnerability),
}
filteredLayers, _, err := c.getLayers(ctx, r, repo, tag, true)
if err != nil {
return report, fmt.Errorf("getting filtered layers failed: %v", err)
}
if len(filteredLayers) == 0 {
fmt.Printf("No need to analyse image %s:%s as there is no non-emtpy layer", repo, tag)
return report, nil
}
for i := len(filteredLayers) - 1; i >= 0; i-- {
// Form the clair layer.
l, err := c.NewClairLayer(ctx, r, repo, filteredLayers, i)
if err != nil {
return report, err
}
// Post the layer.
if _, err := c.PostLayer(ctx, l); err != nil {
return report, err
}
}
report.Name = filteredLayers[0].Digest.String()
vl, err := c.GetLayer(ctx, filteredLayers[0].Digest.String(), true, true)
if err != nil {
return report, err
}
// Get the vulns.
for _, f := range vl.Features {
report.Vulns = append(report.Vulns, f.Vulnerabilities...)
}
vulnsBy := func(sev string, store map[string][]Vulnerability) []Vulnerability {
items, found := store[sev]
if !found {
items = make([]Vulnerability, 0)
store[sev] = items
}
return items
}
// group by severity
for _, v := range report.Vulns {
sevRow := vulnsBy(v.Severity, report.VulnsBySeverity)
report.VulnsBySeverity[v.Severity] = append(sevRow, v)
}
// calculate number of bad vulns
report.BadVulns = len(report.VulnsBySeverity["High"]) + len(report.VulnsBySeverity["Critical"]) + len(report.VulnsBySeverity["Defcon1"])
return report, nil
} | go | func (c *Clair) Vulnerabilities(ctx context.Context, r *registry.Registry, repo, tag string) (VulnerabilityReport, error) {
report := VulnerabilityReport{
RegistryURL: r.Domain,
Repo: repo,
Tag: tag,
Date: time.Now().Local().Format(time.RFC1123),
VulnsBySeverity: make(map[string][]Vulnerability),
}
filteredLayers, _, err := c.getLayers(ctx, r, repo, tag, true)
if err != nil {
return report, fmt.Errorf("getting filtered layers failed: %v", err)
}
if len(filteredLayers) == 0 {
fmt.Printf("No need to analyse image %s:%s as there is no non-emtpy layer", repo, tag)
return report, nil
}
for i := len(filteredLayers) - 1; i >= 0; i-- {
// Form the clair layer.
l, err := c.NewClairLayer(ctx, r, repo, filteredLayers, i)
if err != nil {
return report, err
}
// Post the layer.
if _, err := c.PostLayer(ctx, l); err != nil {
return report, err
}
}
report.Name = filteredLayers[0].Digest.String()
vl, err := c.GetLayer(ctx, filteredLayers[0].Digest.String(), true, true)
if err != nil {
return report, err
}
// Get the vulns.
for _, f := range vl.Features {
report.Vulns = append(report.Vulns, f.Vulnerabilities...)
}
vulnsBy := func(sev string, store map[string][]Vulnerability) []Vulnerability {
items, found := store[sev]
if !found {
items = make([]Vulnerability, 0)
store[sev] = items
}
return items
}
// group by severity
for _, v := range report.Vulns {
sevRow := vulnsBy(v.Severity, report.VulnsBySeverity)
report.VulnsBySeverity[v.Severity] = append(sevRow, v)
}
// calculate number of bad vulns
report.BadVulns = len(report.VulnsBySeverity["High"]) + len(report.VulnsBySeverity["Critical"]) + len(report.VulnsBySeverity["Defcon1"])
return report, nil
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"Vulnerabilities",
"(",
"ctx",
"context",
".",
"Context",
",",
"r",
"*",
"registry",
".",
"Registry",
",",
"repo",
",",
"tag",
"string",
")",
"(",
"VulnerabilityReport",
",",
"error",
")",
"{",
"report",
":=",
"VulnerabilityReport",
"{",
"RegistryURL",
":",
"r",
".",
"Domain",
",",
"Repo",
":",
"repo",
",",
"Tag",
":",
"tag",
",",
"Date",
":",
"time",
".",
"Now",
"(",
")",
".",
"Local",
"(",
")",
".",
"Format",
"(",
"time",
".",
"RFC1123",
")",
",",
"VulnsBySeverity",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"[",
"]",
"Vulnerability",
")",
",",
"}",
"\n\n",
"filteredLayers",
",",
"_",
",",
"err",
":=",
"c",
".",
"getLayers",
"(",
"ctx",
",",
"r",
",",
"repo",
",",
"tag",
",",
"true",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"report",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"filteredLayers",
")",
"==",
"0",
"{",
"fmt",
".",
"Printf",
"(",
"\"",
"\"",
",",
"repo",
",",
"tag",
")",
"\n",
"return",
"report",
",",
"nil",
"\n",
"}",
"\n\n",
"for",
"i",
":=",
"len",
"(",
"filteredLayers",
")",
"-",
"1",
";",
"i",
">=",
"0",
";",
"i",
"--",
"{",
"// Form the clair layer.",
"l",
",",
"err",
":=",
"c",
".",
"NewClairLayer",
"(",
"ctx",
",",
"r",
",",
"repo",
",",
"filteredLayers",
",",
"i",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"report",
",",
"err",
"\n",
"}",
"\n\n",
"// Post the layer.",
"if",
"_",
",",
"err",
":=",
"c",
".",
"PostLayer",
"(",
"ctx",
",",
"l",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"report",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"report",
".",
"Name",
"=",
"filteredLayers",
"[",
"0",
"]",
".",
"Digest",
".",
"String",
"(",
")",
"\n\n",
"vl",
",",
"err",
":=",
"c",
".",
"GetLayer",
"(",
"ctx",
",",
"filteredLayers",
"[",
"0",
"]",
".",
"Digest",
".",
"String",
"(",
")",
",",
"true",
",",
"true",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"report",
",",
"err",
"\n",
"}",
"\n\n",
"// Get the vulns.",
"for",
"_",
",",
"f",
":=",
"range",
"vl",
".",
"Features",
"{",
"report",
".",
"Vulns",
"=",
"append",
"(",
"report",
".",
"Vulns",
",",
"f",
".",
"Vulnerabilities",
"...",
")",
"\n",
"}",
"\n\n",
"vulnsBy",
":=",
"func",
"(",
"sev",
"string",
",",
"store",
"map",
"[",
"string",
"]",
"[",
"]",
"Vulnerability",
")",
"[",
"]",
"Vulnerability",
"{",
"items",
",",
"found",
":=",
"store",
"[",
"sev",
"]",
"\n",
"if",
"!",
"found",
"{",
"items",
"=",
"make",
"(",
"[",
"]",
"Vulnerability",
",",
"0",
")",
"\n",
"store",
"[",
"sev",
"]",
"=",
"items",
"\n",
"}",
"\n",
"return",
"items",
"\n",
"}",
"\n\n",
"// group by severity",
"for",
"_",
",",
"v",
":=",
"range",
"report",
".",
"Vulns",
"{",
"sevRow",
":=",
"vulnsBy",
"(",
"v",
".",
"Severity",
",",
"report",
".",
"VulnsBySeverity",
")",
"\n",
"report",
".",
"VulnsBySeverity",
"[",
"v",
".",
"Severity",
"]",
"=",
"append",
"(",
"sevRow",
",",
"v",
")",
"\n",
"}",
"\n\n",
"// calculate number of bad vulns",
"report",
".",
"BadVulns",
"=",
"len",
"(",
"report",
".",
"VulnsBySeverity",
"[",
"\"",
"\"",
"]",
")",
"+",
"len",
"(",
"report",
".",
"VulnsBySeverity",
"[",
"\"",
"\"",
"]",
")",
"+",
"len",
"(",
"report",
".",
"VulnsBySeverity",
"[",
"\"",
"\"",
"]",
")",
"\n\n",
"return",
"report",
",",
"nil",
"\n",
"}"
] | // Vulnerabilities scans the given repo and tag. | [
"Vulnerabilities",
"scans",
"the",
"given",
"repo",
"and",
"tag",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/vulns.go#L14-L77 | train |
genuinetools/reg | clair/layer.go | GetLayer | func (c *Clair) GetLayer(ctx context.Context, name string, features, vulnerabilities bool) (*Layer, error) {
url := c.url("/v1/layers/%s?features=%t&vulnerabilities=%t", name, features, vulnerabilities)
c.Logf("clair.layers.get url=%s name=%s", url, name)
var respLayer layerEnvelope
if _, err := c.getJSON(ctx, url, &respLayer); err != nil {
return nil, err
}
if respLayer.Error != nil {
return nil, fmt.Errorf("clair error: %s", respLayer.Error.Message)
}
return respLayer.Layer, nil
} | go | func (c *Clair) GetLayer(ctx context.Context, name string, features, vulnerabilities bool) (*Layer, error) {
url := c.url("/v1/layers/%s?features=%t&vulnerabilities=%t", name, features, vulnerabilities)
c.Logf("clair.layers.get url=%s name=%s", url, name)
var respLayer layerEnvelope
if _, err := c.getJSON(ctx, url, &respLayer); err != nil {
return nil, err
}
if respLayer.Error != nil {
return nil, fmt.Errorf("clair error: %s", respLayer.Error.Message)
}
return respLayer.Layer, nil
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"GetLayer",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
",",
"features",
",",
"vulnerabilities",
"bool",
")",
"(",
"*",
"Layer",
",",
"error",
")",
"{",
"url",
":=",
"c",
".",
"url",
"(",
"\"",
"\"",
",",
"name",
",",
"features",
",",
"vulnerabilities",
")",
"\n",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
",",
"name",
")",
"\n\n",
"var",
"respLayer",
"layerEnvelope",
"\n",
"if",
"_",
",",
"err",
":=",
"c",
".",
"getJSON",
"(",
"ctx",
",",
"url",
",",
"&",
"respLayer",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"respLayer",
".",
"Error",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"respLayer",
".",
"Error",
".",
"Message",
")",
"\n",
"}",
"\n\n",
"return",
"respLayer",
".",
"Layer",
",",
"nil",
"\n",
"}"
] | // GetLayer displays a Layer and optionally all of its features and vulnerabilities. | [
"GetLayer",
"displays",
"a",
"Layer",
"and",
"optionally",
"all",
"of",
"its",
"features",
"and",
"vulnerabilities",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/layer.go#L12-L26 | train |
genuinetools/reg | clair/layer.go | PostLayer | func (c *Clair) PostLayer(ctx context.Context, layer *Layer) (*Layer, error) {
url := c.url("/v1/layers")
c.Logf("clair.layers.post url=%s name=%s", url, layer.Name)
b, err := json.Marshal(layerEnvelope{Layer: layer})
if err != nil {
return nil, err
}
c.Logf("clair.layers.post req.Body=%s", string(b))
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.Client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer resp.Body.Close()
c.Logf("clair.layers.post resp.Status=%s", resp.Status)
var respLayer layerEnvelope
if err := json.NewDecoder(resp.Body).Decode(&respLayer); err != nil {
return nil, err
}
if respLayer.Error != nil {
return nil, fmt.Errorf("clair error: %s", respLayer.Error.Message)
}
return respLayer.Layer, err
} | go | func (c *Clair) PostLayer(ctx context.Context, layer *Layer) (*Layer, error) {
url := c.url("/v1/layers")
c.Logf("clair.layers.post url=%s name=%s", url, layer.Name)
b, err := json.Marshal(layerEnvelope{Layer: layer})
if err != nil {
return nil, err
}
c.Logf("clair.layers.post req.Body=%s", string(b))
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.Client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer resp.Body.Close()
c.Logf("clair.layers.post resp.Status=%s", resp.Status)
var respLayer layerEnvelope
if err := json.NewDecoder(resp.Body).Decode(&respLayer); err != nil {
return nil, err
}
if respLayer.Error != nil {
return nil, fmt.Errorf("clair error: %s", respLayer.Error.Message)
}
return respLayer.Layer, err
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"PostLayer",
"(",
"ctx",
"context",
".",
"Context",
",",
"layer",
"*",
"Layer",
")",
"(",
"*",
"Layer",
",",
"error",
")",
"{",
"url",
":=",
"c",
".",
"url",
"(",
"\"",
"\"",
")",
"\n",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
",",
"layer",
".",
"Name",
")",
"\n\n",
"b",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"layerEnvelope",
"{",
"Layer",
":",
"layer",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"string",
"(",
"b",
")",
")",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"url",
",",
"bytes",
".",
"NewReader",
"(",
"b",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"req",
".",
"Header",
".",
"Set",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n\n",
"resp",
",",
"err",
":=",
"c",
".",
"Client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n\n",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"resp",
".",
"Status",
")",
"\n\n",
"var",
"respLayer",
"layerEnvelope",
"\n",
"if",
"err",
":=",
"json",
".",
"NewDecoder",
"(",
"resp",
".",
"Body",
")",
".",
"Decode",
"(",
"&",
"respLayer",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"respLayer",
".",
"Error",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"respLayer",
".",
"Error",
".",
"Message",
")",
"\n",
"}",
"\n\n",
"return",
"respLayer",
".",
"Layer",
",",
"err",
"\n",
"}"
] | // PostLayer performs the analysis of a Layer from the provided path. | [
"PostLayer",
"performs",
"the",
"analysis",
"of",
"a",
"Layer",
"from",
"the",
"provided",
"path",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/layer.go#L29-L65 | train |
genuinetools/reg | clair/layer.go | DeleteLayer | func (c *Clair) DeleteLayer(ctx context.Context, name string) error {
url := c.url("/v1/layers/%s", name)
c.Logf("clair.layers.delete url=%s name=%s", url, name)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
resp, err := c.Client.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
c.Logf("clair.clair resp.Status=%s", resp.Status)
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusAccepted || resp.StatusCode == http.StatusNotFound {
return nil
}
return fmt.Errorf("got status code: %d", resp.StatusCode)
} | go | func (c *Clair) DeleteLayer(ctx context.Context, name string) error {
url := c.url("/v1/layers/%s", name)
c.Logf("clair.layers.delete url=%s name=%s", url, name)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
resp, err := c.Client.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
c.Logf("clair.clair resp.Status=%s", resp.Status)
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusAccepted || resp.StatusCode == http.StatusNotFound {
return nil
}
return fmt.Errorf("got status code: %d", resp.StatusCode)
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"DeleteLayer",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
")",
"error",
"{",
"url",
":=",
"c",
".",
"url",
"(",
"\"",
"\"",
",",
"name",
")",
"\n",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
",",
"name",
")",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"url",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"resp",
",",
"err",
":=",
"c",
".",
"Client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"resp",
".",
"Status",
")",
"\n\n",
"if",
"resp",
".",
"StatusCode",
"==",
"http",
".",
"StatusOK",
"||",
"resp",
".",
"StatusCode",
"==",
"http",
".",
"StatusAccepted",
"||",
"resp",
".",
"StatusCode",
"==",
"http",
".",
"StatusNotFound",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"StatusCode",
")",
"\n",
"}"
] | // DeleteLayer removes a layer reference from clair. | [
"DeleteLayer",
"removes",
"a",
"layer",
"reference",
"from",
"clair",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/layer.go#L68-L89 | train |
genuinetools/reg | registry/catalog.go | Catalog | func (r *Registry) Catalog(ctx context.Context, u string) ([]string, error) {
if u == "" {
u = "/v2/_catalog"
}
uri := r.url(u)
r.Logf("registry.catalog url=%s", uri)
var response catalogResponse
h, err := r.getJSON(ctx, uri, &response)
if err != nil {
return nil, err
}
for _, l := range link.ParseHeader(h) {
if l.Rel == "next" {
unescaped, _ := url.QueryUnescape(l.URI)
repos, err := r.Catalog(ctx, unescaped)
if err != nil {
return nil, err
}
response.Repositories = append(response.Repositories, repos...)
}
}
return response.Repositories, nil
} | go | func (r *Registry) Catalog(ctx context.Context, u string) ([]string, error) {
if u == "" {
u = "/v2/_catalog"
}
uri := r.url(u)
r.Logf("registry.catalog url=%s", uri)
var response catalogResponse
h, err := r.getJSON(ctx, uri, &response)
if err != nil {
return nil, err
}
for _, l := range link.ParseHeader(h) {
if l.Rel == "next" {
unescaped, _ := url.QueryUnescape(l.URI)
repos, err := r.Catalog(ctx, unescaped)
if err != nil {
return nil, err
}
response.Repositories = append(response.Repositories, repos...)
}
}
return response.Repositories, nil
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"Catalog",
"(",
"ctx",
"context",
".",
"Context",
",",
"u",
"string",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"if",
"u",
"==",
"\"",
"\"",
"{",
"u",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"uri",
":=",
"r",
".",
"url",
"(",
"u",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"uri",
")",
"\n\n",
"var",
"response",
"catalogResponse",
"\n",
"h",
",",
"err",
":=",
"r",
".",
"getJSON",
"(",
"ctx",
",",
"uri",
",",
"&",
"response",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"l",
":=",
"range",
"link",
".",
"ParseHeader",
"(",
"h",
")",
"{",
"if",
"l",
".",
"Rel",
"==",
"\"",
"\"",
"{",
"unescaped",
",",
"_",
":=",
"url",
".",
"QueryUnescape",
"(",
"l",
".",
"URI",
")",
"\n",
"repos",
",",
"err",
":=",
"r",
".",
"Catalog",
"(",
"ctx",
",",
"unescaped",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"response",
".",
"Repositories",
"=",
"append",
"(",
"response",
".",
"Repositories",
",",
"repos",
"...",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"response",
".",
"Repositories",
",",
"nil",
"\n",
"}"
] | // Catalog returns the repositories in a registry. | [
"Catalog",
"returns",
"the",
"repositories",
"in",
"a",
"registry",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/catalog.go#L15-L40 | train |
genuinetools/reg | registry/tokentransport.go | RoundTrip | func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := t.Transport.RoundTrip(req)
if err != nil {
return resp, err
}
authService, err := isTokenDemand(resp)
if err != nil {
resp.Body.Close()
return nil, err
}
if authService == nil {
return resp, nil
}
resp.Body.Close()
return t.authAndRetry(authService, req)
} | go | func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := t.Transport.RoundTrip(req)
if err != nil {
return resp, err
}
authService, err := isTokenDemand(resp)
if err != nil {
resp.Body.Close()
return nil, err
}
if authService == nil {
return resp, nil
}
resp.Body.Close()
return t.authAndRetry(authService, req)
} | [
"func",
"(",
"t",
"*",
"TokenTransport",
")",
"RoundTrip",
"(",
"req",
"*",
"http",
".",
"Request",
")",
"(",
"*",
"http",
".",
"Response",
",",
"error",
")",
"{",
"resp",
",",
"err",
":=",
"t",
".",
"Transport",
".",
"RoundTrip",
"(",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"resp",
",",
"err",
"\n",
"}",
"\n\n",
"authService",
",",
"err",
":=",
"isTokenDemand",
"(",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"authService",
"==",
"nil",
"{",
"return",
"resp",
",",
"nil",
"\n",
"}",
"\n\n",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n\n",
"return",
"t",
".",
"authAndRetry",
"(",
"authService",
",",
"req",
")",
"\n",
"}"
] | // RoundTrip defines the round tripper for token transport. | [
"RoundTrip",
"defines",
"the",
"round",
"tripper",
"for",
"token",
"transport",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/tokentransport.go#L25-L44 | train |
genuinetools/reg | registry/tokentransport.go | Token | func (r *Registry) Token(ctx context.Context, url string) (string, error) {
r.Logf("registry.token url=%s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
client := http.DefaultClient
if r.Opt.Insecure {
client = &http.Client{
Timeout: r.Opt.Timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden && gcrMatcher.MatchString(url) {
// GCR is not sending HTTP 401 on missing credentials but a HTTP 403 without
// any further information about why the request failed. Sending the credentials
// from the Docker config fixes this.
return "", ErrBasicAuth
}
a, err := isTokenDemand(resp)
if err != nil {
return "", err
}
if a == nil {
r.Logf("registry.token authService=nil")
return "", nil
}
authReq, err := a.Request(r.Username, r.Password)
if err != nil {
return "", err
}
resp, err = http.DefaultClient.Do(authReq.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("getting token failed with StatusCode != StatusOK but %d", resp.StatusCode)
}
var authToken authToken
if err := json.NewDecoder(resp.Body).Decode(&authToken); err != nil {
return "", err
}
return authToken.String()
} | go | func (r *Registry) Token(ctx context.Context, url string) (string, error) {
r.Logf("registry.token url=%s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
client := http.DefaultClient
if r.Opt.Insecure {
client = &http.Client{
Timeout: r.Opt.Timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden && gcrMatcher.MatchString(url) {
// GCR is not sending HTTP 401 on missing credentials but a HTTP 403 without
// any further information about why the request failed. Sending the credentials
// from the Docker config fixes this.
return "", ErrBasicAuth
}
a, err := isTokenDemand(resp)
if err != nil {
return "", err
}
if a == nil {
r.Logf("registry.token authService=nil")
return "", nil
}
authReq, err := a.Request(r.Username, r.Password)
if err != nil {
return "", err
}
resp, err = http.DefaultClient.Do(authReq.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("getting token failed with StatusCode != StatusOK but %d", resp.StatusCode)
}
var authToken authToken
if err := json.NewDecoder(resp.Body).Decode(&authToken); err != nil {
return "", err
}
return authToken.String()
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"Token",
"(",
"ctx",
"context",
".",
"Context",
",",
"url",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
")",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"url",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"client",
":=",
"http",
".",
"DefaultClient",
"\n",
"if",
"r",
".",
"Opt",
".",
"Insecure",
"{",
"client",
"=",
"&",
"http",
".",
"Client",
"{",
"Timeout",
":",
"r",
".",
"Opt",
".",
"Timeout",
",",
"Transport",
":",
"&",
"http",
".",
"Transport",
"{",
"TLSClientConfig",
":",
"&",
"tls",
".",
"Config",
"{",
"InsecureSkipVerify",
":",
"true",
",",
"}",
",",
"}",
",",
"}",
"\n",
"}",
"\n\n",
"resp",
",",
"err",
":=",
"client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n\n",
"if",
"resp",
".",
"StatusCode",
"==",
"http",
".",
"StatusForbidden",
"&&",
"gcrMatcher",
".",
"MatchString",
"(",
"url",
")",
"{",
"// GCR is not sending HTTP 401 on missing credentials but a HTTP 403 without",
"// any further information about why the request failed. Sending the credentials",
"// from the Docker config fixes this.",
"return",
"\"",
"\"",
",",
"ErrBasicAuth",
"\n",
"}",
"\n\n",
"a",
",",
"err",
":=",
"isTokenDemand",
"(",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"a",
"==",
"nil",
"{",
"r",
".",
"Logf",
"(",
"\"",
"\"",
")",
"\n",
"return",
"\"",
"\"",
",",
"nil",
"\n",
"}",
"\n\n",
"authReq",
",",
"err",
":=",
"a",
".",
"Request",
"(",
"r",
".",
"Username",
",",
"r",
".",
"Password",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"resp",
",",
"err",
"=",
"http",
".",
"DefaultClient",
".",
"Do",
"(",
"authReq",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n\n",
"if",
"resp",
".",
"StatusCode",
"!=",
"http",
".",
"StatusOK",
"{",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"StatusCode",
")",
"\n",
"}",
"\n\n",
"var",
"authToken",
"authToken",
"\n",
"if",
"err",
":=",
"json",
".",
"NewDecoder",
"(",
"resp",
".",
"Body",
")",
".",
"Decode",
"(",
"&",
"authToken",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"authToken",
".",
"String",
"(",
")",
"\n",
"}"
] | // Token returns the required token for the specific resource url. If the registry requires basic authentication, this
// function returns ErrBasicAuth. | [
"Token",
"returns",
"the",
"required",
"token",
"for",
"the",
"specific",
"resource",
"url",
".",
"If",
"the",
"registry",
"requires",
"basic",
"authentication",
"this",
"function",
"returns",
"ErrBasicAuth",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/tokentransport.go#L144-L207 | train |
genuinetools/reg | registry/tokentransport.go | Headers | func (r *Registry) Headers(ctx context.Context, uri string) (map[string]string, error) {
// Get the token.
token, err := r.Token(ctx, uri)
if err != nil {
if err == ErrBasicAuth {
// If we couldn't get a token because the server requires basic auth, just return basic auth headers.
return map[string]string{
"Authorization": fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(r.Username+":"+r.Password))),
}, nil
}
}
if len(token) < 1 {
r.Logf("got empty token for %s", uri)
return map[string]string{}, nil
}
return map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", token),
}, nil
} | go | func (r *Registry) Headers(ctx context.Context, uri string) (map[string]string, error) {
// Get the token.
token, err := r.Token(ctx, uri)
if err != nil {
if err == ErrBasicAuth {
// If we couldn't get a token because the server requires basic auth, just return basic auth headers.
return map[string]string{
"Authorization": fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(r.Username+":"+r.Password))),
}, nil
}
}
if len(token) < 1 {
r.Logf("got empty token for %s", uri)
return map[string]string{}, nil
}
return map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", token),
}, nil
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"Headers",
"(",
"ctx",
"context",
".",
"Context",
",",
"uri",
"string",
")",
"(",
"map",
"[",
"string",
"]",
"string",
",",
"error",
")",
"{",
"// Get the token.",
"token",
",",
"err",
":=",
"r",
".",
"Token",
"(",
"ctx",
",",
"uri",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"err",
"==",
"ErrBasicAuth",
"{",
"// If we couldn't get a token because the server requires basic auth, just return basic auth headers.",
"return",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"base64",
".",
"StdEncoding",
".",
"EncodeToString",
"(",
"[",
"]",
"byte",
"(",
"r",
".",
"Username",
"+",
"\"",
"\"",
"+",
"r",
".",
"Password",
")",
")",
")",
",",
"}",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"token",
")",
"<",
"1",
"{",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"uri",
")",
"\n",
"return",
"map",
"[",
"string",
"]",
"string",
"{",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"token",
")",
",",
"}",
",",
"nil",
"\n",
"}"
] | // Headers returns the authorization headers for a specific uri. | [
"Headers",
"returns",
"the",
"authorization",
"headers",
"for",
"a",
"specific",
"uri",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/tokentransport.go#L210-L230 | train |
genuinetools/reg | registry/layer.go | DownloadLayer | func (r *Registry) DownloadLayer(ctx context.Context, repository string, digest digest.Digest) (io.ReadCloser, error) {
url := r.url("/v2/%s/blobs/%s", repository, digest)
r.Logf("registry.layer.download url=%s repository=%s digest=%s", url, repository, digest)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := r.Client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
return resp.Body, nil
} | go | func (r *Registry) DownloadLayer(ctx context.Context, repository string, digest digest.Digest) (io.ReadCloser, error) {
url := r.url("/v2/%s/blobs/%s", repository, digest)
r.Logf("registry.layer.download url=%s repository=%s digest=%s", url, repository, digest)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := r.Client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
return resp.Body, nil
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"DownloadLayer",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
"string",
",",
"digest",
"digest",
".",
"Digest",
")",
"(",
"io",
".",
"ReadCloser",
",",
"error",
")",
"{",
"url",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"repository",
",",
"digest",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
",",
"repository",
",",
"digest",
")",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"url",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"resp",
",",
"err",
":=",
"r",
".",
"Client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"resp",
".",
"Body",
",",
"nil",
"\n",
"}"
] | // DownloadLayer downloads a specific layer by digest for a repository. | [
"DownloadLayer",
"downloads",
"a",
"specific",
"layer",
"by",
"digest",
"for",
"a",
"repository",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/layer.go#L16-L30 | train |
genuinetools/reg | registry/layer.go | UploadLayer | func (r *Registry) UploadLayer(ctx context.Context, repository string, digest reference.Reference, content io.Reader) error {
uploadURL, token, err := r.initiateUpload(ctx, repository)
if err != nil {
return err
}
q := uploadURL.Query()
q.Set("digest", digest.String())
uploadURL.RawQuery = q.Encode()
r.Logf("registry.layer.upload url=%s repository=%s digest=%s", uploadURL, repository, digest)
upload, err := http.NewRequest("PUT", uploadURL.String(), content)
if err != nil {
return err
}
upload.Header.Set("Content-Type", "application/octet-stream")
upload.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
_, err = r.Client.Do(upload.WithContext(ctx))
return err
} | go | func (r *Registry) UploadLayer(ctx context.Context, repository string, digest reference.Reference, content io.Reader) error {
uploadURL, token, err := r.initiateUpload(ctx, repository)
if err != nil {
return err
}
q := uploadURL.Query()
q.Set("digest", digest.String())
uploadURL.RawQuery = q.Encode()
r.Logf("registry.layer.upload url=%s repository=%s digest=%s", uploadURL, repository, digest)
upload, err := http.NewRequest("PUT", uploadURL.String(), content)
if err != nil {
return err
}
upload.Header.Set("Content-Type", "application/octet-stream")
upload.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
_, err = r.Client.Do(upload.WithContext(ctx))
return err
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"UploadLayer",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
"string",
",",
"digest",
"reference",
".",
"Reference",
",",
"content",
"io",
".",
"Reader",
")",
"error",
"{",
"uploadURL",
",",
"token",
",",
"err",
":=",
"r",
".",
"initiateUpload",
"(",
"ctx",
",",
"repository",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"q",
":=",
"uploadURL",
".",
"Query",
"(",
")",
"\n",
"q",
".",
"Set",
"(",
"\"",
"\"",
",",
"digest",
".",
"String",
"(",
")",
")",
"\n",
"uploadURL",
".",
"RawQuery",
"=",
"q",
".",
"Encode",
"(",
")",
"\n\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"uploadURL",
",",
"repository",
",",
"digest",
")",
"\n\n",
"upload",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"uploadURL",
".",
"String",
"(",
")",
",",
"content",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"upload",
".",
"Header",
".",
"Set",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"upload",
".",
"Header",
".",
"Set",
"(",
"\"",
"\"",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"token",
")",
")",
"\n\n",
"_",
",",
"err",
"=",
"r",
".",
"Client",
".",
"Do",
"(",
"upload",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // UploadLayer uploads a specific layer by digest for a repository. | [
"UploadLayer",
"uploads",
"a",
"specific",
"layer",
"by",
"digest",
"for",
"a",
"repository",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/layer.go#L33-L53 | train |
genuinetools/reg | registry/layer.go | HasLayer | func (r *Registry) HasLayer(ctx context.Context, repository string, digest digest.Digest) (bool, error) {
checkURL := r.url("/v2/%s/blobs/%s", repository, digest)
r.Logf("registry.layer.check url=%s repository=%s digest=%s", checkURL, repository, digest)
req, err := http.NewRequest("HEAD", checkURL, nil)
if err != nil {
return false, err
}
resp, err := r.Client.Do(req.WithContext(ctx))
if err == nil {
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK, nil
}
urlErr, ok := err.(*url.Error)
if !ok {
return false, err
}
httpErr, ok := urlErr.Err.(*httpStatusError)
if !ok {
return false, err
}
if httpErr.Response.StatusCode == http.StatusNotFound {
return false, nil
}
return false, err
} | go | func (r *Registry) HasLayer(ctx context.Context, repository string, digest digest.Digest) (bool, error) {
checkURL := r.url("/v2/%s/blobs/%s", repository, digest)
r.Logf("registry.layer.check url=%s repository=%s digest=%s", checkURL, repository, digest)
req, err := http.NewRequest("HEAD", checkURL, nil)
if err != nil {
return false, err
}
resp, err := r.Client.Do(req.WithContext(ctx))
if err == nil {
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK, nil
}
urlErr, ok := err.(*url.Error)
if !ok {
return false, err
}
httpErr, ok := urlErr.Err.(*httpStatusError)
if !ok {
return false, err
}
if httpErr.Response.StatusCode == http.StatusNotFound {
return false, nil
}
return false, err
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"HasLayer",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
"string",
",",
"digest",
"digest",
".",
"Digest",
")",
"(",
"bool",
",",
"error",
")",
"{",
"checkURL",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"repository",
",",
"digest",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"checkURL",
",",
"repository",
",",
"digest",
")",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"checkURL",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"false",
",",
"err",
"\n",
"}",
"\n",
"resp",
",",
"err",
":=",
"r",
".",
"Client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"return",
"resp",
".",
"StatusCode",
"==",
"http",
".",
"StatusOK",
",",
"nil",
"\n",
"}",
"\n\n",
"urlErr",
",",
"ok",
":=",
"err",
".",
"(",
"*",
"url",
".",
"Error",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"false",
",",
"err",
"\n",
"}",
"\n",
"httpErr",
",",
"ok",
":=",
"urlErr",
".",
"Err",
".",
"(",
"*",
"httpStatusError",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"false",
",",
"err",
"\n",
"}",
"\n",
"if",
"httpErr",
".",
"Response",
".",
"StatusCode",
"==",
"http",
".",
"StatusNotFound",
"{",
"return",
"false",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"false",
",",
"err",
"\n",
"}"
] | // HasLayer returns if the registry contains the specific digest for a repository. | [
"HasLayer",
"returns",
"if",
"the",
"registry",
"contains",
"the",
"specific",
"digest",
"for",
"a",
"repository",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/layer.go#L56-L83 | train |
genuinetools/reg | repoutils/repoutils.go | GetAuthConfig | func GetAuthConfig(username, password, registry string) (types.AuthConfig, error) {
if username != "" && password != "" && registry != "" {
return types.AuthConfig{
Username: username,
Password: password,
ServerAddress: registry,
}, nil
}
dcfg, err := config.Load(config.Dir())
if err != nil {
return types.AuthConfig{}, fmt.Errorf("loading config file failed: %v", err)
}
// return error early if there are no auths saved
if !dcfg.ContainsAuth() {
// If we were passed a registry, just use that.
if registry != "" {
return setDefaultRegistry(types.AuthConfig{
ServerAddress: registry,
}), nil
}
// Otherwise, just use an empty auth config.
return types.AuthConfig{}, nil
}
authConfigs, err := dcfg.GetAllCredentials()
if err != nil {
return types.AuthConfig{}, fmt.Errorf("getting credentials failed: %v", err)
}
// if they passed a specific registry, return those creds _if_ they exist
if registry != "" {
// try with the user input
if creds, ok := authConfigs[registry]; ok {
fixAuthConfig(&creds, registry)
return creds, nil
}
// remove https:// from user input and try again
if strings.HasPrefix(registry, "https://") {
registryCleaned := strings.TrimPrefix(registry, "https://")
if creds, ok := authConfigs[registryCleaned]; ok {
fixAuthConfig(&creds, registryCleaned)
return creds, nil
}
}
// remove http:// from user input and try again
if strings.HasPrefix(registry, "http://") {
registryCleaned := strings.TrimPrefix(registry, "http://")
if creds, ok := authConfigs[registryCleaned]; ok {
fixAuthConfig(&creds, registryCleaned)
return creds, nil
}
}
// add https:// to user input and try again
// see https://github.com/genuinetools/reg/issues/32
if !strings.HasPrefix(registry, "https://") && !strings.HasPrefix(registry, "http://") {
registryCleaned := "https://" + registry
if creds, ok := authConfigs[registryCleaned]; ok {
fixAuthConfig(&creds, registryCleaned)
return creds, nil
}
}
logrus.Debugf("Using registry %q with no authentication", registry)
// Otherwise just use the registry with no auth.
return setDefaultRegistry(types.AuthConfig{
ServerAddress: registry,
}), nil
}
// Just set the auth config as the first registryURL, username and password
// found in the auth config.
for _, creds := range authConfigs {
fmt.Printf("No registry passed. Using registry %q\n", creds.ServerAddress)
return creds, nil
}
// Don't use any authentication.
// We should never get here.
fmt.Println("Not using any authentication")
return types.AuthConfig{}, nil
} | go | func GetAuthConfig(username, password, registry string) (types.AuthConfig, error) {
if username != "" && password != "" && registry != "" {
return types.AuthConfig{
Username: username,
Password: password,
ServerAddress: registry,
}, nil
}
dcfg, err := config.Load(config.Dir())
if err != nil {
return types.AuthConfig{}, fmt.Errorf("loading config file failed: %v", err)
}
// return error early if there are no auths saved
if !dcfg.ContainsAuth() {
// If we were passed a registry, just use that.
if registry != "" {
return setDefaultRegistry(types.AuthConfig{
ServerAddress: registry,
}), nil
}
// Otherwise, just use an empty auth config.
return types.AuthConfig{}, nil
}
authConfigs, err := dcfg.GetAllCredentials()
if err != nil {
return types.AuthConfig{}, fmt.Errorf("getting credentials failed: %v", err)
}
// if they passed a specific registry, return those creds _if_ they exist
if registry != "" {
// try with the user input
if creds, ok := authConfigs[registry]; ok {
fixAuthConfig(&creds, registry)
return creds, nil
}
// remove https:// from user input and try again
if strings.HasPrefix(registry, "https://") {
registryCleaned := strings.TrimPrefix(registry, "https://")
if creds, ok := authConfigs[registryCleaned]; ok {
fixAuthConfig(&creds, registryCleaned)
return creds, nil
}
}
// remove http:// from user input and try again
if strings.HasPrefix(registry, "http://") {
registryCleaned := strings.TrimPrefix(registry, "http://")
if creds, ok := authConfigs[registryCleaned]; ok {
fixAuthConfig(&creds, registryCleaned)
return creds, nil
}
}
// add https:// to user input and try again
// see https://github.com/genuinetools/reg/issues/32
if !strings.HasPrefix(registry, "https://") && !strings.HasPrefix(registry, "http://") {
registryCleaned := "https://" + registry
if creds, ok := authConfigs[registryCleaned]; ok {
fixAuthConfig(&creds, registryCleaned)
return creds, nil
}
}
logrus.Debugf("Using registry %q with no authentication", registry)
// Otherwise just use the registry with no auth.
return setDefaultRegistry(types.AuthConfig{
ServerAddress: registry,
}), nil
}
// Just set the auth config as the first registryURL, username and password
// found in the auth config.
for _, creds := range authConfigs {
fmt.Printf("No registry passed. Using registry %q\n", creds.ServerAddress)
return creds, nil
}
// Don't use any authentication.
// We should never get here.
fmt.Println("Not using any authentication")
return types.AuthConfig{}, nil
} | [
"func",
"GetAuthConfig",
"(",
"username",
",",
"password",
",",
"registry",
"string",
")",
"(",
"types",
".",
"AuthConfig",
",",
"error",
")",
"{",
"if",
"username",
"!=",
"\"",
"\"",
"&&",
"password",
"!=",
"\"",
"\"",
"&&",
"registry",
"!=",
"\"",
"\"",
"{",
"return",
"types",
".",
"AuthConfig",
"{",
"Username",
":",
"username",
",",
"Password",
":",
"password",
",",
"ServerAddress",
":",
"registry",
",",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"dcfg",
",",
"err",
":=",
"config",
".",
"Load",
"(",
"config",
".",
"Dir",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"types",
".",
"AuthConfig",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"// return error early if there are no auths saved",
"if",
"!",
"dcfg",
".",
"ContainsAuth",
"(",
")",
"{",
"// If we were passed a registry, just use that.",
"if",
"registry",
"!=",
"\"",
"\"",
"{",
"return",
"setDefaultRegistry",
"(",
"types",
".",
"AuthConfig",
"{",
"ServerAddress",
":",
"registry",
",",
"}",
")",
",",
"nil",
"\n",
"}",
"\n\n",
"// Otherwise, just use an empty auth config.",
"return",
"types",
".",
"AuthConfig",
"{",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"authConfigs",
",",
"err",
":=",
"dcfg",
".",
"GetAllCredentials",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"types",
".",
"AuthConfig",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"// if they passed a specific registry, return those creds _if_ they exist",
"if",
"registry",
"!=",
"\"",
"\"",
"{",
"// try with the user input",
"if",
"creds",
",",
"ok",
":=",
"authConfigs",
"[",
"registry",
"]",
";",
"ok",
"{",
"fixAuthConfig",
"(",
"&",
"creds",
",",
"registry",
")",
"\n",
"return",
"creds",
",",
"nil",
"\n",
"}",
"\n\n",
"// remove https:// from user input and try again",
"if",
"strings",
".",
"HasPrefix",
"(",
"registry",
",",
"\"",
"\"",
")",
"{",
"registryCleaned",
":=",
"strings",
".",
"TrimPrefix",
"(",
"registry",
",",
"\"",
"\"",
")",
"\n",
"if",
"creds",
",",
"ok",
":=",
"authConfigs",
"[",
"registryCleaned",
"]",
";",
"ok",
"{",
"fixAuthConfig",
"(",
"&",
"creds",
",",
"registryCleaned",
")",
"\n",
"return",
"creds",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"// remove http:// from user input and try again",
"if",
"strings",
".",
"HasPrefix",
"(",
"registry",
",",
"\"",
"\"",
")",
"{",
"registryCleaned",
":=",
"strings",
".",
"TrimPrefix",
"(",
"registry",
",",
"\"",
"\"",
")",
"\n",
"if",
"creds",
",",
"ok",
":=",
"authConfigs",
"[",
"registryCleaned",
"]",
";",
"ok",
"{",
"fixAuthConfig",
"(",
"&",
"creds",
",",
"registryCleaned",
")",
"\n",
"return",
"creds",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"// add https:// to user input and try again",
"// see https://github.com/genuinetools/reg/issues/32",
"if",
"!",
"strings",
".",
"HasPrefix",
"(",
"registry",
",",
"\"",
"\"",
")",
"&&",
"!",
"strings",
".",
"HasPrefix",
"(",
"registry",
",",
"\"",
"\"",
")",
"{",
"registryCleaned",
":=",
"\"",
"\"",
"+",
"registry",
"\n",
"if",
"creds",
",",
"ok",
":=",
"authConfigs",
"[",
"registryCleaned",
"]",
";",
"ok",
"{",
"fixAuthConfig",
"(",
"&",
"creds",
",",
"registryCleaned",
")",
"\n",
"return",
"creds",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"logrus",
".",
"Debugf",
"(",
"\"",
"\"",
",",
"registry",
")",
"\n\n",
"// Otherwise just use the registry with no auth.",
"return",
"setDefaultRegistry",
"(",
"types",
".",
"AuthConfig",
"{",
"ServerAddress",
":",
"registry",
",",
"}",
")",
",",
"nil",
"\n",
"}",
"\n\n",
"// Just set the auth config as the first registryURL, username and password",
"// found in the auth config.",
"for",
"_",
",",
"creds",
":=",
"range",
"authConfigs",
"{",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"creds",
".",
"ServerAddress",
")",
"\n",
"return",
"creds",
",",
"nil",
"\n",
"}",
"\n\n",
"// Don't use any authentication.",
"// We should never get here.",
"fmt",
".",
"Println",
"(",
"\"",
"\"",
")",
"\n",
"return",
"types",
".",
"AuthConfig",
"{",
"}",
",",
"nil",
"\n",
"}"
] | // GetAuthConfig returns the docker registry AuthConfig.
// Optionally takes in the authentication values, otherwise pulls them from the
// docker config file. | [
"GetAuthConfig",
"returns",
"the",
"docker",
"registry",
"AuthConfig",
".",
"Optionally",
"takes",
"in",
"the",
"authentication",
"values",
"otherwise",
"pulls",
"them",
"from",
"the",
"docker",
"config",
"file",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/repoutils/repoutils.go#L23-L110 | train |
genuinetools/reg | repoutils/repoutils.go | GetRepoAndRef | func GetRepoAndRef(image string) (repo, ref string, err error) {
if image == "" {
return "", "", reference.ErrNameEmpty
}
image = addLatestTagSuffix(image)
var parts []string
if strings.Contains(image, "@") {
parts = strings.Split(image, "@")
} else if strings.Contains(image, ":") {
parts = strings.Split(image, ":")
}
repo = parts[0]
if len(parts) > 1 {
ref = parts[1]
}
return
} | go | func GetRepoAndRef(image string) (repo, ref string, err error) {
if image == "" {
return "", "", reference.ErrNameEmpty
}
image = addLatestTagSuffix(image)
var parts []string
if strings.Contains(image, "@") {
parts = strings.Split(image, "@")
} else if strings.Contains(image, ":") {
parts = strings.Split(image, ":")
}
repo = parts[0]
if len(parts) > 1 {
ref = parts[1]
}
return
} | [
"func",
"GetRepoAndRef",
"(",
"image",
"string",
")",
"(",
"repo",
",",
"ref",
"string",
",",
"err",
"error",
")",
"{",
"if",
"image",
"==",
"\"",
"\"",
"{",
"return",
"\"",
"\"",
",",
"\"",
"\"",
",",
"reference",
".",
"ErrNameEmpty",
"\n",
"}",
"\n\n",
"image",
"=",
"addLatestTagSuffix",
"(",
"image",
")",
"\n\n",
"var",
"parts",
"[",
"]",
"string",
"\n",
"if",
"strings",
".",
"Contains",
"(",
"image",
",",
"\"",
"\"",
")",
"{",
"parts",
"=",
"strings",
".",
"Split",
"(",
"image",
",",
"\"",
"\"",
")",
"\n",
"}",
"else",
"if",
"strings",
".",
"Contains",
"(",
"image",
",",
"\"",
"\"",
")",
"{",
"parts",
"=",
"strings",
".",
"Split",
"(",
"image",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"repo",
"=",
"parts",
"[",
"0",
"]",
"\n",
"if",
"len",
"(",
"parts",
")",
">",
"1",
"{",
"ref",
"=",
"parts",
"[",
"1",
"]",
"\n",
"}",
"\n\n",
"return",
"\n",
"}"
] | // GetRepoAndRef parses the repo name and reference. | [
"GetRepoAndRef",
"parses",
"the",
"repo",
"name",
"and",
"reference",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/repoutils/repoutils.go#L123-L143 | train |
genuinetools/reg | registry/digest.go | Digest | func (r *Registry) Digest(ctx context.Context, image Image) (digest.Digest, error) {
if len(image.Digest) > 1 {
// return early if we already have an image digest.
return image.Digest, nil
}
url := r.url("/v2/%s/manifests/%s", image.Path, image.Tag)
r.Logf("registry.manifests.get url=%s repository=%s ref=%s",
url, image.Path, image.Tag)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Add("Accept", schema2.MediaTypeManifest)
resp, err := r.Client.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
return "", fmt.Errorf("got status code: %d", resp.StatusCode)
}
return digest.Parse(resp.Header.Get("Docker-Content-Digest"))
} | go | func (r *Registry) Digest(ctx context.Context, image Image) (digest.Digest, error) {
if len(image.Digest) > 1 {
// return early if we already have an image digest.
return image.Digest, nil
}
url := r.url("/v2/%s/manifests/%s", image.Path, image.Tag)
r.Logf("registry.manifests.get url=%s repository=%s ref=%s",
url, image.Path, image.Tag)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Add("Accept", schema2.MediaTypeManifest)
resp, err := r.Client.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
return "", fmt.Errorf("got status code: %d", resp.StatusCode)
}
return digest.Parse(resp.Header.Get("Docker-Content-Digest"))
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"Digest",
"(",
"ctx",
"context",
".",
"Context",
",",
"image",
"Image",
")",
"(",
"digest",
".",
"Digest",
",",
"error",
")",
"{",
"if",
"len",
"(",
"image",
".",
"Digest",
")",
">",
"1",
"{",
"// return early if we already have an image digest.",
"return",
"image",
".",
"Digest",
",",
"nil",
"\n",
"}",
"\n\n",
"url",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"image",
".",
"Path",
",",
"image",
".",
"Tag",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
",",
"image",
".",
"Path",
",",
"image",
".",
"Tag",
")",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"url",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"req",
".",
"Header",
".",
"Add",
"(",
"\"",
"\"",
",",
"schema2",
".",
"MediaTypeManifest",
")",
"\n",
"resp",
",",
"err",
":=",
"r",
".",
"Client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n\n",
"if",
"resp",
".",
"StatusCode",
"!=",
"http",
".",
"StatusOK",
"&&",
"resp",
".",
"StatusCode",
"!=",
"http",
".",
"StatusNotFound",
"{",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"StatusCode",
")",
"\n",
"}",
"\n\n",
"return",
"digest",
".",
"Parse",
"(",
"resp",
".",
"Header",
".",
"Get",
"(",
"\"",
"\"",
")",
")",
"\n",
"}"
] | // Digest returns the digest for an image. | [
"Digest",
"returns",
"the",
"digest",
"for",
"an",
"image",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/digest.go#L13-L40 | train |
genuinetools/reg | clair/layerutil.go | NewClairLayer | func (c *Clair) NewClairLayer(ctx context.Context, r *registry.Registry, image string, fsLayers map[int]distribution.Descriptor, index int) (*Layer, error) {
var parentName string
if index < len(fsLayers)-1 {
parentName = fsLayers[index+1].Digest.String()
}
// Form the path.
p := strings.Join([]string{r.URL, "v2", image, "blobs", fsLayers[index].Digest.String()}, "/")
// Get the headers.
h, err := r.Headers(ctx, p)
if err != nil {
return nil, err
}
return &Layer{
Name: fsLayers[index].Digest.String(),
Path: p,
ParentName: parentName,
Format: "Docker",
Headers: h,
}, nil
} | go | func (c *Clair) NewClairLayer(ctx context.Context, r *registry.Registry, image string, fsLayers map[int]distribution.Descriptor, index int) (*Layer, error) {
var parentName string
if index < len(fsLayers)-1 {
parentName = fsLayers[index+1].Digest.String()
}
// Form the path.
p := strings.Join([]string{r.URL, "v2", image, "blobs", fsLayers[index].Digest.String()}, "/")
// Get the headers.
h, err := r.Headers(ctx, p)
if err != nil {
return nil, err
}
return &Layer{
Name: fsLayers[index].Digest.String(),
Path: p,
ParentName: parentName,
Format: "Docker",
Headers: h,
}, nil
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"NewClairLayer",
"(",
"ctx",
"context",
".",
"Context",
",",
"r",
"*",
"registry",
".",
"Registry",
",",
"image",
"string",
",",
"fsLayers",
"map",
"[",
"int",
"]",
"distribution",
".",
"Descriptor",
",",
"index",
"int",
")",
"(",
"*",
"Layer",
",",
"error",
")",
"{",
"var",
"parentName",
"string",
"\n",
"if",
"index",
"<",
"len",
"(",
"fsLayers",
")",
"-",
"1",
"{",
"parentName",
"=",
"fsLayers",
"[",
"index",
"+",
"1",
"]",
".",
"Digest",
".",
"String",
"(",
")",
"\n",
"}",
"\n\n",
"// Form the path.",
"p",
":=",
"strings",
".",
"Join",
"(",
"[",
"]",
"string",
"{",
"r",
".",
"URL",
",",
"\"",
"\"",
",",
"image",
",",
"\"",
"\"",
",",
"fsLayers",
"[",
"index",
"]",
".",
"Digest",
".",
"String",
"(",
")",
"}",
",",
"\"",
"\"",
")",
"\n\n",
"// Get the headers.",
"h",
",",
"err",
":=",
"r",
".",
"Headers",
"(",
"ctx",
",",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"Layer",
"{",
"Name",
":",
"fsLayers",
"[",
"index",
"]",
".",
"Digest",
".",
"String",
"(",
")",
",",
"Path",
":",
"p",
",",
"ParentName",
":",
"parentName",
",",
"Format",
":",
"\"",
"\"",
",",
"Headers",
":",
"h",
",",
"}",
",",
"nil",
"\n",
"}"
] | // NewClairLayer will form a layer struct required for a clair scan. | [
"NewClairLayer",
"will",
"form",
"a",
"layer",
"struct",
"required",
"for",
"a",
"clair",
"scan",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/layerutil.go#L14-L36 | train |
genuinetools/reg | clair/layerutil.go | NewClairV3Layer | func (c *Clair) NewClairV3Layer(ctx context.Context, r *registry.Registry, image string, fsLayer distribution.Descriptor) (*clairpb.PostAncestryRequest_PostLayer, error) {
// Form the path.
p := strings.Join([]string{r.URL, "v2", image, "blobs", fsLayer.Digest.String()}, "/")
// Get the headers.
h, err := r.Headers(ctx, p)
if err != nil {
return nil, err
}
return &clairpb.PostAncestryRequest_PostLayer{
Hash: fsLayer.Digest.String(),
Path: p,
Headers: h,
}, nil
} | go | func (c *Clair) NewClairV3Layer(ctx context.Context, r *registry.Registry, image string, fsLayer distribution.Descriptor) (*clairpb.PostAncestryRequest_PostLayer, error) {
// Form the path.
p := strings.Join([]string{r.URL, "v2", image, "blobs", fsLayer.Digest.String()}, "/")
// Get the headers.
h, err := r.Headers(ctx, p)
if err != nil {
return nil, err
}
return &clairpb.PostAncestryRequest_PostLayer{
Hash: fsLayer.Digest.String(),
Path: p,
Headers: h,
}, nil
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"NewClairV3Layer",
"(",
"ctx",
"context",
".",
"Context",
",",
"r",
"*",
"registry",
".",
"Registry",
",",
"image",
"string",
",",
"fsLayer",
"distribution",
".",
"Descriptor",
")",
"(",
"*",
"clairpb",
".",
"PostAncestryRequest_PostLayer",
",",
"error",
")",
"{",
"// Form the path.",
"p",
":=",
"strings",
".",
"Join",
"(",
"[",
"]",
"string",
"{",
"r",
".",
"URL",
",",
"\"",
"\"",
",",
"image",
",",
"\"",
"\"",
",",
"fsLayer",
".",
"Digest",
".",
"String",
"(",
")",
"}",
",",
"\"",
"\"",
")",
"\n\n",
"// Get the headers.",
"h",
",",
"err",
":=",
"r",
".",
"Headers",
"(",
"ctx",
",",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"clairpb",
".",
"PostAncestryRequest_PostLayer",
"{",
"Hash",
":",
"fsLayer",
".",
"Digest",
".",
"String",
"(",
")",
",",
"Path",
":",
"p",
",",
"Headers",
":",
"h",
",",
"}",
",",
"nil",
"\n",
"}"
] | // NewClairV3Layer will form a layer struct required for a clair scan. | [
"NewClairV3Layer",
"will",
"form",
"a",
"layer",
"struct",
"required",
"for",
"a",
"clair",
"scan",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/layerutil.go#L39-L54 | train |
genuinetools/reg | registry/registry.go | New | func New(ctx context.Context, auth types.AuthConfig, opt Opt) (*Registry, error) {
transport := http.DefaultTransport
if opt.Insecure {
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
return newFromTransport(ctx, auth, transport, opt)
} | go | func New(ctx context.Context, auth types.AuthConfig, opt Opt) (*Registry, error) {
transport := http.DefaultTransport
if opt.Insecure {
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
return newFromTransport(ctx, auth, transport, opt)
} | [
"func",
"New",
"(",
"ctx",
"context",
".",
"Context",
",",
"auth",
"types",
".",
"AuthConfig",
",",
"opt",
"Opt",
")",
"(",
"*",
"Registry",
",",
"error",
")",
"{",
"transport",
":=",
"http",
".",
"DefaultTransport",
"\n\n",
"if",
"opt",
".",
"Insecure",
"{",
"transport",
"=",
"&",
"http",
".",
"Transport",
"{",
"TLSClientConfig",
":",
"&",
"tls",
".",
"Config",
"{",
"InsecureSkipVerify",
":",
"true",
",",
"}",
",",
"}",
"\n",
"}",
"\n\n",
"return",
"newFromTransport",
"(",
"ctx",
",",
"auth",
",",
"transport",
",",
"opt",
")",
"\n",
"}"
] | // New creates a new Registry struct with the given URL and credentials. | [
"New",
"creates",
"a",
"new",
"Registry",
"struct",
"with",
"the",
"given",
"URL",
"and",
"credentials",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/registry.go#L55-L67 | train |
genuinetools/reg | registry/registry.go | url | func (r *Registry) url(pathTemplate string, args ...interface{}) string {
pathSuffix := fmt.Sprintf(pathTemplate, args...)
url := fmt.Sprintf("%s%s", r.URL, pathSuffix)
return url
} | go | func (r *Registry) url(pathTemplate string, args ...interface{}) string {
pathSuffix := fmt.Sprintf(pathTemplate, args...)
url := fmt.Sprintf("%s%s", r.URL, pathSuffix)
return url
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"url",
"(",
"pathTemplate",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"string",
"{",
"pathSuffix",
":=",
"fmt",
".",
"Sprintf",
"(",
"pathTemplate",
",",
"args",
"...",
")",
"\n",
"url",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"r",
".",
"URL",
",",
"pathSuffix",
")",
"\n",
"return",
"url",
"\n",
"}"
] | // url returns a registry URL with the passed arguements concatenated. | [
"url",
"returns",
"a",
"registry",
"URL",
"with",
"the",
"passed",
"arguements",
"concatenated",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/registry.go#L140-L144 | train |
genuinetools/reg | clair/ancestry.go | GetAncestry | func (c *Clair) GetAncestry(ctx context.Context, name string) (*clairpb.GetAncestryResponse_Ancestry, error) {
c.Logf("clair.ancestry.get name=%s", name)
if c.grpcConn == nil {
return nil, ErrNilGRPCConn
}
client := clairpb.NewAncestryServiceClient(c.grpcConn)
resp, err := client.GetAncestry(ctx, &clairpb.GetAncestryRequest{
AncestryName: name,
})
if err != nil {
return nil, err
}
if resp == nil {
return nil, errors.New("ancestry response was nil")
}
if resp.GetStatus() != nil {
c.Logf("clair.ancestry.get ClairStatus=%#v", *resp.GetStatus())
}
return resp.GetAncestry(), nil
} | go | func (c *Clair) GetAncestry(ctx context.Context, name string) (*clairpb.GetAncestryResponse_Ancestry, error) {
c.Logf("clair.ancestry.get name=%s", name)
if c.grpcConn == nil {
return nil, ErrNilGRPCConn
}
client := clairpb.NewAncestryServiceClient(c.grpcConn)
resp, err := client.GetAncestry(ctx, &clairpb.GetAncestryRequest{
AncestryName: name,
})
if err != nil {
return nil, err
}
if resp == nil {
return nil, errors.New("ancestry response was nil")
}
if resp.GetStatus() != nil {
c.Logf("clair.ancestry.get ClairStatus=%#v", *resp.GetStatus())
}
return resp.GetAncestry(), nil
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"GetAncestry",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
")",
"(",
"*",
"clairpb",
".",
"GetAncestryResponse_Ancestry",
",",
"error",
")",
"{",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"name",
")",
"\n\n",
"if",
"c",
".",
"grpcConn",
"==",
"nil",
"{",
"return",
"nil",
",",
"ErrNilGRPCConn",
"\n",
"}",
"\n\n",
"client",
":=",
"clairpb",
".",
"NewAncestryServiceClient",
"(",
"c",
".",
"grpcConn",
")",
"\n\n",
"resp",
",",
"err",
":=",
"client",
".",
"GetAncestry",
"(",
"ctx",
",",
"&",
"clairpb",
".",
"GetAncestryRequest",
"{",
"AncestryName",
":",
"name",
",",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"resp",
"==",
"nil",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"resp",
".",
"GetStatus",
"(",
")",
"!=",
"nil",
"{",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"*",
"resp",
".",
"GetStatus",
"(",
")",
")",
"\n",
"}",
"\n\n",
"return",
"resp",
".",
"GetAncestry",
"(",
")",
",",
"nil",
"\n",
"}"
] | // GetAncestry displays an ancestry and all of its features and vulnerabilities. | [
"GetAncestry",
"displays",
"an",
"ancestry",
"and",
"all",
"of",
"its",
"features",
"and",
"vulnerabilities",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/ancestry.go#L16-L41 | train |
genuinetools/reg | clair/ancestry.go | PostAncestry | func (c *Clair) PostAncestry(ctx context.Context, name string, layers []*clairpb.PostAncestryRequest_PostLayer) error {
c.Logf("clair.ancestry.post name=%s", name)
if c.grpcConn == nil {
return ErrNilGRPCConn
}
client := clairpb.NewAncestryServiceClient(c.grpcConn)
resp, err := client.PostAncestry(ctx, &clairpb.PostAncestryRequest{
AncestryName: name,
Layers: layers,
Format: "Docker",
})
if err != nil {
return err
}
if resp == nil {
return errors.New("ancestry response was nil")
}
if resp.GetStatus() != nil {
c.Logf("clair.ancestry.post ClairStatus=%#v", *resp.GetStatus())
}
return nil
} | go | func (c *Clair) PostAncestry(ctx context.Context, name string, layers []*clairpb.PostAncestryRequest_PostLayer) error {
c.Logf("clair.ancestry.post name=%s", name)
if c.grpcConn == nil {
return ErrNilGRPCConn
}
client := clairpb.NewAncestryServiceClient(c.grpcConn)
resp, err := client.PostAncestry(ctx, &clairpb.PostAncestryRequest{
AncestryName: name,
Layers: layers,
Format: "Docker",
})
if err != nil {
return err
}
if resp == nil {
return errors.New("ancestry response was nil")
}
if resp.GetStatus() != nil {
c.Logf("clair.ancestry.post ClairStatus=%#v", *resp.GetStatus())
}
return nil
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"PostAncestry",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
",",
"layers",
"[",
"]",
"*",
"clairpb",
".",
"PostAncestryRequest_PostLayer",
")",
"error",
"{",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"name",
")",
"\n\n",
"if",
"c",
".",
"grpcConn",
"==",
"nil",
"{",
"return",
"ErrNilGRPCConn",
"\n",
"}",
"\n\n",
"client",
":=",
"clairpb",
".",
"NewAncestryServiceClient",
"(",
"c",
".",
"grpcConn",
")",
"\n\n",
"resp",
",",
"err",
":=",
"client",
".",
"PostAncestry",
"(",
"ctx",
",",
"&",
"clairpb",
".",
"PostAncestryRequest",
"{",
"AncestryName",
":",
"name",
",",
"Layers",
":",
"layers",
",",
"Format",
":",
"\"",
"\"",
",",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"resp",
"==",
"nil",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"resp",
".",
"GetStatus",
"(",
")",
"!=",
"nil",
"{",
"c",
".",
"Logf",
"(",
"\"",
"\"",
",",
"*",
"resp",
".",
"GetStatus",
"(",
")",
")",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // PostAncestry performs the analysis of all layers from the provided path. | [
"PostAncestry",
"performs",
"the",
"analysis",
"of",
"all",
"layers",
"from",
"the",
"provided",
"path",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/ancestry.go#L44-L71 | train |
genuinetools/reg | registry/manifest.go | ManifestList | func (r *Registry) ManifestList(ctx context.Context, repository, ref string) (manifestlist.ManifestList, error) {
uri := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifests uri=%s repository=%s ref=%s", uri, repository, ref)
var m manifestlist.ManifestList
if _, err := r.getJSON(ctx, uri, &m); err != nil {
r.Logf("registry.manifests response=%v", m)
return m, err
}
return m, nil
} | go | func (r *Registry) ManifestList(ctx context.Context, repository, ref string) (manifestlist.ManifestList, error) {
uri := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifests uri=%s repository=%s ref=%s", uri, repository, ref)
var m manifestlist.ManifestList
if _, err := r.getJSON(ctx, uri, &m); err != nil {
r.Logf("registry.manifests response=%v", m)
return m, err
}
return m, nil
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"ManifestList",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
",",
"ref",
"string",
")",
"(",
"manifestlist",
".",
"ManifestList",
",",
"error",
")",
"{",
"uri",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"repository",
",",
"ref",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"uri",
",",
"repository",
",",
"ref",
")",
"\n\n",
"var",
"m",
"manifestlist",
".",
"ManifestList",
"\n",
"if",
"_",
",",
"err",
":=",
"r",
".",
"getJSON",
"(",
"ctx",
",",
"uri",
",",
"&",
"m",
")",
";",
"err",
"!=",
"nil",
"{",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"m",
")",
"\n",
"return",
"m",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"m",
",",
"nil",
"\n",
"}"
] | // ManifestList gets the registry v2 manifest list. | [
"ManifestList",
"gets",
"the",
"registry",
"v2",
"manifest",
"list",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/manifest.go#L56-L67 | train |
genuinetools/reg | registry/manifest.go | ManifestV2 | func (r *Registry) ManifestV2(ctx context.Context, repository, ref string) (schema2.Manifest, error) {
uri := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifests uri=%s repository=%s ref=%s", uri, repository, ref)
var m schema2.Manifest
if _, err := r.getJSON(ctx, uri, &m); err != nil {
r.Logf("registry.manifests response=%v", m)
return m, err
}
if m.Versioned.SchemaVersion != 2 {
return m, ErrUnexpectedSchemaVersion
}
return m, nil
} | go | func (r *Registry) ManifestV2(ctx context.Context, repository, ref string) (schema2.Manifest, error) {
uri := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifests uri=%s repository=%s ref=%s", uri, repository, ref)
var m schema2.Manifest
if _, err := r.getJSON(ctx, uri, &m); err != nil {
r.Logf("registry.manifests response=%v", m)
return m, err
}
if m.Versioned.SchemaVersion != 2 {
return m, ErrUnexpectedSchemaVersion
}
return m, nil
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"ManifestV2",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
",",
"ref",
"string",
")",
"(",
"schema2",
".",
"Manifest",
",",
"error",
")",
"{",
"uri",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"repository",
",",
"ref",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"uri",
",",
"repository",
",",
"ref",
")",
"\n\n",
"var",
"m",
"schema2",
".",
"Manifest",
"\n",
"if",
"_",
",",
"err",
":=",
"r",
".",
"getJSON",
"(",
"ctx",
",",
"uri",
",",
"&",
"m",
")",
";",
"err",
"!=",
"nil",
"{",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"m",
")",
"\n",
"return",
"m",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"m",
".",
"Versioned",
".",
"SchemaVersion",
"!=",
"2",
"{",
"return",
"m",
",",
"ErrUnexpectedSchemaVersion",
"\n",
"}",
"\n\n",
"return",
"m",
",",
"nil",
"\n",
"}"
] | // ManifestV2 gets the registry v2 manifest. | [
"ManifestV2",
"gets",
"the",
"registry",
"v2",
"manifest",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/manifest.go#L70-L85 | train |
genuinetools/reg | registry/manifest.go | ManifestV1 | func (r *Registry) ManifestV1(ctx context.Context, repository, ref string) (schema1.SignedManifest, error) {
uri := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifests uri=%s repository=%s ref=%s", uri, repository, ref)
var m schema1.SignedManifest
if _, err := r.getJSON(ctx, uri, &m); err != nil {
r.Logf("registry.manifests response=%v", m)
return m, err
}
if m.Versioned.SchemaVersion != 1 {
return m, ErrUnexpectedSchemaVersion
}
return m, nil
} | go | func (r *Registry) ManifestV1(ctx context.Context, repository, ref string) (schema1.SignedManifest, error) {
uri := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifests uri=%s repository=%s ref=%s", uri, repository, ref)
var m schema1.SignedManifest
if _, err := r.getJSON(ctx, uri, &m); err != nil {
r.Logf("registry.manifests response=%v", m)
return m, err
}
if m.Versioned.SchemaVersion != 1 {
return m, ErrUnexpectedSchemaVersion
}
return m, nil
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"ManifestV1",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
",",
"ref",
"string",
")",
"(",
"schema1",
".",
"SignedManifest",
",",
"error",
")",
"{",
"uri",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"repository",
",",
"ref",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"uri",
",",
"repository",
",",
"ref",
")",
"\n\n",
"var",
"m",
"schema1",
".",
"SignedManifest",
"\n",
"if",
"_",
",",
"err",
":=",
"r",
".",
"getJSON",
"(",
"ctx",
",",
"uri",
",",
"&",
"m",
")",
";",
"err",
"!=",
"nil",
"{",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"m",
")",
"\n",
"return",
"m",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"m",
".",
"Versioned",
".",
"SchemaVersion",
"!=",
"1",
"{",
"return",
"m",
",",
"ErrUnexpectedSchemaVersion",
"\n",
"}",
"\n\n",
"return",
"m",
",",
"nil",
"\n",
"}"
] | // ManifestV1 gets the registry v1 manifest. | [
"ManifestV1",
"gets",
"the",
"registry",
"v1",
"manifest",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/manifest.go#L88-L103 | train |
genuinetools/reg | registry/manifest.go | PutManifest | func (r *Registry) PutManifest(ctx context.Context, repository, ref string, manifest distribution.Manifest) error {
url := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifest.put url=%s repository=%s reference=%s", url, repository, ref)
b, err := json.Marshal(manifest)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Set("Content-Type", schema2.MediaTypeManifest)
resp, err := r.Client.Do(req.WithContext(ctx))
if resp != nil {
defer resp.Body.Close()
}
return err
} | go | func (r *Registry) PutManifest(ctx context.Context, repository, ref string, manifest distribution.Manifest) error {
url := r.url("/v2/%s/manifests/%s", repository, ref)
r.Logf("registry.manifest.put url=%s repository=%s reference=%s", url, repository, ref)
b, err := json.Marshal(manifest)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Set("Content-Type", schema2.MediaTypeManifest)
resp, err := r.Client.Do(req.WithContext(ctx))
if resp != nil {
defer resp.Body.Close()
}
return err
} | [
"func",
"(",
"r",
"*",
"Registry",
")",
"PutManifest",
"(",
"ctx",
"context",
".",
"Context",
",",
"repository",
",",
"ref",
"string",
",",
"manifest",
"distribution",
".",
"Manifest",
")",
"error",
"{",
"url",
":=",
"r",
".",
"url",
"(",
"\"",
"\"",
",",
"repository",
",",
"ref",
")",
"\n",
"r",
".",
"Logf",
"(",
"\"",
"\"",
",",
"url",
",",
"repository",
",",
"ref",
")",
"\n\n",
"b",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"manifest",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"url",
",",
"bytes",
".",
"NewBuffer",
"(",
"b",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"req",
".",
"Header",
".",
"Set",
"(",
"\"",
"\"",
",",
"schema2",
".",
"MediaTypeManifest",
")",
"\n",
"resp",
",",
"err",
":=",
"r",
".",
"Client",
".",
"Do",
"(",
"req",
".",
"WithContext",
"(",
"ctx",
")",
")",
"\n",
"if",
"resp",
"!=",
"nil",
"{",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"}",
"\n",
"return",
"err",
"\n",
"}"
] | // PutManifest calls a PUT for the specific manifest for an image. | [
"PutManifest",
"calls",
"a",
"PUT",
"for",
"the",
"specific",
"manifest",
"for",
"an",
"image",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/manifest.go#L106-L126 | train |
genuinetools/reg | clair/clair.go | New | func New(url string, opt Opt) (*Clair, error) {
transport := http.DefaultTransport
grpcOpt := []grpc.DialOption{}
if opt.Insecure {
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
grpcOpt = append(grpcOpt, grpc.WithInsecure())
}
errorTransport := &ErrorTransport{
Transport: transport,
}
// set the logging
logf := Quiet
if opt.Debug {
logf = Log
}
conn, err := grpc.Dial(url, grpcOpt...)
if err != nil {
logf("grpc dial %s failed: %v", url, err)
}
registry := &Clair{
URL: url,
Client: &http.Client{
Timeout: opt.Timeout,
Transport: errorTransport,
},
Logf: logf,
grpcConn: conn,
}
return registry, nil
} | go | func New(url string, opt Opt) (*Clair, error) {
transport := http.DefaultTransport
grpcOpt := []grpc.DialOption{}
if opt.Insecure {
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
grpcOpt = append(grpcOpt, grpc.WithInsecure())
}
errorTransport := &ErrorTransport{
Transport: transport,
}
// set the logging
logf := Quiet
if opt.Debug {
logf = Log
}
conn, err := grpc.Dial(url, grpcOpt...)
if err != nil {
logf("grpc dial %s failed: %v", url, err)
}
registry := &Clair{
URL: url,
Client: &http.Client{
Timeout: opt.Timeout,
Transport: errorTransport,
},
Logf: logf,
grpcConn: conn,
}
return registry, nil
} | [
"func",
"New",
"(",
"url",
"string",
",",
"opt",
"Opt",
")",
"(",
"*",
"Clair",
",",
"error",
")",
"{",
"transport",
":=",
"http",
".",
"DefaultTransport",
"\n\n",
"grpcOpt",
":=",
"[",
"]",
"grpc",
".",
"DialOption",
"{",
"}",
"\n\n",
"if",
"opt",
".",
"Insecure",
"{",
"transport",
"=",
"&",
"http",
".",
"Transport",
"{",
"TLSClientConfig",
":",
"&",
"tls",
".",
"Config",
"{",
"InsecureSkipVerify",
":",
"true",
",",
"}",
",",
"}",
"\n\n",
"grpcOpt",
"=",
"append",
"(",
"grpcOpt",
",",
"grpc",
".",
"WithInsecure",
"(",
")",
")",
"\n",
"}",
"\n\n",
"errorTransport",
":=",
"&",
"ErrorTransport",
"{",
"Transport",
":",
"transport",
",",
"}",
"\n\n",
"// set the logging",
"logf",
":=",
"Quiet",
"\n",
"if",
"opt",
".",
"Debug",
"{",
"logf",
"=",
"Log",
"\n",
"}",
"\n\n",
"conn",
",",
"err",
":=",
"grpc",
".",
"Dial",
"(",
"url",
",",
"grpcOpt",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"logf",
"(",
"\"",
"\"",
",",
"url",
",",
"err",
")",
"\n",
"}",
"\n\n",
"registry",
":=",
"&",
"Clair",
"{",
"URL",
":",
"url",
",",
"Client",
":",
"&",
"http",
".",
"Client",
"{",
"Timeout",
":",
"opt",
".",
"Timeout",
",",
"Transport",
":",
"errorTransport",
",",
"}",
",",
"Logf",
":",
"logf",
",",
"grpcConn",
":",
"conn",
",",
"}",
"\n\n",
"return",
"registry",
",",
"nil",
"\n",
"}"
] | // New creates a new Clair struct with the given URL and credentials. | [
"New",
"creates",
"a",
"new",
"Clair",
"struct",
"with",
"the",
"given",
"URL",
"and",
"credentials",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/clair.go#L42-L83 | train |
genuinetools/reg | clair/clair.go | url | func (c *Clair) url(pathTemplate string, args ...interface{}) string {
pathSuffix := fmt.Sprintf(pathTemplate, args...)
url := fmt.Sprintf("%s%s", c.URL, pathSuffix)
return url
} | go | func (c *Clair) url(pathTemplate string, args ...interface{}) string {
pathSuffix := fmt.Sprintf(pathTemplate, args...)
url := fmt.Sprintf("%s%s", c.URL, pathSuffix)
return url
} | [
"func",
"(",
"c",
"*",
"Clair",
")",
"url",
"(",
"pathTemplate",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"string",
"{",
"pathSuffix",
":=",
"fmt",
".",
"Sprintf",
"(",
"pathTemplate",
",",
"args",
"...",
")",
"\n",
"url",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"c",
".",
"URL",
",",
"pathSuffix",
")",
"\n",
"return",
"url",
"\n",
"}"
] | // url returns a clair URL with the passed arguments concatenated. | [
"url",
"returns",
"a",
"clair",
"URL",
"with",
"the",
"passed",
"arguments",
"concatenated",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/clair/clair.go#L91-L95 | train |
genuinetools/reg | registry/basictransport.go | RoundTrip | func (t *BasicTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.String(), t.URL) && req.Header.Get("Authorization") == "" {
if t.Username != "" || t.Password != "" {
req.SetBasicAuth(t.Username, t.Password)
}
}
resp, err := t.Transport.RoundTrip(req)
return resp, err
} | go | func (t *BasicTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.String(), t.URL) && req.Header.Get("Authorization") == "" {
if t.Username != "" || t.Password != "" {
req.SetBasicAuth(t.Username, t.Password)
}
}
resp, err := t.Transport.RoundTrip(req)
return resp, err
} | [
"func",
"(",
"t",
"*",
"BasicTransport",
")",
"RoundTrip",
"(",
"req",
"*",
"http",
".",
"Request",
")",
"(",
"*",
"http",
".",
"Response",
",",
"error",
")",
"{",
"if",
"strings",
".",
"HasPrefix",
"(",
"req",
".",
"URL",
".",
"String",
"(",
")",
",",
"t",
".",
"URL",
")",
"&&",
"req",
".",
"Header",
".",
"Get",
"(",
"\"",
"\"",
")",
"==",
"\"",
"\"",
"{",
"if",
"t",
".",
"Username",
"!=",
"\"",
"\"",
"||",
"t",
".",
"Password",
"!=",
"\"",
"\"",
"{",
"req",
".",
"SetBasicAuth",
"(",
"t",
".",
"Username",
",",
"t",
".",
"Password",
")",
"\n",
"}",
"\n",
"}",
"\n",
"resp",
",",
"err",
":=",
"t",
".",
"Transport",
".",
"RoundTrip",
"(",
"req",
")",
"\n",
"return",
"resp",
",",
"err",
"\n",
"}"
] | // RoundTrip defines the round tripper for basic auth transport. | [
"RoundTrip",
"defines",
"the",
"round",
"tripper",
"for",
"basic",
"auth",
"transport",
"."
] | d959057b30da67d5f162790f9d5b5160686901fd | https://github.com/genuinetools/reg/blob/d959057b30da67d5f162790f9d5b5160686901fd/registry/basictransport.go#L17-L25 | train |
Subsets and Splits
SQL Console for semeru/code-text-go
Retrieves a limited set of code samples with their languages, with a specific case adjustment for 'Go' language.