repo stringlengths 5 67 | path stringlengths 4 218 | func_name stringlengths 0 151 | original_string stringlengths 52 373k | language stringclasses 6 values | code stringlengths 52 373k | code_tokens listlengths 10 512 | docstring stringlengths 3 47.2k | docstring_tokens listlengths 3 234 | sha stringlengths 40 40 | url stringlengths 85 339 | partition stringclasses 3 values |
|---|---|---|---|---|---|---|---|---|---|---|---|
pelletier/go-toml | marshal.go | QuoteMapKeys | func (e *Encoder) QuoteMapKeys(v bool) *Encoder {
e.quoteMapKeys = v
return e
} | go | func (e *Encoder) QuoteMapKeys(v bool) *Encoder {
e.quoteMapKeys = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"QuoteMapKeys",
"(",
"v",
"bool",
")",
"*",
"Encoder",
"{",
"e",
".",
"quoteMapKeys",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // QuoteMapKeys sets up the encoder to encode
// maps with string type keys with quoted TOML keys.
//
// This relieves the character limitations on map keys. | [
"QuoteMapKeys",
"sets",
"up",
"the",
"encoder",
"to",
"encode",
"maps",
"with",
"string",
"type",
"keys",
"with",
"quoted",
"TOML",
"keys",
".",
"This",
"relieves",
"the",
"character",
"limitations",
"on",
"map",
"keys",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L221-L224 | train |
pelletier/go-toml | marshal.go | Order | func (e *Encoder) Order(ord marshalOrder) *Encoder {
e.order = ord
return e
} | go | func (e *Encoder) Order(ord marshalOrder) *Encoder {
e.order = ord
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"Order",
"(",
"ord",
"marshalOrder",
")",
"*",
"Encoder",
"{",
"e",
".",
"order",
"=",
"ord",
"\n",
"return",
"e",
"\n",
"}"
] | // Order allows to change in which order fields will be written to the output stream. | [
"Order",
"allows",
"to",
"change",
"in",
"which",
"order",
"fields",
"will",
"be",
"written",
"to",
"the",
"output",
"stream",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L246-L249 | train |
pelletier/go-toml | marshal.go | SetTagComment | func (e *Encoder) SetTagComment(v string) *Encoder {
e.comment = v
return e
} | go | func (e *Encoder) SetTagComment(v string) *Encoder {
e.comment = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"SetTagComment",
"(",
"v",
"string",
")",
"*",
"Encoder",
"{",
"e",
".",
"comment",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // SetTagComment allows changing default tag "comment" | [
"SetTagComment",
"allows",
"changing",
"default",
"tag",
"comment"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L258-L261 | train |
pelletier/go-toml | marshal.go | SetTagCommented | func (e *Encoder) SetTagCommented(v string) *Encoder {
e.commented = v
return e
} | go | func (e *Encoder) SetTagCommented(v string) *Encoder {
e.commented = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"SetTagCommented",
"(",
"v",
"string",
")",
"*",
"Encoder",
"{",
"e",
".",
"commented",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // SetTagCommented allows changing default tag "commented" | [
"SetTagCommented",
"allows",
"changing",
"default",
"tag",
"commented"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L264-L267 | train |
pelletier/go-toml | marshal.go | SetTagMultiline | func (e *Encoder) SetTagMultiline(v string) *Encoder {
e.multiline = v
return e
} | go | func (e *Encoder) SetTagMultiline(v string) *Encoder {
e.multiline = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"SetTagMultiline",
"(",
"v",
"string",
")",
"*",
"Encoder",
"{",
"e",
".",
"multiline",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // SetTagMultiline allows changing default tag "multiline" | [
"SetTagMultiline",
"allows",
"changing",
"default",
"tag",
"multiline"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L270-L273 | train |
pelletier/go-toml | marshal.go | nextTree | func (e *Encoder) nextTree() *Tree {
return newTreeWithPosition(Position{Line: e.line, Col: 1})
} | go | func (e *Encoder) nextTree() *Tree {
return newTreeWithPosition(Position{Line: e.line, Col: 1})
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"nextTree",
"(",
")",
"*",
"Tree",
"{",
"return",
"newTreeWithPosition",
"(",
"Position",
"{",
"Line",
":",
"e",
".",
"line",
",",
"Col",
":",
"1",
"}",
")",
"\n",
"}"
] | // Create next tree with a position based on Encoder.line | [
"Create",
"next",
"tree",
"with",
"a",
"position",
"based",
"on",
"Encoder",
".",
"line"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L304-L306 | train |
pelletier/go-toml | marshal.go | valueToTree | func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
if mtype.Kind() == reflect.Ptr {
return e.valueToTree(mtype.Elem(), mval.Elem())
}
tval := e.nextTree()
switch mtype.Kind() {
case reflect.Struct:
for i := 0; i < mtype.NumField(); i++ {
mtypef, mvalf := mtype.Field(i), mval.Field(i)
opts := tomlOptions(mtypef, e.annotation)
if opts.include && (!opts.omitempty || !isZero(mvalf)) {
val, err := e.valueToToml(mtypef.Type, mvalf)
if err != nil {
return nil, err
}
tval.SetWithOptions(opts.name, SetOptions{
Comment: opts.comment,
Commented: opts.commented,
Multiline: opts.multiline,
}, val)
}
}
case reflect.Map:
keys := mval.MapKeys()
if e.order == OrderPreserve && len(keys) > 0 {
// Sorting []reflect.Value is not straight forward.
//
// OrderPreserve will support deterministic results when string is used
// as the key to maps.
typ := keys[0].Type()
kind := keys[0].Kind()
if kind == reflect.String {
ikeys := make([]string, len(keys))
for i := range keys {
ikeys[i] = keys[i].Interface().(string)
}
sort.Strings(ikeys)
for i := range ikeys {
keys[i] = reflect.ValueOf(ikeys[i]).Convert(typ)
}
}
}
for _, key := range keys {
mvalf := mval.MapIndex(key)
val, err := e.valueToToml(mtype.Elem(), mvalf)
if err != nil {
return nil, err
}
if e.quoteMapKeys {
keyStr, err := tomlValueStringRepresentation(key.String(), "", e.arraysOneElementPerLine)
if err != nil {
return nil, err
}
tval.SetPath([]string{keyStr}, val)
} else {
tval.Set(key.String(), val)
}
}
}
return tval, nil
} | go | func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
if mtype.Kind() == reflect.Ptr {
return e.valueToTree(mtype.Elem(), mval.Elem())
}
tval := e.nextTree()
switch mtype.Kind() {
case reflect.Struct:
for i := 0; i < mtype.NumField(); i++ {
mtypef, mvalf := mtype.Field(i), mval.Field(i)
opts := tomlOptions(mtypef, e.annotation)
if opts.include && (!opts.omitempty || !isZero(mvalf)) {
val, err := e.valueToToml(mtypef.Type, mvalf)
if err != nil {
return nil, err
}
tval.SetWithOptions(opts.name, SetOptions{
Comment: opts.comment,
Commented: opts.commented,
Multiline: opts.multiline,
}, val)
}
}
case reflect.Map:
keys := mval.MapKeys()
if e.order == OrderPreserve && len(keys) > 0 {
// Sorting []reflect.Value is not straight forward.
//
// OrderPreserve will support deterministic results when string is used
// as the key to maps.
typ := keys[0].Type()
kind := keys[0].Kind()
if kind == reflect.String {
ikeys := make([]string, len(keys))
for i := range keys {
ikeys[i] = keys[i].Interface().(string)
}
sort.Strings(ikeys)
for i := range ikeys {
keys[i] = reflect.ValueOf(ikeys[i]).Convert(typ)
}
}
}
for _, key := range keys {
mvalf := mval.MapIndex(key)
val, err := e.valueToToml(mtype.Elem(), mvalf)
if err != nil {
return nil, err
}
if e.quoteMapKeys {
keyStr, err := tomlValueStringRepresentation(key.String(), "", e.arraysOneElementPerLine)
if err != nil {
return nil, err
}
tval.SetPath([]string{keyStr}, val)
} else {
tval.Set(key.String(), val)
}
}
}
return tval, nil
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToTree",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"*",
"Tree",
",",
"error",
")",
"{",
"if",
"mtype",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{... | // Convert given marshal struct or map value to toml tree | [
"Convert",
"given",
"marshal",
"struct",
"or",
"map",
"value",
"to",
"toml",
"tree"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L309-L370 | train |
pelletier/go-toml | marshal.go | valueToTreeSlice | func (e *Encoder) valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*Tree, error) {
tval := make([]*Tree, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToTree(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | go | func (e *Encoder) valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*Tree, error) {
tval := make([]*Tree, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToTree(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToTreeSlice",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"[",
"]",
"*",
"Tree",
",",
"error",
")",
"{",
"tval",
":=",
"make",
"(",
"[",
"]",
"*",
"Tree",
",",... | // Convert given marshal slice to slice of Toml trees | [
"Convert",
"given",
"marshal",
"slice",
"to",
"slice",
"of",
"Toml",
"trees"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L373-L383 | train |
pelletier/go-toml | marshal.go | valueToOtherSlice | func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
tval := make([]interface{}, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToToml(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | go | func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
tval := make([]interface{}, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToToml(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToOtherSlice",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"tval",
":=",
"make",
"(",
"[",
"]",
"interface",
"{",
... | // Convert given marshal slice to slice of toml values | [
"Convert",
"given",
"marshal",
"slice",
"to",
"slice",
"of",
"toml",
"values"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L386-L396 | train |
pelletier/go-toml | marshal.go | valueToToml | func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
e.line++
if mtype.Kind() == reflect.Ptr {
return e.valueToToml(mtype.Elem(), mval.Elem())
}
switch {
case isCustomMarshaler(mtype):
return callCustomMarshaler(mval)
case isTree(mtype):
return e.valueToTree(mtype, mval)
case isTreeSlice(mtype):
return e.valueToTreeSlice(mtype, mval)
case isOtherSlice(mtype):
return e.valueToOtherSlice(mtype, mval)
default:
switch mtype.Kind() {
case reflect.Bool:
return mval.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if mtype.Kind() == reflect.Int64 && mtype == reflect.TypeOf(time.Duration(1)) {
return fmt.Sprint(mval), nil
}
return mval.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return mval.Uint(), nil
case reflect.Float32, reflect.Float64:
return mval.Float(), nil
case reflect.String:
return mval.String(), nil
case reflect.Struct:
return mval.Interface().(time.Time), nil
default:
return nil, fmt.Errorf("Marshal can't handle %v(%v)", mtype, mtype.Kind())
}
}
} | go | func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
e.line++
if mtype.Kind() == reflect.Ptr {
return e.valueToToml(mtype.Elem(), mval.Elem())
}
switch {
case isCustomMarshaler(mtype):
return callCustomMarshaler(mval)
case isTree(mtype):
return e.valueToTree(mtype, mval)
case isTreeSlice(mtype):
return e.valueToTreeSlice(mtype, mval)
case isOtherSlice(mtype):
return e.valueToOtherSlice(mtype, mval)
default:
switch mtype.Kind() {
case reflect.Bool:
return mval.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if mtype.Kind() == reflect.Int64 && mtype == reflect.TypeOf(time.Duration(1)) {
return fmt.Sprint(mval), nil
}
return mval.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return mval.Uint(), nil
case reflect.Float32, reflect.Float64:
return mval.Float(), nil
case reflect.String:
return mval.String(), nil
case reflect.Struct:
return mval.Interface().(time.Time), nil
default:
return nil, fmt.Errorf("Marshal can't handle %v(%v)", mtype, mtype.Kind())
}
}
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToToml",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"e",
".",
"line",
"++",
"\n",
"if",
"mtype",
".",
"Kind",
"... | // Convert given marshal value to toml value | [
"Convert",
"given",
"marshal",
"value",
"to",
"toml",
"value"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L399-L434 | train |
pelletier/go-toml | marshal.go | Unmarshal | func (t *Tree) Unmarshal(v interface{}) error {
d := Decoder{tval: t, tagName: tagFieldName}
return d.unmarshal(v)
} | go | func (t *Tree) Unmarshal(v interface{}) error {
d := Decoder{tval: t, tagName: tagFieldName}
return d.unmarshal(v)
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"Unmarshal",
"(",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"d",
":=",
"Decoder",
"{",
"tval",
":",
"t",
",",
"tagName",
":",
"tagFieldName",
"}",
"\n",
"return",
"d",
".",
"unmarshal",
"(",
"v",
")",
"\n"... | // Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
// sub-structs, and only definite types can be unmarshaled. | [
"Unmarshal",
"attempts",
"to",
"unmarshal",
"the",
"Tree",
"into",
"a",
"Go",
"struct",
"pointed",
"by",
"v",
".",
"Neither",
"Unmarshaler",
"interfaces",
"nor",
"UnmarshalTOML",
"functions",
"are",
"supported",
"for",
"sub",
"-",
"structs",
"and",
"only",
"de... | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L439-L442 | train |
pelletier/go-toml | marshal.go | Decode | func (d *Decoder) Decode(v interface{}) error {
var err error
d.tval, err = LoadReader(d.r)
if err != nil {
return err
}
return d.unmarshal(v)
} | go | func (d *Decoder) Decode(v interface{}) error {
var err error
d.tval, err = LoadReader(d.r)
if err != nil {
return err
}
return d.unmarshal(v)
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"Decode",
"(",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"var",
"err",
"error",
"\n",
"d",
".",
"tval",
",",
"err",
"=",
"LoadReader",
"(",
"d",
".",
"r",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"... | // Decode reads a TOML-encoded value from it's input
// and unmarshals it in the value pointed at by v.
//
// See the documentation for Marshal for details. | [
"Decode",
"reads",
"a",
"TOML",
"-",
"encoded",
"value",
"from",
"it",
"s",
"input",
"and",
"unmarshals",
"it",
"in",
"the",
"value",
"pointed",
"at",
"by",
"v",
".",
"See",
"the",
"documentation",
"for",
"Marshal",
"for",
"details",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L500-L507 | train |
pelletier/go-toml | marshal.go | valueFromOtherSlice | func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
mval := reflect.MakeSlice(mtype, len(tval), len(tval))
for i := 0; i < len(tval); i++ {
val, err := d.valueFromToml(mtype.Elem(), tval[i])
if err != nil {
return mval, err
}
mval.Index(i).Set(val)
}
return mval, nil
} | go | func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
mval := reflect.MakeSlice(mtype, len(tval), len(tval))
for i := 0; i < len(tval); i++ {
val, err := d.valueFromToml(mtype.Elem(), tval[i])
if err != nil {
return mval, err
}
mval.Index(i).Set(val)
}
return mval, nil
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"valueFromOtherSlice",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"tval",
"[",
"]",
"interface",
"{",
"}",
")",
"(",
"reflect",
".",
"Value",
",",
"error",
")",
"{",
"mval",
":=",
"reflect",
".",
"MakeSlice",
"(... | // Convert toml value to marshal primitive slice, using marshal type | [
"Convert",
"toml",
"value",
"to",
"marshal",
"primitive",
"slice",
"using",
"marshal",
"type"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L638-L648 | train |
pelletier/go-toml | lexer.go | read | func (l *tomlLexer) read() rune {
r := l.peek()
if r == '\n' {
l.endbufferLine++
l.endbufferCol = 1
} else {
l.endbufferCol++
}
l.inputIdx++
return r
} | go | func (l *tomlLexer) read() rune {
r := l.peek()
if r == '\n' {
l.endbufferLine++
l.endbufferCol = 1
} else {
l.endbufferCol++
}
l.inputIdx++
return r
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"read",
"(",
")",
"rune",
"{",
"r",
":=",
"l",
".",
"peek",
"(",
")",
"\n",
"if",
"r",
"==",
"'\\n'",
"{",
"l",
".",
"endbufferLine",
"++",
"\n",
"l",
".",
"endbufferCol",
"=",
"1",
"\n",
"}",
"else",
... | // Basic read operations on input | [
"Basic",
"read",
"operations",
"on",
"input"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L38-L48 | train |
pelletier/go-toml | lexer.go | lexKey | func (l *tomlLexer) lexKey() tomlLexStateFn {
growingString := ""
for r := l.peek(); isKeyChar(r) || r == '\n' || r == '\r'; r = l.peek() {
if r == '"' {
l.next()
str, err := l.lexStringAsString(`"`, false, true)
if err != nil {
return l.errorf(err.Error())
}
growingString += "\"" + str + "\""
l.next()
continue
} else if r == '\'' {
l.next()
str, err := l.lexLiteralStringAsString(`'`, false)
if err != nil {
return l.errorf(err.Error())
}
growingString += "'" + str + "'"
l.next()
continue
} else if r == '\n' {
return l.errorf("keys cannot contain new lines")
} else if isSpace(r) {
break
} else if r == '.' {
// skip
} else if !isValidBareChar(r) {
return l.errorf("keys cannot contain %c character", r)
}
growingString += string(r)
l.next()
}
l.emitWithValue(tokenKey, growingString)
return l.lexVoid
} | go | func (l *tomlLexer) lexKey() tomlLexStateFn {
growingString := ""
for r := l.peek(); isKeyChar(r) || r == '\n' || r == '\r'; r = l.peek() {
if r == '"' {
l.next()
str, err := l.lexStringAsString(`"`, false, true)
if err != nil {
return l.errorf(err.Error())
}
growingString += "\"" + str + "\""
l.next()
continue
} else if r == '\'' {
l.next()
str, err := l.lexLiteralStringAsString(`'`, false)
if err != nil {
return l.errorf(err.Error())
}
growingString += "'" + str + "'"
l.next()
continue
} else if r == '\n' {
return l.errorf("keys cannot contain new lines")
} else if isSpace(r) {
break
} else if r == '.' {
// skip
} else if !isValidBareChar(r) {
return l.errorf("keys cannot contain %c character", r)
}
growingString += string(r)
l.next()
}
l.emitWithValue(tokenKey, growingString)
return l.lexVoid
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexKey",
"(",
")",
"tomlLexStateFn",
"{",
"growingString",
":=",
"\"\"",
"\n",
"for",
"r",
":=",
"l",
".",
"peek",
"(",
")",
";",
"isKeyChar",
"(",
"r",
")",
"||",
"r",
"==",
"'\\n'",
"||",
"r",
"==",
"'\... | // Parse the key and emits its value without escape sequences.
// bare keys, basic string keys and literal string keys are supported. | [
"Parse",
"the",
"key",
"and",
"emits",
"its",
"value",
"without",
"escape",
"sequences",
".",
"bare",
"keys",
"basic",
"string",
"keys",
"and",
"literal",
"string",
"keys",
"are",
"supported",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L302-L338 | train |
pelletier/go-toml | lexer.go | lexStringAsString | func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine, acceptNewLines bool) (string, error) {
growingString := ""
if discardLeadingNewLine {
if l.follow("\r\n") {
l.skip()
l.skip()
} else if l.peek() == '\n' {
l.skip()
}
}
for {
if l.follow(terminator) {
return growingString, nil
}
if l.follow("\\") {
l.next()
switch l.peek() {
case '\r':
fallthrough
case '\n':
fallthrough
case '\t':
fallthrough
case ' ':
// skip all whitespace chars following backslash
for strings.ContainsRune("\r\n\t ", l.peek()) {
l.next()
}
case '"':
growingString += "\""
l.next()
case 'n':
growingString += "\n"
l.next()
case 'b':
growingString += "\b"
l.next()
case 'f':
growingString += "\f"
l.next()
case '/':
growingString += "/"
l.next()
case 't':
growingString += "\t"
l.next()
case 'r':
growingString += "\r"
l.next()
case '\\':
growingString += "\\"
l.next()
case 'u':
l.next()
code := ""
for i := 0; i < 4; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 32)
if err != nil {
return "", errors.New("invalid unicode escape: \\u" + code)
}
growingString += string(rune(intcode))
case 'U':
l.next()
code := ""
for i := 0; i < 8; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 64)
if err != nil {
return "", errors.New("invalid unicode escape: \\U" + code)
}
growingString += string(rune(intcode))
default:
return "", errors.New("invalid escape sequence: \\" + string(l.peek()))
}
} else {
r := l.peek()
if 0x00 <= r && r <= 0x1F && !(acceptNewLines && (r == '\n' || r == '\r')) {
return "", fmt.Errorf("unescaped control character %U", r)
}
l.next()
growingString += string(r)
}
if l.peek() == eof {
break
}
}
return "", errors.New("unclosed string")
} | go | func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine, acceptNewLines bool) (string, error) {
growingString := ""
if discardLeadingNewLine {
if l.follow("\r\n") {
l.skip()
l.skip()
} else if l.peek() == '\n' {
l.skip()
}
}
for {
if l.follow(terminator) {
return growingString, nil
}
if l.follow("\\") {
l.next()
switch l.peek() {
case '\r':
fallthrough
case '\n':
fallthrough
case '\t':
fallthrough
case ' ':
// skip all whitespace chars following backslash
for strings.ContainsRune("\r\n\t ", l.peek()) {
l.next()
}
case '"':
growingString += "\""
l.next()
case 'n':
growingString += "\n"
l.next()
case 'b':
growingString += "\b"
l.next()
case 'f':
growingString += "\f"
l.next()
case '/':
growingString += "/"
l.next()
case 't':
growingString += "\t"
l.next()
case 'r':
growingString += "\r"
l.next()
case '\\':
growingString += "\\"
l.next()
case 'u':
l.next()
code := ""
for i := 0; i < 4; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 32)
if err != nil {
return "", errors.New("invalid unicode escape: \\u" + code)
}
growingString += string(rune(intcode))
case 'U':
l.next()
code := ""
for i := 0; i < 8; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 64)
if err != nil {
return "", errors.New("invalid unicode escape: \\U" + code)
}
growingString += string(rune(intcode))
default:
return "", errors.New("invalid escape sequence: \\" + string(l.peek()))
}
} else {
r := l.peek()
if 0x00 <= r && r <= 0x1F && !(acceptNewLines && (r == '\n' || r == '\r')) {
return "", fmt.Errorf("unescaped control character %U", r)
}
l.next()
growingString += string(r)
}
if l.peek() == eof {
break
}
}
return "", errors.New("unclosed string")
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexStringAsString",
"(",
"terminator",
"string",
",",
"discardLeadingNewLine",
",",
"acceptNewLines",
"bool",
")",
"(",
"string",
",",
"error",
")",
"{",
"growingString",
":=",
"\"\"",
"\n",
"if",
"discardLeadingNewLine"... | // Lex a string and return the results as a string.
// Terminator is the substring indicating the end of the token.
// The resulting string does not include the terminator. | [
"Lex",
"a",
"string",
"and",
"return",
"the",
"results",
"as",
"a",
"string",
".",
"Terminator",
"is",
"the",
"substring",
"indicating",
"the",
"end",
"of",
"the",
"token",
".",
"The",
"resulting",
"string",
"does",
"not",
"include",
"the",
"terminator",
"... | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L414-L520 | train |
pelletier/go-toml | lexer.go | lexInsideTableArrayKey | func (l *tomlLexer) lexInsideTableArrayKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroupArray)
}
l.next()
if l.peek() != ']' {
break
}
l.next()
l.emit(tokenDoubleRightBracket)
return l.lexVoid
case '[':
return l.errorf("table array key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table array key")
} | go | func (l *tomlLexer) lexInsideTableArrayKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroupArray)
}
l.next()
if l.peek() != ']' {
break
}
l.next()
l.emit(tokenDoubleRightBracket)
return l.lexVoid
case '[':
return l.errorf("table array key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table array key")
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexInsideTableArrayKey",
"(",
")",
"tomlLexStateFn",
"{",
"for",
"r",
":=",
"l",
".",
"peek",
"(",
")",
";",
"r",
"!=",
"eof",
";",
"r",
"=",
"l",
".",
"peek",
"(",
")",
"{",
"switch",
"r",
"{",
"case",
... | // Parse the key till "]]", but only bare keys are supported | [
"Parse",
"the",
"key",
"till",
"]]",
"but",
"only",
"bare",
"keys",
"are",
"supported"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L564-L585 | train |
pelletier/go-toml | lexer.go | lexInsideTableKey | func (l *tomlLexer) lexInsideTableKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroup)
}
l.next()
l.emit(tokenRightBracket)
return l.lexVoid
case '[':
return l.errorf("table key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table key")
} | go | func (l *tomlLexer) lexInsideTableKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroup)
}
l.next()
l.emit(tokenRightBracket)
return l.lexVoid
case '[':
return l.errorf("table key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table key")
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexInsideTableKey",
"(",
")",
"tomlLexStateFn",
"{",
"for",
"r",
":=",
"l",
".",
"peek",
"(",
")",
";",
"r",
"!=",
"eof",
";",
"r",
"=",
"l",
".",
"peek",
"(",
")",
"{",
"switch",
"r",
"{",
"case",
"']'... | // Parse the key till "]" but only bare keys are supported | [
"Parse",
"the",
"key",
"till",
"]",
"but",
"only",
"bare",
"keys",
"are",
"supported"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L588-L605 | train |
pelletier/go-toml | toml.go | TreeFromMap | func TreeFromMap(m map[string]interface{}) (*Tree, error) {
result, err := toTree(m)
if err != nil {
return nil, err
}
return result.(*Tree), nil
} | go | func TreeFromMap(m map[string]interface{}) (*Tree, error) {
result, err := toTree(m)
if err != nil {
return nil, err
}
return result.(*Tree), nil
} | [
"func",
"TreeFromMap",
"(",
"m",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
")",
"(",
"*",
"Tree",
",",
"error",
")",
"{",
"result",
",",
"err",
":=",
"toTree",
"(",
"m",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"e... | // TreeFromMap initializes a new Tree object using the given map. | [
"TreeFromMap",
"initializes",
"a",
"new",
"Tree",
"object",
"using",
"the",
"given",
"map",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L41-L47 | train |
pelletier/go-toml | toml.go | Has | func (t *Tree) Has(key string) bool {
if key == "" {
return false
}
return t.HasPath(strings.Split(key, "."))
} | go | func (t *Tree) Has(key string) bool {
if key == "" {
return false
}
return t.HasPath(strings.Split(key, "."))
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"Has",
"(",
"key",
"string",
")",
"bool",
"{",
"if",
"key",
"==",
"\"\"",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"t",
".",
"HasPath",
"(",
"strings",
".",
"Split",
"(",
"key",
",",
"\".\"",
")",
"... | // Has returns a boolean indicating if the given key exists. | [
"Has",
"returns",
"a",
"boolean",
"indicating",
"if",
"the",
"given",
"key",
"exists",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L55-L60 | train |
pelletier/go-toml | toml.go | HasPath | func (t *Tree) HasPath(keys []string) bool {
return t.GetPath(keys) != nil
} | go | func (t *Tree) HasPath(keys []string) bool {
return t.GetPath(keys) != nil
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"HasPath",
"(",
"keys",
"[",
"]",
"string",
")",
"bool",
"{",
"return",
"t",
".",
"GetPath",
"(",
"keys",
")",
"!=",
"nil",
"\n",
"}"
] | // HasPath returns true if the given path of keys exists, false otherwise. | [
"HasPath",
"returns",
"true",
"if",
"the",
"given",
"path",
"of",
"keys",
"exists",
"false",
"otherwise",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L63-L65 | train |
pelletier/go-toml | toml.go | GetPath | func (t *Tree) GetPath(keys []string) interface{} {
if len(keys) == 0 {
return t
}
subtree := t
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return nil
}
switch node := value.(type) {
case *Tree:
subtree = node
case []*Tree:
// go to most recent element
if len(node) == 0 {
return nil
}
subtree = node[len(node)-1]
default:
return nil // cannot navigate through other node types
}
}
// branch based on final node type
switch node := subtree.values[keys[len(keys)-1]].(type) {
case *tomlValue:
return node.value
default:
return node
}
} | go | func (t *Tree) GetPath(keys []string) interface{} {
if len(keys) == 0 {
return t
}
subtree := t
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return nil
}
switch node := value.(type) {
case *Tree:
subtree = node
case []*Tree:
// go to most recent element
if len(node) == 0 {
return nil
}
subtree = node[len(node)-1]
default:
return nil // cannot navigate through other node types
}
}
// branch based on final node type
switch node := subtree.values[keys[len(keys)-1]].(type) {
case *tomlValue:
return node.value
default:
return node
}
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"GetPath",
"(",
"keys",
"[",
"]",
"string",
")",
"interface",
"{",
"}",
"{",
"if",
"len",
"(",
"keys",
")",
"==",
"0",
"{",
"return",
"t",
"\n",
"}",
"\n",
"subtree",
":=",
"t",
"\n",
"for",
"_",
",",
"inte... | // GetPath returns the element in the tree indicated by 'keys'.
// If keys is of length zero, the current tree is returned. | [
"GetPath",
"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#L92-L122 | train |
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",
"(",
... | // 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",
"_",
... | // 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... | // 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... | // 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",
"}",... | // 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",
"for",
"_",
",",
"rr",
":=",
"range",
"value",
"{",
"switch",
"rr",
"{",
"case",
"'\\b'",
":",
"b",
".",
"WriteString",
"(",
"`\\... | // 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",
"prese... | 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",
... | // 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",
... | 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"... | // 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",
"switch",
"v",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"time",
".",
"Ti... | // 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",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"time",
".",... | // 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",
"switch",
"b",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"bool",
":",
"return",
"b",
",",
"nil",
"\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",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"float64",
":",
"return",
"s",
",",
"ni... | // 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",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"int",
":",
"return",
"int32",
"(",
"s",
... | // 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",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"v",
",",
"err",
":=",
"strc... | // 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",
"switch",
"s",
":=",
"i",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"return",
"s"... | // 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",
",",
"\"2006-01-02T15:04:05\"",
",",
"time",
".",
"RFC1123Z",
... | // 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",
")",
"{",
"path",
",",
"_",
":=",
"exec",
".",
"LookPath",
"(",
"\"ip\"",
")",
"\n",
"if",
"path",
"==",
"\"\"",
"{",
"path",
"=",
"\"/sbin/ip\"",
"\n",
"}",
"\n",
"return",
"routeInfo... | // 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",
"socka... | // 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",
"(",
... | // 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",
"(",
"\"sockaddr.Parse\"",
")... | // 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",
")",
"{",
"tmpl",
",",
"err",
":=",
"tmplIn",
".",
"Option",
"(",
"\"mi... | // 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",
"(",
"\"%0128s\"",
",",
"bi",
".",
"Text",
"(",
"2",
")"... | // 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",
"wa... | 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",
"{",
"xIPv6",
":=",
"xAddr",
".",
"FirstUsable",
... | // 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",
"(",
")",
".",
"(",
"IPv6A... | // 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",
"mask",
":=",
"new",
"(",
"big",
".",
"Int",
")",
"... | // 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",
"(",
"\"Unable to create an IPv6Addr from %+q: %v\"",
",... | // 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",
... | // 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",
"... | // 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",
... | // 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",
")",
"... | // 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",
"(",
"\"[%s]:%d\"",
",",
"ipv6",
".",
"NetIP",
"(",
")",
".",
"String",
"(",
")",
",",
"ipv6",
".... | // 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",
"return",
"fn",
"(",
... | // 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",
"if",
"len",
"(",
"ipv6By... | // 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",
"if",
"len",
"(",
"usageText",
")",
"==",
"0",
"{",
"return",
"\"\"",
"\n",
"}",
"\n",
"descriptionText",
":=",
"wordwrap",
".",
"Wrap... | // 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",
"var",
"output",
"[",
"]",
"string",
"\n",
"for",
"_",
",",
"f",
":=",
"range",
"... | // 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",
"if",
"us",
".",
"Path",
"(",
")",
... | // 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",
"(",
"\"Unable to create a UnixSock from %+q: %v\"",
",",
... | // 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",
"return",
"fn",
"(",
"us",
... | // 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",
"return",
"fn",
"(",
"ifA... | // 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",
"}",
... | // 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",
"rfcBlacklist",
":=",
"map",
"[",
"uint",
"]",
"struct",
"{",
"}",
"{",
"ForwardingBlacklist",
... | // 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",
"(",
"[",
"]",
"... | // 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",
"matchedIf... | // 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",
"ifAddrs",
":=",
"make",
... | // 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",... | 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",
"defaultIfName",
",",
"err",
":=",
... | // 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",
"!=",... | // 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",
"!=... | // 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",
",",
... | // 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",
":=",
"ran... | // 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",
"|",
... | 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... | // 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"... | // 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",
... | // 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",
"switch",
"strings",
".",
"ToLower",
"... | // 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",
"clauses",
":=",
"strings"... | // 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",
"ifs",
":=",
"make",
"(",
"IfAddrs",
",",
"0... | // 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",
")",
... | // 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",
")",
"{",
"if",
"int",
"(",
"lim",
")",
">",
"len",
"(",
"in",
")",
"{",
"lim",
"=",
"uint",
"(",
"len",
"(",
"in",
")",
")",
"\n",
"}",
"\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",
... | // 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",
"{",
"ret... | // 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",
"\\n",
"\n",
"ifNameRe",
":=",
"ifNa... | // 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",
"(",
"\"%032s\"",
",",
"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",
"wa... | 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"... | // 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",
"(",
")",
... | // 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.