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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
pelletier/go-toml | toml.go | GetPosition | func (t *Tree) GetPosition(key string) Position {
if key == "" {
return t.position
}
return t.GetPositionPath(strings.Split(key, "."))
} | go | func (t *Tree) GetPosition(key string) Position {
if key == "" {
return t.position
}
return t.GetPositionPath(strings.Split(key, "."))
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"GetPosition",
"(",
"key",
"string",
")",
"Position",
"{",
"if",
"key",
"==",
"\"",
"\"",
"{",
"return",
"t",
".",
"position",
"\n",
"}",
"\n",
"return",
"t",
".",
"GetPositionPath",
"(",
"strings",
".",
"Split",
"(",
"key",
",",
"\"",
"\"",
")",
")",
"\n",
"}"
] | // GetPosition returns the position of the given key. | [
"GetPosition",
"returns",
"the",
"position",
"of",
"the",
"given",
"key",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L125-L130 | train |
pelletier/go-toml | toml.go | GetPositionPath | func (t *Tree) GetPositionPath(keys []string) Position {
if len(keys) == 0 {
return t.position
}
subtree := t
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return Position{0, 0}
}
switch node := value.(type) {
case *Tree:
subtree = node
case []*Tree:
// go to most recent element
if len(node) == 0 {
return Position{0, 0}
}
subtree = node[len(node)-1]
default:
return Position{0, 0}
}
}
// branch based on final node type
switch node := subtree.values[keys[len(keys)-1]].(type) {
case *tomlValue:
return node.position
case *Tree:
return node.position
case []*Tree:
// go to most recent element
if len(node) == 0 {
return Position{0, 0}
}
return node[len(node)-1].position
default:
return Position{0, 0}
}
} | go | func (t *Tree) GetPositionPath(keys []string) Position {
if len(keys) == 0 {
return t.position
}
subtree := t
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return Position{0, 0}
}
switch node := value.(type) {
case *Tree:
subtree = node
case []*Tree:
// go to most recent element
if len(node) == 0 {
return Position{0, 0}
}
subtree = node[len(node)-1]
default:
return Position{0, 0}
}
}
// branch based on final node type
switch node := subtree.values[keys[len(keys)-1]].(type) {
case *tomlValue:
return node.position
case *Tree:
return node.position
case []*Tree:
// go to most recent element
if len(node) == 0 {
return Position{0, 0}
}
return node[len(node)-1].position
default:
return Position{0, 0}
}
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"GetPositionPath",
"(",
"keys",
"[",
"]",
"string",
")",
"Position",
"{",
"if",
"len",
"(",
"keys",
")",
"==",
"0",
"{",
"return",
"t",
".",
"position",
"\n",
"}",
"\n",
"subtree",
":=",
"t",
"\n",
"for",
"_",
",",
"intermediateKey",
":=",
"range",
"keys",
"[",
":",
"len",
"(",
"keys",
")",
"-",
"1",
"]",
"{",
"value",
",",
"exists",
":=",
"subtree",
".",
"values",
"[",
"intermediateKey",
"]",
"\n",
"if",
"!",
"exists",
"{",
"return",
"Position",
"{",
"0",
",",
"0",
"}",
"\n",
"}",
"\n",
"switch",
"node",
":=",
"value",
".",
"(",
"type",
")",
"{",
"case",
"*",
"Tree",
":",
"subtree",
"=",
"node",
"\n",
"case",
"[",
"]",
"*",
"Tree",
":",
"// go to most recent element",
"if",
"len",
"(",
"node",
")",
"==",
"0",
"{",
"return",
"Position",
"{",
"0",
",",
"0",
"}",
"\n",
"}",
"\n",
"subtree",
"=",
"node",
"[",
"len",
"(",
"node",
")",
"-",
"1",
"]",
"\n",
"default",
":",
"return",
"Position",
"{",
"0",
",",
"0",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"// branch based on final node type",
"switch",
"node",
":=",
"subtree",
".",
"values",
"[",
"keys",
"[",
"len",
"(",
"keys",
")",
"-",
"1",
"]",
"]",
".",
"(",
"type",
")",
"{",
"case",
"*",
"tomlValue",
":",
"return",
"node",
".",
"position",
"\n",
"case",
"*",
"Tree",
":",
"return",
"node",
".",
"position",
"\n",
"case",
"[",
"]",
"*",
"Tree",
":",
"// go to most recent element",
"if",
"len",
"(",
"node",
")",
"==",
"0",
"{",
"return",
"Position",
"{",
"0",
",",
"0",
"}",
"\n",
"}",
"\n",
"return",
"node",
"[",
"len",
"(",
"node",
")",
"-",
"1",
"]",
".",
"position",
"\n",
"default",
":",
"return",
"Position",
"{",
"0",
",",
"0",
"}",
"\n",
"}",
"\n",
"}"
] | // GetPositionPath returns the element in the tree indicated by 'keys'.
// If keys is of length zero, the current tree is returned. | [
"GetPositionPath",
"returns",
"the",
"element",
"in",
"the",
"tree",
"indicated",
"by",
"keys",
".",
"If",
"keys",
"is",
"of",
"length",
"zero",
"the",
"current",
"tree",
"is",
"returned",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L134-L172 | train |
pelletier/go-toml | toml.go | GetDefault | func (t *Tree) GetDefault(key string, def interface{}) interface{} {
val := t.Get(key)
if val == nil {
return def
}
return val
} | go | func (t *Tree) GetDefault(key string, def interface{}) interface{} {
val := t.Get(key)
if val == nil {
return def
}
return val
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"GetDefault",
"(",
"key",
"string",
",",
"def",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"val",
":=",
"t",
".",
"Get",
"(",
"key",
")",
"\n",
"if",
"val",
"==",
"nil",
"{",
"return",
"def",
"\n",
"}",
"\n",
"return",
"val",
"\n",
"}"
] | // GetDefault works like Get but with a default value | [
"GetDefault",
"works",
"like",
"Get",
"but",
"with",
"a",
"default",
"value"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L175-L181 | train |
pelletier/go-toml | toml.go | LoadReader | func LoadReader(reader io.Reader) (tree *Tree, err error) {
inputBytes, err := ioutil.ReadAll(reader)
if err != nil {
return
}
tree, err = LoadBytes(inputBytes)
return
} | go | func LoadReader(reader io.Reader) (tree *Tree, err error) {
inputBytes, err := ioutil.ReadAll(reader)
if err != nil {
return
}
tree, err = LoadBytes(inputBytes)
return
} | [
"func",
"LoadReader",
"(",
"reader",
"io",
".",
"Reader",
")",
"(",
"tree",
"*",
"Tree",
",",
"err",
"error",
")",
"{",
"inputBytes",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"reader",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"tree",
",",
"err",
"=",
"LoadBytes",
"(",
"inputBytes",
")",
"\n",
"return",
"\n",
"}"
] | // LoadReader creates a Tree from any io.Reader. | [
"LoadReader",
"creates",
"a",
"Tree",
"from",
"any",
"io",
".",
"Reader",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L371-L378 | train |
pelletier/go-toml | toml.go | Load | func Load(content string) (tree *Tree, err error) {
return LoadBytes([]byte(content))
} | go | func Load(content string) (tree *Tree, err error) {
return LoadBytes([]byte(content))
} | [
"func",
"Load",
"(",
"content",
"string",
")",
"(",
"tree",
"*",
"Tree",
",",
"err",
"error",
")",
"{",
"return",
"LoadBytes",
"(",
"[",
"]",
"byte",
"(",
"content",
")",
")",
"\n",
"}"
] | // Load creates a Tree from a string. | [
"Load",
"creates",
"a",
"Tree",
"from",
"a",
"string",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L381-L383 | train |
pelletier/go-toml | toml.go | LoadFile | func LoadFile(path string) (tree *Tree, err error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return LoadReader(file)
} | go | func LoadFile(path string) (tree *Tree, err error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return LoadReader(file)
} | [
"func",
"LoadFile",
"(",
"path",
"string",
")",
"(",
"tree",
"*",
"Tree",
",",
"err",
"error",
")",
"{",
"file",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"path",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"defer",
"file",
".",
"Close",
"(",
")",
"\n",
"return",
"LoadReader",
"(",
"file",
")",
"\n",
"}"
] | // LoadFile creates a Tree from a file. | [
"LoadFile",
"creates",
"a",
"Tree",
"from",
"a",
"file",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L386-L393 | train |
pelletier/go-toml | tomltree_write.go | encodeMultilineTomlString | func encodeMultilineTomlString(value string) string {
var b bytes.Buffer
for _, rr := range value {
switch rr {
case '\b':
b.WriteString(`\b`)
case '\t':
b.WriteString("\t")
case '\n':
b.WriteString("\n")
case '\f':
b.WriteString(`\f`)
case '\r':
b.WriteString("\r")
case '"':
b.WriteString(`"`)
case '\\':
b.WriteString(`\`)
default:
intRr := uint16(rr)
if intRr < 0x001F {
b.WriteString(fmt.Sprintf("\\u%0.4X", intRr))
} else {
b.WriteRune(rr)
}
}
}
return b.String()
} | go | func encodeMultilineTomlString(value string) string {
var b bytes.Buffer
for _, rr := range value {
switch rr {
case '\b':
b.WriteString(`\b`)
case '\t':
b.WriteString("\t")
case '\n':
b.WriteString("\n")
case '\f':
b.WriteString(`\f`)
case '\r':
b.WriteString("\r")
case '"':
b.WriteString(`"`)
case '\\':
b.WriteString(`\`)
default:
intRr := uint16(rr)
if intRr < 0x001F {
b.WriteString(fmt.Sprintf("\\u%0.4X", intRr))
} else {
b.WriteRune(rr)
}
}
}
return b.String()
} | [
"func",
"encodeMultilineTomlString",
"(",
"value",
"string",
")",
"string",
"{",
"var",
"b",
"bytes",
".",
"Buffer",
"\n\n",
"for",
"_",
",",
"rr",
":=",
"range",
"value",
"{",
"switch",
"rr",
"{",
"case",
"'\\b'",
":",
"b",
".",
"WriteString",
"(",
"`\\b`",
")",
"\n",
"case",
"'\\t'",
":",
"b",
".",
"WriteString",
"(",
"\"",
"\\t",
"\"",
")",
"\n",
"case",
"'\\n'",
":",
"b",
".",
"WriteString",
"(",
"\"",
"\\n",
"\"",
")",
"\n",
"case",
"'\\f'",
":",
"b",
".",
"WriteString",
"(",
"`\\f`",
")",
"\n",
"case",
"'\\r'",
":",
"b",
".",
"WriteString",
"(",
"\"",
"\\r",
"\"",
")",
"\n",
"case",
"'\"'",
":",
"b",
".",
"WriteString",
"(",
"`\"`",
")",
"\n",
"case",
"'\\\\'",
":",
"b",
".",
"WriteString",
"(",
"`\\`",
")",
"\n",
"default",
":",
"intRr",
":=",
"uint16",
"(",
"rr",
")",
"\n",
"if",
"intRr",
"<",
"0x001F",
"{",
"b",
".",
"WriteString",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\\\\",
"\"",
",",
"intRr",
")",
")",
"\n",
"}",
"else",
"{",
"b",
".",
"WriteRune",
"(",
"rr",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"b",
".",
"String",
"(",
")",
"\n",
"}"
] | // Encodes a string to a TOML-compliant multi-line string value
// This function is a clone of the existing encodeTomlString function, except that whitespace characters
// are preserved. Quotation marks and backslashes are also not escaped. | [
"Encodes",
"a",
"string",
"to",
"a",
"TOML",
"-",
"compliant",
"multi",
"-",
"line",
"string",
"value",
"This",
"function",
"is",
"a",
"clone",
"of",
"the",
"existing",
"encodeTomlString",
"function",
"except",
"that",
"whitespace",
"characters",
"are",
"preserved",
".",
"Quotation",
"marks",
"and",
"backslashes",
"are",
"also",
"not",
"escaped",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/tomltree_write.go#L30-L59 | train |
pelletier/go-toml | tomltree_write.go | WriteTo | func (t *Tree) WriteTo(w io.Writer) (int64, error) {
return t.writeTo(w, "", "", 0, false)
} | go | func (t *Tree) WriteTo(w io.Writer) (int64, error) {
return t.writeTo(w, "", "", 0, false)
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"WriteTo",
"(",
"w",
"io",
".",
"Writer",
")",
"(",
"int64",
",",
"error",
")",
"{",
"return",
"t",
".",
"writeTo",
"(",
"w",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"0",
",",
"false",
")",
"\n",
"}"
] | // WriteTo encode the Tree as Toml and writes it to the writer w.
// Returns the number of bytes written in case of success, or an error if anything happened. | [
"WriteTo",
"encode",
"the",
"Tree",
"as",
"Toml",
"and",
"writes",
"it",
"to",
"the",
"writer",
"w",
".",
"Returns",
"the",
"number",
"of",
"bytes",
"written",
"in",
"case",
"of",
"success",
"or",
"an",
"error",
"if",
"anything",
"happened",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/tomltree_write.go#L382-L384 | train |
pelletier/go-toml | tomltree_write.go | ToTomlString | func (t *Tree) ToTomlString() (string, error) {
var buf bytes.Buffer
_, err := t.WriteTo(&buf)
if err != nil {
return "", err
}
return buf.String(), nil
} | go | func (t *Tree) ToTomlString() (string, error) {
var buf bytes.Buffer
_, err := t.WriteTo(&buf)
if err != nil {
return "", err
}
return buf.String(), nil
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"ToTomlString",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"buf",
"bytes",
".",
"Buffer",
"\n",
"_",
",",
"err",
":=",
"t",
".",
"WriteTo",
"(",
"&",
"buf",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"buf",
".",
"String",
"(",
")",
",",
"nil",
"\n",
"}"
] | // ToTomlString generates a human-readable representation of the current tree.
// Output spans multiple lines, and is suitable for ingest by a TOML parser.
// If the conversion cannot be performed, ToString returns a non-nil error. | [
"ToTomlString",
"generates",
"a",
"human",
"-",
"readable",
"representation",
"of",
"the",
"current",
"tree",
".",
"Output",
"spans",
"multiple",
"lines",
"and",
"is",
"suitable",
"for",
"ingest",
"by",
"a",
"TOML",
"parser",
".",
"If",
"the",
"conversion",
"cannot",
"be",
"performed",
"ToString",
"returns",
"a",
"non",
"-",
"nil",
"error",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/tomltree_write.go#L389-L396 | train |
pelletier/go-toml | query/query.go | Execute | func (q *Query) Execute(tree *toml.Tree) *Result {
result := &Result{
items: []interface{}{},
positions: []toml.Position{},
}
if q.root == nil {
result.appendResult(tree, tree.GetPosition(""))
} else {
ctx := &queryContext{
result: result,
filters: q.filters,
}
ctx.lastPosition = tree.Position()
q.root.call(tree, ctx)
}
return result
} | go | func (q *Query) Execute(tree *toml.Tree) *Result {
result := &Result{
items: []interface{}{},
positions: []toml.Position{},
}
if q.root == nil {
result.appendResult(tree, tree.GetPosition(""))
} else {
ctx := &queryContext{
result: result,
filters: q.filters,
}
ctx.lastPosition = tree.Position()
q.root.call(tree, ctx)
}
return result
} | [
"func",
"(",
"q",
"*",
"Query",
")",
"Execute",
"(",
"tree",
"*",
"toml",
".",
"Tree",
")",
"*",
"Result",
"{",
"result",
":=",
"&",
"Result",
"{",
"items",
":",
"[",
"]",
"interface",
"{",
"}",
"{",
"}",
",",
"positions",
":",
"[",
"]",
"toml",
".",
"Position",
"{",
"}",
",",
"}",
"\n",
"if",
"q",
".",
"root",
"==",
"nil",
"{",
"result",
".",
"appendResult",
"(",
"tree",
",",
"tree",
".",
"GetPosition",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"else",
"{",
"ctx",
":=",
"&",
"queryContext",
"{",
"result",
":",
"result",
",",
"filters",
":",
"q",
".",
"filters",
",",
"}",
"\n",
"ctx",
".",
"lastPosition",
"=",
"tree",
".",
"Position",
"(",
")",
"\n",
"q",
".",
"root",
".",
"call",
"(",
"tree",
",",
"ctx",
")",
"\n",
"}",
"\n",
"return",
"result",
"\n",
"}"
] | // Execute executes a query against a Tree, and returns the result of the query. | [
"Execute",
"executes",
"a",
"query",
"against",
"a",
"Tree",
"and",
"returns",
"the",
"result",
"of",
"the",
"query",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/query/query.go#L93-L109 | train |
spf13/cast | caste.go | ToTimeE | func ToTimeE(i interface{}) (tim time.Time, err error) {
i = indirect(i)
switch v := i.(type) {
case time.Time:
return v, nil
case string:
return StringToDate(v)
case int:
return time.Unix(int64(v), 0), nil
case int64:
return time.Unix(v, 0), nil
case int32:
return time.Unix(int64(v), 0), nil
case uint:
return time.Unix(int64(v), 0), nil
case uint64:
return time.Unix(int64(v), 0), nil
case uint32:
return time.Unix(int64(v), 0), nil
default:
return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", i, i)
}
} | go | func ToTimeE(i interface{}) (tim time.Time, err error) {
i = indirect(i)
switch v := i.(type) {
case time.Time:
return v, nil
case string:
return StringToDate(v)
case int:
return time.Unix(int64(v), 0), nil
case int64:
return time.Unix(v, 0), nil
case int32:
return time.Unix(int64(v), 0), nil
case uint:
return time.Unix(int64(v), 0), nil
case uint64:
return time.Unix(int64(v), 0), nil
case uint32:
return time.Unix(int64(v), 0), nil
default:
return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", i, i)
}
} | [
"func",
"ToTimeE",
"(",
"i",
"interface",
"{",
"}",
")",
"(",
"tim",
"time",
".",
"Time",
",",
"err",
"error",
")",
"{",
"i",
"=",
"indirect",
"(",
"i",
")",
"\n\n",
"switch",
"v",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"time",
".",
"Time",
":",
"return",
"v",
",",
"nil",
"\n",
"case",
"string",
":",
"return",
"StringToDate",
"(",
"v",
")",
"\n",
"case",
"int",
":",
"return",
"time",
".",
"Unix",
"(",
"int64",
"(",
"v",
")",
",",
"0",
")",
",",
"nil",
"\n",
"case",
"int64",
":",
"return",
"time",
".",
"Unix",
"(",
"v",
",",
"0",
")",
",",
"nil",
"\n",
"case",
"int32",
":",
"return",
"time",
".",
"Unix",
"(",
"int64",
"(",
"v",
")",
",",
"0",
")",
",",
"nil",
"\n",
"case",
"uint",
":",
"return",
"time",
".",
"Unix",
"(",
"int64",
"(",
"v",
")",
",",
"0",
")",
",",
"nil",
"\n",
"case",
"uint64",
":",
"return",
"time",
".",
"Unix",
"(",
"int64",
"(",
"v",
")",
",",
"0",
")",
",",
"nil",
"\n",
"case",
"uint32",
":",
"return",
"time",
".",
"Unix",
"(",
"int64",
"(",
"v",
")",
",",
"0",
")",
",",
"nil",
"\n",
"default",
":",
"return",
"time",
".",
"Time",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"}",
"\n",
"}"
] | // ToTimeE casts an interface to a time.Time type. | [
"ToTimeE",
"casts",
"an",
"interface",
"to",
"a",
"time",
".",
"Time",
"type",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L22-L45 | train |
spf13/cast | caste.go | ToDurationE | func ToDurationE(i interface{}) (d time.Duration, err error) {
i = indirect(i)
switch s := i.(type) {
case time.Duration:
return s, nil
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
d = time.Duration(ToInt64(s))
return
case float32, float64:
d = time.Duration(ToFloat64(s))
return
case string:
if strings.ContainsAny(s, "nsuµmh") {
d, err = time.ParseDuration(s)
} else {
d, err = time.ParseDuration(s + "ns")
}
return
default:
err = fmt.Errorf("unable to cast %#v of type %T to Duration", i, i)
return
}
} | go | func ToDurationE(i interface{}) (d time.Duration, err error) {
i = indirect(i)
switch s := i.(type) {
case time.Duration:
return s, nil
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
d = time.Duration(ToInt64(s))
return
case float32, float64:
d = time.Duration(ToFloat64(s))
return
case string:
if strings.ContainsAny(s, "nsuµmh") {
d, err = time.ParseDuration(s)
} else {
d, err = time.ParseDuration(s + "ns")
}
return
default:
err = fmt.Errorf("unable to cast %#v of type %T to Duration", i, i)
return
}
} | [
"func",
"ToDurationE",
"(",
"i",
"interface",
"{",
"}",
")",
"(",
"d",
"time",
".",
"Duration",
",",
"err",
"error",
")",
"{",
"i",
"=",
"indirect",
"(",
"i",
")",
"\n\n",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"time",
".",
"Duration",
":",
"return",
"s",
",",
"nil",
"\n",
"case",
"int",
",",
"int64",
",",
"int32",
",",
"int16",
",",
"int8",
",",
"uint",
",",
"uint64",
",",
"uint32",
",",
"uint16",
",",
"uint8",
":",
"d",
"=",
"time",
".",
"Duration",
"(",
"ToInt64",
"(",
"s",
")",
")",
"\n",
"return",
"\n",
"case",
"float32",
",",
"float64",
":",
"d",
"=",
"time",
".",
"Duration",
"(",
"ToFloat64",
"(",
"s",
")",
")",
"\n",
"return",
"\n",
"case",
"string",
":",
"if",
"strings",
".",
"ContainsAny",
"(",
"s",
",",
"\"",
")",
" ",
"",
"d",
",",
"err",
"=",
"time",
".",
"ParseDuration",
"(",
"s",
")",
"\n",
"}",
"else",
"{",
"d",
",",
"err",
"=",
"time",
".",
"ParseDuration",
"(",
"s",
"+",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"\n",
"default",
":",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}"
] | // ToDurationE casts an interface to a time.Duration type. | [
"ToDurationE",
"casts",
"an",
"interface",
"to",
"a",
"time",
".",
"Duration",
"type",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L48-L71 | train |
spf13/cast | caste.go | ToBoolE | func ToBoolE(i interface{}) (bool, error) {
i = indirect(i)
switch b := i.(type) {
case bool:
return b, nil
case nil:
return false, nil
case int:
if i.(int) != 0 {
return true, nil
}
return false, nil
case string:
return strconv.ParseBool(i.(string))
default:
return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
}
} | go | func ToBoolE(i interface{}) (bool, error) {
i = indirect(i)
switch b := i.(type) {
case bool:
return b, nil
case nil:
return false, nil
case int:
if i.(int) != 0 {
return true, nil
}
return false, nil
case string:
return strconv.ParseBool(i.(string))
default:
return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
}
} | [
"func",
"ToBoolE",
"(",
"i",
"interface",
"{",
"}",
")",
"(",
"bool",
",",
"error",
")",
"{",
"i",
"=",
"indirect",
"(",
"i",
")",
"\n\n",
"switch",
"b",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"bool",
":",
"return",
"b",
",",
"nil",
"\n",
"case",
"nil",
":",
"return",
"false",
",",
"nil",
"\n",
"case",
"int",
":",
"if",
"i",
".",
"(",
"int",
")",
"!=",
"0",
"{",
"return",
"true",
",",
"nil",
"\n",
"}",
"\n",
"return",
"false",
",",
"nil",
"\n",
"case",
"string",
":",
"return",
"strconv",
".",
"ParseBool",
"(",
"i",
".",
"(",
"string",
")",
")",
"\n",
"default",
":",
"return",
"false",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"}",
"\n",
"}"
] | // ToBoolE casts an interface to a bool type. | [
"ToBoolE",
"casts",
"an",
"interface",
"to",
"a",
"bool",
"type",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L74-L92 | train |
spf13/cast | caste.go | ToFloat64E | func ToFloat64E(i interface{}) (float64, error) {
i = indirect(i)
switch s := i.(type) {
case float64:
return s, nil
case float32:
return float64(s), nil
case int:
return float64(s), nil
case int64:
return float64(s), nil
case int32:
return float64(s), nil
case int16:
return float64(s), nil
case int8:
return float64(s), nil
case uint:
return float64(s), nil
case uint64:
return float64(s), nil
case uint32:
return float64(s), nil
case uint16:
return float64(s), nil
case uint8:
return float64(s), nil
case string:
v, err := strconv.ParseFloat(s, 64)
if err == nil {
return v, nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
case bool:
if s {
return 1, nil
}
return 0, nil
default:
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
}
} | go | func ToFloat64E(i interface{}) (float64, error) {
i = indirect(i)
switch s := i.(type) {
case float64:
return s, nil
case float32:
return float64(s), nil
case int:
return float64(s), nil
case int64:
return float64(s), nil
case int32:
return float64(s), nil
case int16:
return float64(s), nil
case int8:
return float64(s), nil
case uint:
return float64(s), nil
case uint64:
return float64(s), nil
case uint32:
return float64(s), nil
case uint16:
return float64(s), nil
case uint8:
return float64(s), nil
case string:
v, err := strconv.ParseFloat(s, 64)
if err == nil {
return v, nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
case bool:
if s {
return 1, nil
}
return 0, nil
default:
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
}
} | [
"func",
"ToFloat64E",
"(",
"i",
"interface",
"{",
"}",
")",
"(",
"float64",
",",
"error",
")",
"{",
"i",
"=",
"indirect",
"(",
"i",
")",
"\n\n",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"float64",
":",
"return",
"s",
",",
"nil",
"\n",
"case",
"float32",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int64",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int32",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int16",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int8",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint64",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint32",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint16",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint8",
":",
"return",
"float64",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"string",
":",
"v",
",",
"err",
":=",
"strconv",
".",
"ParseFloat",
"(",
"s",
",",
"64",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"v",
",",
"nil",
"\n",
"}",
"\n",
"return",
"0",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"case",
"bool",
":",
"if",
"s",
"{",
"return",
"1",
",",
"nil",
"\n",
"}",
"\n",
"return",
"0",
",",
"nil",
"\n",
"default",
":",
"return",
"0",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"}",
"\n",
"}"
] | // ToFloat64E casts an interface to a float64 type. | [
"ToFloat64E",
"casts",
"an",
"interface",
"to",
"a",
"float64",
"type",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L95-L137 | train |
spf13/cast | caste.go | ToInt32E | func ToInt32E(i interface{}) (int32, error) {
i = indirect(i)
switch s := i.(type) {
case int:
return int32(s), nil
case int64:
return int32(s), nil
case int32:
return s, nil
case int16:
return int32(s), nil
case int8:
return int32(s), nil
case uint:
return int32(s), nil
case uint64:
return int32(s), nil
case uint32:
return int32(s), nil
case uint16:
return int32(s), nil
case uint8:
return int32(s), nil
case float64:
return int32(s), nil
case float32:
return int32(s), nil
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
return int32(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
case bool:
if s {
return 1, nil
}
return 0, nil
case nil:
return 0, nil
default:
return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
}
} | go | func ToInt32E(i interface{}) (int32, error) {
i = indirect(i)
switch s := i.(type) {
case int:
return int32(s), nil
case int64:
return int32(s), nil
case int32:
return s, nil
case int16:
return int32(s), nil
case int8:
return int32(s), nil
case uint:
return int32(s), nil
case uint64:
return int32(s), nil
case uint32:
return int32(s), nil
case uint16:
return int32(s), nil
case uint8:
return int32(s), nil
case float64:
return int32(s), nil
case float32:
return int32(s), nil
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
return int32(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
case bool:
if s {
return 1, nil
}
return 0, nil
case nil:
return 0, nil
default:
return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
}
} | [
"func",
"ToInt32E",
"(",
"i",
"interface",
"{",
"}",
")",
"(",
"int32",
",",
"error",
")",
"{",
"i",
"=",
"indirect",
"(",
"i",
")",
"\n\n",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"int",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int64",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int32",
":",
"return",
"s",
",",
"nil",
"\n",
"case",
"int16",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int8",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint64",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint32",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint16",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint8",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"float64",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"float32",
":",
"return",
"int32",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"string",
":",
"v",
",",
"err",
":=",
"strconv",
".",
"ParseInt",
"(",
"s",
",",
"0",
",",
"0",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"int32",
"(",
"v",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"0",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"case",
"bool",
":",
"if",
"s",
"{",
"return",
"1",
",",
"nil",
"\n",
"}",
"\n",
"return",
"0",
",",
"nil",
"\n",
"case",
"nil",
":",
"return",
"0",
",",
"nil",
"\n",
"default",
":",
"return",
"0",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"}",
"\n",
"}"
] | // ToInt32E casts an interface to an int32 type. | [
"ToInt32E",
"casts",
"an",
"interface",
"to",
"an",
"int32",
"type",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L232-L276 | train |
spf13/cast | caste.go | ToUintE | func ToUintE(i interface{}) (uint, error) {
i = indirect(i)
switch s := i.(type) {
case string:
v, err := strconv.ParseUint(s, 0, 0)
if err == nil {
return uint(v), nil
}
return 0, fmt.Errorf("unable to cast %#v to uint: %s", i, err)
case int:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int64:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int32:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int16:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int8:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case uint:
return s, nil
case uint64:
return uint(s), nil
case uint32:
return uint(s), nil
case uint16:
return uint(s), nil
case uint8:
return uint(s), nil
case float64:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case float32:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case bool:
if s {
return 1, nil
}
return 0, nil
case nil:
return 0, nil
default:
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
}
} | go | func ToUintE(i interface{}) (uint, error) {
i = indirect(i)
switch s := i.(type) {
case string:
v, err := strconv.ParseUint(s, 0, 0)
if err == nil {
return uint(v), nil
}
return 0, fmt.Errorf("unable to cast %#v to uint: %s", i, err)
case int:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int64:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int32:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int16:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case int8:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case uint:
return s, nil
case uint64:
return uint(s), nil
case uint32:
return uint(s), nil
case uint16:
return uint(s), nil
case uint8:
return uint(s), nil
case float64:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case float32:
if s < 0 {
return 0, errNegativeNotAllowed
}
return uint(s), nil
case bool:
if s {
return 1, nil
}
return 0, nil
case nil:
return 0, nil
default:
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
}
} | [
"func",
"ToUintE",
"(",
"i",
"interface",
"{",
"}",
")",
"(",
"uint",
",",
"error",
")",
"{",
"i",
"=",
"indirect",
"(",
"i",
")",
"\n\n",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"v",
",",
"err",
":=",
"strconv",
".",
"ParseUint",
"(",
"s",
",",
"0",
",",
"0",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"uint",
"(",
"v",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"0",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"err",
")",
"\n",
"case",
"int",
":",
"if",
"s",
"<",
"0",
"{",
"return",
"0",
",",
"errNegativeNotAllowed",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int64",
":",
"if",
"s",
"<",
"0",
"{",
"return",
"0",
",",
"errNegativeNotAllowed",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int32",
":",
"if",
"s",
"<",
"0",
"{",
"return",
"0",
",",
"errNegativeNotAllowed",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int16",
":",
"if",
"s",
"<",
"0",
"{",
"return",
"0",
",",
"errNegativeNotAllowed",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int8",
":",
"if",
"s",
"<",
"0",
"{",
"return",
"0",
",",
"errNegativeNotAllowed",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint",
":",
"return",
"s",
",",
"nil",
"\n",
"case",
"uint64",
":",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint32",
":",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint16",
":",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"uint8",
":",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"float64",
":",
"if",
"s",
"<",
"0",
"{",
"return",
"0",
",",
"errNegativeNotAllowed",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"float32",
":",
"if",
"s",
"<",
"0",
"{",
"return",
"0",
",",
"errNegativeNotAllowed",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"bool",
":",
"if",
"s",
"{",
"return",
"1",
",",
"nil",
"\n",
"}",
"\n",
"return",
"0",
",",
"nil",
"\n",
"case",
"nil",
":",
"return",
"0",
",",
"nil",
"\n",
"default",
":",
"return",
"0",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"}",
"\n",
"}"
] | // ToUintE casts an interface to a uint type. | [
"ToUintE",
"casts",
"an",
"interface",
"to",
"a",
"uint",
"type",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L420-L485 | train |
spf13/cast | caste.go | ToStringE | func ToStringE(i interface{}) (string, error) {
i = indirectToStringerOrError(i)
switch s := i.(type) {
case string:
return s, nil
case bool:
return strconv.FormatBool(s), nil
case float64:
return strconv.FormatFloat(s, 'f', -1, 64), nil
case float32:
return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
case int:
return strconv.Itoa(s), nil
case int64:
return strconv.FormatInt(s, 10), nil
case int32:
return strconv.Itoa(int(s)), nil
case int16:
return strconv.FormatInt(int64(s), 10), nil
case int8:
return strconv.FormatInt(int64(s), 10), nil
case uint:
return strconv.FormatInt(int64(s), 10), nil
case uint64:
return strconv.FormatInt(int64(s), 10), nil
case uint32:
return strconv.FormatInt(int64(s), 10), nil
case uint16:
return strconv.FormatInt(int64(s), 10), nil
case uint8:
return strconv.FormatInt(int64(s), 10), nil
case []byte:
return string(s), nil
case template.HTML:
return string(s), nil
case template.URL:
return string(s), nil
case template.JS:
return string(s), nil
case template.CSS:
return string(s), nil
case template.HTMLAttr:
return string(s), nil
case nil:
return "", nil
case fmt.Stringer:
return s.String(), nil
case error:
return s.Error(), nil
default:
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
}
} | go | func ToStringE(i interface{}) (string, error) {
i = indirectToStringerOrError(i)
switch s := i.(type) {
case string:
return s, nil
case bool:
return strconv.FormatBool(s), nil
case float64:
return strconv.FormatFloat(s, 'f', -1, 64), nil
case float32:
return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
case int:
return strconv.Itoa(s), nil
case int64:
return strconv.FormatInt(s, 10), nil
case int32:
return strconv.Itoa(int(s)), nil
case int16:
return strconv.FormatInt(int64(s), 10), nil
case int8:
return strconv.FormatInt(int64(s), 10), nil
case uint:
return strconv.FormatInt(int64(s), 10), nil
case uint64:
return strconv.FormatInt(int64(s), 10), nil
case uint32:
return strconv.FormatInt(int64(s), 10), nil
case uint16:
return strconv.FormatInt(int64(s), 10), nil
case uint8:
return strconv.FormatInt(int64(s), 10), nil
case []byte:
return string(s), nil
case template.HTML:
return string(s), nil
case template.URL:
return string(s), nil
case template.JS:
return string(s), nil
case template.CSS:
return string(s), nil
case template.HTMLAttr:
return string(s), nil
case nil:
return "", nil
case fmt.Stringer:
return s.String(), nil
case error:
return s.Error(), nil
default:
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
}
} | [
"func",
"ToStringE",
"(",
"i",
"interface",
"{",
"}",
")",
"(",
"string",
",",
"error",
")",
"{",
"i",
"=",
"indirectToStringerOrError",
"(",
"i",
")",
"\n\n",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"return",
"s",
",",
"nil",
"\n",
"case",
"bool",
":",
"return",
"strconv",
".",
"FormatBool",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"float64",
":",
"return",
"strconv",
".",
"FormatFloat",
"(",
"s",
",",
"'f'",
",",
"-",
"1",
",",
"64",
")",
",",
"nil",
"\n",
"case",
"float32",
":",
"return",
"strconv",
".",
"FormatFloat",
"(",
"float64",
"(",
"s",
")",
",",
"'f'",
",",
"-",
"1",
",",
"32",
")",
",",
"nil",
"\n",
"case",
"int",
":",
"return",
"strconv",
".",
"Itoa",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"int64",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"s",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"int32",
":",
"return",
"strconv",
".",
"Itoa",
"(",
"int",
"(",
"s",
")",
")",
",",
"nil",
"\n",
"case",
"int16",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"s",
")",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"int8",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"s",
")",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"uint",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"s",
")",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"uint64",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"s",
")",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"uint32",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"s",
")",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"uint16",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"s",
")",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"uint8",
":",
"return",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"s",
")",
",",
"10",
")",
",",
"nil",
"\n",
"case",
"[",
"]",
"byte",
":",
"return",
"string",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"template",
".",
"HTML",
":",
"return",
"string",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"template",
".",
"URL",
":",
"return",
"string",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"template",
".",
"JS",
":",
"return",
"string",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"template",
".",
"CSS",
":",
"return",
"string",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"template",
".",
"HTMLAttr",
":",
"return",
"string",
"(",
"s",
")",
",",
"nil",
"\n",
"case",
"nil",
":",
"return",
"\"",
"\"",
",",
"nil",
"\n",
"case",
"fmt",
".",
"Stringer",
":",
"return",
"s",
".",
"String",
"(",
")",
",",
"nil",
"\n",
"case",
"error",
":",
"return",
"s",
".",
"Error",
"(",
")",
",",
"nil",
"\n",
"default",
":",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"i",
",",
"i",
")",
"\n",
"}",
"\n",
"}"
] | // ToStringE casts an interface to a string type. | [
"ToStringE",
"casts",
"an",
"interface",
"to",
"a",
"string",
"type",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L799-L852 | train |
spf13/cast | caste.go | StringToDate | func StringToDate(s string) (time.Time, error) {
return parseDateWith(s, []string{
time.RFC3339,
"2006-01-02T15:04:05", // iso8601 without timezone
time.RFC1123Z,
time.RFC1123,
time.RFC822Z,
time.RFC822,
time.RFC850,
time.ANSIC,
time.UnixDate,
time.RubyDate,
"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
"2006-01-02",
"02 Jan 2006",
"2006-01-02T15:04:05-0700", // RFC3339 without timezone hh:mm colon
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05Z07:00", // RFC3339 without T
"2006-01-02 15:04:05Z0700", // RFC3339 without T or timezone hh:mm colon
"2006-01-02 15:04:05",
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
})
} | go | func StringToDate(s string) (time.Time, error) {
return parseDateWith(s, []string{
time.RFC3339,
"2006-01-02T15:04:05", // iso8601 without timezone
time.RFC1123Z,
time.RFC1123,
time.RFC822Z,
time.RFC822,
time.RFC850,
time.ANSIC,
time.UnixDate,
time.RubyDate,
"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
"2006-01-02",
"02 Jan 2006",
"2006-01-02T15:04:05-0700", // RFC3339 without timezone hh:mm colon
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05Z07:00", // RFC3339 without T
"2006-01-02 15:04:05Z0700", // RFC3339 without T or timezone hh:mm colon
"2006-01-02 15:04:05",
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
})
} | [
"func",
"StringToDate",
"(",
"s",
"string",
")",
"(",
"time",
".",
"Time",
",",
"error",
")",
"{",
"return",
"parseDateWith",
"(",
"s",
",",
"[",
"]",
"string",
"{",
"time",
".",
"RFC3339",
",",
"\"",
"\"",
",",
"// iso8601 without timezone",
"time",
".",
"RFC1123Z",
",",
"time",
".",
"RFC1123",
",",
"time",
".",
"RFC822Z",
",",
"time",
".",
"RFC822",
",",
"time",
".",
"RFC850",
",",
"time",
".",
"ANSIC",
",",
"time",
".",
"UnixDate",
",",
"time",
".",
"RubyDate",
",",
"\"",
"\"",
",",
"// Time.String()",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"// RFC3339 without timezone hh:mm colon",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"// RFC3339 without T",
"\"",
"\"",
",",
"// RFC3339 without T or timezone hh:mm colon",
"\"",
"\"",
",",
"time",
".",
"Kitchen",
",",
"time",
".",
"Stamp",
",",
"time",
".",
"StampMilli",
",",
"time",
".",
"StampMicro",
",",
"time",
".",
"StampNano",
",",
"}",
")",
"\n",
"}"
] | // StringToDate attempts to parse a string into a time.Time type using a
// predefined list of formats. If no suitable format is found, an error is
// returned. | [
"StringToDate",
"attempts",
"to",
"parse",
"a",
"string",
"into",
"a",
"time",
".",
"Time",
"type",
"using",
"a",
"predefined",
"list",
"of",
"formats",
".",
"If",
"no",
"suitable",
"format",
"is",
"found",
"an",
"error",
"is",
"returned",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L1206-L1233 | train |
spf13/cast | caste.go | jsonStringToObject | func jsonStringToObject(s string, v interface{}) error {
data := []byte(s)
return json.Unmarshal(data, v)
} | go | func jsonStringToObject(s string, v interface{}) error {
data := []byte(s)
return json.Unmarshal(data, v)
} | [
"func",
"jsonStringToObject",
"(",
"s",
"string",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"data",
":=",
"[",
"]",
"byte",
"(",
"s",
")",
"\n",
"return",
"json",
".",
"Unmarshal",
"(",
"data",
",",
"v",
")",
"\n",
"}"
] | // jsonStringToObject attempts to unmarshall a string as JSON into
// the object passed as pointer. | [
"jsonStringToObject",
"attempts",
"to",
"unmarshall",
"a",
"string",
"as",
"JSON",
"into",
"the",
"object",
"passed",
"as",
"pointer",
"."
] | 8c9545af88b134710ab1cd196795e7f2388358d7 | https://github.com/spf13/cast/blob/8c9545af88b134710ab1cd196795e7f2388358d7/caste.go#L1246-L1249 | train |
hashicorp/go-sockaddr | route_info_linux.go | NewRouteInfo | func NewRouteInfo() (routeInfo, error) {
// CoreOS Container Linux moved ip to /usr/bin/ip, so look it up on
// $PATH and fallback to /sbin/ip on error.
path, _ := exec.LookPath("ip")
if path == "" {
path = "/sbin/ip"
}
return routeInfo{
cmds: map[string][]string{"ip": {path, "route"}},
}, nil
} | go | func NewRouteInfo() (routeInfo, error) {
// CoreOS Container Linux moved ip to /usr/bin/ip, so look it up on
// $PATH and fallback to /sbin/ip on error.
path, _ := exec.LookPath("ip")
if path == "" {
path = "/sbin/ip"
}
return routeInfo{
cmds: map[string][]string{"ip": {path, "route"}},
}, nil
} | [
"func",
"NewRouteInfo",
"(",
")",
"(",
"routeInfo",
",",
"error",
")",
"{",
"// CoreOS Container Linux moved ip to /usr/bin/ip, so look it up on",
"// $PATH and fallback to /sbin/ip on error.",
"path",
",",
"_",
":=",
"exec",
".",
"LookPath",
"(",
"\"",
"\"",
")",
"\n",
"if",
"path",
"==",
"\"",
"\"",
"{",
"path",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"return",
"routeInfo",
"{",
"cmds",
":",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
"{",
"\"",
"\"",
":",
"{",
"path",
",",
"\"",
"\"",
"}",
"}",
",",
"}",
",",
"nil",
"\n",
"}"
] | // NewRouteInfo returns a Linux-specific implementation of the RouteInfo
// interface. | [
"NewRouteInfo",
"returns",
"a",
"Linux",
"-",
"specific",
"implementation",
"of",
"the",
"RouteInfo",
"interface",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/route_info_linux.go#L16-L27 | train |
hashicorp/go-sockaddr | template/template.go | Attr | func Attr(selectorName string, ifAddrsRaw interface{}) (string, error) {
switch v := ifAddrsRaw.(type) {
case sockaddr.IfAddr:
return sockaddr.IfAttr(selectorName, v)
case sockaddr.IfAddrs:
return sockaddr.IfAttrs(selectorName, v)
default:
return "", fmt.Errorf("unable to obtain attribute %s from type %T (%v)", selectorName, ifAddrsRaw, ifAddrsRaw)
}
} | go | func Attr(selectorName string, ifAddrsRaw interface{}) (string, error) {
switch v := ifAddrsRaw.(type) {
case sockaddr.IfAddr:
return sockaddr.IfAttr(selectorName, v)
case sockaddr.IfAddrs:
return sockaddr.IfAttrs(selectorName, v)
default:
return "", fmt.Errorf("unable to obtain attribute %s from type %T (%v)", selectorName, ifAddrsRaw, ifAddrsRaw)
}
} | [
"func",
"Attr",
"(",
"selectorName",
"string",
",",
"ifAddrsRaw",
"interface",
"{",
"}",
")",
"(",
"string",
",",
"error",
")",
"{",
"switch",
"v",
":=",
"ifAddrsRaw",
".",
"(",
"type",
")",
"{",
"case",
"sockaddr",
".",
"IfAddr",
":",
"return",
"sockaddr",
".",
"IfAttr",
"(",
"selectorName",
",",
"v",
")",
"\n",
"case",
"sockaddr",
".",
"IfAddrs",
":",
"return",
"sockaddr",
".",
"IfAttrs",
"(",
"selectorName",
",",
"v",
")",
"\n",
"default",
":",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"selectorName",
",",
"ifAddrsRaw",
",",
"ifAddrsRaw",
")",
"\n",
"}",
"\n",
"}"
] | // Attr returns the attribute from the ifAddrRaw argument. If the argument is
// an IfAddrs, only the first element will be evaluated for resolution. | [
"Attr",
"returns",
"the",
"attribute",
"from",
"the",
"ifAddrRaw",
"argument",
".",
"If",
"the",
"argument",
"is",
"an",
"IfAddrs",
"only",
"the",
"first",
"element",
"will",
"be",
"evaluated",
"for",
"resolution",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/template/template.go#L106-L115 | train |
hashicorp/go-sockaddr | template/template.go | Parse | func Parse(input string) (string, error) {
addrs, err := sockaddr.GetAllInterfaces()
if err != nil {
return "", errwrap.Wrapf("unable to query interface addresses: {{err}}", err)
}
return ParseIfAddrs(input, addrs)
} | go | func Parse(input string) (string, error) {
addrs, err := sockaddr.GetAllInterfaces()
if err != nil {
return "", errwrap.Wrapf("unable to query interface addresses: {{err}}", err)
}
return ParseIfAddrs(input, addrs)
} | [
"func",
"Parse",
"(",
"input",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"addrs",
",",
"err",
":=",
"sockaddr",
".",
"GetAllInterfaces",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"errwrap",
".",
"Wrapf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"ParseIfAddrs",
"(",
"input",
",",
"addrs",
")",
"\n",
"}"
] | // Parse parses input as template input using the addresses available on the
// host, then returns the string output if there are no errors. | [
"Parse",
"parses",
"input",
"as",
"template",
"input",
"using",
"the",
"addresses",
"available",
"on",
"the",
"host",
"then",
"returns",
"the",
"string",
"output",
"if",
"there",
"are",
"no",
"errors",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/template/template.go#L119-L126 | train |
hashicorp/go-sockaddr | template/template.go | ParseIfAddrs | func ParseIfAddrs(input string, ifAddrs sockaddr.IfAddrs) (string, error) {
return ParseIfAddrsTemplate(input, ifAddrs, template.New("sockaddr.Parse"))
} | go | func ParseIfAddrs(input string, ifAddrs sockaddr.IfAddrs) (string, error) {
return ParseIfAddrsTemplate(input, ifAddrs, template.New("sockaddr.Parse"))
} | [
"func",
"ParseIfAddrs",
"(",
"input",
"string",
",",
"ifAddrs",
"sockaddr",
".",
"IfAddrs",
")",
"(",
"string",
",",
"error",
")",
"{",
"return",
"ParseIfAddrsTemplate",
"(",
"input",
",",
"ifAddrs",
",",
"template",
".",
"New",
"(",
"\"",
"\"",
")",
")",
"\n",
"}"
] | // ParseIfAddrs parses input as template input using the IfAddrs inputs, then
// returns the string output if there are no errors. | [
"ParseIfAddrs",
"parses",
"input",
"as",
"template",
"input",
"using",
"the",
"IfAddrs",
"inputs",
"then",
"returns",
"the",
"string",
"output",
"if",
"there",
"are",
"no",
"errors",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/template/template.go#L130-L132 | train |
hashicorp/go-sockaddr | template/template.go | ParseIfAddrsTemplate | func ParseIfAddrsTemplate(input string, ifAddrs sockaddr.IfAddrs, tmplIn *template.Template) (string, error) {
// Create a template, add the function map, and parse the text.
tmpl, err := tmplIn.Option("missingkey=error").
Funcs(SourceFuncs).
Funcs(SortFuncs).
Funcs(FilterFuncs).
Funcs(HelperFuncs).
Parse(input)
if err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("unable to parse template %+q: {{err}}", input), err)
}
var outWriter bytes.Buffer
err = tmpl.Execute(&outWriter, ifAddrs)
if err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("unable to execute sockaddr input %+q: {{err}}", input), err)
}
return outWriter.String(), nil
} | go | func ParseIfAddrsTemplate(input string, ifAddrs sockaddr.IfAddrs, tmplIn *template.Template) (string, error) {
// Create a template, add the function map, and parse the text.
tmpl, err := tmplIn.Option("missingkey=error").
Funcs(SourceFuncs).
Funcs(SortFuncs).
Funcs(FilterFuncs).
Funcs(HelperFuncs).
Parse(input)
if err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("unable to parse template %+q: {{err}}", input), err)
}
var outWriter bytes.Buffer
err = tmpl.Execute(&outWriter, ifAddrs)
if err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("unable to execute sockaddr input %+q: {{err}}", input), err)
}
return outWriter.String(), nil
} | [
"func",
"ParseIfAddrsTemplate",
"(",
"input",
"string",
",",
"ifAddrs",
"sockaddr",
".",
"IfAddrs",
",",
"tmplIn",
"*",
"template",
".",
"Template",
")",
"(",
"string",
",",
"error",
")",
"{",
"// Create a template, add the function map, and parse the text.",
"tmpl",
",",
"err",
":=",
"tmplIn",
".",
"Option",
"(",
"\"",
"\"",
")",
".",
"Funcs",
"(",
"SourceFuncs",
")",
".",
"Funcs",
"(",
"SortFuncs",
")",
".",
"Funcs",
"(",
"FilterFuncs",
")",
".",
"Funcs",
"(",
"HelperFuncs",
")",
".",
"Parse",
"(",
"input",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"errwrap",
".",
"Wrapf",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"input",
")",
",",
"err",
")",
"\n",
"}",
"\n\n",
"var",
"outWriter",
"bytes",
".",
"Buffer",
"\n",
"err",
"=",
"tmpl",
".",
"Execute",
"(",
"&",
"outWriter",
",",
"ifAddrs",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"errwrap",
".",
"Wrapf",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"input",
")",
",",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"outWriter",
".",
"String",
"(",
")",
",",
"nil",
"\n",
"}"
] | // ParseIfAddrsTemplate parses input as template input using the IfAddrs inputs,
// then returns the string output if there are no errors. | [
"ParseIfAddrsTemplate",
"parses",
"input",
"as",
"template",
"input",
"using",
"the",
"IfAddrs",
"inputs",
"then",
"returns",
"the",
"string",
"output",
"if",
"there",
"are",
"no",
"errors",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/template/template.go#L136-L155 | train |
hashicorp/go-sockaddr | ipv6addr.go | AddressBinString | func (ipv6 IPv6Addr) AddressBinString() string {
bi := big.Int(*ipv6.Address)
return fmt.Sprintf("%0128s", bi.Text(2))
} | go | func (ipv6 IPv6Addr) AddressBinString() string {
bi := big.Int(*ipv6.Address)
return fmt.Sprintf("%0128s", bi.Text(2))
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"AddressBinString",
"(",
")",
"string",
"{",
"bi",
":=",
"big",
".",
"Int",
"(",
"*",
"ipv6",
".",
"Address",
")",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"bi",
".",
"Text",
"(",
"2",
")",
")",
"\n",
"}"
] | // AddressBinString returns a string with the IPv6Addr's Address represented
// as a sequence of '0' and '1' characters. This method is useful for
// debugging or by operators who want to inspect an address. | [
"AddressBinString",
"returns",
"a",
"string",
"with",
"the",
"IPv6Addr",
"s",
"Address",
"represented",
"as",
"a",
"sequence",
"of",
"0",
"and",
"1",
"characters",
".",
"This",
"method",
"is",
"useful",
"for",
"debugging",
"or",
"by",
"operators",
"who",
"want",
"to",
"inspect",
"an",
"address",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L161-L164 | train |
hashicorp/go-sockaddr | ipv6addr.go | ContainsAddress | func (ipv6 IPv6Addr) ContainsAddress(x IPv6Address) bool {
xAddr := IPv6Addr{
Address: x,
Mask: ipv6HostMask,
}
{
xIPv6 := xAddr.FirstUsable().(IPv6Addr)
yIPv6 := ipv6.FirstUsable().(IPv6Addr)
if xIPv6.CmpAddress(yIPv6) >= 1 {
return false
}
}
{
xIPv6 := xAddr.LastUsable().(IPv6Addr)
yIPv6 := ipv6.LastUsable().(IPv6Addr)
if xIPv6.CmpAddress(yIPv6) <= -1 {
return false
}
}
return true
} | go | func (ipv6 IPv6Addr) ContainsAddress(x IPv6Address) bool {
xAddr := IPv6Addr{
Address: x,
Mask: ipv6HostMask,
}
{
xIPv6 := xAddr.FirstUsable().(IPv6Addr)
yIPv6 := ipv6.FirstUsable().(IPv6Addr)
if xIPv6.CmpAddress(yIPv6) >= 1 {
return false
}
}
{
xIPv6 := xAddr.LastUsable().(IPv6Addr)
yIPv6 := ipv6.LastUsable().(IPv6Addr)
if xIPv6.CmpAddress(yIPv6) <= -1 {
return false
}
}
return true
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"ContainsAddress",
"(",
"x",
"IPv6Address",
")",
"bool",
"{",
"xAddr",
":=",
"IPv6Addr",
"{",
"Address",
":",
"x",
",",
"Mask",
":",
"ipv6HostMask",
",",
"}",
"\n\n",
"{",
"xIPv6",
":=",
"xAddr",
".",
"FirstUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"yIPv6",
":=",
"ipv6",
".",
"FirstUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"if",
"xIPv6",
".",
"CmpAddress",
"(",
"yIPv6",
")",
">=",
"1",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n\n",
"{",
"xIPv6",
":=",
"xAddr",
".",
"LastUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"yIPv6",
":=",
"ipv6",
".",
"LastUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"if",
"xIPv6",
".",
"CmpAddress",
"(",
"yIPv6",
")",
"<=",
"-",
"1",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // ContainsAddress returns true if the IPv6Address is contained within the
// receiver. | [
"ContainsAddress",
"returns",
"true",
"if",
"the",
"IPv6Address",
"is",
"contained",
"within",
"the",
"receiver",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L267-L289 | train |
hashicorp/go-sockaddr | ipv6addr.go | ContainsNetwork | func (x IPv6Addr) ContainsNetwork(y IPv6Addr) bool {
{
xIPv6 := x.FirstUsable().(IPv6Addr)
yIPv6 := y.FirstUsable().(IPv6Addr)
if ret := xIPv6.CmpAddress(yIPv6); ret >= 1 {
return false
}
}
{
xIPv6 := x.LastUsable().(IPv6Addr)
yIPv6 := y.LastUsable().(IPv6Addr)
if ret := xIPv6.CmpAddress(yIPv6); ret <= -1 {
return false
}
}
return true
} | go | func (x IPv6Addr) ContainsNetwork(y IPv6Addr) bool {
{
xIPv6 := x.FirstUsable().(IPv6Addr)
yIPv6 := y.FirstUsable().(IPv6Addr)
if ret := xIPv6.CmpAddress(yIPv6); ret >= 1 {
return false
}
}
{
xIPv6 := x.LastUsable().(IPv6Addr)
yIPv6 := y.LastUsable().(IPv6Addr)
if ret := xIPv6.CmpAddress(yIPv6); ret <= -1 {
return false
}
}
return true
} | [
"func",
"(",
"x",
"IPv6Addr",
")",
"ContainsNetwork",
"(",
"y",
"IPv6Addr",
")",
"bool",
"{",
"{",
"xIPv6",
":=",
"x",
".",
"FirstUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"yIPv6",
":=",
"y",
".",
"FirstUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"if",
"ret",
":=",
"xIPv6",
".",
"CmpAddress",
"(",
"yIPv6",
")",
";",
"ret",
">=",
"1",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n\n",
"{",
"xIPv6",
":=",
"x",
".",
"LastUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"yIPv6",
":=",
"y",
".",
"LastUsable",
"(",
")",
".",
"(",
"IPv6Addr",
")",
"\n",
"if",
"ret",
":=",
"xIPv6",
".",
"CmpAddress",
"(",
"yIPv6",
")",
";",
"ret",
"<=",
"-",
"1",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // ContainsNetwork returns true if the network from IPv6Addr is contained within
// the receiver. | [
"ContainsNetwork",
"returns",
"true",
"if",
"the",
"network",
"from",
"IPv6Addr",
"is",
"contained",
"within",
"the",
"receiver",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L293-L310 | train |
hashicorp/go-sockaddr | ipv6addr.go | LastUsable | func (ipv6 IPv6Addr) LastUsable() IPAddr {
addr := new(big.Int)
addr.Set(ipv6.Address)
mask := new(big.Int)
mask.Set(ipv6.Mask)
negMask := new(big.Int)
negMask.Xor(ipv6HostMask, mask)
lastAddr := new(big.Int)
lastAddr.And(addr, mask)
lastAddr.Or(lastAddr, negMask)
return IPv6Addr{
Address: IPv6Address(lastAddr),
Mask: ipv6HostMask,
}
} | go | func (ipv6 IPv6Addr) LastUsable() IPAddr {
addr := new(big.Int)
addr.Set(ipv6.Address)
mask := new(big.Int)
mask.Set(ipv6.Mask)
negMask := new(big.Int)
negMask.Xor(ipv6HostMask, mask)
lastAddr := new(big.Int)
lastAddr.And(addr, mask)
lastAddr.Or(lastAddr, negMask)
return IPv6Addr{
Address: IPv6Address(lastAddr),
Mask: ipv6HostMask,
}
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"LastUsable",
"(",
")",
"IPAddr",
"{",
"addr",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"\n",
"addr",
".",
"Set",
"(",
"ipv6",
".",
"Address",
")",
"\n\n",
"mask",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"\n",
"mask",
".",
"Set",
"(",
"ipv6",
".",
"Mask",
")",
"\n\n",
"negMask",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"\n",
"negMask",
".",
"Xor",
"(",
"ipv6HostMask",
",",
"mask",
")",
"\n\n",
"lastAddr",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"\n",
"lastAddr",
".",
"And",
"(",
"addr",
",",
"mask",
")",
"\n",
"lastAddr",
".",
"Or",
"(",
"lastAddr",
",",
"negMask",
")",
"\n\n",
"return",
"IPv6Addr",
"{",
"Address",
":",
"IPv6Address",
"(",
"lastAddr",
")",
",",
"Mask",
":",
"ipv6HostMask",
",",
"}",
"\n",
"}"
] | // LastUsable returns the last address in a given network. | [
"LastUsable",
"returns",
"the",
"last",
"address",
"in",
"a",
"given",
"network",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L390-L408 | train |
hashicorp/go-sockaddr | ipv6addr.go | MustIPv6Addr | func MustIPv6Addr(addr string) IPv6Addr {
ipv6, err := NewIPv6Addr(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create an IPv6Addr from %+q: %v", addr, err))
}
return ipv6
} | go | func MustIPv6Addr(addr string) IPv6Addr {
ipv6, err := NewIPv6Addr(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create an IPv6Addr from %+q: %v", addr, err))
}
return ipv6
} | [
"func",
"MustIPv6Addr",
"(",
"addr",
"string",
")",
"IPv6Addr",
"{",
"ipv6",
",",
"err",
":=",
"NewIPv6Addr",
"(",
"addr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"addr",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"return",
"ipv6",
"\n",
"}"
] | // MustIPv6Addr is a helper method that must return an IPv6Addr or panic on
// invalid input. | [
"MustIPv6Addr",
"is",
"a",
"helper",
"method",
"that",
"must",
"return",
"an",
"IPv6Addr",
"or",
"panic",
"on",
"invalid",
"input",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L442-L448 | train |
hashicorp/go-sockaddr | ipv6addr.go | NetIPMask | func (ipv6 IPv6Addr) NetIPMask() *net.IPMask {
ipv6Mask := make(net.IPMask, IPv6len)
m := big.Int(*ipv6.Mask)
copy(ipv6Mask, m.Bytes())
return &ipv6Mask
} | go | func (ipv6 IPv6Addr) NetIPMask() *net.IPMask {
ipv6Mask := make(net.IPMask, IPv6len)
m := big.Int(*ipv6.Mask)
copy(ipv6Mask, m.Bytes())
return &ipv6Mask
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"NetIPMask",
"(",
")",
"*",
"net",
".",
"IPMask",
"{",
"ipv6Mask",
":=",
"make",
"(",
"net",
".",
"IPMask",
",",
"IPv6len",
")",
"\n",
"m",
":=",
"big",
".",
"Int",
"(",
"*",
"ipv6",
".",
"Mask",
")",
"\n",
"copy",
"(",
"ipv6Mask",
",",
"m",
".",
"Bytes",
"(",
")",
")",
"\n",
"return",
"&",
"ipv6Mask",
"\n",
"}"
] | // NetIPMask create a new net.IPMask from the IPv6Addr. | [
"NetIPMask",
"create",
"a",
"new",
"net",
".",
"IPMask",
"from",
"the",
"IPv6Addr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L456-L461 | train |
hashicorp/go-sockaddr | ipv6addr.go | NetIPNet | func (ipv6 IPv6Addr) NetIPNet() *net.IPNet {
ipv6net := &net.IPNet{}
ipv6net.IP = make(net.IP, IPv6len)
copy(ipv6net.IP, *ipv6.NetIP())
ipv6net.Mask = *ipv6.NetIPMask()
return ipv6net
} | go | func (ipv6 IPv6Addr) NetIPNet() *net.IPNet {
ipv6net := &net.IPNet{}
ipv6net.IP = make(net.IP, IPv6len)
copy(ipv6net.IP, *ipv6.NetIP())
ipv6net.Mask = *ipv6.NetIPMask()
return ipv6net
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"NetIPNet",
"(",
")",
"*",
"net",
".",
"IPNet",
"{",
"ipv6net",
":=",
"&",
"net",
".",
"IPNet",
"{",
"}",
"\n",
"ipv6net",
".",
"IP",
"=",
"make",
"(",
"net",
".",
"IP",
",",
"IPv6len",
")",
"\n",
"copy",
"(",
"ipv6net",
".",
"IP",
",",
"*",
"ipv6",
".",
"NetIP",
"(",
")",
")",
"\n",
"ipv6net",
".",
"Mask",
"=",
"*",
"ipv6",
".",
"NetIPMask",
"(",
")",
"\n",
"return",
"ipv6net",
"\n",
"}"
] | // Network returns a pointer to the net.IPNet within IPv4Addr receiver. | [
"Network",
"returns",
"a",
"pointer",
"to",
"the",
"net",
".",
"IPNet",
"within",
"IPv4Addr",
"receiver",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L464-L470 | train |
hashicorp/go-sockaddr | ipv6addr.go | NetworkAddress | func (ipv6 IPv6Addr) NetworkAddress() IPv6Network {
addr := new(big.Int)
addr.SetBytes((*ipv6.Address).Bytes())
mask := new(big.Int)
mask.SetBytes(*ipv6.NetIPMask())
netAddr := new(big.Int)
netAddr.And(addr, mask)
return IPv6Network(netAddr)
} | go | func (ipv6 IPv6Addr) NetworkAddress() IPv6Network {
addr := new(big.Int)
addr.SetBytes((*ipv6.Address).Bytes())
mask := new(big.Int)
mask.SetBytes(*ipv6.NetIPMask())
netAddr := new(big.Int)
netAddr.And(addr, mask)
return IPv6Network(netAddr)
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"NetworkAddress",
"(",
")",
"IPv6Network",
"{",
"addr",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"\n",
"addr",
".",
"SetBytes",
"(",
"(",
"*",
"ipv6",
".",
"Address",
")",
".",
"Bytes",
"(",
")",
")",
"\n\n",
"mask",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"\n",
"mask",
".",
"SetBytes",
"(",
"*",
"ipv6",
".",
"NetIPMask",
"(",
")",
")",
"\n\n",
"netAddr",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"\n",
"netAddr",
".",
"And",
"(",
"addr",
",",
"mask",
")",
"\n\n",
"return",
"IPv6Network",
"(",
"netAddr",
")",
"\n",
"}"
] | // NetworkAddress returns an IPv6Network of the IPv6Addr's network address. | [
"NetworkAddress",
"returns",
"an",
"IPv6Network",
"of",
"the",
"IPv6Addr",
"s",
"network",
"address",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L481-L492 | train |
hashicorp/go-sockaddr | ipv6addr.go | Octets | func (ipv6 IPv6Addr) Octets() []int {
x := make([]int, IPv6len)
for i, b := range *bigIntToNetIPv6(ipv6.Address) {
x[i] = int(b)
}
return x
} | go | func (ipv6 IPv6Addr) Octets() []int {
x := make([]int, IPv6len)
for i, b := range *bigIntToNetIPv6(ipv6.Address) {
x[i] = int(b)
}
return x
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"Octets",
"(",
")",
"[",
"]",
"int",
"{",
"x",
":=",
"make",
"(",
"[",
"]",
"int",
",",
"IPv6len",
")",
"\n",
"for",
"i",
",",
"b",
":=",
"range",
"*",
"bigIntToNetIPv6",
"(",
"ipv6",
".",
"Address",
")",
"{",
"x",
"[",
"i",
"]",
"=",
"int",
"(",
"b",
")",
"\n",
"}",
"\n\n",
"return",
"x",
"\n",
"}"
] | // Octets returns a slice of the 16 octets in an IPv6Addr's Address. The
// order of the bytes is big endian. | [
"Octets",
"returns",
"a",
"slice",
"of",
"the",
"16",
"octets",
"in",
"an",
"IPv6Addr",
"s",
"Address",
".",
"The",
"order",
"of",
"the",
"bytes",
"is",
"big",
"endian",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L496-L503 | train |
hashicorp/go-sockaddr | ipv6addr.go | String | func (ipv6 IPv6Addr) String() string {
if ipv6.Port != 0 {
return fmt.Sprintf("[%s]:%d", ipv6.NetIP().String(), ipv6.Port)
}
if ipv6.Maskbits() == 128 {
return ipv6.NetIP().String()
}
return fmt.Sprintf("%s/%d", ipv6.NetIP().String(), ipv6.Maskbits())
} | go | func (ipv6 IPv6Addr) String() string {
if ipv6.Port != 0 {
return fmt.Sprintf("[%s]:%d", ipv6.NetIP().String(), ipv6.Port)
}
if ipv6.Maskbits() == 128 {
return ipv6.NetIP().String()
}
return fmt.Sprintf("%s/%d", ipv6.NetIP().String(), ipv6.Maskbits())
} | [
"func",
"(",
"ipv6",
"IPv6Addr",
")",
"String",
"(",
")",
"string",
"{",
"if",
"ipv6",
".",
"Port",
"!=",
"0",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"ipv6",
".",
"NetIP",
"(",
")",
".",
"String",
"(",
")",
",",
"ipv6",
".",
"Port",
")",
"\n",
"}",
"\n\n",
"if",
"ipv6",
".",
"Maskbits",
"(",
")",
"==",
"128",
"{",
"return",
"ipv6",
".",
"NetIP",
"(",
")",
".",
"String",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"ipv6",
".",
"NetIP",
"(",
")",
".",
"String",
"(",
")",
",",
"ipv6",
".",
"Maskbits",
"(",
")",
")",
"\n",
"}"
] | // String returns a string representation of the IPv6Addr | [
"String",
"returns",
"a",
"string",
"representation",
"of",
"the",
"IPv6Addr"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L506-L516 | train |
hashicorp/go-sockaddr | ipv6addr.go | IPv6AddrAttr | func IPv6AddrAttr(ipv6 IPv6Addr, selector AttrName) string {
fn, found := ipv6AddrAttrMap[selector]
if !found {
return ""
}
return fn(ipv6)
} | go | func IPv6AddrAttr(ipv6 IPv6Addr, selector AttrName) string {
fn, found := ipv6AddrAttrMap[selector]
if !found {
return ""
}
return fn(ipv6)
} | [
"func",
"IPv6AddrAttr",
"(",
"ipv6",
"IPv6Addr",
",",
"selector",
"AttrName",
")",
"string",
"{",
"fn",
",",
"found",
":=",
"ipv6AddrAttrMap",
"[",
"selector",
"]",
"\n",
"if",
"!",
"found",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n\n",
"return",
"fn",
"(",
"ipv6",
")",
"\n",
"}"
] | // IPv6AddrAttr returns a string representation of an attribute for the given
// IPv6Addr. | [
"IPv6AddrAttr",
"returns",
"a",
"string",
"representation",
"of",
"an",
"attribute",
"for",
"the",
"given",
"IPv6Addr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L530-L537 | train |
hashicorp/go-sockaddr | ipv6addr.go | bigIntToNetIPv6 | func bigIntToNetIPv6(bi *big.Int) *net.IP {
x := make(net.IP, IPv6len)
ipv6Bytes := bi.Bytes()
// It's possibe for ipv6Bytes to be less than IPv6len bytes in size. If
// they are different sizes we to pad the size of response.
if len(ipv6Bytes) < IPv6len {
buf := new(bytes.Buffer)
buf.Grow(IPv6len)
for i := len(ipv6Bytes); i < IPv6len; i++ {
if err := binary.Write(buf, binary.BigEndian, byte(0)); err != nil {
panic(fmt.Sprintf("Unable to pad byte %d of input %v: %v", i, bi, err))
}
}
for _, b := range ipv6Bytes {
if err := binary.Write(buf, binary.BigEndian, b); err != nil {
panic(fmt.Sprintf("Unable to preserve endianness of input %v: %v", bi, err))
}
}
ipv6Bytes = buf.Bytes()
}
i := copy(x, ipv6Bytes)
if i != IPv6len {
panic("IPv6 wrong size")
}
return &x
} | go | func bigIntToNetIPv6(bi *big.Int) *net.IP {
x := make(net.IP, IPv6len)
ipv6Bytes := bi.Bytes()
// It's possibe for ipv6Bytes to be less than IPv6len bytes in size. If
// they are different sizes we to pad the size of response.
if len(ipv6Bytes) < IPv6len {
buf := new(bytes.Buffer)
buf.Grow(IPv6len)
for i := len(ipv6Bytes); i < IPv6len; i++ {
if err := binary.Write(buf, binary.BigEndian, byte(0)); err != nil {
panic(fmt.Sprintf("Unable to pad byte %d of input %v: %v", i, bi, err))
}
}
for _, b := range ipv6Bytes {
if err := binary.Write(buf, binary.BigEndian, b); err != nil {
panic(fmt.Sprintf("Unable to preserve endianness of input %v: %v", bi, err))
}
}
ipv6Bytes = buf.Bytes()
}
i := copy(x, ipv6Bytes)
if i != IPv6len {
panic("IPv6 wrong size")
}
return &x
} | [
"func",
"bigIntToNetIPv6",
"(",
"bi",
"*",
"big",
".",
"Int",
")",
"*",
"net",
".",
"IP",
"{",
"x",
":=",
"make",
"(",
"net",
".",
"IP",
",",
"IPv6len",
")",
"\n",
"ipv6Bytes",
":=",
"bi",
".",
"Bytes",
"(",
")",
"\n\n",
"// It's possibe for ipv6Bytes to be less than IPv6len bytes in size. If",
"// they are different sizes we to pad the size of response.",
"if",
"len",
"(",
"ipv6Bytes",
")",
"<",
"IPv6len",
"{",
"buf",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n",
"buf",
".",
"Grow",
"(",
"IPv6len",
")",
"\n\n",
"for",
"i",
":=",
"len",
"(",
"ipv6Bytes",
")",
";",
"i",
"<",
"IPv6len",
";",
"i",
"++",
"{",
"if",
"err",
":=",
"binary",
".",
"Write",
"(",
"buf",
",",
"binary",
".",
"BigEndian",
",",
"byte",
"(",
"0",
")",
")",
";",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"i",
",",
"bi",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"b",
":=",
"range",
"ipv6Bytes",
"{",
"if",
"err",
":=",
"binary",
".",
"Write",
"(",
"buf",
",",
"binary",
".",
"BigEndian",
",",
"b",
")",
";",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"bi",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"ipv6Bytes",
"=",
"buf",
".",
"Bytes",
"(",
")",
"\n",
"}",
"\n",
"i",
":=",
"copy",
"(",
"x",
",",
"ipv6Bytes",
")",
"\n",
"if",
"i",
"!=",
"IPv6len",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"&",
"x",
"\n",
"}"
] | // bigIntToNetIPv6 is a helper function that correctly returns a net.IP with the
// correctly padded values. | [
"bigIntToNetIPv6",
"is",
"a",
"helper",
"function",
"that",
"correctly",
"returns",
"a",
"net",
".",
"IP",
"with",
"the",
"correctly",
"padded",
"values",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv6addr.go#L562-L591 | train |
hashicorp/go-sockaddr | cmd/sockaddr/command/autohelp.go | MakeHelp | func MakeHelp(c AutoHelp) string {
usageText := c.Usage()
// If the length of Usage() is zero, then assume this is a hidden
// command.
if len(usageText) == 0 {
return ""
}
descriptionText := wordwrap.WrapString(c.Description(), 60)
descrLines := strings.Split(descriptionText, "\n")
prefixedLines := make([]string, len(descrLines))
for i := range descrLines {
prefixedLines[i] = " " + descrLines[i]
}
descriptionText = strings.Join(prefixedLines, "\n")
c.InitOpts()
flags := []*flag.Flag{}
c.VisitAllFlags(func(f *flag.Flag) {
flags = append(flags, f)
})
optionsText := OptionsHelpOutput(flags)
var helpOutput string
switch {
case len(optionsText) == 0 && len(descriptionText) == 0:
helpOutput = usageText
case len(optionsText) == 0:
helpOutput = fmt.Sprintf(`Usage: %s
%s`,
usageText, descriptionText)
case len(descriptionText) == 0 && len(optionsText) > 0:
helpOutput = fmt.Sprintf(`Usage: %s
Options:
%s`,
usageText, optionsText)
default:
helpOutput = fmt.Sprintf(`Usage: %s
%s
Options:
%s`,
usageText, descriptionText, optionsText)
}
return strings.TrimSpace(helpOutput)
} | go | func MakeHelp(c AutoHelp) string {
usageText := c.Usage()
// If the length of Usage() is zero, then assume this is a hidden
// command.
if len(usageText) == 0 {
return ""
}
descriptionText := wordwrap.WrapString(c.Description(), 60)
descrLines := strings.Split(descriptionText, "\n")
prefixedLines := make([]string, len(descrLines))
for i := range descrLines {
prefixedLines[i] = " " + descrLines[i]
}
descriptionText = strings.Join(prefixedLines, "\n")
c.InitOpts()
flags := []*flag.Flag{}
c.VisitAllFlags(func(f *flag.Flag) {
flags = append(flags, f)
})
optionsText := OptionsHelpOutput(flags)
var helpOutput string
switch {
case len(optionsText) == 0 && len(descriptionText) == 0:
helpOutput = usageText
case len(optionsText) == 0:
helpOutput = fmt.Sprintf(`Usage: %s
%s`,
usageText, descriptionText)
case len(descriptionText) == 0 && len(optionsText) > 0:
helpOutput = fmt.Sprintf(`Usage: %s
Options:
%s`,
usageText, optionsText)
default:
helpOutput = fmt.Sprintf(`Usage: %s
%s
Options:
%s`,
usageText, descriptionText, optionsText)
}
return strings.TrimSpace(helpOutput)
} | [
"func",
"MakeHelp",
"(",
"c",
"AutoHelp",
")",
"string",
"{",
"usageText",
":=",
"c",
".",
"Usage",
"(",
")",
"\n\n",
"// If the length of Usage() is zero, then assume this is a hidden",
"// command.",
"if",
"len",
"(",
"usageText",
")",
"==",
"0",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n\n",
"descriptionText",
":=",
"wordwrap",
".",
"WrapString",
"(",
"c",
".",
"Description",
"(",
")",
",",
"60",
")",
"\n",
"descrLines",
":=",
"strings",
".",
"Split",
"(",
"descriptionText",
",",
"\"",
"\\n",
"\"",
")",
"\n",
"prefixedLines",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"descrLines",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"descrLines",
"{",
"prefixedLines",
"[",
"i",
"]",
"=",
"\"",
"\"",
"+",
"descrLines",
"[",
"i",
"]",
"\n",
"}",
"\n",
"descriptionText",
"=",
"strings",
".",
"Join",
"(",
"prefixedLines",
",",
"\"",
"\\n",
"\"",
")",
"\n\n",
"c",
".",
"InitOpts",
"(",
")",
"\n",
"flags",
":=",
"[",
"]",
"*",
"flag",
".",
"Flag",
"{",
"}",
"\n",
"c",
".",
"VisitAllFlags",
"(",
"func",
"(",
"f",
"*",
"flag",
".",
"Flag",
")",
"{",
"flags",
"=",
"append",
"(",
"flags",
",",
"f",
")",
"\n",
"}",
")",
"\n",
"optionsText",
":=",
"OptionsHelpOutput",
"(",
"flags",
")",
"\n\n",
"var",
"helpOutput",
"string",
"\n",
"switch",
"{",
"case",
"len",
"(",
"optionsText",
")",
"==",
"0",
"&&",
"len",
"(",
"descriptionText",
")",
"==",
"0",
":",
"helpOutput",
"=",
"usageText",
"\n",
"case",
"len",
"(",
"optionsText",
")",
"==",
"0",
":",
"helpOutput",
"=",
"fmt",
".",
"Sprintf",
"(",
"`Usage: %s\n\n%s`",
",",
"usageText",
",",
"descriptionText",
")",
"\n",
"case",
"len",
"(",
"descriptionText",
")",
"==",
"0",
"&&",
"len",
"(",
"optionsText",
")",
">",
"0",
":",
"helpOutput",
"=",
"fmt",
".",
"Sprintf",
"(",
"`Usage: %s\n\nOptions:\n\n%s`",
",",
"usageText",
",",
"optionsText",
")",
"\n",
"default",
":",
"helpOutput",
"=",
"fmt",
".",
"Sprintf",
"(",
"`Usage: %s\n\n%s\n\nOptions:\n\n%s`",
",",
"usageText",
",",
"descriptionText",
",",
"optionsText",
")",
"\n",
"}",
"\n\n",
"return",
"strings",
".",
"TrimSpace",
"(",
"helpOutput",
")",
"\n",
"}"
] | // MakeHelp generates a help string based on the capabilities of the Command | [
"MakeHelp",
"generates",
"a",
"help",
"string",
"based",
"on",
"the",
"capabilities",
"of",
"the",
"Command"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/cmd/sockaddr/command/autohelp.go#L23-L75 | train |
hashicorp/go-sockaddr | cmd/sockaddr/command/autohelp.go | OptionsHelpOutput | func OptionsHelpOutput(flags []*flag.Flag) string {
sort.Sort(ByName(flags))
var output []string
for _, f := range flags {
if len(f.Usage) == 0 {
continue
}
output = append(output, fmt.Sprintf("-%s | %s", f.Name, f.Usage))
}
optionsOutput := columnize.Format(output, &columnize.Config{
Delim: "|",
Glue: " ",
Prefix: " ",
Empty: "",
})
return optionsOutput
} | go | func OptionsHelpOutput(flags []*flag.Flag) string {
sort.Sort(ByName(flags))
var output []string
for _, f := range flags {
if len(f.Usage) == 0 {
continue
}
output = append(output, fmt.Sprintf("-%s | %s", f.Name, f.Usage))
}
optionsOutput := columnize.Format(output, &columnize.Config{
Delim: "|",
Glue: " ",
Prefix: " ",
Empty: "",
})
return optionsOutput
} | [
"func",
"OptionsHelpOutput",
"(",
"flags",
"[",
"]",
"*",
"flag",
".",
"Flag",
")",
"string",
"{",
"sort",
".",
"Sort",
"(",
"ByName",
"(",
"flags",
")",
")",
"\n\n",
"var",
"output",
"[",
"]",
"string",
"\n",
"for",
"_",
",",
"f",
":=",
"range",
"flags",
"{",
"if",
"len",
"(",
"f",
".",
"Usage",
")",
"==",
"0",
"{",
"continue",
"\n",
"}",
"\n\n",
"output",
"=",
"append",
"(",
"output",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"f",
".",
"Name",
",",
"f",
".",
"Usage",
")",
")",
"\n",
"}",
"\n\n",
"optionsOutput",
":=",
"columnize",
".",
"Format",
"(",
"output",
",",
"&",
"columnize",
".",
"Config",
"{",
"Delim",
":",
"\"",
"\"",
",",
"Glue",
":",
"\"",
"\"",
",",
"Prefix",
":",
"\"",
"\"",
",",
"Empty",
":",
"\"",
"\"",
",",
"}",
")",
"\n",
"return",
"optionsOutput",
"\n",
"}"
] | // OptionsHelpOutput returns a string of formatted options | [
"OptionsHelpOutput",
"returns",
"a",
"string",
"of",
"formatted",
"options"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/cmd/sockaddr/command/autohelp.go#L102-L121 | train |
hashicorp/go-sockaddr | unixsock.go | Equal | func (us UnixSock) Equal(sa SockAddr) bool {
usb, ok := sa.(UnixSock)
if !ok {
return false
}
if us.Path() != usb.Path() {
return false
}
return true
} | go | func (us UnixSock) Equal(sa SockAddr) bool {
usb, ok := sa.(UnixSock)
if !ok {
return false
}
if us.Path() != usb.Path() {
return false
}
return true
} | [
"func",
"(",
"us",
"UnixSock",
")",
"Equal",
"(",
"sa",
"SockAddr",
")",
"bool",
"{",
"usb",
",",
"ok",
":=",
"sa",
".",
"(",
"UnixSock",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"us",
".",
"Path",
"(",
")",
"!=",
"usb",
".",
"Path",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] | // Equal returns true if a SockAddr is equal to the receiving UnixSock. | [
"Equal",
"returns",
"true",
"if",
"a",
"SockAddr",
"is",
"equal",
"to",
"the",
"receiving",
"UnixSock",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/unixsock.go#L57-L68 | train |
hashicorp/go-sockaddr | unixsock.go | MustUnixSock | func MustUnixSock(addr string) UnixSock {
us, err := NewUnixSock(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create a UnixSock from %+q: %v", addr, err))
}
return us
} | go | func MustUnixSock(addr string) UnixSock {
us, err := NewUnixSock(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create a UnixSock from %+q: %v", addr, err))
}
return us
} | [
"func",
"MustUnixSock",
"(",
"addr",
"string",
")",
"UnixSock",
"{",
"us",
",",
"err",
":=",
"NewUnixSock",
"(",
"addr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"addr",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"return",
"us",
"\n",
"}"
] | // MustUnixSock is a helper method that must return an UnixSock or panic on
// invalid input. | [
"MustUnixSock",
"is",
"a",
"helper",
"method",
"that",
"must",
"return",
"an",
"UnixSock",
"or",
"panic",
"on",
"invalid",
"input",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/unixsock.go#L84-L90 | train |
hashicorp/go-sockaddr | unixsock.go | UnixSockAttr | func UnixSockAttr(us UnixSock, attrName AttrName) string {
fn, found := unixAttrMap[attrName]
if !found {
return ""
}
return fn(us)
} | go | func UnixSockAttr(us UnixSock, attrName AttrName) string {
fn, found := unixAttrMap[attrName]
if !found {
return ""
}
return fn(us)
} | [
"func",
"UnixSockAttr",
"(",
"us",
"UnixSock",
",",
"attrName",
"AttrName",
")",
"string",
"{",
"fn",
",",
"found",
":=",
"unixAttrMap",
"[",
"attrName",
"]",
"\n",
"if",
"!",
"found",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n\n",
"return",
"fn",
"(",
"us",
")",
"\n",
"}"
] | // UnixSockAttr returns a string representation of an attribute for the given
// UnixSock. | [
"UnixSockAttr",
"returns",
"a",
"string",
"representation",
"of",
"an",
"attribute",
"for",
"the",
"given",
"UnixSock",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/unixsock.go#L114-L121 | train |
hashicorp/go-sockaddr | ifaddr.go | IfAddrAttr | func IfAddrAttr(ifAddr IfAddr, attrName AttrName) string {
fn, found := ifAddrAttrMap[attrName]
if !found {
return ""
}
return fn(ifAddr)
} | go | func IfAddrAttr(ifAddr IfAddr, attrName AttrName) string {
fn, found := ifAddrAttrMap[attrName]
if !found {
return ""
}
return fn(ifAddr)
} | [
"func",
"IfAddrAttr",
"(",
"ifAddr",
"IfAddr",
",",
"attrName",
"AttrName",
")",
"string",
"{",
"fn",
",",
"found",
":=",
"ifAddrAttrMap",
"[",
"attrName",
"]",
"\n",
"if",
"!",
"found",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n\n",
"return",
"fn",
"(",
"ifAddr",
")",
"\n",
"}"
] | // IfAddrAttr returns a string representation of an attribute for the given
// IfAddr. | [
"IfAddrAttr",
"returns",
"a",
"string",
"representation",
"of",
"an",
"attribute",
"for",
"the",
"given",
"IfAddr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddr.go#L229-L236 | train |
hashicorp/go-sockaddr | rfc.go | IsRFC | func IsRFC(rfcNum uint, sa SockAddr) bool {
rfcNetMap := KnownRFCs()
rfcNets, ok := rfcNetMap[rfcNum]
if !ok {
return false
}
var contained bool
for _, rfcNet := range rfcNets {
if rfcNet.Contains(sa) {
contained = true
break
}
}
return contained
} | go | func IsRFC(rfcNum uint, sa SockAddr) bool {
rfcNetMap := KnownRFCs()
rfcNets, ok := rfcNetMap[rfcNum]
if !ok {
return false
}
var contained bool
for _, rfcNet := range rfcNets {
if rfcNet.Contains(sa) {
contained = true
break
}
}
return contained
} | [
"func",
"IsRFC",
"(",
"rfcNum",
"uint",
",",
"sa",
"SockAddr",
")",
"bool",
"{",
"rfcNetMap",
":=",
"KnownRFCs",
"(",
")",
"\n",
"rfcNets",
",",
"ok",
":=",
"rfcNetMap",
"[",
"rfcNum",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"var",
"contained",
"bool",
"\n",
"for",
"_",
",",
"rfcNet",
":=",
"range",
"rfcNets",
"{",
"if",
"rfcNet",
".",
"Contains",
"(",
"sa",
")",
"{",
"contained",
"=",
"true",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"contained",
"\n",
"}"
] | // IsRFC tests to see if an SockAddr matches the specified RFC | [
"IsRFC",
"tests",
"to",
"see",
"if",
"an",
"SockAddr",
"matches",
"the",
"specified",
"RFC"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/rfc.go#L9-L24 | train |
hashicorp/go-sockaddr | rfc.go | VisitAllRFCs | func VisitAllRFCs(fn func(rfcNum uint, sockaddrs SockAddrs)) {
rfcNetMap := KnownRFCs()
// Blacklist of faux-RFCs. Don't show the world that we're abusing the
// RFC system in this library.
rfcBlacklist := map[uint]struct{}{
ForwardingBlacklist: {},
}
for rfcNum, sas := range rfcNetMap {
if _, found := rfcBlacklist[rfcNum]; !found {
fn(rfcNum, sas)
}
}
} | go | func VisitAllRFCs(fn func(rfcNum uint, sockaddrs SockAddrs)) {
rfcNetMap := KnownRFCs()
// Blacklist of faux-RFCs. Don't show the world that we're abusing the
// RFC system in this library.
rfcBlacklist := map[uint]struct{}{
ForwardingBlacklist: {},
}
for rfcNum, sas := range rfcNetMap {
if _, found := rfcBlacklist[rfcNum]; !found {
fn(rfcNum, sas)
}
}
} | [
"func",
"VisitAllRFCs",
"(",
"fn",
"func",
"(",
"rfcNum",
"uint",
",",
"sockaddrs",
"SockAddrs",
")",
")",
"{",
"rfcNetMap",
":=",
"KnownRFCs",
"(",
")",
"\n\n",
"// Blacklist of faux-RFCs. Don't show the world that we're abusing the",
"// RFC system in this library.",
"rfcBlacklist",
":=",
"map",
"[",
"uint",
"]",
"struct",
"{",
"}",
"{",
"ForwardingBlacklist",
":",
"{",
"}",
",",
"}",
"\n\n",
"for",
"rfcNum",
",",
"sas",
":=",
"range",
"rfcNetMap",
"{",
"if",
"_",
",",
"found",
":=",
"rfcBlacklist",
"[",
"rfcNum",
"]",
";",
"!",
"found",
"{",
"fn",
"(",
"rfcNum",
",",
"sas",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // VisitAllRFCs iterates over all known RFCs and calls the visitor | [
"VisitAllRFCs",
"iterates",
"over",
"all",
"known",
"RFCs",
"and",
"calls",
"the",
"visitor"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/rfc.go#L934-L948 | train |
hashicorp/go-sockaddr | route_info.go | VisitCommands | func (ri routeInfo) VisitCommands(fn func(name string, cmd []string)) {
for k, v := range ri.cmds {
cmds := append([]string(nil), v...)
fn(k, cmds)
}
} | go | func (ri routeInfo) VisitCommands(fn func(name string, cmd []string)) {
for k, v := range ri.cmds {
cmds := append([]string(nil), v...)
fn(k, cmds)
}
} | [
"func",
"(",
"ri",
"routeInfo",
")",
"VisitCommands",
"(",
"fn",
"func",
"(",
"name",
"string",
",",
"cmd",
"[",
"]",
"string",
")",
")",
"{",
"for",
"k",
",",
"v",
":=",
"range",
"ri",
".",
"cmds",
"{",
"cmds",
":=",
"append",
"(",
"[",
"]",
"string",
"(",
"nil",
")",
",",
"v",
"...",
")",
"\n",
"fn",
"(",
"k",
",",
"cmds",
")",
"\n",
"}",
"\n",
"}"
] | // VisitCommands visits each command used by the platform-specific RouteInfo
// implementation. | [
"VisitCommands",
"visits",
"each",
"command",
"used",
"by",
"the",
"platform",
"-",
"specific",
"RouteInfo",
"implementation",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/route_info.go#L14-L19 | train |
hashicorp/go-sockaddr | ifaddrs.go | Sort | func (ms *multiIfAddrSorter) Sort(ifAddrs IfAddrs) {
ms.ifAddrs = ifAddrs
sort.Sort(ms)
} | go | func (ms *multiIfAddrSorter) Sort(ifAddrs IfAddrs) {
ms.ifAddrs = ifAddrs
sort.Sort(ms)
} | [
"func",
"(",
"ms",
"*",
"multiIfAddrSorter",
")",
"Sort",
"(",
"ifAddrs",
"IfAddrs",
")",
"{",
"ms",
".",
"ifAddrs",
"=",
"ifAddrs",
"\n",
"sort",
".",
"Sort",
"(",
"ms",
")",
"\n",
"}"
] | // Sort sorts the argument slice according to the Cmp functions passed to
// OrderedIfAddrBy. | [
"Sort",
"sorts",
"the",
"argument",
"slice",
"according",
"to",
"the",
"Cmp",
"functions",
"passed",
"to",
"OrderedIfAddrBy",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L40-L43 | train |
hashicorp/go-sockaddr | ifaddrs.go | AscIfAddress | func AscIfAddress(p1Ptr, p2Ptr *IfAddr) int {
return AscAddress(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func AscIfAddress(p1Ptr, p2Ptr *IfAddr) int {
return AscAddress(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"AscIfAddress",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"AscAddress",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // AscIfAddress is a sorting function to sort IfAddrs by their respective
// address type. Non-equal types are deferred in the sort. | [
"AscIfAddress",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"IfAddrs",
"by",
"their",
"respective",
"address",
"type",
".",
"Non",
"-",
"equal",
"types",
"are",
"deferred",
"in",
"the",
"sort",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L100-L102 | train |
hashicorp/go-sockaddr | ifaddrs.go | AscIfName | func AscIfName(p1Ptr, p2Ptr *IfAddr) int {
return strings.Compare(p1Ptr.Name, p2Ptr.Name)
} | go | func AscIfName(p1Ptr, p2Ptr *IfAddr) int {
return strings.Compare(p1Ptr.Name, p2Ptr.Name)
} | [
"func",
"AscIfName",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"strings",
".",
"Compare",
"(",
"p1Ptr",
".",
"Name",
",",
"p2Ptr",
".",
"Name",
")",
"\n",
"}"
] | // AscIfName is a sorting function to sort IfAddrs by their interface names. | [
"AscIfName",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"IfAddrs",
"by",
"their",
"interface",
"names",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L139-L141 | train |
hashicorp/go-sockaddr | ifaddrs.go | AscIfNetworkSize | func AscIfNetworkSize(p1Ptr, p2Ptr *IfAddr) int {
return AscNetworkSize(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func AscIfNetworkSize(p1Ptr, p2Ptr *IfAddr) int {
return AscNetworkSize(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"AscIfNetworkSize",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"AscNetworkSize",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // AscIfNetworkSize is a sorting function to sort IfAddrs by their respective
// network mask size. | [
"AscIfNetworkSize",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"IfAddrs",
"by",
"their",
"respective",
"network",
"mask",
"size",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L145-L147 | train |
hashicorp/go-sockaddr | ifaddrs.go | AscIfPort | func AscIfPort(p1Ptr, p2Ptr *IfAddr) int {
return AscPort(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func AscIfPort(p1Ptr, p2Ptr *IfAddr) int {
return AscPort(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"AscIfPort",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"AscPort",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // AscIfPort is a sorting function to sort IfAddrs by their respective
// port type. Non-equal types are deferred in the sort. | [
"AscIfPort",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"IfAddrs",
"by",
"their",
"respective",
"port",
"type",
".",
"Non",
"-",
"equal",
"types",
"are",
"deferred",
"in",
"the",
"sort",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L151-L153 | train |
hashicorp/go-sockaddr | ifaddrs.go | AscIfType | func AscIfType(p1Ptr, p2Ptr *IfAddr) int {
return AscType(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func AscIfType(p1Ptr, p2Ptr *IfAddr) int {
return AscType(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"AscIfType",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"AscType",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // AscIfType is a sorting function to sort IfAddrs by their respective address
// type. Non-equal types are deferred in the sort. | [
"AscIfType",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"IfAddrs",
"by",
"their",
"respective",
"address",
"type",
".",
"Non",
"-",
"equal",
"types",
"are",
"deferred",
"in",
"the",
"sort",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L165-L167 | train |
hashicorp/go-sockaddr | ifaddrs.go | DescIfAddress | func DescIfAddress(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscAddress(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func DescIfAddress(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscAddress(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"DescIfAddress",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"-",
"1",
"*",
"AscAddress",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // DescIfAddress is identical to AscIfAddress but reverse ordered. | [
"DescIfAddress",
"is",
"identical",
"to",
"AscIfAddress",
"but",
"reverse",
"ordered",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L170-L172 | train |
hashicorp/go-sockaddr | ifaddrs.go | DescIfName | func DescIfName(p1Ptr, p2Ptr *IfAddr) int {
return -1 * strings.Compare(p1Ptr.Name, p2Ptr.Name)
} | go | func DescIfName(p1Ptr, p2Ptr *IfAddr) int {
return -1 * strings.Compare(p1Ptr.Name, p2Ptr.Name)
} | [
"func",
"DescIfName",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"-",
"1",
"*",
"strings",
".",
"Compare",
"(",
"p1Ptr",
".",
"Name",
",",
"p2Ptr",
".",
"Name",
")",
"\n",
"}"
] | // DescIfName is identical to AscIfName but reverse ordered. | [
"DescIfName",
"is",
"identical",
"to",
"AscIfName",
"but",
"reverse",
"ordered",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L180-L182 | train |
hashicorp/go-sockaddr | ifaddrs.go | DescIfNetworkSize | func DescIfNetworkSize(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscNetworkSize(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func DescIfNetworkSize(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscNetworkSize(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"DescIfNetworkSize",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"-",
"1",
"*",
"AscNetworkSize",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // DescIfNetworkSize is identical to AscIfNetworkSize but reverse ordered. | [
"DescIfNetworkSize",
"is",
"identical",
"to",
"AscIfNetworkSize",
"but",
"reverse",
"ordered",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L185-L187 | train |
hashicorp/go-sockaddr | ifaddrs.go | DescIfPort | func DescIfPort(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscPort(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func DescIfPort(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscPort(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"DescIfPort",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"-",
"1",
"*",
"AscPort",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // DescIfPort is identical to AscIfPort but reverse ordered. | [
"DescIfPort",
"is",
"identical",
"to",
"AscIfPort",
"but",
"reverse",
"ordered",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L190-L192 | train |
hashicorp/go-sockaddr | ifaddrs.go | DescIfPrivate | func DescIfPrivate(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscPrivate(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func DescIfPrivate(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscPrivate(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"DescIfPrivate",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"-",
"1",
"*",
"AscPrivate",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // DescIfPrivate is identical to AscIfPrivate but reverse ordered. | [
"DescIfPrivate",
"is",
"identical",
"to",
"AscIfPrivate",
"but",
"reverse",
"ordered",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L195-L197 | train |
hashicorp/go-sockaddr | ifaddrs.go | DescIfType | func DescIfType(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscType(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | go | func DescIfType(p1Ptr, p2Ptr *IfAddr) int {
return -1 * AscType(&p1Ptr.SockAddr, &p2Ptr.SockAddr)
} | [
"func",
"DescIfType",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"IfAddr",
")",
"int",
"{",
"return",
"-",
"1",
"*",
"AscType",
"(",
"&",
"p1Ptr",
".",
"SockAddr",
",",
"&",
"p2Ptr",
".",
"SockAddr",
")",
"\n",
"}"
] | // DescIfType is identical to AscIfType but reverse ordered. | [
"DescIfType",
"is",
"identical",
"to",
"AscIfType",
"but",
"reverse",
"ordered",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L200-L202 | train |
hashicorp/go-sockaddr | ifaddrs.go | FilterIfByType | func FilterIfByType(ifAddrs IfAddrs, type_ SockAddrType) (matchedIfs, excludedIfs IfAddrs) {
excludedIfs = make(IfAddrs, 0, len(ifAddrs))
matchedIfs = make(IfAddrs, 0, len(ifAddrs))
for _, ifAddr := range ifAddrs {
if ifAddr.SockAddr.Type()&type_ != 0 {
matchedIfs = append(matchedIfs, ifAddr)
} else {
excludedIfs = append(excludedIfs, ifAddr)
}
}
return matchedIfs, excludedIfs
} | go | func FilterIfByType(ifAddrs IfAddrs, type_ SockAddrType) (matchedIfs, excludedIfs IfAddrs) {
excludedIfs = make(IfAddrs, 0, len(ifAddrs))
matchedIfs = make(IfAddrs, 0, len(ifAddrs))
for _, ifAddr := range ifAddrs {
if ifAddr.SockAddr.Type()&type_ != 0 {
matchedIfs = append(matchedIfs, ifAddr)
} else {
excludedIfs = append(excludedIfs, ifAddr)
}
}
return matchedIfs, excludedIfs
} | [
"func",
"FilterIfByType",
"(",
"ifAddrs",
"IfAddrs",
",",
"type_",
"SockAddrType",
")",
"(",
"matchedIfs",
",",
"excludedIfs",
"IfAddrs",
")",
"{",
"excludedIfs",
"=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ifAddrs",
")",
")",
"\n",
"matchedIfs",
"=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ifAddrs",
")",
")",
"\n\n",
"for",
"_",
",",
"ifAddr",
":=",
"range",
"ifAddrs",
"{",
"if",
"ifAddr",
".",
"SockAddr",
".",
"Type",
"(",
")",
"&",
"type_",
"!=",
"0",
"{",
"matchedIfs",
"=",
"append",
"(",
"matchedIfs",
",",
"ifAddr",
")",
"\n",
"}",
"else",
"{",
"excludedIfs",
"=",
"append",
"(",
"excludedIfs",
",",
"ifAddr",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"matchedIfs",
",",
"excludedIfs",
"\n",
"}"
] | // FilterIfByType filters IfAddrs and returns a list of the matching type | [
"FilterIfByType",
"filters",
"IfAddrs",
"and",
"returns",
"a",
"list",
"of",
"the",
"matching",
"type"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L205-L217 | train |
hashicorp/go-sockaddr | ifaddrs.go | GetAllInterfaces | func GetAllInterfaces() (IfAddrs, error) {
ifs, err := net.Interfaces()
if err != nil {
return nil, err
}
ifAddrs := make(IfAddrs, 0, len(ifs))
for _, intf := range ifs {
addrs, err := intf.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ipAddr IPAddr
ipAddr, err = NewIPAddr(addr.String())
if err != nil {
return IfAddrs{}, fmt.Errorf("unable to create an IP address from %q", addr.String())
}
ifAddr := IfAddr{
SockAddr: ipAddr,
Interface: intf,
}
ifAddrs = append(ifAddrs, ifAddr)
}
}
return ifAddrs, nil
} | go | func GetAllInterfaces() (IfAddrs, error) {
ifs, err := net.Interfaces()
if err != nil {
return nil, err
}
ifAddrs := make(IfAddrs, 0, len(ifs))
for _, intf := range ifs {
addrs, err := intf.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ipAddr IPAddr
ipAddr, err = NewIPAddr(addr.String())
if err != nil {
return IfAddrs{}, fmt.Errorf("unable to create an IP address from %q", addr.String())
}
ifAddr := IfAddr{
SockAddr: ipAddr,
Interface: intf,
}
ifAddrs = append(ifAddrs, ifAddr)
}
}
return ifAddrs, nil
} | [
"func",
"GetAllInterfaces",
"(",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"ifs",
",",
"err",
":=",
"net",
".",
"Interfaces",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"ifAddrs",
":=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ifs",
")",
")",
"\n",
"for",
"_",
",",
"intf",
":=",
"range",
"ifs",
"{",
"addrs",
",",
"err",
":=",
"intf",
".",
"Addrs",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"addr",
":=",
"range",
"addrs",
"{",
"var",
"ipAddr",
"IPAddr",
"\n",
"ipAddr",
",",
"err",
"=",
"NewIPAddr",
"(",
"addr",
".",
"String",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"addr",
".",
"String",
"(",
")",
")",
"\n",
"}",
"\n\n",
"ifAddr",
":=",
"IfAddr",
"{",
"SockAddr",
":",
"ipAddr",
",",
"Interface",
":",
"intf",
",",
"}",
"\n",
"ifAddrs",
"=",
"append",
"(",
"ifAddrs",
",",
"ifAddr",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"ifAddrs",
",",
"nil",
"\n",
"}"
] | // GetAllInterfaces iterates over all available network interfaces and finds all
// available IP addresses on each interface and converts them to
// sockaddr.IPAddrs, and returning the result as an array of IfAddr. | [
"GetAllInterfaces",
"iterates",
"over",
"all",
"available",
"network",
"interfaces",
"and",
"finds",
"all",
"available",
"IP",
"addresses",
"on",
"each",
"interface",
"and",
"converts",
"them",
"to",
"sockaddr",
".",
"IPAddrs",
"and",
"returning",
"the",
"result",
"as",
"an",
"array",
"of",
"IfAddr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L242-L271 | train |
hashicorp/go-sockaddr | ifaddrs.go | GetDefaultInterfaces | func GetDefaultInterfaces() (IfAddrs, error) {
ri, err := NewRouteInfo()
if err != nil {
return nil, err
}
defaultIfName, err := ri.GetDefaultInterfaceName()
if err != nil {
return nil, err
}
var defaultIfs, ifAddrs IfAddrs
ifAddrs, err = GetAllInterfaces()
for _, ifAddr := range ifAddrs {
if ifAddr.Name == defaultIfName {
defaultIfs = append(defaultIfs, ifAddr)
}
}
return defaultIfs, nil
} | go | func GetDefaultInterfaces() (IfAddrs, error) {
ri, err := NewRouteInfo()
if err != nil {
return nil, err
}
defaultIfName, err := ri.GetDefaultInterfaceName()
if err != nil {
return nil, err
}
var defaultIfs, ifAddrs IfAddrs
ifAddrs, err = GetAllInterfaces()
for _, ifAddr := range ifAddrs {
if ifAddr.Name == defaultIfName {
defaultIfs = append(defaultIfs, ifAddr)
}
}
return defaultIfs, nil
} | [
"func",
"GetDefaultInterfaces",
"(",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"ri",
",",
"err",
":=",
"NewRouteInfo",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"defaultIfName",
",",
"err",
":=",
"ri",
".",
"GetDefaultInterfaceName",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"defaultIfs",
",",
"ifAddrs",
"IfAddrs",
"\n",
"ifAddrs",
",",
"err",
"=",
"GetAllInterfaces",
"(",
")",
"\n",
"for",
"_",
",",
"ifAddr",
":=",
"range",
"ifAddrs",
"{",
"if",
"ifAddr",
".",
"Name",
"==",
"defaultIfName",
"{",
"defaultIfs",
"=",
"append",
"(",
"defaultIfs",
",",
"ifAddr",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"defaultIfs",
",",
"nil",
"\n",
"}"
] | // GetDefaultInterfaces returns IfAddrs of the addresses attached to the default
// route. | [
"GetDefaultInterfaces",
"returns",
"IfAddrs",
"of",
"the",
"addresses",
"attached",
"to",
"the",
"default",
"route",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L275-L295 | train |
hashicorp/go-sockaddr | ifaddrs.go | IfByAddress | func IfByAddress(inputRe string, ifAddrs IfAddrs) (matched, remainder IfAddrs, err error) {
re, err := regexp.Compile(inputRe)
if err != nil {
return nil, nil, fmt.Errorf("Unable to compile address regexp %+q: %v", inputRe, err)
}
matchedAddrs := make(IfAddrs, 0, len(ifAddrs))
excludedAddrs := make(IfAddrs, 0, len(ifAddrs))
for _, addr := range ifAddrs {
if re.MatchString(addr.SockAddr.String()) {
matchedAddrs = append(matchedAddrs, addr)
} else {
excludedAddrs = append(excludedAddrs, addr)
}
}
return matchedAddrs, excludedAddrs, nil
} | go | func IfByAddress(inputRe string, ifAddrs IfAddrs) (matched, remainder IfAddrs, err error) {
re, err := regexp.Compile(inputRe)
if err != nil {
return nil, nil, fmt.Errorf("Unable to compile address regexp %+q: %v", inputRe, err)
}
matchedAddrs := make(IfAddrs, 0, len(ifAddrs))
excludedAddrs := make(IfAddrs, 0, len(ifAddrs))
for _, addr := range ifAddrs {
if re.MatchString(addr.SockAddr.String()) {
matchedAddrs = append(matchedAddrs, addr)
} else {
excludedAddrs = append(excludedAddrs, addr)
}
}
return matchedAddrs, excludedAddrs, nil
} | [
"func",
"IfByAddress",
"(",
"inputRe",
"string",
",",
"ifAddrs",
"IfAddrs",
")",
"(",
"matched",
",",
"remainder",
"IfAddrs",
",",
"err",
"error",
")",
"{",
"re",
",",
"err",
":=",
"regexp",
".",
"Compile",
"(",
"inputRe",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"inputRe",
",",
"err",
")",
"\n",
"}",
"\n\n",
"matchedAddrs",
":=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ifAddrs",
")",
")",
"\n",
"excludedAddrs",
":=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ifAddrs",
")",
")",
"\n",
"for",
"_",
",",
"addr",
":=",
"range",
"ifAddrs",
"{",
"if",
"re",
".",
"MatchString",
"(",
"addr",
".",
"SockAddr",
".",
"String",
"(",
")",
")",
"{",
"matchedAddrs",
"=",
"append",
"(",
"matchedAddrs",
",",
"addr",
")",
"\n",
"}",
"else",
"{",
"excludedAddrs",
"=",
"append",
"(",
"excludedAddrs",
",",
"addr",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"matchedAddrs",
",",
"excludedAddrs",
",",
"nil",
"\n",
"}"
] | // IfByAddress returns a list of matched and non-matched IfAddrs, or an error if
// the regexp fails to compile. | [
"IfByAddress",
"returns",
"a",
"list",
"of",
"matched",
"and",
"non",
"-",
"matched",
"IfAddrs",
"or",
"an",
"error",
"if",
"the",
"regexp",
"fails",
"to",
"compile",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L395-L412 | train |
hashicorp/go-sockaddr | ifaddrs.go | IfByPort | func IfByPort(inputRe string, ifAddrs IfAddrs) (matchedIfs, excludedIfs IfAddrs, err error) {
re, err := regexp.Compile(inputRe)
if err != nil {
return nil, nil, fmt.Errorf("Unable to compile port regexp %+q: %v", inputRe, err)
}
ipIfs, nonIfs := FilterIfByType(ifAddrs, TypeIP)
matchedIfs = make(IfAddrs, 0, len(ipIfs))
excludedIfs = append(IfAddrs(nil), nonIfs...)
for _, addr := range ipIfs {
ipAddr := ToIPAddr(addr.SockAddr)
if ipAddr == nil {
continue
}
port := strconv.FormatInt(int64((*ipAddr).IPPort()), 10)
if re.MatchString(port) {
matchedIfs = append(matchedIfs, addr)
} else {
excludedIfs = append(excludedIfs, addr)
}
}
return matchedIfs, excludedIfs, nil
} | go | func IfByPort(inputRe string, ifAddrs IfAddrs) (matchedIfs, excludedIfs IfAddrs, err error) {
re, err := regexp.Compile(inputRe)
if err != nil {
return nil, nil, fmt.Errorf("Unable to compile port regexp %+q: %v", inputRe, err)
}
ipIfs, nonIfs := FilterIfByType(ifAddrs, TypeIP)
matchedIfs = make(IfAddrs, 0, len(ipIfs))
excludedIfs = append(IfAddrs(nil), nonIfs...)
for _, addr := range ipIfs {
ipAddr := ToIPAddr(addr.SockAddr)
if ipAddr == nil {
continue
}
port := strconv.FormatInt(int64((*ipAddr).IPPort()), 10)
if re.MatchString(port) {
matchedIfs = append(matchedIfs, addr)
} else {
excludedIfs = append(excludedIfs, addr)
}
}
return matchedIfs, excludedIfs, nil
} | [
"func",
"IfByPort",
"(",
"inputRe",
"string",
",",
"ifAddrs",
"IfAddrs",
")",
"(",
"matchedIfs",
",",
"excludedIfs",
"IfAddrs",
",",
"err",
"error",
")",
"{",
"re",
",",
"err",
":=",
"regexp",
".",
"Compile",
"(",
"inputRe",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"inputRe",
",",
"err",
")",
"\n",
"}",
"\n\n",
"ipIfs",
",",
"nonIfs",
":=",
"FilterIfByType",
"(",
"ifAddrs",
",",
"TypeIP",
")",
"\n",
"matchedIfs",
"=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ipIfs",
")",
")",
"\n",
"excludedIfs",
"=",
"append",
"(",
"IfAddrs",
"(",
"nil",
")",
",",
"nonIfs",
"...",
")",
"\n",
"for",
"_",
",",
"addr",
":=",
"range",
"ipIfs",
"{",
"ipAddr",
":=",
"ToIPAddr",
"(",
"addr",
".",
"SockAddr",
")",
"\n",
"if",
"ipAddr",
"==",
"nil",
"{",
"continue",
"\n",
"}",
"\n\n",
"port",
":=",
"strconv",
".",
"FormatInt",
"(",
"int64",
"(",
"(",
"*",
"ipAddr",
")",
".",
"IPPort",
"(",
")",
")",
",",
"10",
")",
"\n",
"if",
"re",
".",
"MatchString",
"(",
"port",
")",
"{",
"matchedIfs",
"=",
"append",
"(",
"matchedIfs",
",",
"addr",
")",
"\n",
"}",
"else",
"{",
"excludedIfs",
"=",
"append",
"(",
"excludedIfs",
",",
"addr",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"matchedIfs",
",",
"excludedIfs",
",",
"nil",
"\n",
"}"
] | // IfByPort returns a list of matched and non-matched IfAddrs, or an error if
// the regexp fails to compile. | [
"IfByPort",
"returns",
"a",
"list",
"of",
"matched",
"and",
"non",
"-",
"matched",
"IfAddrs",
"or",
"an",
"error",
"if",
"the",
"regexp",
"fails",
"to",
"compile",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L437-L461 | train |
hashicorp/go-sockaddr | ifaddrs.go | IfByRFC | func IfByRFC(selectorParam string, ifAddrs IfAddrs) (matched, remainder IfAddrs, err error) {
inputRFC, err := strconv.ParseUint(selectorParam, 10, 64)
if err != nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("unable to parse RFC number %q: %v", selectorParam, err)
}
matchedIfAddrs := make(IfAddrs, 0, len(ifAddrs))
remainingIfAddrs := make(IfAddrs, 0, len(ifAddrs))
rfcNetMap := KnownRFCs()
rfcNets, ok := rfcNetMap[uint(inputRFC)]
if !ok {
return nil, nil, fmt.Errorf("unsupported RFC %d", inputRFC)
}
for _, ifAddr := range ifAddrs {
var contained bool
for _, rfcNet := range rfcNets {
if rfcNet.Contains(ifAddr.SockAddr) {
matchedIfAddrs = append(matchedIfAddrs, ifAddr)
contained = true
break
}
}
if !contained {
remainingIfAddrs = append(remainingIfAddrs, ifAddr)
}
}
return matchedIfAddrs, remainingIfAddrs, nil
} | go | func IfByRFC(selectorParam string, ifAddrs IfAddrs) (matched, remainder IfAddrs, err error) {
inputRFC, err := strconv.ParseUint(selectorParam, 10, 64)
if err != nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("unable to parse RFC number %q: %v", selectorParam, err)
}
matchedIfAddrs := make(IfAddrs, 0, len(ifAddrs))
remainingIfAddrs := make(IfAddrs, 0, len(ifAddrs))
rfcNetMap := KnownRFCs()
rfcNets, ok := rfcNetMap[uint(inputRFC)]
if !ok {
return nil, nil, fmt.Errorf("unsupported RFC %d", inputRFC)
}
for _, ifAddr := range ifAddrs {
var contained bool
for _, rfcNet := range rfcNets {
if rfcNet.Contains(ifAddr.SockAddr) {
matchedIfAddrs = append(matchedIfAddrs, ifAddr)
contained = true
break
}
}
if !contained {
remainingIfAddrs = append(remainingIfAddrs, ifAddr)
}
}
return matchedIfAddrs, remainingIfAddrs, nil
} | [
"func",
"IfByRFC",
"(",
"selectorParam",
"string",
",",
"ifAddrs",
"IfAddrs",
")",
"(",
"matched",
",",
"remainder",
"IfAddrs",
",",
"err",
"error",
")",
"{",
"inputRFC",
",",
"err",
":=",
"strconv",
".",
"ParseUint",
"(",
"selectorParam",
",",
"10",
",",
"64",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"selectorParam",
",",
"err",
")",
"\n",
"}",
"\n\n",
"matchedIfAddrs",
":=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ifAddrs",
")",
")",
"\n",
"remainingIfAddrs",
":=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ifAddrs",
")",
")",
"\n\n",
"rfcNetMap",
":=",
"KnownRFCs",
"(",
")",
"\n",
"rfcNets",
",",
"ok",
":=",
"rfcNetMap",
"[",
"uint",
"(",
"inputRFC",
")",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"inputRFC",
")",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"ifAddr",
":=",
"range",
"ifAddrs",
"{",
"var",
"contained",
"bool",
"\n",
"for",
"_",
",",
"rfcNet",
":=",
"range",
"rfcNets",
"{",
"if",
"rfcNet",
".",
"Contains",
"(",
"ifAddr",
".",
"SockAddr",
")",
"{",
"matchedIfAddrs",
"=",
"append",
"(",
"matchedIfAddrs",
",",
"ifAddr",
")",
"\n",
"contained",
"=",
"true",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"!",
"contained",
"{",
"remainingIfAddrs",
"=",
"append",
"(",
"remainingIfAddrs",
",",
"ifAddr",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"matchedIfAddrs",
",",
"remainingIfAddrs",
",",
"nil",
"\n",
"}"
] | // IfByRFC returns a list of matched and non-matched IfAddrs that contain the
// relevant RFC-specified traits. | [
"IfByRFC",
"returns",
"a",
"list",
"of",
"matched",
"and",
"non",
"-",
"matched",
"IfAddrs",
"that",
"contain",
"the",
"relevant",
"RFC",
"-",
"specified",
"traits",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L465-L495 | train |
hashicorp/go-sockaddr | ifaddrs.go | IfByRFCs | func IfByRFCs(selectorParam string, ifAddrs IfAddrs) (matched, remainder IfAddrs, err error) {
var includedIfs, excludedIfs IfAddrs
for _, rfcStr := range strings.Split(selectorParam, "|") {
includedRFCIfs, excludedRFCIfs, err := IfByRFC(rfcStr, ifAddrs)
if err != nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("unable to lookup RFC number %q: %v", rfcStr, err)
}
includedIfs = append(includedIfs, includedRFCIfs...)
excludedIfs = append(excludedIfs, excludedRFCIfs...)
}
return includedIfs, excludedIfs, nil
} | go | func IfByRFCs(selectorParam string, ifAddrs IfAddrs) (matched, remainder IfAddrs, err error) {
var includedIfs, excludedIfs IfAddrs
for _, rfcStr := range strings.Split(selectorParam, "|") {
includedRFCIfs, excludedRFCIfs, err := IfByRFC(rfcStr, ifAddrs)
if err != nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("unable to lookup RFC number %q: %v", rfcStr, err)
}
includedIfs = append(includedIfs, includedRFCIfs...)
excludedIfs = append(excludedIfs, excludedRFCIfs...)
}
return includedIfs, excludedIfs, nil
} | [
"func",
"IfByRFCs",
"(",
"selectorParam",
"string",
",",
"ifAddrs",
"IfAddrs",
")",
"(",
"matched",
",",
"remainder",
"IfAddrs",
",",
"err",
"error",
")",
"{",
"var",
"includedIfs",
",",
"excludedIfs",
"IfAddrs",
"\n",
"for",
"_",
",",
"rfcStr",
":=",
"range",
"strings",
".",
"Split",
"(",
"selectorParam",
",",
"\"",
"\"",
")",
"{",
"includedRFCIfs",
",",
"excludedRFCIfs",
",",
"err",
":=",
"IfByRFC",
"(",
"rfcStr",
",",
"ifAddrs",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"rfcStr",
",",
"err",
")",
"\n",
"}",
"\n",
"includedIfs",
"=",
"append",
"(",
"includedIfs",
",",
"includedRFCIfs",
"...",
")",
"\n",
"excludedIfs",
"=",
"append",
"(",
"excludedIfs",
",",
"excludedRFCIfs",
"...",
")",
"\n",
"}",
"\n\n",
"return",
"includedIfs",
",",
"excludedIfs",
",",
"nil",
"\n",
"}"
] | // IfByRFCs returns a list of matched and non-matched IfAddrs that contain the
// relevant RFC-specified traits. Multiple RFCs can be specified and separated
// by the `|` symbol. No protection is taken to ensure an IfAddr does not end
// up in both the included and excluded list. | [
"IfByRFCs",
"returns",
"a",
"list",
"of",
"matched",
"and",
"non",
"-",
"matched",
"IfAddrs",
"that",
"contain",
"the",
"relevant",
"RFC",
"-",
"specified",
"traits",
".",
"Multiple",
"RFCs",
"can",
"be",
"specified",
"and",
"separated",
"by",
"the",
"|",
"symbol",
".",
"No",
"protection",
"is",
"taken",
"to",
"ensure",
"an",
"IfAddr",
"does",
"not",
"end",
"up",
"in",
"both",
"the",
"included",
"and",
"excluded",
"list",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L501-L513 | train |
hashicorp/go-sockaddr | ifaddrs.go | IfByMaskSize | func IfByMaskSize(selectorParam string, ifAddrs IfAddrs) (matchedIfs, excludedIfs IfAddrs, err error) {
maskSize, err := strconv.ParseUint(selectorParam, 10, 64)
if err != nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("invalid exclude size argument (%q): %v", selectorParam, err)
}
ipIfs, nonIfs := FilterIfByType(ifAddrs, TypeIP)
matchedIfs = make(IfAddrs, 0, len(ipIfs))
excludedIfs = append(IfAddrs(nil), nonIfs...)
for _, addr := range ipIfs {
ipAddr := ToIPAddr(addr.SockAddr)
if ipAddr == nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("unable to filter mask sizes on non-IP type %s: %v", addr.SockAddr.Type().String(), addr.SockAddr.String())
}
switch {
case (*ipAddr).Type()&TypeIPv4 != 0 && maskSize > 32:
return IfAddrs{}, IfAddrs{}, fmt.Errorf("mask size out of bounds for IPv4 address: %d", maskSize)
case (*ipAddr).Type()&TypeIPv6 != 0 && maskSize > 128:
return IfAddrs{}, IfAddrs{}, fmt.Errorf("mask size out of bounds for IPv6 address: %d", maskSize)
}
if (*ipAddr).Maskbits() == int(maskSize) {
matchedIfs = append(matchedIfs, addr)
} else {
excludedIfs = append(excludedIfs, addr)
}
}
return matchedIfs, excludedIfs, nil
} | go | func IfByMaskSize(selectorParam string, ifAddrs IfAddrs) (matchedIfs, excludedIfs IfAddrs, err error) {
maskSize, err := strconv.ParseUint(selectorParam, 10, 64)
if err != nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("invalid exclude size argument (%q): %v", selectorParam, err)
}
ipIfs, nonIfs := FilterIfByType(ifAddrs, TypeIP)
matchedIfs = make(IfAddrs, 0, len(ipIfs))
excludedIfs = append(IfAddrs(nil), nonIfs...)
for _, addr := range ipIfs {
ipAddr := ToIPAddr(addr.SockAddr)
if ipAddr == nil {
return IfAddrs{}, IfAddrs{}, fmt.Errorf("unable to filter mask sizes on non-IP type %s: %v", addr.SockAddr.Type().String(), addr.SockAddr.String())
}
switch {
case (*ipAddr).Type()&TypeIPv4 != 0 && maskSize > 32:
return IfAddrs{}, IfAddrs{}, fmt.Errorf("mask size out of bounds for IPv4 address: %d", maskSize)
case (*ipAddr).Type()&TypeIPv6 != 0 && maskSize > 128:
return IfAddrs{}, IfAddrs{}, fmt.Errorf("mask size out of bounds for IPv6 address: %d", maskSize)
}
if (*ipAddr).Maskbits() == int(maskSize) {
matchedIfs = append(matchedIfs, addr)
} else {
excludedIfs = append(excludedIfs, addr)
}
}
return matchedIfs, excludedIfs, nil
} | [
"func",
"IfByMaskSize",
"(",
"selectorParam",
"string",
",",
"ifAddrs",
"IfAddrs",
")",
"(",
"matchedIfs",
",",
"excludedIfs",
"IfAddrs",
",",
"err",
"error",
")",
"{",
"maskSize",
",",
"err",
":=",
"strconv",
".",
"ParseUint",
"(",
"selectorParam",
",",
"10",
",",
"64",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"selectorParam",
",",
"err",
")",
"\n",
"}",
"\n\n",
"ipIfs",
",",
"nonIfs",
":=",
"FilterIfByType",
"(",
"ifAddrs",
",",
"TypeIP",
")",
"\n",
"matchedIfs",
"=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"ipIfs",
")",
")",
"\n",
"excludedIfs",
"=",
"append",
"(",
"IfAddrs",
"(",
"nil",
")",
",",
"nonIfs",
"...",
")",
"\n",
"for",
"_",
",",
"addr",
":=",
"range",
"ipIfs",
"{",
"ipAddr",
":=",
"ToIPAddr",
"(",
"addr",
".",
"SockAddr",
")",
"\n",
"if",
"ipAddr",
"==",
"nil",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"addr",
".",
"SockAddr",
".",
"Type",
"(",
")",
".",
"String",
"(",
")",
",",
"addr",
".",
"SockAddr",
".",
"String",
"(",
")",
")",
"\n",
"}",
"\n\n",
"switch",
"{",
"case",
"(",
"*",
"ipAddr",
")",
".",
"Type",
"(",
")",
"&",
"TypeIPv4",
"!=",
"0",
"&&",
"maskSize",
">",
"32",
":",
"return",
"IfAddrs",
"{",
"}",
",",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"maskSize",
")",
"\n",
"case",
"(",
"*",
"ipAddr",
")",
".",
"Type",
"(",
")",
"&",
"TypeIPv6",
"!=",
"0",
"&&",
"maskSize",
">",
"128",
":",
"return",
"IfAddrs",
"{",
"}",
",",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"maskSize",
")",
"\n",
"}",
"\n\n",
"if",
"(",
"*",
"ipAddr",
")",
".",
"Maskbits",
"(",
")",
"==",
"int",
"(",
"maskSize",
")",
"{",
"matchedIfs",
"=",
"append",
"(",
"matchedIfs",
",",
"addr",
")",
"\n",
"}",
"else",
"{",
"excludedIfs",
"=",
"append",
"(",
"excludedIfs",
",",
"addr",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"matchedIfs",
",",
"excludedIfs",
",",
"nil",
"\n",
"}"
] | // IfByMaskSize returns a list of matched and non-matched IfAddrs that have the
// matching mask size. | [
"IfByMaskSize",
"returns",
"a",
"list",
"of",
"matched",
"and",
"non",
"-",
"matched",
"IfAddrs",
"that",
"have",
"the",
"matching",
"mask",
"size",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L517-L547 | train |
hashicorp/go-sockaddr | ifaddrs.go | IfByNetwork | func IfByNetwork(selectorParam string, inputIfAddrs IfAddrs) (IfAddrs, IfAddrs, error) {
var includedIfs, excludedIfs IfAddrs
for _, netStr := range strings.Split(selectorParam, "|") {
netAddr, err := NewIPAddr(netStr)
if err != nil {
return nil, nil, fmt.Errorf("unable to create an IP address from %+q: %v", netStr, err)
}
for _, ifAddr := range inputIfAddrs {
if netAddr.Contains(ifAddr.SockAddr) {
includedIfs = append(includedIfs, ifAddr)
} else {
excludedIfs = append(excludedIfs, ifAddr)
}
}
}
return includedIfs, excludedIfs, nil
} | go | func IfByNetwork(selectorParam string, inputIfAddrs IfAddrs) (IfAddrs, IfAddrs, error) {
var includedIfs, excludedIfs IfAddrs
for _, netStr := range strings.Split(selectorParam, "|") {
netAddr, err := NewIPAddr(netStr)
if err != nil {
return nil, nil, fmt.Errorf("unable to create an IP address from %+q: %v", netStr, err)
}
for _, ifAddr := range inputIfAddrs {
if netAddr.Contains(ifAddr.SockAddr) {
includedIfs = append(includedIfs, ifAddr)
} else {
excludedIfs = append(excludedIfs, ifAddr)
}
}
}
return includedIfs, excludedIfs, nil
} | [
"func",
"IfByNetwork",
"(",
"selectorParam",
"string",
",",
"inputIfAddrs",
"IfAddrs",
")",
"(",
"IfAddrs",
",",
"IfAddrs",
",",
"error",
")",
"{",
"var",
"includedIfs",
",",
"excludedIfs",
"IfAddrs",
"\n",
"for",
"_",
",",
"netStr",
":=",
"range",
"strings",
".",
"Split",
"(",
"selectorParam",
",",
"\"",
"\"",
")",
"{",
"netAddr",
",",
"err",
":=",
"NewIPAddr",
"(",
"netStr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"netStr",
",",
"err",
")",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"ifAddr",
":=",
"range",
"inputIfAddrs",
"{",
"if",
"netAddr",
".",
"Contains",
"(",
"ifAddr",
".",
"SockAddr",
")",
"{",
"includedIfs",
"=",
"append",
"(",
"includedIfs",
",",
"ifAddr",
")",
"\n",
"}",
"else",
"{",
"excludedIfs",
"=",
"append",
"(",
"excludedIfs",
",",
"ifAddr",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"includedIfs",
",",
"excludedIfs",
",",
"nil",
"\n",
"}"
] | // IfByNetwork returns an IfAddrs that are equal to or included within the
// network passed in by selector. | [
"IfByNetwork",
"returns",
"an",
"IfAddrs",
"that",
"are",
"equal",
"to",
"or",
"included",
"within",
"the",
"network",
"passed",
"in",
"by",
"selector",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L704-L722 | train |
hashicorp/go-sockaddr | ifaddrs.go | IfAddrsMath | func IfAddrsMath(operation, value string, inputIfAddrs IfAddrs) (IfAddrs, error) {
outputAddrs := make(IfAddrs, 0, len(inputIfAddrs))
for _, ifAddr := range inputIfAddrs {
result, err := IfAddrMath(operation, value, ifAddr)
if err != nil {
return IfAddrs{}, fmt.Errorf("unable to perform an IPMath operation on %s: %v", ifAddr, err)
}
outputAddrs = append(outputAddrs, result)
}
return outputAddrs, nil
} | go | func IfAddrsMath(operation, value string, inputIfAddrs IfAddrs) (IfAddrs, error) {
outputAddrs := make(IfAddrs, 0, len(inputIfAddrs))
for _, ifAddr := range inputIfAddrs {
result, err := IfAddrMath(operation, value, ifAddr)
if err != nil {
return IfAddrs{}, fmt.Errorf("unable to perform an IPMath operation on %s: %v", ifAddr, err)
}
outputAddrs = append(outputAddrs, result)
}
return outputAddrs, nil
} | [
"func",
"IfAddrsMath",
"(",
"operation",
",",
"value",
"string",
",",
"inputIfAddrs",
"IfAddrs",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"outputAddrs",
":=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"inputIfAddrs",
")",
")",
"\n",
"for",
"_",
",",
"ifAddr",
":=",
"range",
"inputIfAddrs",
"{",
"result",
",",
"err",
":=",
"IfAddrMath",
"(",
"operation",
",",
"value",
",",
"ifAddr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"ifAddr",
",",
"err",
")",
"\n",
"}",
"\n",
"outputAddrs",
"=",
"append",
"(",
"outputAddrs",
",",
"result",
")",
"\n",
"}",
"\n",
"return",
"outputAddrs",
",",
"nil",
"\n",
"}"
] | // IfAddrsMath will apply an IfAddrMath operation each IfAddr struct. Any
// failure will result in zero results. | [
"IfAddrsMath",
"will",
"apply",
"an",
"IfAddrMath",
"operation",
"each",
"IfAddr",
"struct",
".",
"Any",
"failure",
"will",
"result",
"in",
"zero",
"results",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L951-L961 | train |
hashicorp/go-sockaddr | ifaddrs.go | ExcludeIfs | func ExcludeIfs(selectorName, selectorParam string, inputIfAddrs IfAddrs) (IfAddrs, error) {
var excludedIfs IfAddrs
var err error
switch strings.ToLower(selectorName) {
case "address":
_, excludedIfs, err = IfByAddress(selectorParam, inputIfAddrs)
case "flag", "flags":
_, excludedIfs, err = IfByFlag(selectorParam, inputIfAddrs)
case "name":
_, excludedIfs, err = IfByName(selectorParam, inputIfAddrs)
case "network":
_, excludedIfs, err = IfByNetwork(selectorParam, inputIfAddrs)
case "port":
_, excludedIfs, err = IfByPort(selectorParam, inputIfAddrs)
case "rfc", "rfcs":
_, excludedIfs, err = IfByRFCs(selectorParam, inputIfAddrs)
case "size":
_, excludedIfs, err = IfByMaskSize(selectorParam, inputIfAddrs)
case "type":
_, excludedIfs, err = IfByType(selectorParam, inputIfAddrs)
default:
return IfAddrs{}, fmt.Errorf("invalid exclude selector %q", selectorName)
}
if err != nil {
return IfAddrs{}, err
}
return excludedIfs, nil
} | go | func ExcludeIfs(selectorName, selectorParam string, inputIfAddrs IfAddrs) (IfAddrs, error) {
var excludedIfs IfAddrs
var err error
switch strings.ToLower(selectorName) {
case "address":
_, excludedIfs, err = IfByAddress(selectorParam, inputIfAddrs)
case "flag", "flags":
_, excludedIfs, err = IfByFlag(selectorParam, inputIfAddrs)
case "name":
_, excludedIfs, err = IfByName(selectorParam, inputIfAddrs)
case "network":
_, excludedIfs, err = IfByNetwork(selectorParam, inputIfAddrs)
case "port":
_, excludedIfs, err = IfByPort(selectorParam, inputIfAddrs)
case "rfc", "rfcs":
_, excludedIfs, err = IfByRFCs(selectorParam, inputIfAddrs)
case "size":
_, excludedIfs, err = IfByMaskSize(selectorParam, inputIfAddrs)
case "type":
_, excludedIfs, err = IfByType(selectorParam, inputIfAddrs)
default:
return IfAddrs{}, fmt.Errorf("invalid exclude selector %q", selectorName)
}
if err != nil {
return IfAddrs{}, err
}
return excludedIfs, nil
} | [
"func",
"ExcludeIfs",
"(",
"selectorName",
",",
"selectorParam",
"string",
",",
"inputIfAddrs",
"IfAddrs",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"var",
"excludedIfs",
"IfAddrs",
"\n",
"var",
"err",
"error",
"\n\n",
"switch",
"strings",
".",
"ToLower",
"(",
"selectorName",
")",
"{",
"case",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByAddress",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByFlag",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"case",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByName",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"case",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByNetwork",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"case",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByPort",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByRFCs",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"case",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByMaskSize",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"case",
"\"",
"\"",
":",
"_",
",",
"excludedIfs",
",",
"err",
"=",
"IfByType",
"(",
"selectorParam",
",",
"inputIfAddrs",
")",
"\n",
"default",
":",
"return",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"selectorName",
")",
"\n",
"}",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"excludedIfs",
",",
"nil",
"\n",
"}"
] | // ExcludeIfs returns an IfAddrs based on the passed in selector. | [
"ExcludeIfs",
"returns",
"an",
"IfAddrs",
"based",
"on",
"the",
"passed",
"in",
"selector",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L997-L1027 | train |
hashicorp/go-sockaddr | ifaddrs.go | SortIfBy | func SortIfBy(selectorParam string, inputIfAddrs IfAddrs) (IfAddrs, error) {
sortedIfs := append(IfAddrs(nil), inputIfAddrs...)
clauses := strings.Split(selectorParam, ",")
sortFuncs := make([]CmpIfAddrFunc, len(clauses))
for i, clause := range clauses {
switch strings.TrimSpace(strings.ToLower(clause)) {
case "+address", "address":
// The "address" selector returns an array of IfAddrs
// ordered by the network address. IfAddrs that are not
// comparable will be at the end of the list and in a
// non-deterministic order.
sortFuncs[i] = AscIfAddress
case "-address":
sortFuncs[i] = DescIfAddress
case "+default", "default":
sortFuncs[i] = AscIfDefault
case "-default":
sortFuncs[i] = DescIfDefault
case "+name", "name":
// The "name" selector returns an array of IfAddrs
// ordered by the interface name.
sortFuncs[i] = AscIfName
case "-name":
sortFuncs[i] = DescIfName
case "+port", "port":
// The "port" selector returns an array of IfAddrs
// ordered by the port, if included in the IfAddr.
// IfAddrs that are not comparable will be at the end of
// the list and in a non-deterministic order.
sortFuncs[i] = AscIfPort
case "-port":
sortFuncs[i] = DescIfPort
case "+private", "private":
// The "private" selector returns an array of IfAddrs
// ordered by private addresses first. IfAddrs that are
// not comparable will be at the end of the list and in
// a non-deterministic order.
sortFuncs[i] = AscIfPrivate
case "-private":
sortFuncs[i] = DescIfPrivate
case "+size", "size":
// The "size" selector returns an array of IfAddrs
// ordered by the size of the network mask, smaller mask
// (larger number of hosts per network) to largest
// (e.g. a /24 sorts before a /32).
sortFuncs[i] = AscIfNetworkSize
case "-size":
sortFuncs[i] = DescIfNetworkSize
case "+type", "type":
// The "type" selector returns an array of IfAddrs
// ordered by the type of the IfAddr. The sort order is
// Unix, IPv4, then IPv6.
sortFuncs[i] = AscIfType
case "-type":
sortFuncs[i] = DescIfType
default:
// Return an empty list for invalid sort types.
return IfAddrs{}, fmt.Errorf("unknown sort type: %q", clause)
}
}
OrderedIfAddrBy(sortFuncs...).Sort(sortedIfs)
return sortedIfs, nil
} | go | func SortIfBy(selectorParam string, inputIfAddrs IfAddrs) (IfAddrs, error) {
sortedIfs := append(IfAddrs(nil), inputIfAddrs...)
clauses := strings.Split(selectorParam, ",")
sortFuncs := make([]CmpIfAddrFunc, len(clauses))
for i, clause := range clauses {
switch strings.TrimSpace(strings.ToLower(clause)) {
case "+address", "address":
// The "address" selector returns an array of IfAddrs
// ordered by the network address. IfAddrs that are not
// comparable will be at the end of the list and in a
// non-deterministic order.
sortFuncs[i] = AscIfAddress
case "-address":
sortFuncs[i] = DescIfAddress
case "+default", "default":
sortFuncs[i] = AscIfDefault
case "-default":
sortFuncs[i] = DescIfDefault
case "+name", "name":
// The "name" selector returns an array of IfAddrs
// ordered by the interface name.
sortFuncs[i] = AscIfName
case "-name":
sortFuncs[i] = DescIfName
case "+port", "port":
// The "port" selector returns an array of IfAddrs
// ordered by the port, if included in the IfAddr.
// IfAddrs that are not comparable will be at the end of
// the list and in a non-deterministic order.
sortFuncs[i] = AscIfPort
case "-port":
sortFuncs[i] = DescIfPort
case "+private", "private":
// The "private" selector returns an array of IfAddrs
// ordered by private addresses first. IfAddrs that are
// not comparable will be at the end of the list and in
// a non-deterministic order.
sortFuncs[i] = AscIfPrivate
case "-private":
sortFuncs[i] = DescIfPrivate
case "+size", "size":
// The "size" selector returns an array of IfAddrs
// ordered by the size of the network mask, smaller mask
// (larger number of hosts per network) to largest
// (e.g. a /24 sorts before a /32).
sortFuncs[i] = AscIfNetworkSize
case "-size":
sortFuncs[i] = DescIfNetworkSize
case "+type", "type":
// The "type" selector returns an array of IfAddrs
// ordered by the type of the IfAddr. The sort order is
// Unix, IPv4, then IPv6.
sortFuncs[i] = AscIfType
case "-type":
sortFuncs[i] = DescIfType
default:
// Return an empty list for invalid sort types.
return IfAddrs{}, fmt.Errorf("unknown sort type: %q", clause)
}
}
OrderedIfAddrBy(sortFuncs...).Sort(sortedIfs)
return sortedIfs, nil
} | [
"func",
"SortIfBy",
"(",
"selectorParam",
"string",
",",
"inputIfAddrs",
"IfAddrs",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"sortedIfs",
":=",
"append",
"(",
"IfAddrs",
"(",
"nil",
")",
",",
"inputIfAddrs",
"...",
")",
"\n\n",
"clauses",
":=",
"strings",
".",
"Split",
"(",
"selectorParam",
",",
"\"",
"\"",
")",
"\n",
"sortFuncs",
":=",
"make",
"(",
"[",
"]",
"CmpIfAddrFunc",
",",
"len",
"(",
"clauses",
")",
")",
"\n\n",
"for",
"i",
",",
"clause",
":=",
"range",
"clauses",
"{",
"switch",
"strings",
".",
"TrimSpace",
"(",
"strings",
".",
"ToLower",
"(",
"clause",
")",
")",
"{",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// The \"address\" selector returns an array of IfAddrs",
"// ordered by the network address. IfAddrs that are not",
"// comparable will be at the end of the list and in a",
"// non-deterministic order.",
"sortFuncs",
"[",
"i",
"]",
"=",
"AscIfAddress",
"\n",
"case",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"DescIfAddress",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"AscIfDefault",
"\n",
"case",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"DescIfDefault",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// The \"name\" selector returns an array of IfAddrs",
"// ordered by the interface name.",
"sortFuncs",
"[",
"i",
"]",
"=",
"AscIfName",
"\n",
"case",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"DescIfName",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// The \"port\" selector returns an array of IfAddrs",
"// ordered by the port, if included in the IfAddr.",
"// IfAddrs that are not comparable will be at the end of",
"// the list and in a non-deterministic order.",
"sortFuncs",
"[",
"i",
"]",
"=",
"AscIfPort",
"\n",
"case",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"DescIfPort",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// The \"private\" selector returns an array of IfAddrs",
"// ordered by private addresses first. IfAddrs that are",
"// not comparable will be at the end of the list and in",
"// a non-deterministic order.",
"sortFuncs",
"[",
"i",
"]",
"=",
"AscIfPrivate",
"\n",
"case",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"DescIfPrivate",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// The \"size\" selector returns an array of IfAddrs",
"// ordered by the size of the network mask, smaller mask",
"// (larger number of hosts per network) to largest",
"// (e.g. a /24 sorts before a /32).",
"sortFuncs",
"[",
"i",
"]",
"=",
"AscIfNetworkSize",
"\n",
"case",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"DescIfNetworkSize",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// The \"type\" selector returns an array of IfAddrs",
"// ordered by the type of the IfAddr. The sort order is",
"// Unix, IPv4, then IPv6.",
"sortFuncs",
"[",
"i",
"]",
"=",
"AscIfType",
"\n",
"case",
"\"",
"\"",
":",
"sortFuncs",
"[",
"i",
"]",
"=",
"DescIfType",
"\n",
"default",
":",
"// Return an empty list for invalid sort types.",
"return",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"clause",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"OrderedIfAddrBy",
"(",
"sortFuncs",
"...",
")",
".",
"Sort",
"(",
"sortedIfs",
")",
"\n\n",
"return",
"sortedIfs",
",",
"nil",
"\n",
"}"
] | // SortIfBy returns an IfAddrs sorted based on the passed in selector. Multiple
// sort clauses can be passed in as a comma delimited list without whitespace. | [
"SortIfBy",
"returns",
"an",
"IfAddrs",
"sorted",
"based",
"on",
"the",
"passed",
"in",
"selector",
".",
"Multiple",
"sort",
"clauses",
"can",
"be",
"passed",
"in",
"as",
"a",
"comma",
"delimited",
"list",
"without",
"whitespace",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L1031-L1097 | train |
hashicorp/go-sockaddr | ifaddrs.go | UniqueIfAddrsBy | func UniqueIfAddrsBy(selectorName string, inputIfAddrs IfAddrs) (IfAddrs, error) {
attrName := strings.ToLower(selectorName)
ifs := make(IfAddrs, 0, len(inputIfAddrs))
var lastMatch string
for _, ifAddr := range inputIfAddrs {
var out string
switch attrName {
case "address":
out = ifAddr.SockAddr.String()
case "name":
out = ifAddr.Name
default:
return nil, fmt.Errorf("unsupported unique constraint %+q", selectorName)
}
switch {
case lastMatch == "", lastMatch != out:
lastMatch = out
ifs = append(ifs, ifAddr)
case lastMatch == out:
continue
}
}
return ifs, nil
} | go | func UniqueIfAddrsBy(selectorName string, inputIfAddrs IfAddrs) (IfAddrs, error) {
attrName := strings.ToLower(selectorName)
ifs := make(IfAddrs, 0, len(inputIfAddrs))
var lastMatch string
for _, ifAddr := range inputIfAddrs {
var out string
switch attrName {
case "address":
out = ifAddr.SockAddr.String()
case "name":
out = ifAddr.Name
default:
return nil, fmt.Errorf("unsupported unique constraint %+q", selectorName)
}
switch {
case lastMatch == "", lastMatch != out:
lastMatch = out
ifs = append(ifs, ifAddr)
case lastMatch == out:
continue
}
}
return ifs, nil
} | [
"func",
"UniqueIfAddrsBy",
"(",
"selectorName",
"string",
",",
"inputIfAddrs",
"IfAddrs",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"attrName",
":=",
"strings",
".",
"ToLower",
"(",
"selectorName",
")",
"\n\n",
"ifs",
":=",
"make",
"(",
"IfAddrs",
",",
"0",
",",
"len",
"(",
"inputIfAddrs",
")",
")",
"\n",
"var",
"lastMatch",
"string",
"\n",
"for",
"_",
",",
"ifAddr",
":=",
"range",
"inputIfAddrs",
"{",
"var",
"out",
"string",
"\n",
"switch",
"attrName",
"{",
"case",
"\"",
"\"",
":",
"out",
"=",
"ifAddr",
".",
"SockAddr",
".",
"String",
"(",
")",
"\n",
"case",
"\"",
"\"",
":",
"out",
"=",
"ifAddr",
".",
"Name",
"\n",
"default",
":",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"selectorName",
")",
"\n",
"}",
"\n\n",
"switch",
"{",
"case",
"lastMatch",
"==",
"\"",
"\"",
",",
"lastMatch",
"!=",
"out",
":",
"lastMatch",
"=",
"out",
"\n",
"ifs",
"=",
"append",
"(",
"ifs",
",",
"ifAddr",
")",
"\n",
"case",
"lastMatch",
"==",
"out",
":",
"continue",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"ifs",
",",
"nil",
"\n",
"}"
] | // UniqueIfAddrsBy creates a unique set of IfAddrs based on the matching
// selector. UniqueIfAddrsBy assumes the input has already been sorted. | [
"UniqueIfAddrsBy",
"creates",
"a",
"unique",
"set",
"of",
"IfAddrs",
"based",
"on",
"the",
"matching",
"selector",
".",
"UniqueIfAddrsBy",
"assumes",
"the",
"input",
"has",
"already",
"been",
"sorted",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L1101-L1127 | train |
hashicorp/go-sockaddr | ifaddrs.go | JoinIfAddrs | func JoinIfAddrs(selectorName string, joinStr string, inputIfAddrs IfAddrs) (string, error) {
outputs := make([]string, 0, len(inputIfAddrs))
attrName := AttrName(strings.ToLower(selectorName))
for _, ifAddr := range inputIfAddrs {
var attrVal string
var err error
attrVal, err = ifAddr.Attr(attrName)
if err != nil {
return "", err
}
outputs = append(outputs, attrVal)
}
return strings.Join(outputs, joinStr), nil
} | go | func JoinIfAddrs(selectorName string, joinStr string, inputIfAddrs IfAddrs) (string, error) {
outputs := make([]string, 0, len(inputIfAddrs))
attrName := AttrName(strings.ToLower(selectorName))
for _, ifAddr := range inputIfAddrs {
var attrVal string
var err error
attrVal, err = ifAddr.Attr(attrName)
if err != nil {
return "", err
}
outputs = append(outputs, attrVal)
}
return strings.Join(outputs, joinStr), nil
} | [
"func",
"JoinIfAddrs",
"(",
"selectorName",
"string",
",",
"joinStr",
"string",
",",
"inputIfAddrs",
"IfAddrs",
")",
"(",
"string",
",",
"error",
")",
"{",
"outputs",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"0",
",",
"len",
"(",
"inputIfAddrs",
")",
")",
"\n",
"attrName",
":=",
"AttrName",
"(",
"strings",
".",
"ToLower",
"(",
"selectorName",
")",
")",
"\n\n",
"for",
"_",
",",
"ifAddr",
":=",
"range",
"inputIfAddrs",
"{",
"var",
"attrVal",
"string",
"\n",
"var",
"err",
"error",
"\n",
"attrVal",
",",
"err",
"=",
"ifAddr",
".",
"Attr",
"(",
"attrName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"outputs",
"=",
"append",
"(",
"outputs",
",",
"attrVal",
")",
"\n",
"}",
"\n",
"return",
"strings",
".",
"Join",
"(",
"outputs",
",",
"joinStr",
")",
",",
"nil",
"\n",
"}"
] | // JoinIfAddrs joins an IfAddrs and returns a string | [
"JoinIfAddrs",
"joins",
"an",
"IfAddrs",
"and",
"returns",
"a",
"string"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L1130-L1144 | train |
hashicorp/go-sockaddr | ifaddrs.go | LimitIfAddrs | func LimitIfAddrs(lim uint, in IfAddrs) (IfAddrs, error) {
// Clamp the limit to the length of the array
if int(lim) > len(in) {
lim = uint(len(in))
}
return in[0:lim], nil
} | go | func LimitIfAddrs(lim uint, in IfAddrs) (IfAddrs, error) {
// Clamp the limit to the length of the array
if int(lim) > len(in) {
lim = uint(len(in))
}
return in[0:lim], nil
} | [
"func",
"LimitIfAddrs",
"(",
"lim",
"uint",
",",
"in",
"IfAddrs",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"// Clamp the limit to the length of the array",
"if",
"int",
"(",
"lim",
")",
">",
"len",
"(",
"in",
")",
"{",
"lim",
"=",
"uint",
"(",
"len",
"(",
"in",
")",
")",
"\n",
"}",
"\n\n",
"return",
"in",
"[",
"0",
":",
"lim",
"]",
",",
"nil",
"\n",
"}"
] | // LimitIfAddrs returns a slice of IfAddrs based on the specified limit. | [
"LimitIfAddrs",
"returns",
"a",
"slice",
"of",
"IfAddrs",
"based",
"on",
"the",
"specified",
"limit",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L1147-L1154 | train |
hashicorp/go-sockaddr | ifaddrs.go | OffsetIfAddrs | func OffsetIfAddrs(off int, in IfAddrs) (IfAddrs, error) {
var end bool
if off < 0 {
end = true
off = off * -1
}
if off > len(in) {
return IfAddrs{}, fmt.Errorf("unable to seek past the end of the interface array: offset (%d) exceeds the number of interfaces (%d)", off, len(in))
}
if end {
return in[len(in)-off:], nil
}
return in[off:], nil
} | go | func OffsetIfAddrs(off int, in IfAddrs) (IfAddrs, error) {
var end bool
if off < 0 {
end = true
off = off * -1
}
if off > len(in) {
return IfAddrs{}, fmt.Errorf("unable to seek past the end of the interface array: offset (%d) exceeds the number of interfaces (%d)", off, len(in))
}
if end {
return in[len(in)-off:], nil
}
return in[off:], nil
} | [
"func",
"OffsetIfAddrs",
"(",
"off",
"int",
",",
"in",
"IfAddrs",
")",
"(",
"IfAddrs",
",",
"error",
")",
"{",
"var",
"end",
"bool",
"\n",
"if",
"off",
"<",
"0",
"{",
"end",
"=",
"true",
"\n",
"off",
"=",
"off",
"*",
"-",
"1",
"\n",
"}",
"\n\n",
"if",
"off",
">",
"len",
"(",
"in",
")",
"{",
"return",
"IfAddrs",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"off",
",",
"len",
"(",
"in",
")",
")",
"\n",
"}",
"\n\n",
"if",
"end",
"{",
"return",
"in",
"[",
"len",
"(",
"in",
")",
"-",
"off",
":",
"]",
",",
"nil",
"\n",
"}",
"\n",
"return",
"in",
"[",
"off",
":",
"]",
",",
"nil",
"\n",
"}"
] | // OffsetIfAddrs returns a slice of IfAddrs based on the specified offset. | [
"OffsetIfAddrs",
"returns",
"a",
"slice",
"of",
"IfAddrs",
"based",
"on",
"the",
"specified",
"offset",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L1157-L1172 | train |
hashicorp/go-sockaddr | ifaddrs.go | parseDefaultIfNameWindows | func parseDefaultIfNameWindows(routeOut, ipconfigOut string) (string, error) {
defaultIPAddr, err := parseDefaultIPAddrWindowsRoute(routeOut)
if err != nil {
return "", err
}
ifName, err := parseDefaultIfNameWindowsIPConfig(defaultIPAddr, ipconfigOut)
if err != nil {
return "", err
}
return ifName, nil
} | go | func parseDefaultIfNameWindows(routeOut, ipconfigOut string) (string, error) {
defaultIPAddr, err := parseDefaultIPAddrWindowsRoute(routeOut)
if err != nil {
return "", err
}
ifName, err := parseDefaultIfNameWindowsIPConfig(defaultIPAddr, ipconfigOut)
if err != nil {
return "", err
}
return ifName, nil
} | [
"func",
"parseDefaultIfNameWindows",
"(",
"routeOut",
",",
"ipconfigOut",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"defaultIPAddr",
",",
"err",
":=",
"parseDefaultIPAddrWindowsRoute",
"(",
"routeOut",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"ifName",
",",
"err",
":=",
"parseDefaultIfNameWindowsIPConfig",
"(",
"defaultIPAddr",
",",
"ipconfigOut",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"ifName",
",",
"nil",
"\n",
"}"
] | // parseDefaultIfNameWindows parses the default interface from `netstat -rn` and
// `ipconfig` on Windows. | [
"parseDefaultIfNameWindows",
"parses",
"the",
"default",
"interface",
"from",
"netstat",
"-",
"rn",
"and",
"ipconfig",
"on",
"Windows",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L1244-L1256 | train |
hashicorp/go-sockaddr | ifaddrs.go | parseDefaultIfNameWindowsIPConfig | func parseDefaultIfNameWindowsIPConfig(defaultIPAddr, routeOut string) (string, error) {
lines := strings.Split(routeOut, "\n")
ifNameRe := ifNameRE.Copy()
ipAddrRe := ipAddrRE.Copy()
var ifName string
for _, line := range lines {
switch ifNameMatches := ifNameRe.FindStringSubmatch(line); {
case len(ifNameMatches) > 1:
ifName = ifNameMatches[1]
continue
}
switch ipAddrMatches := ipAddrRe.FindStringSubmatch(line); {
case len(ipAddrMatches) > 1 && ipAddrMatches[1] == defaultIPAddr:
return ifName, nil
}
}
return "", errors.New("No default interface found with matching IP")
} | go | func parseDefaultIfNameWindowsIPConfig(defaultIPAddr, routeOut string) (string, error) {
lines := strings.Split(routeOut, "\n")
ifNameRe := ifNameRE.Copy()
ipAddrRe := ipAddrRE.Copy()
var ifName string
for _, line := range lines {
switch ifNameMatches := ifNameRe.FindStringSubmatch(line); {
case len(ifNameMatches) > 1:
ifName = ifNameMatches[1]
continue
}
switch ipAddrMatches := ipAddrRe.FindStringSubmatch(line); {
case len(ipAddrMatches) > 1 && ipAddrMatches[1] == defaultIPAddr:
return ifName, nil
}
}
return "", errors.New("No default interface found with matching IP")
} | [
"func",
"parseDefaultIfNameWindowsIPConfig",
"(",
"defaultIPAddr",
",",
"routeOut",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"lines",
":=",
"strings",
".",
"Split",
"(",
"routeOut",
",",
"\"",
"\\n",
"\"",
")",
"\n",
"ifNameRe",
":=",
"ifNameRE",
".",
"Copy",
"(",
")",
"\n",
"ipAddrRe",
":=",
"ipAddrRE",
".",
"Copy",
"(",
")",
"\n",
"var",
"ifName",
"string",
"\n",
"for",
"_",
",",
"line",
":=",
"range",
"lines",
"{",
"switch",
"ifNameMatches",
":=",
"ifNameRe",
".",
"FindStringSubmatch",
"(",
"line",
")",
";",
"{",
"case",
"len",
"(",
"ifNameMatches",
")",
">",
"1",
":",
"ifName",
"=",
"ifNameMatches",
"[",
"1",
"]",
"\n",
"continue",
"\n",
"}",
"\n\n",
"switch",
"ipAddrMatches",
":=",
"ipAddrRe",
".",
"FindStringSubmatch",
"(",
"line",
")",
";",
"{",
"case",
"len",
"(",
"ipAddrMatches",
")",
">",
"1",
"&&",
"ipAddrMatches",
"[",
"1",
"]",
"==",
"defaultIPAddr",
":",
"return",
"ifName",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}"
] | // parseDefaultIfNameWindowsIPConfig parses the output of `ipconfig` to find the
// interface name forwarding traffic to the default gateway. | [
"parseDefaultIfNameWindowsIPConfig",
"parses",
"the",
"output",
"of",
"ipconfig",
"to",
"find",
"the",
"interface",
"name",
"forwarding",
"traffic",
"to",
"the",
"default",
"gateway",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ifaddrs.go#L1285-L1304 | train |
hashicorp/go-sockaddr | ipv4addr.go | AddressBinString | func (ipv4 IPv4Addr) AddressBinString() string {
return fmt.Sprintf("%032s", strconv.FormatUint(uint64(ipv4.Address), 2))
} | go | func (ipv4 IPv4Addr) AddressBinString() string {
return fmt.Sprintf("%032s", strconv.FormatUint(uint64(ipv4.Address), 2))
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"AddressBinString",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"strconv",
".",
"FormatUint",
"(",
"uint64",
"(",
"ipv4",
".",
"Address",
")",
",",
"2",
")",
")",
"\n",
"}"
] | // AddressBinString returns a string with the IPv4Addr's Address represented
// as a sequence of '0' and '1' characters. This method is useful for
// debugging or by operators who want to inspect an address. | [
"AddressBinString",
"returns",
"a",
"string",
"with",
"the",
"IPv4Addr",
"s",
"Address",
"represented",
"as",
"a",
"sequence",
"of",
"0",
"and",
"1",
"characters",
".",
"This",
"method",
"is",
"useful",
"for",
"debugging",
"or",
"by",
"operators",
"who",
"want",
"to",
"inspect",
"an",
"address",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L136-L138 | train |
hashicorp/go-sockaddr | ipv4addr.go | BroadcastAddress | func (ipv4 IPv4Addr) BroadcastAddress() IPv4Network {
return IPv4Network(uint32(ipv4.Address)&uint32(ipv4.Mask) | ^uint32(ipv4.Mask))
} | go | func (ipv4 IPv4Addr) BroadcastAddress() IPv4Network {
return IPv4Network(uint32(ipv4.Address)&uint32(ipv4.Mask) | ^uint32(ipv4.Mask))
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"BroadcastAddress",
"(",
")",
"IPv4Network",
"{",
"return",
"IPv4Network",
"(",
"uint32",
"(",
"ipv4",
".",
"Address",
")",
"&",
"uint32",
"(",
"ipv4",
".",
"Mask",
")",
"|",
"^",
"uint32",
"(",
"ipv4",
".",
"Mask",
")",
")",
"\n",
"}"
] | // BroadcastAddress returns a IPv4Network of the IPv4Addr's broadcast
// address. | [
"BroadcastAddress",
"returns",
"a",
"IPv4Network",
"of",
"the",
"IPv4Addr",
"s",
"broadcast",
"address",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L162-L164 | train |
hashicorp/go-sockaddr | ipv4addr.go | ContainsAddress | func (ipv4 IPv4Addr) ContainsAddress(x IPv4Address) bool {
return IPv4Address(ipv4.NetworkAddress()) <= x &&
IPv4Address(ipv4.BroadcastAddress()) >= x
} | go | func (ipv4 IPv4Addr) ContainsAddress(x IPv4Address) bool {
return IPv4Address(ipv4.NetworkAddress()) <= x &&
IPv4Address(ipv4.BroadcastAddress()) >= x
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"ContainsAddress",
"(",
"x",
"IPv4Address",
")",
"bool",
"{",
"return",
"IPv4Address",
"(",
"ipv4",
".",
"NetworkAddress",
"(",
")",
")",
"<=",
"x",
"&&",
"IPv4Address",
"(",
"ipv4",
".",
"BroadcastAddress",
"(",
")",
")",
">=",
"x",
"\n",
"}"
] | // ContainsAddress returns true if the IPv4Address is contained within the
// receiver. | [
"ContainsAddress",
"returns",
"true",
"if",
"the",
"IPv4Address",
"is",
"contained",
"within",
"the",
"receiver",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L262-L265 | train |
hashicorp/go-sockaddr | ipv4addr.go | ContainsNetwork | func (ipv4 IPv4Addr) ContainsNetwork(x IPv4Addr) bool {
return ipv4.NetworkAddress() <= x.NetworkAddress() &&
ipv4.BroadcastAddress() >= x.BroadcastAddress()
} | go | func (ipv4 IPv4Addr) ContainsNetwork(x IPv4Addr) bool {
return ipv4.NetworkAddress() <= x.NetworkAddress() &&
ipv4.BroadcastAddress() >= x.BroadcastAddress()
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"ContainsNetwork",
"(",
"x",
"IPv4Addr",
")",
"bool",
"{",
"return",
"ipv4",
".",
"NetworkAddress",
"(",
")",
"<=",
"x",
".",
"NetworkAddress",
"(",
")",
"&&",
"ipv4",
".",
"BroadcastAddress",
"(",
")",
">=",
"x",
".",
"BroadcastAddress",
"(",
")",
"\n",
"}"
] | // ContainsNetwork returns true if the network from IPv4Addr is contained
// within the receiver. | [
"ContainsNetwork",
"returns",
"true",
"if",
"the",
"network",
"from",
"IPv4Addr",
"is",
"contained",
"within",
"the",
"receiver",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L269-L272 | train |
hashicorp/go-sockaddr | ipv4addr.go | LastUsable | func (ipv4 IPv4Addr) LastUsable() IPAddr {
addr := ipv4.BroadcastAddress()
// If /32, return the address itself. If /31 assume a point-to-point
// link and return the upper address.
if ipv4.Maskbits() < 31 {
addr--
}
return IPv4Addr{
Address: IPv4Address(addr),
Mask: IPv4HostMask,
}
} | go | func (ipv4 IPv4Addr) LastUsable() IPAddr {
addr := ipv4.BroadcastAddress()
// If /32, return the address itself. If /31 assume a point-to-point
// link and return the upper address.
if ipv4.Maskbits() < 31 {
addr--
}
return IPv4Addr{
Address: IPv4Address(addr),
Mask: IPv4HostMask,
}
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"LastUsable",
"(",
")",
"IPAddr",
"{",
"addr",
":=",
"ipv4",
".",
"BroadcastAddress",
"(",
")",
"\n\n",
"// If /32, return the address itself. If /31 assume a point-to-point",
"// link and return the upper address.",
"if",
"ipv4",
".",
"Maskbits",
"(",
")",
"<",
"31",
"{",
"addr",
"--",
"\n",
"}",
"\n\n",
"return",
"IPv4Addr",
"{",
"Address",
":",
"IPv4Address",
"(",
"addr",
")",
",",
"Mask",
":",
"IPv4HostMask",
",",
"}",
"\n",
"}"
] | // LastUsable returns the last address before the broadcast address in a
// given network. | [
"LastUsable",
"returns",
"the",
"last",
"address",
"before",
"the",
"broadcast",
"address",
"in",
"a",
"given",
"network",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L359-L372 | train |
hashicorp/go-sockaddr | ipv4addr.go | MustIPv4Addr | func MustIPv4Addr(addr string) IPv4Addr {
ipv4, err := NewIPv4Addr(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create an IPv4Addr from %+q: %v", addr, err))
}
return ipv4
} | go | func MustIPv4Addr(addr string) IPv4Addr {
ipv4, err := NewIPv4Addr(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create an IPv4Addr from %+q: %v", addr, err))
}
return ipv4
} | [
"func",
"MustIPv4Addr",
"(",
"addr",
"string",
")",
"IPv4Addr",
"{",
"ipv4",
",",
"err",
":=",
"NewIPv4Addr",
"(",
"addr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"addr",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"return",
"ipv4",
"\n",
"}"
] | // MustIPv4Addr is a helper method that must return an IPv4Addr or panic on
// invalid input. | [
"MustIPv4Addr",
"is",
"a",
"helper",
"method",
"that",
"must",
"return",
"an",
"IPv4Addr",
"or",
"panic",
"on",
"invalid",
"input",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L405-L411 | train |
hashicorp/go-sockaddr | ipv4addr.go | NetIPMask | func (ipv4 IPv4Addr) NetIPMask() *net.IPMask {
ipv4Mask := net.IPMask{}
ipv4Mask = make(net.IPMask, IPv4len)
binary.BigEndian.PutUint32(ipv4Mask, uint32(ipv4.Mask))
return &ipv4Mask
} | go | func (ipv4 IPv4Addr) NetIPMask() *net.IPMask {
ipv4Mask := net.IPMask{}
ipv4Mask = make(net.IPMask, IPv4len)
binary.BigEndian.PutUint32(ipv4Mask, uint32(ipv4.Mask))
return &ipv4Mask
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"NetIPMask",
"(",
")",
"*",
"net",
".",
"IPMask",
"{",
"ipv4Mask",
":=",
"net",
".",
"IPMask",
"{",
"}",
"\n",
"ipv4Mask",
"=",
"make",
"(",
"net",
".",
"IPMask",
",",
"IPv4len",
")",
"\n",
"binary",
".",
"BigEndian",
".",
"PutUint32",
"(",
"ipv4Mask",
",",
"uint32",
"(",
"ipv4",
".",
"Mask",
")",
")",
"\n",
"return",
"&",
"ipv4Mask",
"\n",
"}"
] | // NetIPMask create a new net.IPMask from the IPv4Addr. | [
"NetIPMask",
"create",
"a",
"new",
"net",
".",
"IPMask",
"from",
"the",
"IPv4Addr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L422-L427 | train |
hashicorp/go-sockaddr | ipv4addr.go | NetIPNet | func (ipv4 IPv4Addr) NetIPNet() *net.IPNet {
ipv4net := &net.IPNet{}
ipv4net.IP = make(net.IP, IPv4len)
binary.BigEndian.PutUint32(ipv4net.IP, uint32(ipv4.NetworkAddress()))
ipv4net.Mask = *ipv4.NetIPMask()
return ipv4net
} | go | func (ipv4 IPv4Addr) NetIPNet() *net.IPNet {
ipv4net := &net.IPNet{}
ipv4net.IP = make(net.IP, IPv4len)
binary.BigEndian.PutUint32(ipv4net.IP, uint32(ipv4.NetworkAddress()))
ipv4net.Mask = *ipv4.NetIPMask()
return ipv4net
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"NetIPNet",
"(",
")",
"*",
"net",
".",
"IPNet",
"{",
"ipv4net",
":=",
"&",
"net",
".",
"IPNet",
"{",
"}",
"\n",
"ipv4net",
".",
"IP",
"=",
"make",
"(",
"net",
".",
"IP",
",",
"IPv4len",
")",
"\n",
"binary",
".",
"BigEndian",
".",
"PutUint32",
"(",
"ipv4net",
".",
"IP",
",",
"uint32",
"(",
"ipv4",
".",
"NetworkAddress",
"(",
")",
")",
")",
"\n",
"ipv4net",
".",
"Mask",
"=",
"*",
"ipv4",
".",
"NetIPMask",
"(",
")",
"\n",
"return",
"ipv4net",
"\n",
"}"
] | // NetIPNet create a new net.IPNet from the IPv4Addr. | [
"NetIPNet",
"create",
"a",
"new",
"net",
".",
"IPNet",
"from",
"the",
"IPv4Addr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L430-L436 | train |
hashicorp/go-sockaddr | ipv4addr.go | NetworkAddress | func (ipv4 IPv4Addr) NetworkAddress() IPv4Network {
return IPv4Network(uint32(ipv4.Address) & uint32(ipv4.Mask))
} | go | func (ipv4 IPv4Addr) NetworkAddress() IPv4Network {
return IPv4Network(uint32(ipv4.Address) & uint32(ipv4.Mask))
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"NetworkAddress",
"(",
")",
"IPv4Network",
"{",
"return",
"IPv4Network",
"(",
"uint32",
"(",
"ipv4",
".",
"Address",
")",
"&",
"uint32",
"(",
"ipv4",
".",
"Mask",
")",
")",
"\n",
"}"
] | // NetworkAddress returns an IPv4Network of the IPv4Addr's network address. | [
"NetworkAddress",
"returns",
"an",
"IPv4Network",
"of",
"the",
"IPv4Addr",
"s",
"network",
"address",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L447-L449 | train |
hashicorp/go-sockaddr | ipv4addr.go | Octets | func (ipv4 IPv4Addr) Octets() []int {
return []int{
int(ipv4.Address >> 24),
int((ipv4.Address >> 16) & 0xff),
int((ipv4.Address >> 8) & 0xff),
int(ipv4.Address & 0xff),
}
} | go | func (ipv4 IPv4Addr) Octets() []int {
return []int{
int(ipv4.Address >> 24),
int((ipv4.Address >> 16) & 0xff),
int((ipv4.Address >> 8) & 0xff),
int(ipv4.Address & 0xff),
}
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"Octets",
"(",
")",
"[",
"]",
"int",
"{",
"return",
"[",
"]",
"int",
"{",
"int",
"(",
"ipv4",
".",
"Address",
">>",
"24",
")",
",",
"int",
"(",
"(",
"ipv4",
".",
"Address",
">>",
"16",
")",
"&",
"0xff",
")",
",",
"int",
"(",
"(",
"ipv4",
".",
"Address",
">>",
"8",
")",
"&",
"0xff",
")",
",",
"int",
"(",
"ipv4",
".",
"Address",
"&",
"0xff",
")",
",",
"}",
"\n",
"}"
] | // Octets returns a slice of the four octets in an IPv4Addr's Address. The
// order of the bytes is big endian. | [
"Octets",
"returns",
"a",
"slice",
"of",
"the",
"four",
"octets",
"in",
"an",
"IPv4Addr",
"s",
"Address",
".",
"The",
"order",
"of",
"the",
"bytes",
"is",
"big",
"endian",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L453-L460 | train |
hashicorp/go-sockaddr | ipv4addr.go | String | func (ipv4 IPv4Addr) String() string {
if ipv4.Port != 0 {
return fmt.Sprintf("%s:%d", ipv4.NetIP().String(), ipv4.Port)
}
if ipv4.Maskbits() == 32 {
return ipv4.NetIP().String()
}
return fmt.Sprintf("%s/%d", ipv4.NetIP().String(), ipv4.Maskbits())
} | go | func (ipv4 IPv4Addr) String() string {
if ipv4.Port != 0 {
return fmt.Sprintf("%s:%d", ipv4.NetIP().String(), ipv4.Port)
}
if ipv4.Maskbits() == 32 {
return ipv4.NetIP().String()
}
return fmt.Sprintf("%s/%d", ipv4.NetIP().String(), ipv4.Maskbits())
} | [
"func",
"(",
"ipv4",
"IPv4Addr",
")",
"String",
"(",
")",
"string",
"{",
"if",
"ipv4",
".",
"Port",
"!=",
"0",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"ipv4",
".",
"NetIP",
"(",
")",
".",
"String",
"(",
")",
",",
"ipv4",
".",
"Port",
")",
"\n",
"}",
"\n\n",
"if",
"ipv4",
".",
"Maskbits",
"(",
")",
"==",
"32",
"{",
"return",
"ipv4",
".",
"NetIP",
"(",
")",
".",
"String",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"ipv4",
".",
"NetIP",
"(",
")",
".",
"String",
"(",
")",
",",
"ipv4",
".",
"Maskbits",
"(",
")",
")",
"\n",
"}"
] | // String returns a string representation of the IPv4Addr | [
"String",
"returns",
"a",
"string",
"representation",
"of",
"the",
"IPv4Addr"
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L463-L473 | train |
hashicorp/go-sockaddr | ipv4addr.go | IPv4AddrAttr | func IPv4AddrAttr(ipv4 IPv4Addr, selector AttrName) string {
fn, found := ipv4AddrAttrMap[selector]
if !found {
return ""
}
return fn(ipv4)
} | go | func IPv4AddrAttr(ipv4 IPv4Addr, selector AttrName) string {
fn, found := ipv4AddrAttrMap[selector]
if !found {
return ""
}
return fn(ipv4)
} | [
"func",
"IPv4AddrAttr",
"(",
"ipv4",
"IPv4Addr",
",",
"selector",
"AttrName",
")",
"string",
"{",
"fn",
",",
"found",
":=",
"ipv4AddrAttrMap",
"[",
"selector",
"]",
"\n",
"if",
"!",
"found",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n\n",
"return",
"fn",
"(",
"ipv4",
")",
"\n",
"}"
] | // IPv4AddrAttr returns a string representation of an attribute for the given
// IPv4Addr. | [
"IPv4AddrAttr",
"returns",
"a",
"string",
"representation",
"of",
"an",
"attribute",
"for",
"the",
"given",
"IPv4Addr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipv4addr.go#L482-L489 | train |
hashicorp/go-sockaddr | ipaddr.go | NewIPAddr | func NewIPAddr(addr string) (IPAddr, error) {
ipv4Addr, err := NewIPv4Addr(addr)
if err == nil {
return ipv4Addr, nil
}
ipv6Addr, err := NewIPv6Addr(addr)
if err == nil {
return ipv6Addr, nil
}
return nil, fmt.Errorf("invalid IPAddr %v", addr)
} | go | func NewIPAddr(addr string) (IPAddr, error) {
ipv4Addr, err := NewIPv4Addr(addr)
if err == nil {
return ipv4Addr, nil
}
ipv6Addr, err := NewIPv6Addr(addr)
if err == nil {
return ipv6Addr, nil
}
return nil, fmt.Errorf("invalid IPAddr %v", addr)
} | [
"func",
"NewIPAddr",
"(",
"addr",
"string",
")",
"(",
"IPAddr",
",",
"error",
")",
"{",
"ipv4Addr",
",",
"err",
":=",
"NewIPv4Addr",
"(",
"addr",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"ipv4Addr",
",",
"nil",
"\n",
"}",
"\n\n",
"ipv6Addr",
",",
"err",
":=",
"NewIPv6Addr",
"(",
"addr",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"ipv6Addr",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"addr",
")",
"\n",
"}"
] | // NewIPAddr creates a new IPAddr from a string. Returns nil if the string is
// not an IPv4 or an IPv6 address. | [
"NewIPAddr",
"creates",
"a",
"new",
"IPAddr",
"from",
"a",
"string",
".",
"Returns",
"nil",
"if",
"the",
"string",
"is",
"not",
"an",
"IPv4",
"or",
"an",
"IPv6",
"address",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipaddr.go#L55-L67 | train |
hashicorp/go-sockaddr | ipaddr.go | IPAddrAttr | func IPAddrAttr(ip IPAddr, selector AttrName) string {
fn, found := ipAddrAttrMap[selector]
if !found {
return ""
}
return fn(ip)
} | go | func IPAddrAttr(ip IPAddr, selector AttrName) string {
fn, found := ipAddrAttrMap[selector]
if !found {
return ""
}
return fn(ip)
} | [
"func",
"IPAddrAttr",
"(",
"ip",
"IPAddr",
",",
"selector",
"AttrName",
")",
"string",
"{",
"fn",
",",
"found",
":=",
"ipAddrAttrMap",
"[",
"selector",
"]",
"\n",
"if",
"!",
"found",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n\n",
"return",
"fn",
"(",
"ip",
")",
"\n",
"}"
] | // IPAddrAttr returns a string representation of an attribute for the given
// IPAddr. | [
"IPAddrAttr",
"returns",
"a",
"string",
"representation",
"of",
"an",
"attribute",
"for",
"the",
"given",
"IPAddr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipaddr.go#L71-L78 | train |
hashicorp/go-sockaddr | ipaddr.go | MustIPAddr | func MustIPAddr(addr string) IPAddr {
ip, err := NewIPAddr(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create an IPAddr from %+q: %v", addr, err))
}
return ip
} | go | func MustIPAddr(addr string) IPAddr {
ip, err := NewIPAddr(addr)
if err != nil {
panic(fmt.Sprintf("Unable to create an IPAddr from %+q: %v", addr, err))
}
return ip
} | [
"func",
"MustIPAddr",
"(",
"addr",
"string",
")",
"IPAddr",
"{",
"ip",
",",
"err",
":=",
"NewIPAddr",
"(",
"addr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"addr",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"return",
"ip",
"\n",
"}"
] | // MustIPAddr is a helper method that must return an IPAddr or panic on invalid
// input. | [
"MustIPAddr",
"is",
"a",
"helper",
"method",
"that",
"must",
"return",
"an",
"IPAddr",
"or",
"panic",
"on",
"invalid",
"input",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/ipaddr.go#L87-L93 | train |
hashicorp/go-sockaddr | sockaddrs.go | Sort | func (ms *multiAddrSorter) Sort(sockAddrs SockAddrs) {
ms.addrs = sockAddrs
sort.Sort(ms)
} | go | func (ms *multiAddrSorter) Sort(sockAddrs SockAddrs) {
ms.addrs = sockAddrs
sort.Sort(ms)
} | [
"func",
"(",
"ms",
"*",
"multiAddrSorter",
")",
"Sort",
"(",
"sockAddrs",
"SockAddrs",
")",
"{",
"ms",
".",
"addrs",
"=",
"sockAddrs",
"\n",
"sort",
".",
"Sort",
"(",
"ms",
")",
"\n",
"}"
] | // Sort sorts the argument slice according to the Cmp functions passed to
// OrderedAddrBy. | [
"Sort",
"sorts",
"the",
"argument",
"slice",
"according",
"to",
"the",
"Cmp",
"functions",
"passed",
"to",
"OrderedAddrBy",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/sockaddrs.go#L26-L29 | train |
hashicorp/go-sockaddr | sockaddrs.go | AscAddress | func AscAddress(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
switch v := p1.(type) {
case IPv4Addr:
return v.CmpAddress(p2)
case IPv6Addr:
return v.CmpAddress(p2)
case UnixSock:
return v.CmpAddress(p2)
default:
return sortDeferDecision
}
} | go | func AscAddress(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
switch v := p1.(type) {
case IPv4Addr:
return v.CmpAddress(p2)
case IPv6Addr:
return v.CmpAddress(p2)
case UnixSock:
return v.CmpAddress(p2)
default:
return sortDeferDecision
}
} | [
"func",
"AscAddress",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"SockAddr",
")",
"int",
"{",
"p1",
":=",
"*",
"p1Ptr",
"\n",
"p2",
":=",
"*",
"p2Ptr",
"\n\n",
"switch",
"v",
":=",
"p1",
".",
"(",
"type",
")",
"{",
"case",
"IPv4Addr",
":",
"return",
"v",
".",
"CmpAddress",
"(",
"p2",
")",
"\n",
"case",
"IPv6Addr",
":",
"return",
"v",
".",
"CmpAddress",
"(",
"p2",
")",
"\n",
"case",
"UnixSock",
":",
"return",
"v",
".",
"CmpAddress",
"(",
"p2",
")",
"\n",
"default",
":",
"return",
"sortDeferDecision",
"\n",
"}",
"\n",
"}"
] | // AscAddress is a sorting function to sort SockAddrs by their respective
// address type. Non-equal types are deferred in the sort. | [
"AscAddress",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"SockAddrs",
"by",
"their",
"respective",
"address",
"type",
".",
"Non",
"-",
"equal",
"types",
"are",
"deferred",
"in",
"the",
"sort",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/sockaddrs.go#L94-L108 | train |
hashicorp/go-sockaddr | sockaddrs.go | AscPort | func AscPort(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
switch v := p1.(type) {
case IPv4Addr:
return v.CmpPort(p2)
case IPv6Addr:
return v.CmpPort(p2)
default:
return sortDeferDecision
}
} | go | func AscPort(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
switch v := p1.(type) {
case IPv4Addr:
return v.CmpPort(p2)
case IPv6Addr:
return v.CmpPort(p2)
default:
return sortDeferDecision
}
} | [
"func",
"AscPort",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"SockAddr",
")",
"int",
"{",
"p1",
":=",
"*",
"p1Ptr",
"\n",
"p2",
":=",
"*",
"p2Ptr",
"\n\n",
"switch",
"v",
":=",
"p1",
".",
"(",
"type",
")",
"{",
"case",
"IPv4Addr",
":",
"return",
"v",
".",
"CmpPort",
"(",
"p2",
")",
"\n",
"case",
"IPv6Addr",
":",
"return",
"v",
".",
"CmpPort",
"(",
"p2",
")",
"\n",
"default",
":",
"return",
"sortDeferDecision",
"\n",
"}",
"\n",
"}"
] | // AscPort is a sorting function to sort SockAddrs by their respective address
// type. Non-equal types are deferred in the sort. | [
"AscPort",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"SockAddrs",
"by",
"their",
"respective",
"address",
"type",
".",
"Non",
"-",
"equal",
"types",
"are",
"deferred",
"in",
"the",
"sort",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/sockaddrs.go#L112-L124 | train |
hashicorp/go-sockaddr | sockaddrs.go | AscNetworkSize | func AscNetworkSize(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
p1Type := p1.Type()
p2Type := p2.Type()
// Network size operations on non-IP types make no sense
if p1Type != p2Type && p1Type != TypeIP {
return sortDeferDecision
}
ipA := p1.(IPAddr)
ipB := p2.(IPAddr)
return bytes.Compare([]byte(*ipA.NetIPMask()), []byte(*ipB.NetIPMask()))
} | go | func AscNetworkSize(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
p1Type := p1.Type()
p2Type := p2.Type()
// Network size operations on non-IP types make no sense
if p1Type != p2Type && p1Type != TypeIP {
return sortDeferDecision
}
ipA := p1.(IPAddr)
ipB := p2.(IPAddr)
return bytes.Compare([]byte(*ipA.NetIPMask()), []byte(*ipB.NetIPMask()))
} | [
"func",
"AscNetworkSize",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"SockAddr",
")",
"int",
"{",
"p1",
":=",
"*",
"p1Ptr",
"\n",
"p2",
":=",
"*",
"p2Ptr",
"\n",
"p1Type",
":=",
"p1",
".",
"Type",
"(",
")",
"\n",
"p2Type",
":=",
"p2",
".",
"Type",
"(",
")",
"\n\n",
"// Network size operations on non-IP types make no sense",
"if",
"p1Type",
"!=",
"p2Type",
"&&",
"p1Type",
"!=",
"TypeIP",
"{",
"return",
"sortDeferDecision",
"\n",
"}",
"\n\n",
"ipA",
":=",
"p1",
".",
"(",
"IPAddr",
")",
"\n",
"ipB",
":=",
"p2",
".",
"(",
"IPAddr",
")",
"\n\n",
"return",
"bytes",
".",
"Compare",
"(",
"[",
"]",
"byte",
"(",
"*",
"ipA",
".",
"NetIPMask",
"(",
")",
")",
",",
"[",
"]",
"byte",
"(",
"*",
"ipB",
".",
"NetIPMask",
"(",
")",
")",
")",
"\n",
"}"
] | // AscNetworkSize is a sorting function to sort SockAddrs based on their network
// size. Non-equal types are deferred in the sort. | [
"AscNetworkSize",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"SockAddrs",
"based",
"on",
"their",
"network",
"size",
".",
"Non",
"-",
"equal",
"types",
"are",
"deferred",
"in",
"the",
"sort",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/sockaddrs.go#L144-L159 | train |
hashicorp/go-sockaddr | sockaddrs.go | AscType | func AscType(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
p1Type := p1.Type()
p2Type := p2.Type()
switch {
case p1Type < p2Type:
return sortReceiverBeforeArg
case p1Type == p2Type:
return sortDeferDecision
case p1Type > p2Type:
return sortArgBeforeReceiver
default:
return sortDeferDecision
}
} | go | func AscType(p1Ptr, p2Ptr *SockAddr) int {
p1 := *p1Ptr
p2 := *p2Ptr
p1Type := p1.Type()
p2Type := p2.Type()
switch {
case p1Type < p2Type:
return sortReceiverBeforeArg
case p1Type == p2Type:
return sortDeferDecision
case p1Type > p2Type:
return sortArgBeforeReceiver
default:
return sortDeferDecision
}
} | [
"func",
"AscType",
"(",
"p1Ptr",
",",
"p2Ptr",
"*",
"SockAddr",
")",
"int",
"{",
"p1",
":=",
"*",
"p1Ptr",
"\n",
"p2",
":=",
"*",
"p2Ptr",
"\n",
"p1Type",
":=",
"p1",
".",
"Type",
"(",
")",
"\n",
"p2Type",
":=",
"p2",
".",
"Type",
"(",
")",
"\n",
"switch",
"{",
"case",
"p1Type",
"<",
"p2Type",
":",
"return",
"sortReceiverBeforeArg",
"\n",
"case",
"p1Type",
"==",
"p2Type",
":",
"return",
"sortDeferDecision",
"\n",
"case",
"p1Type",
">",
"p2Type",
":",
"return",
"sortArgBeforeReceiver",
"\n",
"default",
":",
"return",
"sortDeferDecision",
"\n",
"}",
"\n",
"}"
] | // AscType is a sorting function to sort "more secure" types before
// "less-secure" types. | [
"AscType",
"is",
"a",
"sorting",
"function",
"to",
"sort",
"more",
"secure",
"types",
"before",
"less",
"-",
"secure",
"types",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/sockaddrs.go#L163-L178 | train |
hashicorp/go-sockaddr | sockaddr.go | ToIPAddr | func ToIPAddr(sa SockAddr) *IPAddr {
ipa, ok := sa.(IPAddr)
if !ok {
return nil
}
return &ipa
} | go | func ToIPAddr(sa SockAddr) *IPAddr {
ipa, ok := sa.(IPAddr)
if !ok {
return nil
}
return &ipa
} | [
"func",
"ToIPAddr",
"(",
"sa",
"SockAddr",
")",
"*",
"IPAddr",
"{",
"ipa",
",",
"ok",
":=",
"sa",
".",
"(",
"IPAddr",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"&",
"ipa",
"\n",
"}"
] | // ToIPAddr returns an IPAddr type or nil if the type conversion fails. | [
"ToIPAddr",
"returns",
"an",
"IPAddr",
"type",
"or",
"nil",
"if",
"the",
"type",
"conversion",
"fails",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/sockaddr.go#L92-L98 | train |
hashicorp/go-sockaddr | sockaddr.go | SockAddrAttr | func SockAddrAttr(sa SockAddr, selector AttrName) string {
fn, found := sockAddrAttrMap[selector]
if !found {
return ""
}
return fn(sa)
} | go | func SockAddrAttr(sa SockAddr, selector AttrName) string {
fn, found := sockAddrAttrMap[selector]
if !found {
return ""
}
return fn(sa)
} | [
"func",
"SockAddrAttr",
"(",
"sa",
"SockAddr",
",",
"selector",
"AttrName",
")",
"string",
"{",
"fn",
",",
"found",
":=",
"sockAddrAttrMap",
"[",
"selector",
"]",
"\n",
"if",
"!",
"found",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n\n",
"return",
"fn",
"(",
"sa",
")",
"\n",
"}"
] | // SockAddrAttr returns a string representation of an attribute for the given
// SockAddr. | [
"SockAddrAttr",
"returns",
"a",
"string",
"representation",
"of",
"an",
"attribute",
"for",
"the",
"given",
"SockAddr",
"."
] | c7188e74f6acae5a989bdc959aa779f8b9f42faf | https://github.com/hashicorp/go-sockaddr/blob/c7188e74f6acae5a989bdc959aa779f8b9f42faf/sockaddr.go#L132-L139 | train |
Jeffail/gabs | gabs.go | Search | func (g *Container) Search(hierarchy ...string) *Container {
var object interface{}
object = g.Data()
for target := 0; target < len(hierarchy); target++ {
if mmap, ok := object.(map[string]interface{}); ok {
object, ok = mmap[hierarchy[target]]
if !ok {
return nil
}
} else if marray, ok := object.([]interface{}); ok {
tmpArray := []interface{}{}
for _, val := range marray {
tmpGabs := &Container{val}
res := tmpGabs.Search(hierarchy[target:]...)
if res != nil {
tmpArray = append(tmpArray, res.Data())
}
}
if len(tmpArray) == 0 {
return nil
}
return &Container{tmpArray}
} else {
return nil
}
}
return &Container{object}
} | go | func (g *Container) Search(hierarchy ...string) *Container {
var object interface{}
object = g.Data()
for target := 0; target < len(hierarchy); target++ {
if mmap, ok := object.(map[string]interface{}); ok {
object, ok = mmap[hierarchy[target]]
if !ok {
return nil
}
} else if marray, ok := object.([]interface{}); ok {
tmpArray := []interface{}{}
for _, val := range marray {
tmpGabs := &Container{val}
res := tmpGabs.Search(hierarchy[target:]...)
if res != nil {
tmpArray = append(tmpArray, res.Data())
}
}
if len(tmpArray) == 0 {
return nil
}
return &Container{tmpArray}
} else {
return nil
}
}
return &Container{object}
} | [
"func",
"(",
"g",
"*",
"Container",
")",
"Search",
"(",
"hierarchy",
"...",
"string",
")",
"*",
"Container",
"{",
"var",
"object",
"interface",
"{",
"}",
"\n\n",
"object",
"=",
"g",
".",
"Data",
"(",
")",
"\n",
"for",
"target",
":=",
"0",
";",
"target",
"<",
"len",
"(",
"hierarchy",
")",
";",
"target",
"++",
"{",
"if",
"mmap",
",",
"ok",
":=",
"object",
".",
"(",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
")",
";",
"ok",
"{",
"object",
",",
"ok",
"=",
"mmap",
"[",
"hierarchy",
"[",
"target",
"]",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"}",
"else",
"if",
"marray",
",",
"ok",
":=",
"object",
".",
"(",
"[",
"]",
"interface",
"{",
"}",
")",
";",
"ok",
"{",
"tmpArray",
":=",
"[",
"]",
"interface",
"{",
"}",
"{",
"}",
"\n",
"for",
"_",
",",
"val",
":=",
"range",
"marray",
"{",
"tmpGabs",
":=",
"&",
"Container",
"{",
"val",
"}",
"\n",
"res",
":=",
"tmpGabs",
".",
"Search",
"(",
"hierarchy",
"[",
"target",
":",
"]",
"...",
")",
"\n",
"if",
"res",
"!=",
"nil",
"{",
"tmpArray",
"=",
"append",
"(",
"tmpArray",
",",
"res",
".",
"Data",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"len",
"(",
"tmpArray",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"&",
"Container",
"{",
"tmpArray",
"}",
"\n",
"}",
"else",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"&",
"Container",
"{",
"object",
"}",
"\n",
"}"
] | // Search - Attempt to find and return an object within the JSON structure by specifying the
// hierarchy of field names to locate the target. If the search encounters an array and has not
// reached the end target then it will iterate each object of the array for the target and return
// all of the results in a JSON array. | [
"Search",
"-",
"Attempt",
"to",
"find",
"and",
"return",
"an",
"object",
"within",
"the",
"JSON",
"structure",
"by",
"specifying",
"the",
"hierarchy",
"of",
"field",
"names",
"to",
"locate",
"the",
"target",
".",
"If",
"the",
"search",
"encounters",
"an",
"array",
"and",
"has",
"not",
"reached",
"the",
"end",
"target",
"then",
"it",
"will",
"iterate",
"each",
"object",
"of",
"the",
"array",
"for",
"the",
"target",
"and",
"return",
"all",
"of",
"the",
"results",
"in",
"a",
"JSON",
"array",
"."
] | 74ef14cf8f407ee3ca8ec01bb6f52d6104edea80 | https://github.com/Jeffail/gabs/blob/74ef14cf8f407ee3ca8ec01bb6f52d6104edea80/gabs.go#L93-L121 | train |
Jeffail/gabs | gabs.go | Exists | func (g *Container) Exists(hierarchy ...string) bool {
return g.Search(hierarchy...) != nil
} | go | func (g *Container) Exists(hierarchy ...string) bool {
return g.Search(hierarchy...) != nil
} | [
"func",
"(",
"g",
"*",
"Container",
")",
"Exists",
"(",
"hierarchy",
"...",
"string",
")",
"bool",
"{",
"return",
"g",
".",
"Search",
"(",
"hierarchy",
"...",
")",
"!=",
"nil",
"\n",
"}"
] | // Exists - Checks whether a path exists. | [
"Exists",
"-",
"Checks",
"whether",
"a",
"path",
"exists",
"."
] | 74ef14cf8f407ee3ca8ec01bb6f52d6104edea80 | https://github.com/Jeffail/gabs/blob/74ef14cf8f407ee3ca8ec01bb6f52d6104edea80/gabs.go#L129-L131 | train |
Jeffail/gabs | gabs.go | ExistsP | func (g *Container) ExistsP(path string) bool {
return g.Exists(strings.Split(path, ".")...)
} | go | func (g *Container) ExistsP(path string) bool {
return g.Exists(strings.Split(path, ".")...)
} | [
"func",
"(",
"g",
"*",
"Container",
")",
"ExistsP",
"(",
"path",
"string",
")",
"bool",
"{",
"return",
"g",
".",
"Exists",
"(",
"strings",
".",
"Split",
"(",
"path",
",",
"\"",
"\"",
")",
"...",
")",
"\n",
"}"
] | // ExistsP - Checks whether a dot notation path exists. | [
"ExistsP",
"-",
"Checks",
"whether",
"a",
"dot",
"notation",
"path",
"exists",
"."
] | 74ef14cf8f407ee3ca8ec01bb6f52d6104edea80 | https://github.com/Jeffail/gabs/blob/74ef14cf8f407ee3ca8ec01bb6f52d6104edea80/gabs.go#L134-L136 | 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.