repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
sequence | docstring
stringlengths 6
2.61k
| docstring_tokens
sequence | sha
stringlengths 40
40
| url
stringlengths 85
252
| partition
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|
open-policy-agent/opa | topdown/trace.go | HasBody | func (evt *Event) HasBody() bool {
_, ok := evt.Node.(ast.Body)
return ok
} | go | func (evt *Event) HasBody() bool {
_, ok := evt.Node.(ast.Body)
return ok
} | [
"func",
"(",
"evt",
"*",
"Event",
")",
"HasBody",
"(",
")",
"bool",
"{",
"_",
",",
"ok",
":=",
"evt",
".",
"Node",
".",
"(",
"ast",
".",
"Body",
")",
"\n",
"return",
"ok",
"\n",
"}"
] | // HasBody returns true if the Event contains an ast.Body. | [
"HasBody",
"returns",
"true",
"if",
"the",
"Event",
"contains",
"an",
"ast",
".",
"Body",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/trace.go#L64-L67 | train |
open-policy-agent/opa | topdown/trace.go | HasExpr | func (evt *Event) HasExpr() bool {
_, ok := evt.Node.(*ast.Expr)
return ok
} | go | func (evt *Event) HasExpr() bool {
_, ok := evt.Node.(*ast.Expr)
return ok
} | [
"func",
"(",
"evt",
"*",
"Event",
")",
"HasExpr",
"(",
")",
"bool",
"{",
"_",
",",
"ok",
":=",
"evt",
".",
"Node",
".",
"(",
"*",
"ast",
".",
"Expr",
")",
"\n",
"return",
"ok",
"\n",
"}"
] | // HasExpr returns true if the Event contains an ast.Expr. | [
"HasExpr",
"returns",
"true",
"if",
"the",
"Event",
"contains",
"an",
"ast",
".",
"Expr",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/trace.go#L70-L73 | train |
open-policy-agent/opa | topdown/trace.go | Equal | func (evt *Event) Equal(other *Event) bool {
if evt.Op != other.Op {
return false
}
if evt.QueryID != other.QueryID {
return false
}
if evt.ParentID != other.ParentID {
return false
}
if !evt.equalNodes(other) {
return false
}
return evt.Locals.Equal(other.Locals)
} | go | func (evt *Event) Equal(other *Event) bool {
if evt.Op != other.Op {
return false
}
if evt.QueryID != other.QueryID {
return false
}
if evt.ParentID != other.ParentID {
return false
}
if !evt.equalNodes(other) {
return false
}
return evt.Locals.Equal(other.Locals)
} | [
"func",
"(",
"evt",
"*",
"Event",
")",
"Equal",
"(",
"other",
"*",
"Event",
")",
"bool",
"{",
"if",
"evt",
".",
"Op",
"!=",
"other",
".",
"Op",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"evt",
".",
"QueryID",
"!=",
"other",
".",
"QueryID",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"evt",
".",
"ParentID",
"!=",
"other",
".",
"ParentID",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"!",
"evt",
".",
"equalNodes",
"(",
"other",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"evt",
".",
"Locals",
".",
"Equal",
"(",
"other",
".",
"Locals",
")",
"\n",
"}"
] | // Equal returns true if this event is equal to the other event. | [
"Equal",
"returns",
"true",
"if",
"this",
"event",
"is",
"equal",
"to",
"the",
"other",
"event",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/trace.go#L76-L90 | train |
open-policy-agent/opa | topdown/trace.go | PrettyTrace | func PrettyTrace(w io.Writer, trace []*Event) {
depths := depths{}
for _, event := range trace {
depth := depths.GetOrSet(event.QueryID, event.ParentID)
fmt.Fprintln(w, formatEvent(event, depth))
}
} | go | func PrettyTrace(w io.Writer, trace []*Event) {
depths := depths{}
for _, event := range trace {
depth := depths.GetOrSet(event.QueryID, event.ParentID)
fmt.Fprintln(w, formatEvent(event, depth))
}
} | [
"func",
"PrettyTrace",
"(",
"w",
"io",
".",
"Writer",
",",
"trace",
"[",
"]",
"*",
"Event",
")",
"{",
"depths",
":=",
"depths",
"{",
"}",
"\n",
"for",
"_",
",",
"event",
":=",
"range",
"trace",
"{",
"depth",
":=",
"depths",
".",
"GetOrSet",
"(",
"event",
".",
"QueryID",
",",
"event",
".",
"ParentID",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"w",
",",
"formatEvent",
"(",
"event",
",",
"depth",
")",
")",
"\n",
"}",
"\n",
"}"
] | // PrettyTrace pretty prints the trace to the writer. | [
"PrettyTrace",
"pretty",
"prints",
"the",
"trace",
"to",
"the",
"writer",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/trace.go#L145-L151 | train |
open-policy-agent/opa | plugins/plugins.go | New | func New(raw []byte, id string, store storage.Store, opts ...func(*Manager)) (*Manager, error) {
parsedConfig, err := config.ParseConfig(raw, id)
if err != nil {
return nil, err
}
services, err := parseServicesConfig(parsedConfig.Services)
if err != nil {
return nil, err
}
m := &Manager{
Store: store,
Config: parsedConfig,
ID: id,
services: services,
}
for _, f := range opts {
f(m)
}
return m, nil
} | go | func New(raw []byte, id string, store storage.Store, opts ...func(*Manager)) (*Manager, error) {
parsedConfig, err := config.ParseConfig(raw, id)
if err != nil {
return nil, err
}
services, err := parseServicesConfig(parsedConfig.Services)
if err != nil {
return nil, err
}
m := &Manager{
Store: store,
Config: parsedConfig,
ID: id,
services: services,
}
for _, f := range opts {
f(m)
}
return m, nil
} | [
"func",
"New",
"(",
"raw",
"[",
"]",
"byte",
",",
"id",
"string",
",",
"store",
"storage",
".",
"Store",
",",
"opts",
"...",
"func",
"(",
"*",
"Manager",
")",
")",
"(",
"*",
"Manager",
",",
"error",
")",
"{",
"parsedConfig",
",",
"err",
":=",
"config",
".",
"ParseConfig",
"(",
"raw",
",",
"id",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"services",
",",
"err",
":=",
"parseServicesConfig",
"(",
"parsedConfig",
".",
"Services",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"m",
":=",
"&",
"Manager",
"{",
"Store",
":",
"store",
",",
"Config",
":",
"parsedConfig",
",",
"ID",
":",
"id",
",",
"services",
":",
"services",
",",
"}",
"\n\n",
"for",
"_",
",",
"f",
":=",
"range",
"opts",
"{",
"f",
"(",
"m",
")",
"\n",
"}",
"\n\n",
"return",
"m",
",",
"nil",
"\n",
"}"
] | // New creates a new Manager using config. | [
"New",
"creates",
"a",
"new",
"Manager",
"using",
"config",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L116-L140 | train |
open-policy-agent/opa | plugins/plugins.go | Labels | func (m *Manager) Labels() map[string]string {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.Config.Labels
} | go | func (m *Manager) Labels() map[string]string {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.Config.Labels
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Labels",
"(",
")",
"map",
"[",
"string",
"]",
"string",
"{",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"return",
"m",
".",
"Config",
".",
"Labels",
"\n",
"}"
] | // Labels returns the set of labels from the configuration. | [
"Labels",
"returns",
"the",
"set",
"of",
"labels",
"from",
"the",
"configuration",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L143-L147 | train |
open-policy-agent/opa | plugins/plugins.go | Register | func (m *Manager) Register(name string, plugin Plugin) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.plugins = append(m.plugins, namedplugin{
name: name,
plugin: plugin,
})
} | go | func (m *Manager) Register(name string, plugin Plugin) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.plugins = append(m.plugins, namedplugin{
name: name,
plugin: plugin,
})
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Register",
"(",
"name",
"string",
",",
"plugin",
"Plugin",
")",
"{",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"m",
".",
"plugins",
"=",
"append",
"(",
"m",
".",
"plugins",
",",
"namedplugin",
"{",
"name",
":",
"name",
",",
"plugin",
":",
"plugin",
",",
"}",
")",
"\n",
"}"
] | // Register adds a plugin to the manager. When the manager is started, all of
// the plugins will be started. | [
"Register",
"adds",
"a",
"plugin",
"to",
"the",
"manager",
".",
"When",
"the",
"manager",
"is",
"started",
"all",
"of",
"the",
"plugins",
"will",
"be",
"started",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L151-L158 | train |
open-policy-agent/opa | plugins/plugins.go | Plugins | func (m *Manager) Plugins() []string {
m.mtx.Lock()
defer m.mtx.Unlock()
result := make([]string, len(m.plugins))
for i := range m.plugins {
result[i] = m.plugins[i].name
}
return result
} | go | func (m *Manager) Plugins() []string {
m.mtx.Lock()
defer m.mtx.Unlock()
result := make([]string, len(m.plugins))
for i := range m.plugins {
result[i] = m.plugins[i].name
}
return result
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Plugins",
"(",
")",
"[",
"]",
"string",
"{",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"result",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"m",
".",
"plugins",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"m",
".",
"plugins",
"{",
"result",
"[",
"i",
"]",
"=",
"m",
".",
"plugins",
"[",
"i",
"]",
".",
"name",
"\n",
"}",
"\n",
"return",
"result",
"\n",
"}"
] | // Plugins returns the list of plugins registered with the manager. | [
"Plugins",
"returns",
"the",
"list",
"of",
"plugins",
"registered",
"with",
"the",
"manager",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L161-L169 | train |
open-policy-agent/opa | plugins/plugins.go | Plugin | func (m *Manager) Plugin(name string) Plugin {
m.mtx.Lock()
defer m.mtx.Unlock()
for i := range m.plugins {
if m.plugins[i].name == name {
return m.plugins[i].plugin
}
}
return nil
} | go | func (m *Manager) Plugin(name string) Plugin {
m.mtx.Lock()
defer m.mtx.Unlock()
for i := range m.plugins {
if m.plugins[i].name == name {
return m.plugins[i].plugin
}
}
return nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Plugin",
"(",
"name",
"string",
")",
"Plugin",
"{",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"for",
"i",
":=",
"range",
"m",
".",
"plugins",
"{",
"if",
"m",
".",
"plugins",
"[",
"i",
"]",
".",
"name",
"==",
"name",
"{",
"return",
"m",
".",
"plugins",
"[",
"i",
"]",
".",
"plugin",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Plugin returns the plugin registered with name or nil if name is not found. | [
"Plugin",
"returns",
"the",
"plugin",
"registered",
"with",
"name",
"or",
"nil",
"if",
"name",
"is",
"not",
"found",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L172-L181 | train |
open-policy-agent/opa | plugins/plugins.go | GetCompiler | func (m *Manager) GetCompiler() *ast.Compiler {
m.compilerMux.RLock()
defer m.compilerMux.RUnlock()
return m.compiler
} | go | func (m *Manager) GetCompiler() *ast.Compiler {
m.compilerMux.RLock()
defer m.compilerMux.RUnlock()
return m.compiler
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"GetCompiler",
"(",
")",
"*",
"ast",
".",
"Compiler",
"{",
"m",
".",
"compilerMux",
".",
"RLock",
"(",
")",
"\n",
"defer",
"m",
".",
"compilerMux",
".",
"RUnlock",
"(",
")",
"\n",
"return",
"m",
".",
"compiler",
"\n",
"}"
] | // GetCompiler returns the manager's compiler. | [
"GetCompiler",
"returns",
"the",
"manager",
"s",
"compiler",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L184-L188 | train |
open-policy-agent/opa | plugins/plugins.go | RegisterCompilerTrigger | func (m *Manager) RegisterCompilerTrigger(f func(txn storage.Transaction)) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.registeredTriggers = append(m.registeredTriggers, f)
} | go | func (m *Manager) RegisterCompilerTrigger(f func(txn storage.Transaction)) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.registeredTriggers = append(m.registeredTriggers, f)
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"RegisterCompilerTrigger",
"(",
"f",
"func",
"(",
"txn",
"storage",
".",
"Transaction",
")",
")",
"{",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"m",
".",
"registeredTriggers",
"=",
"append",
"(",
"m",
".",
"registeredTriggers",
",",
"f",
")",
"\n",
"}"
] | // RegisterCompilerTrigger registers for change notifications when the compiler
// is changed. | [
"RegisterCompilerTrigger",
"registers",
"for",
"change",
"notifications",
"when",
"the",
"compiler",
"is",
"changed",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L198-L202 | train |
open-policy-agent/opa | plugins/plugins.go | Start | func (m *Manager) Start(ctx context.Context) error {
if m == nil {
return nil
}
err := storage.Txn(ctx, m.Store, storage.TransactionParams{}, func(txn storage.Transaction) error {
compiler, err := loadCompilerFromStore(ctx, m.Store, txn)
if err != nil {
return err
}
m.setCompiler(compiler)
return nil
})
if err != nil {
return err
}
if err := func() error {
m.mtx.Lock()
defer m.mtx.Unlock()
for _, p := range m.plugins {
if err := p.plugin.Start(ctx); err != nil {
return err
}
}
return nil
}(); err != nil {
return err
}
config := storage.TriggerConfig{OnCommit: m.onCommit}
return storage.Txn(ctx, m.Store, storage.WriteParams, func(txn storage.Transaction) error {
_, err := m.Store.Register(ctx, txn, config)
return err
})
} | go | func (m *Manager) Start(ctx context.Context) error {
if m == nil {
return nil
}
err := storage.Txn(ctx, m.Store, storage.TransactionParams{}, func(txn storage.Transaction) error {
compiler, err := loadCompilerFromStore(ctx, m.Store, txn)
if err != nil {
return err
}
m.setCompiler(compiler)
return nil
})
if err != nil {
return err
}
if err := func() error {
m.mtx.Lock()
defer m.mtx.Unlock()
for _, p := range m.plugins {
if err := p.plugin.Start(ctx); err != nil {
return err
}
}
return nil
}(); err != nil {
return err
}
config := storage.TriggerConfig{OnCommit: m.onCommit}
return storage.Txn(ctx, m.Store, storage.WriteParams, func(txn storage.Transaction) error {
_, err := m.Store.Register(ctx, txn, config)
return err
})
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Start",
"(",
"ctx",
"context",
".",
"Context",
")",
"error",
"{",
"if",
"m",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"err",
":=",
"storage",
".",
"Txn",
"(",
"ctx",
",",
"m",
".",
"Store",
",",
"storage",
".",
"TransactionParams",
"{",
"}",
",",
"func",
"(",
"txn",
"storage",
".",
"Transaction",
")",
"error",
"{",
"compiler",
",",
"err",
":=",
"loadCompilerFromStore",
"(",
"ctx",
",",
"m",
".",
"Store",
",",
"txn",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"m",
".",
"setCompiler",
"(",
"compiler",
")",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"func",
"(",
")",
"error",
"{",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n\n",
"for",
"_",
",",
"p",
":=",
"range",
"m",
".",
"plugins",
"{",
"if",
"err",
":=",
"p",
".",
"plugin",
".",
"Start",
"(",
"ctx",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"config",
":=",
"storage",
".",
"TriggerConfig",
"{",
"OnCommit",
":",
"m",
".",
"onCommit",
"}",
"\n\n",
"return",
"storage",
".",
"Txn",
"(",
"ctx",
",",
"m",
".",
"Store",
",",
"storage",
".",
"WriteParams",
",",
"func",
"(",
"txn",
"storage",
".",
"Transaction",
")",
"error",
"{",
"_",
",",
"err",
":=",
"m",
".",
"Store",
".",
"Register",
"(",
"ctx",
",",
"txn",
",",
"config",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"}"
] | // Start starts the manager. | [
"Start",
"starts",
"the",
"manager",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L205-L244 | train |
open-policy-agent/opa | plugins/plugins.go | Stop | func (m *Manager) Stop(ctx context.Context) {
m.mtx.Lock()
defer m.mtx.Unlock()
for _, p := range m.plugins {
p.plugin.Stop(ctx)
}
} | go | func (m *Manager) Stop(ctx context.Context) {
m.mtx.Lock()
defer m.mtx.Unlock()
for _, p := range m.plugins {
p.plugin.Stop(ctx)
}
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Stop",
"(",
"ctx",
"context",
".",
"Context",
")",
"{",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"for",
"_",
",",
"p",
":=",
"range",
"m",
".",
"plugins",
"{",
"p",
".",
"plugin",
".",
"Stop",
"(",
"ctx",
")",
"\n",
"}",
"\n",
"}"
] | // Stop stops the manager, stopping all the plugins registered with it | [
"Stop",
"stops",
"the",
"manager",
"stopping",
"all",
"the",
"plugins",
"registered",
"with",
"it"
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L247-L253 | train |
open-policy-agent/opa | plugins/plugins.go | Reconfigure | func (m *Manager) Reconfigure(config *config.Config) error {
services, err := parseServicesConfig(config.Services)
if err != nil {
return err
}
m.mtx.Lock()
defer m.mtx.Unlock()
config.Labels = m.Config.Labels // don't overwrite labels
m.Config = config
for name, client := range services {
m.services[name] = client
}
return nil
} | go | func (m *Manager) Reconfigure(config *config.Config) error {
services, err := parseServicesConfig(config.Services)
if err != nil {
return err
}
m.mtx.Lock()
defer m.mtx.Unlock()
config.Labels = m.Config.Labels // don't overwrite labels
m.Config = config
for name, client := range services {
m.services[name] = client
}
return nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Reconfigure",
"(",
"config",
"*",
"config",
".",
"Config",
")",
"error",
"{",
"services",
",",
"err",
":=",
"parseServicesConfig",
"(",
"config",
".",
"Services",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"m",
".",
"mtx",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"mtx",
".",
"Unlock",
"(",
")",
"\n",
"config",
".",
"Labels",
"=",
"m",
".",
"Config",
".",
"Labels",
"// don't overwrite labels",
"\n",
"m",
".",
"Config",
"=",
"config",
"\n",
"for",
"name",
",",
"client",
":=",
"range",
"services",
"{",
"m",
".",
"services",
"[",
"name",
"]",
"=",
"client",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Reconfigure updates the configuration on the manager. | [
"Reconfigure",
"updates",
"the",
"configuration",
"on",
"the",
"manager",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L256-L269 | train |
open-policy-agent/opa | plugins/plugins.go | Client | func (m *Manager) Client(name string) rest.Client {
return m.services[name]
} | go | func (m *Manager) Client(name string) rest.Client {
return m.services[name]
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Client",
"(",
"name",
"string",
")",
"rest",
".",
"Client",
"{",
"return",
"m",
".",
"services",
"[",
"name",
"]",
"\n",
"}"
] | // Client returns a client for communicating with a remote service. | [
"Client",
"returns",
"a",
"client",
"for",
"communicating",
"with",
"a",
"remote",
"service",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L306-L308 | train |
open-policy-agent/opa | plugins/plugins.go | Services | func (m *Manager) Services() []string {
s := make([]string, 0, len(m.services))
for name := range m.services {
s = append(s, name)
}
return s
} | go | func (m *Manager) Services() []string {
s := make([]string, 0, len(m.services))
for name := range m.services {
s = append(s, name)
}
return s
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Services",
"(",
")",
"[",
"]",
"string",
"{",
"s",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"0",
",",
"len",
"(",
"m",
".",
"services",
")",
")",
"\n",
"for",
"name",
":=",
"range",
"m",
".",
"services",
"{",
"s",
"=",
"append",
"(",
"s",
",",
"name",
")",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // Services returns a list of services that m can provide clients for. | [
"Services",
"returns",
"a",
"list",
"of",
"services",
"that",
"m",
"can",
"provide",
"clients",
"for",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/plugins/plugins.go#L311-L317 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | Get | func (c Cache) Get(k interface{}) (interface{}, bool) {
v, ok := c[k]
return v, ok
} | go | func (c Cache) Get(k interface{}) (interface{}, bool) {
v, ok := c[k]
return v, ok
} | [
"func",
"(",
"c",
"Cache",
")",
"Get",
"(",
"k",
"interface",
"{",
"}",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
")",
"{",
"v",
",",
"ok",
":=",
"c",
"[",
"k",
"]",
"\n",
"return",
"v",
",",
"ok",
"\n",
"}"
] | // Get returns the cached value for k. | [
"Get",
"returns",
"the",
"cached",
"value",
"for",
"k",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L26-L29 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | NewOperandErr | func NewOperandErr(pos int, f string, a ...interface{}) error {
f = fmt.Sprintf("operand %v ", pos) + f
return ErrOperand(fmt.Sprintf(f, a...))
} | go | func NewOperandErr(pos int, f string, a ...interface{}) error {
f = fmt.Sprintf("operand %v ", pos) + f
return ErrOperand(fmt.Sprintf(f, a...))
} | [
"func",
"NewOperandErr",
"(",
"pos",
"int",
",",
"f",
"string",
",",
"a",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"f",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"pos",
")",
"+",
"f",
"\n",
"return",
"ErrOperand",
"(",
"fmt",
".",
"Sprintf",
"(",
"f",
",",
"a",
"...",
")",
")",
"\n",
"}"
] | // NewOperandErr returns a generic operand error. | [
"NewOperandErr",
"returns",
"a",
"generic",
"operand",
"error",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L41-L44 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | NewOperandTypeErr | func NewOperandTypeErr(pos int, got ast.Value, expected ...string) error {
if len(expected) == 1 {
return NewOperandErr(pos, "must be %v but got %v", expected[0], ast.TypeName(got))
}
return NewOperandErr(pos, "must be one of {%v} but got %v", strings.Join(expected, ", "), ast.TypeName(got))
} | go | func NewOperandTypeErr(pos int, got ast.Value, expected ...string) error {
if len(expected) == 1 {
return NewOperandErr(pos, "must be %v but got %v", expected[0], ast.TypeName(got))
}
return NewOperandErr(pos, "must be one of {%v} but got %v", strings.Join(expected, ", "), ast.TypeName(got))
} | [
"func",
"NewOperandTypeErr",
"(",
"pos",
"int",
",",
"got",
"ast",
".",
"Value",
",",
"expected",
"...",
"string",
")",
"error",
"{",
"if",
"len",
"(",
"expected",
")",
"==",
"1",
"{",
"return",
"NewOperandErr",
"(",
"pos",
",",
"\"",
"\"",
",",
"expected",
"[",
"0",
"]",
",",
"ast",
".",
"TypeName",
"(",
"got",
")",
")",
"\n",
"}",
"\n\n",
"return",
"NewOperandErr",
"(",
"pos",
",",
"\"",
"\"",
",",
"strings",
".",
"Join",
"(",
"expected",
",",
"\"",
"\"",
")",
",",
"ast",
".",
"TypeName",
"(",
"got",
")",
")",
"\n",
"}"
] | // NewOperandTypeErr returns an operand error indicating the operand's type was wrong. | [
"NewOperandTypeErr",
"returns",
"an",
"operand",
"error",
"indicating",
"the",
"operand",
"s",
"type",
"was",
"wrong",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L47-L54 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | NewOperandElementErr | func NewOperandElementErr(pos int, composite ast.Value, got ast.Value, expected ...string) error {
tpe := ast.TypeName(composite)
if len(expected) == 1 {
return NewOperandErr(pos, "must be %v of %vs but got %v containing %v", tpe, expected[0], tpe, ast.TypeName(got))
}
return NewOperandErr(pos, "must be %v of (any of) {%v} but got %v containing %v", tpe, strings.Join(expected, ", "), tpe, ast.TypeName(got))
} | go | func NewOperandElementErr(pos int, composite ast.Value, got ast.Value, expected ...string) error {
tpe := ast.TypeName(composite)
if len(expected) == 1 {
return NewOperandErr(pos, "must be %v of %vs but got %v containing %v", tpe, expected[0], tpe, ast.TypeName(got))
}
return NewOperandErr(pos, "must be %v of (any of) {%v} but got %v containing %v", tpe, strings.Join(expected, ", "), tpe, ast.TypeName(got))
} | [
"func",
"NewOperandElementErr",
"(",
"pos",
"int",
",",
"composite",
"ast",
".",
"Value",
",",
"got",
"ast",
".",
"Value",
",",
"expected",
"...",
"string",
")",
"error",
"{",
"tpe",
":=",
"ast",
".",
"TypeName",
"(",
"composite",
")",
"\n\n",
"if",
"len",
"(",
"expected",
")",
"==",
"1",
"{",
"return",
"NewOperandErr",
"(",
"pos",
",",
"\"",
"\"",
",",
"tpe",
",",
"expected",
"[",
"0",
"]",
",",
"tpe",
",",
"ast",
".",
"TypeName",
"(",
"got",
")",
")",
"\n",
"}",
"\n\n",
"return",
"NewOperandErr",
"(",
"pos",
",",
"\"",
"\"",
",",
"tpe",
",",
"strings",
".",
"Join",
"(",
"expected",
",",
"\"",
"\"",
")",
",",
"tpe",
",",
"ast",
".",
"TypeName",
"(",
"got",
")",
")",
"\n",
"}"
] | // NewOperandElementErr returns an operand error indicating an element in the
// composite operand was wrong. | [
"NewOperandElementErr",
"returns",
"an",
"operand",
"error",
"indicating",
"an",
"element",
"in",
"the",
"composite",
"operand",
"was",
"wrong",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L58-L67 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | NewOperandEnumErr | func NewOperandEnumErr(pos int, expected ...string) error {
if len(expected) == 1 {
return NewOperandErr(pos, "must be %v", expected[0])
}
return NewOperandErr(pos, "must be one of {%v}", strings.Join(expected, ", "))
} | go | func NewOperandEnumErr(pos int, expected ...string) error {
if len(expected) == 1 {
return NewOperandErr(pos, "must be %v", expected[0])
}
return NewOperandErr(pos, "must be one of {%v}", strings.Join(expected, ", "))
} | [
"func",
"NewOperandEnumErr",
"(",
"pos",
"int",
",",
"expected",
"...",
"string",
")",
"error",
"{",
"if",
"len",
"(",
"expected",
")",
"==",
"1",
"{",
"return",
"NewOperandErr",
"(",
"pos",
",",
"\"",
"\"",
",",
"expected",
"[",
"0",
"]",
")",
"\n",
"}",
"\n\n",
"return",
"NewOperandErr",
"(",
"pos",
",",
"\"",
"\"",
",",
"strings",
".",
"Join",
"(",
"expected",
",",
"\"",
"\"",
")",
")",
"\n",
"}"
] | // NewOperandEnumErr returns an operand error indicating a value was wrong. | [
"NewOperandEnumErr",
"returns",
"an",
"operand",
"error",
"indicating",
"a",
"value",
"was",
"wrong",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L70-L77 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | IntOperand | func IntOperand(x ast.Value, pos int) (int, error) {
n, ok := x.(ast.Number)
if !ok {
return 0, NewOperandTypeErr(pos, x, "number")
}
i, ok := n.Int()
if !ok {
return 0, NewOperandErr(pos, "must be integer number but got floating-point number")
}
return i, nil
} | go | func IntOperand(x ast.Value, pos int) (int, error) {
n, ok := x.(ast.Number)
if !ok {
return 0, NewOperandTypeErr(pos, x, "number")
}
i, ok := n.Int()
if !ok {
return 0, NewOperandErr(pos, "must be integer number but got floating-point number")
}
return i, nil
} | [
"func",
"IntOperand",
"(",
"x",
"ast",
".",
"Value",
",",
"pos",
"int",
")",
"(",
"int",
",",
"error",
")",
"{",
"n",
",",
"ok",
":=",
"x",
".",
"(",
"ast",
".",
"Number",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
",",
"NewOperandTypeErr",
"(",
"pos",
",",
"x",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"i",
",",
"ok",
":=",
"n",
".",
"Int",
"(",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
",",
"NewOperandErr",
"(",
"pos",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"return",
"i",
",",
"nil",
"\n",
"}"
] | // IntOperand converts x to an int. If the cast fails, a descriptive error is
// returned. | [
"IntOperand",
"converts",
"x",
"to",
"an",
"int",
".",
"If",
"the",
"cast",
"fails",
"a",
"descriptive",
"error",
"is",
"returned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L81-L93 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | NumberOperand | func NumberOperand(x ast.Value, pos int) (ast.Number, error) {
n, ok := x.(ast.Number)
if !ok {
return ast.Number(""), NewOperandTypeErr(pos, x, "number")
}
return n, nil
} | go | func NumberOperand(x ast.Value, pos int) (ast.Number, error) {
n, ok := x.(ast.Number)
if !ok {
return ast.Number(""), NewOperandTypeErr(pos, x, "number")
}
return n, nil
} | [
"func",
"NumberOperand",
"(",
"x",
"ast",
".",
"Value",
",",
"pos",
"int",
")",
"(",
"ast",
".",
"Number",
",",
"error",
")",
"{",
"n",
",",
"ok",
":=",
"x",
".",
"(",
"ast",
".",
"Number",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"ast",
".",
"Number",
"(",
"\"",
"\"",
")",
",",
"NewOperandTypeErr",
"(",
"pos",
",",
"x",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"n",
",",
"nil",
"\n",
"}"
] | // NumberOperand converts x to a number. If the cast fails, a descriptive error is
// returned. | [
"NumberOperand",
"converts",
"x",
"to",
"a",
"number",
".",
"If",
"the",
"cast",
"fails",
"a",
"descriptive",
"error",
"is",
"returned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L97-L103 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | SetOperand | func SetOperand(x ast.Value, pos int) (ast.Set, error) {
s, ok := x.(ast.Set)
if !ok {
return nil, NewOperandTypeErr(pos, x, "set")
}
return s, nil
} | go | func SetOperand(x ast.Value, pos int) (ast.Set, error) {
s, ok := x.(ast.Set)
if !ok {
return nil, NewOperandTypeErr(pos, x, "set")
}
return s, nil
} | [
"func",
"SetOperand",
"(",
"x",
"ast",
".",
"Value",
",",
"pos",
"int",
")",
"(",
"ast",
".",
"Set",
",",
"error",
")",
"{",
"s",
",",
"ok",
":=",
"x",
".",
"(",
"ast",
".",
"Set",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"NewOperandTypeErr",
"(",
"pos",
",",
"x",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"s",
",",
"nil",
"\n",
"}"
] | // SetOperand converts x to a set. If the cast fails, a descriptive error is
// returned. | [
"SetOperand",
"converts",
"x",
"to",
"a",
"set",
".",
"If",
"the",
"cast",
"fails",
"a",
"descriptive",
"error",
"is",
"returned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L107-L113 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | StringOperand | func StringOperand(x ast.Value, pos int) (ast.String, error) {
s, ok := x.(ast.String)
if !ok {
return ast.String(""), NewOperandTypeErr(pos, x, "string")
}
return s, nil
} | go | func StringOperand(x ast.Value, pos int) (ast.String, error) {
s, ok := x.(ast.String)
if !ok {
return ast.String(""), NewOperandTypeErr(pos, x, "string")
}
return s, nil
} | [
"func",
"StringOperand",
"(",
"x",
"ast",
".",
"Value",
",",
"pos",
"int",
")",
"(",
"ast",
".",
"String",
",",
"error",
")",
"{",
"s",
",",
"ok",
":=",
"x",
".",
"(",
"ast",
".",
"String",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"ast",
".",
"String",
"(",
"\"",
"\"",
")",
",",
"NewOperandTypeErr",
"(",
"pos",
",",
"x",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"s",
",",
"nil",
"\n",
"}"
] | // StringOperand converts x to a string. If the cast fails, a descriptive error is
// returned. | [
"StringOperand",
"converts",
"x",
"to",
"a",
"string",
".",
"If",
"the",
"cast",
"fails",
"a",
"descriptive",
"error",
"is",
"returned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L117-L123 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | ObjectOperand | func ObjectOperand(x ast.Value, pos int) (ast.Object, error) {
o, ok := x.(ast.Object)
if !ok {
return nil, NewOperandTypeErr(pos, x, "object")
}
return o, nil
} | go | func ObjectOperand(x ast.Value, pos int) (ast.Object, error) {
o, ok := x.(ast.Object)
if !ok {
return nil, NewOperandTypeErr(pos, x, "object")
}
return o, nil
} | [
"func",
"ObjectOperand",
"(",
"x",
"ast",
".",
"Value",
",",
"pos",
"int",
")",
"(",
"ast",
".",
"Object",
",",
"error",
")",
"{",
"o",
",",
"ok",
":=",
"x",
".",
"(",
"ast",
".",
"Object",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"NewOperandTypeErr",
"(",
"pos",
",",
"x",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"o",
",",
"nil",
"\n",
"}"
] | // ObjectOperand converts x to an object. If the cast fails, a descriptive
// error is returned. | [
"ObjectOperand",
"converts",
"x",
"to",
"an",
"object",
".",
"If",
"the",
"cast",
"fails",
"a",
"descriptive",
"error",
"is",
"returned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L127-L133 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | ArrayOperand | func ArrayOperand(x ast.Value, pos int) (ast.Array, error) {
a, ok := x.(ast.Array)
if !ok {
return nil, NewOperandTypeErr(pos, x, "array")
}
return a, nil
} | go | func ArrayOperand(x ast.Value, pos int) (ast.Array, error) {
a, ok := x.(ast.Array)
if !ok {
return nil, NewOperandTypeErr(pos, x, "array")
}
return a, nil
} | [
"func",
"ArrayOperand",
"(",
"x",
"ast",
".",
"Value",
",",
"pos",
"int",
")",
"(",
"ast",
".",
"Array",
",",
"error",
")",
"{",
"a",
",",
"ok",
":=",
"x",
".",
"(",
"ast",
".",
"Array",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"NewOperandTypeErr",
"(",
"pos",
",",
"x",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"a",
",",
"nil",
"\n",
"}"
] | // ArrayOperand converts x to an array. If the cast fails, a descriptive
// error is returned. | [
"ArrayOperand",
"converts",
"x",
"to",
"an",
"array",
".",
"If",
"the",
"cast",
"fails",
"a",
"descriptive",
"error",
"is",
"returned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L137-L143 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | NumberToFloat | func NumberToFloat(n ast.Number) *big.Float {
r, ok := new(big.Float).SetString(string(n))
if !ok {
panic("illegal value")
}
return r
} | go | func NumberToFloat(n ast.Number) *big.Float {
r, ok := new(big.Float).SetString(string(n))
if !ok {
panic("illegal value")
}
return r
} | [
"func",
"NumberToFloat",
"(",
"n",
"ast",
".",
"Number",
")",
"*",
"big",
".",
"Float",
"{",
"r",
",",
"ok",
":=",
"new",
"(",
"big",
".",
"Float",
")",
".",
"SetString",
"(",
"string",
"(",
"n",
")",
")",
"\n",
"if",
"!",
"ok",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"r",
"\n",
"}"
] | // NumberToFloat converts n to a big float. | [
"NumberToFloat",
"converts",
"n",
"to",
"a",
"big",
"float",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L146-L152 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | FloatToNumber | func FloatToNumber(f *big.Float) ast.Number {
return ast.Number(f.String())
} | go | func FloatToNumber(f *big.Float) ast.Number {
return ast.Number(f.String())
} | [
"func",
"FloatToNumber",
"(",
"f",
"*",
"big",
".",
"Float",
")",
"ast",
".",
"Number",
"{",
"return",
"ast",
".",
"Number",
"(",
"f",
".",
"String",
"(",
")",
")",
"\n",
"}"
] | // FloatToNumber converts f to a number. | [
"FloatToNumber",
"converts",
"f",
"to",
"a",
"number",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L155-L157 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | NumberToInt | func NumberToInt(n ast.Number) (*big.Int, error) {
r, ok := new(big.Int).SetString(string(n), 10)
if !ok {
return nil, fmt.Errorf("illegal value")
}
return r, nil
} | go | func NumberToInt(n ast.Number) (*big.Int, error) {
r, ok := new(big.Int).SetString(string(n), 10)
if !ok {
return nil, fmt.Errorf("illegal value")
}
return r, nil
} | [
"func",
"NumberToInt",
"(",
"n",
"ast",
".",
"Number",
")",
"(",
"*",
"big",
".",
"Int",
",",
"error",
")",
"{",
"r",
",",
"ok",
":=",
"new",
"(",
"big",
".",
"Int",
")",
".",
"SetString",
"(",
"string",
"(",
"n",
")",
",",
"10",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"r",
",",
"nil",
"\n",
"}"
] | // NumberToInt converts n to a big int.
// If n cannot be converted to an big int, an error is returned. | [
"NumberToInt",
"converts",
"n",
"to",
"a",
"big",
"int",
".",
"If",
"n",
"cannot",
"be",
"converted",
"to",
"an",
"big",
"int",
"an",
"error",
"is",
"returned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L161-L167 | train |
open-policy-agent/opa | topdown/builtins/builtins.go | IntToNumber | func IntToNumber(i *big.Int) ast.Number {
return ast.Number(i.String())
} | go | func IntToNumber(i *big.Int) ast.Number {
return ast.Number(i.String())
} | [
"func",
"IntToNumber",
"(",
"i",
"*",
"big",
".",
"Int",
")",
"ast",
".",
"Number",
"{",
"return",
"ast",
".",
"Number",
"(",
"i",
".",
"String",
"(",
")",
")",
"\n",
"}"
] | // IntToNumber converts i to a number. | [
"IntToNumber",
"converts",
"i",
"to",
"a",
"number",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/builtins/builtins.go#L170-L172 | train |
open-policy-agent/opa | topdown/save.go | ContainsRecursive | func (ss *saveSet) ContainsRecursive(t *ast.Term, b *bindings) bool {
found := false
ast.WalkTerms(t, func(x *ast.Term) bool {
if _, ok := x.Value.(ast.Var); ok {
x1, b1 := b.apply(x)
if x1 != x || b1 != b {
if ss.ContainsRecursive(x1, b1) {
found = true
}
} else if ss.Contains(x1, b1) {
found = true
}
}
return found
})
return found
} | go | func (ss *saveSet) ContainsRecursive(t *ast.Term, b *bindings) bool {
found := false
ast.WalkTerms(t, func(x *ast.Term) bool {
if _, ok := x.Value.(ast.Var); ok {
x1, b1 := b.apply(x)
if x1 != x || b1 != b {
if ss.ContainsRecursive(x1, b1) {
found = true
}
} else if ss.Contains(x1, b1) {
found = true
}
}
return found
})
return found
} | [
"func",
"(",
"ss",
"*",
"saveSet",
")",
"ContainsRecursive",
"(",
"t",
"*",
"ast",
".",
"Term",
",",
"b",
"*",
"bindings",
")",
"bool",
"{",
"found",
":=",
"false",
"\n",
"ast",
".",
"WalkTerms",
"(",
"t",
",",
"func",
"(",
"x",
"*",
"ast",
".",
"Term",
")",
"bool",
"{",
"if",
"_",
",",
"ok",
":=",
"x",
".",
"Value",
".",
"(",
"ast",
".",
"Var",
")",
";",
"ok",
"{",
"x1",
",",
"b1",
":=",
"b",
".",
"apply",
"(",
"x",
")",
"\n",
"if",
"x1",
"!=",
"x",
"||",
"b1",
"!=",
"b",
"{",
"if",
"ss",
".",
"ContainsRecursive",
"(",
"x1",
",",
"b1",
")",
"{",
"found",
"=",
"true",
"\n",
"}",
"\n",
"}",
"else",
"if",
"ss",
".",
"Contains",
"(",
"x1",
",",
"b1",
")",
"{",
"found",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"found",
"\n",
"}",
")",
"\n",
"return",
"found",
"\n",
"}"
] | // ContainsRecursive retruns true if the term t is or contains a term that is
// contained in the save set. This function will close over the binding list
// when it encounters vars. | [
"ContainsRecursive",
"retruns",
"true",
"if",
"the",
"term",
"t",
"is",
"or",
"contains",
"a",
"term",
"that",
"is",
"contained",
"in",
"the",
"save",
"set",
".",
"This",
"function",
"will",
"close",
"over",
"the",
"binding",
"list",
"when",
"it",
"encounters",
"vars",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/topdown/save.go#L53-L69 | train |
open-policy-agent/opa | types/types.go | NewArray | func NewArray(static []Type, dynamic Type) *Array {
return &Array{
static: static,
dynamic: dynamic,
}
} | go | func NewArray(static []Type, dynamic Type) *Array {
return &Array{
static: static,
dynamic: dynamic,
}
} | [
"func",
"NewArray",
"(",
"static",
"[",
"]",
"Type",
",",
"dynamic",
"Type",
")",
"*",
"Array",
"{",
"return",
"&",
"Array",
"{",
"static",
":",
"static",
",",
"dynamic",
":",
"dynamic",
",",
"}",
"\n",
"}"
] | // NewArray returns a new Array type. | [
"NewArray",
"returns",
"a",
"new",
"Array",
"type",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L136-L141 | train |
open-policy-agent/opa | types/types.go | Select | func (t *Array) Select(pos int) Type {
if len(t.static) > pos {
return t.static[pos]
}
if t.dynamic != nil {
return t.dynamic
}
return nil
} | go | func (t *Array) Select(pos int) Type {
if len(t.static) > pos {
return t.static[pos]
}
if t.dynamic != nil {
return t.dynamic
}
return nil
} | [
"func",
"(",
"t",
"*",
"Array",
")",
"Select",
"(",
"pos",
"int",
")",
"Type",
"{",
"if",
"len",
"(",
"t",
".",
"static",
")",
">",
"pos",
"{",
"return",
"t",
".",
"static",
"[",
"pos",
"]",
"\n",
"}",
"\n",
"if",
"t",
".",
"dynamic",
"!=",
"nil",
"{",
"return",
"t",
".",
"dynamic",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Select returns the type of element at the zero-based pos. | [
"Select",
"returns",
"the",
"type",
"of",
"element",
"at",
"the",
"zero",
"-",
"based",
"pos",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L184-L192 | train |
open-policy-agent/opa | types/types.go | NewStaticProperty | func NewStaticProperty(key interface{}, value Type) *StaticProperty {
return &StaticProperty{
Key: key,
Value: value,
}
} | go | func NewStaticProperty(key interface{}, value Type) *StaticProperty {
return &StaticProperty{
Key: key,
Value: value,
}
} | [
"func",
"NewStaticProperty",
"(",
"key",
"interface",
"{",
"}",
",",
"value",
"Type",
")",
"*",
"StaticProperty",
"{",
"return",
"&",
"StaticProperty",
"{",
"Key",
":",
"key",
",",
"Value",
":",
"value",
",",
"}",
"\n",
"}"
] | // NewStaticProperty returns a new StaticProperty object. | [
"NewStaticProperty",
"returns",
"a",
"new",
"StaticProperty",
"object",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L229-L234 | train |
open-policy-agent/opa | types/types.go | NewDynamicProperty | func NewDynamicProperty(key, value Type) *DynamicProperty {
return &DynamicProperty{
Key: key,
Value: value,
}
} | go | func NewDynamicProperty(key, value Type) *DynamicProperty {
return &DynamicProperty{
Key: key,
Value: value,
}
} | [
"func",
"NewDynamicProperty",
"(",
"key",
",",
"value",
"Type",
")",
"*",
"DynamicProperty",
"{",
"return",
"&",
"DynamicProperty",
"{",
"Key",
":",
"key",
",",
"Value",
":",
"value",
",",
"}",
"\n",
"}"
] | // NewDynamicProperty returns a new DynamicProperty object. | [
"NewDynamicProperty",
"returns",
"a",
"new",
"DynamicProperty",
"object",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L251-L256 | train |
open-policy-agent/opa | types/types.go | NewObject | func NewObject(static []*StaticProperty, dynamic *DynamicProperty) *Object {
sort.Slice(static, func(i, j int) bool {
cmp := util.Compare(static[i].Key, static[j].Key)
return cmp == -1
})
return &Object{
static: static,
dynamic: dynamic,
}
} | go | func NewObject(static []*StaticProperty, dynamic *DynamicProperty) *Object {
sort.Slice(static, func(i, j int) bool {
cmp := util.Compare(static[i].Key, static[j].Key)
return cmp == -1
})
return &Object{
static: static,
dynamic: dynamic,
}
} | [
"func",
"NewObject",
"(",
"static",
"[",
"]",
"*",
"StaticProperty",
",",
"dynamic",
"*",
"DynamicProperty",
")",
"*",
"Object",
"{",
"sort",
".",
"Slice",
"(",
"static",
",",
"func",
"(",
"i",
",",
"j",
"int",
")",
"bool",
"{",
"cmp",
":=",
"util",
".",
"Compare",
"(",
"static",
"[",
"i",
"]",
".",
"Key",
",",
"static",
"[",
"j",
"]",
".",
"Key",
")",
"\n",
"return",
"cmp",
"==",
"-",
"1",
"\n",
"}",
")",
"\n",
"return",
"&",
"Object",
"{",
"static",
":",
"static",
",",
"dynamic",
":",
"dynamic",
",",
"}",
"\n",
"}"
] | // NewObject returns a new Object type. | [
"NewObject",
"returns",
"a",
"new",
"Object",
"type",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L277-L286 | train |
open-policy-agent/opa | types/types.go | DynamicValue | func (t *Object) DynamicValue() Type {
if t.dynamic == nil {
return nil
}
return t.dynamic.Value
} | go | func (t *Object) DynamicValue() Type {
if t.dynamic == nil {
return nil
}
return t.dynamic.Value
} | [
"func",
"(",
"t",
"*",
"Object",
")",
"DynamicValue",
"(",
")",
"Type",
"{",
"if",
"t",
".",
"dynamic",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"t",
".",
"dynamic",
".",
"Value",
"\n",
"}"
] | // DynamicValue returns the type of the object's dynamic elements. | [
"DynamicValue",
"returns",
"the",
"type",
"of",
"the",
"object",
"s",
"dynamic",
"elements",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L305-L310 | train |
open-policy-agent/opa | types/types.go | Keys | func (t *Object) Keys() []interface{} {
sl := make([]interface{}, 0, len(t.static))
for _, p := range t.static {
sl = append(sl, p.Key)
}
return sl
} | go | func (t *Object) Keys() []interface{} {
sl := make([]interface{}, 0, len(t.static))
for _, p := range t.static {
sl = append(sl, p.Key)
}
return sl
} | [
"func",
"(",
"t",
"*",
"Object",
")",
"Keys",
"(",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"sl",
":=",
"make",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"0",
",",
"len",
"(",
"t",
".",
"static",
")",
")",
"\n",
"for",
"_",
",",
"p",
":=",
"range",
"t",
".",
"static",
"{",
"sl",
"=",
"append",
"(",
"sl",
",",
"p",
".",
"Key",
")",
"\n",
"}",
"\n",
"return",
"sl",
"\n",
"}"
] | // Keys returns the keys of the object's static elements. | [
"Keys",
"returns",
"the",
"keys",
"of",
"the",
"object",
"s",
"static",
"elements",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L313-L319 | train |
open-policy-agent/opa | types/types.go | Select | func (t *Object) Select(name interface{}) Type {
for _, p := range t.static {
if util.Compare(p.Key, name) == 0 {
return p.Value
}
}
if t.dynamic != nil {
if Contains(t.dynamic.Key, TypeOf(name)) {
return t.dynamic.Value
}
}
return nil
} | go | func (t *Object) Select(name interface{}) Type {
for _, p := range t.static {
if util.Compare(p.Key, name) == 0 {
return p.Value
}
}
if t.dynamic != nil {
if Contains(t.dynamic.Key, TypeOf(name)) {
return t.dynamic.Value
}
}
return nil
} | [
"func",
"(",
"t",
"*",
"Object",
")",
"Select",
"(",
"name",
"interface",
"{",
"}",
")",
"Type",
"{",
"for",
"_",
",",
"p",
":=",
"range",
"t",
".",
"static",
"{",
"if",
"util",
".",
"Compare",
"(",
"p",
".",
"Key",
",",
"name",
")",
"==",
"0",
"{",
"return",
"p",
".",
"Value",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"t",
".",
"dynamic",
"!=",
"nil",
"{",
"if",
"Contains",
"(",
"t",
".",
"dynamic",
".",
"Key",
",",
"TypeOf",
"(",
"name",
")",
")",
"{",
"return",
"t",
".",
"dynamic",
".",
"Value",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Select returns the type of the named property. | [
"Select",
"returns",
"the",
"type",
"of",
"the",
"named",
"property",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L336-L348 | train |
open-policy-agent/opa | types/types.go | NewAny | func NewAny(of ...Type) Any {
sl := make(Any, len(of))
for i := range sl {
sl[i] = of[i]
}
return sl
} | go | func NewAny(of ...Type) Any {
sl := make(Any, len(of))
for i := range sl {
sl[i] = of[i]
}
return sl
} | [
"func",
"NewAny",
"(",
"of",
"...",
"Type",
")",
"Any",
"{",
"sl",
":=",
"make",
"(",
"Any",
",",
"len",
"(",
"of",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"sl",
"{",
"sl",
"[",
"i",
"]",
"=",
"of",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"sl",
"\n",
"}"
] | // NewAny returns a new Any type. | [
"NewAny",
"returns",
"a",
"new",
"Any",
"type",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L357-L363 | train |
open-policy-agent/opa | types/types.go | Contains | func (t Any) Contains(other Type) bool {
if _, ok := other.(*Function); ok {
return false
}
for i := range t {
if Compare(t[i], other) == 0 {
return true
}
}
return len(t) == 0
} | go | func (t Any) Contains(other Type) bool {
if _, ok := other.(*Function); ok {
return false
}
for i := range t {
if Compare(t[i], other) == 0 {
return true
}
}
return len(t) == 0
} | [
"func",
"(",
"t",
"Any",
")",
"Contains",
"(",
"other",
"Type",
")",
"bool",
"{",
"if",
"_",
",",
"ok",
":=",
"other",
".",
"(",
"*",
"Function",
")",
";",
"ok",
"{",
"return",
"false",
"\n",
"}",
"\n",
"for",
"i",
":=",
"range",
"t",
"{",
"if",
"Compare",
"(",
"t",
"[",
"i",
"]",
",",
"other",
")",
"==",
"0",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"len",
"(",
"t",
")",
"==",
"0",
"\n",
"}"
] | // Contains returns true if t is a superset of other. | [
"Contains",
"returns",
"true",
"if",
"t",
"is",
"a",
"superset",
"of",
"other",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L366-L376 | train |
open-policy-agent/opa | types/types.go | Merge | func (t Any) Merge(other Type) Any {
if otherAny, ok := other.(Any); ok {
return t.Union(otherAny)
}
if t.Contains(other) {
return t
}
return append(t, other)
} | go | func (t Any) Merge(other Type) Any {
if otherAny, ok := other.(Any); ok {
return t.Union(otherAny)
}
if t.Contains(other) {
return t
}
return append(t, other)
} | [
"func",
"(",
"t",
"Any",
")",
"Merge",
"(",
"other",
"Type",
")",
"Any",
"{",
"if",
"otherAny",
",",
"ok",
":=",
"other",
".",
"(",
"Any",
")",
";",
"ok",
"{",
"return",
"t",
".",
"Union",
"(",
"otherAny",
")",
"\n",
"}",
"\n",
"if",
"t",
".",
"Contains",
"(",
"other",
")",
"{",
"return",
"t",
"\n",
"}",
"\n",
"return",
"append",
"(",
"t",
",",
"other",
")",
"\n",
"}"
] | // Merge return a new Any type that is the superset of t and other. | [
"Merge",
"return",
"a",
"new",
"Any",
"type",
"that",
"is",
"the",
"superset",
"of",
"t",
"and",
"other",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L387-L395 | train |
open-policy-agent/opa | types/types.go | Union | func (t Any) Union(other Any) Any {
if len(t) == 0 {
return t
}
if len(other) == 0 {
return other
}
cpy := make(Any, len(t))
for i := range cpy {
cpy[i] = t[i]
}
for i := range other {
if !cpy.Contains(other[i]) {
cpy = append(cpy, other[i])
}
}
return cpy
} | go | func (t Any) Union(other Any) Any {
if len(t) == 0 {
return t
}
if len(other) == 0 {
return other
}
cpy := make(Any, len(t))
for i := range cpy {
cpy[i] = t[i]
}
for i := range other {
if !cpy.Contains(other[i]) {
cpy = append(cpy, other[i])
}
}
return cpy
} | [
"func",
"(",
"t",
"Any",
")",
"Union",
"(",
"other",
"Any",
")",
"Any",
"{",
"if",
"len",
"(",
"t",
")",
"==",
"0",
"{",
"return",
"t",
"\n",
"}",
"\n",
"if",
"len",
"(",
"other",
")",
"==",
"0",
"{",
"return",
"other",
"\n",
"}",
"\n",
"cpy",
":=",
"make",
"(",
"Any",
",",
"len",
"(",
"t",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"cpy",
"{",
"cpy",
"[",
"i",
"]",
"=",
"t",
"[",
"i",
"]",
"\n",
"}",
"\n",
"for",
"i",
":=",
"range",
"other",
"{",
"if",
"!",
"cpy",
".",
"Contains",
"(",
"other",
"[",
"i",
"]",
")",
"{",
"cpy",
"=",
"append",
"(",
"cpy",
",",
"other",
"[",
"i",
"]",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"cpy",
"\n",
"}"
] | // Union returns a new Any type that is the union of the two Any types. | [
"Union",
"returns",
"a",
"new",
"Any",
"type",
"that",
"is",
"the",
"union",
"of",
"the",
"two",
"Any",
"types",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L398-L415 | train |
open-policy-agent/opa | types/types.go | Union | func (t *Function) Union(other *Function) *Function {
if other == nil {
return t
} else if t == nil {
return other
}
a := t.Args()
b := other.Args()
if len(a) != len(b) {
return nil
}
args := make([]Type, len(a))
for i := range a {
args[i] = Or(a[i], b[i])
}
return NewFunction(args, Or(t.Result(), other.Result()))
} | go | func (t *Function) Union(other *Function) *Function {
if other == nil {
return t
} else if t == nil {
return other
}
a := t.Args()
b := other.Args()
if len(a) != len(b) {
return nil
}
args := make([]Type, len(a))
for i := range a {
args[i] = Or(a[i], b[i])
}
return NewFunction(args, Or(t.Result(), other.Result()))
} | [
"func",
"(",
"t",
"*",
"Function",
")",
"Union",
"(",
"other",
"*",
"Function",
")",
"*",
"Function",
"{",
"if",
"other",
"==",
"nil",
"{",
"return",
"t",
"\n",
"}",
"else",
"if",
"t",
"==",
"nil",
"{",
"return",
"other",
"\n",
"}",
"\n",
"a",
":=",
"t",
".",
"Args",
"(",
")",
"\n",
"b",
":=",
"other",
".",
"Args",
"(",
")",
"\n",
"if",
"len",
"(",
"a",
")",
"!=",
"len",
"(",
"b",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"args",
":=",
"make",
"(",
"[",
"]",
"Type",
",",
"len",
"(",
"a",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"a",
"{",
"args",
"[",
"i",
"]",
"=",
"Or",
"(",
"a",
"[",
"i",
"]",
",",
"b",
"[",
"i",
"]",
")",
"\n",
"}",
"\n\n",
"return",
"NewFunction",
"(",
"args",
",",
"Or",
"(",
"t",
".",
"Result",
"(",
")",
",",
"other",
".",
"Result",
"(",
")",
")",
")",
"\n",
"}"
] | // Union returns a new function represnting the union of t and other. Functions
// must have the same arity to be unioned. | [
"Union",
"returns",
"a",
"new",
"function",
"represnting",
"the",
"union",
"of",
"t",
"and",
"other",
".",
"Functions",
"must",
"have",
"the",
"same",
"arity",
"to",
"be",
"unioned",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L491-L508 | train |
open-policy-agent/opa | types/types.go | Contains | func Contains(a, b Type) bool {
if any, ok := a.(Any); ok {
return any.Contains(b)
}
return Compare(a, b) == 0
} | go | func Contains(a, b Type) bool {
if any, ok := a.(Any); ok {
return any.Contains(b)
}
return Compare(a, b) == 0
} | [
"func",
"Contains",
"(",
"a",
",",
"b",
"Type",
")",
"bool",
"{",
"if",
"any",
",",
"ok",
":=",
"a",
".",
"(",
"Any",
")",
";",
"ok",
"{",
"return",
"any",
".",
"Contains",
"(",
"b",
")",
"\n",
"}",
"\n",
"return",
"Compare",
"(",
"a",
",",
"b",
")",
"==",
"0",
"\n",
"}"
] | // Contains returns true if a is a superset or equal to b. | [
"Contains",
"returns",
"true",
"if",
"a",
"is",
"a",
"superset",
"or",
"equal",
"to",
"b",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L614-L619 | train |
open-policy-agent/opa | types/types.go | Or | func Or(a, b Type) Type {
if a == nil {
return b
} else if b == nil {
return a
}
fA, ok1 := a.(*Function)
fB, ok2 := b.(*Function)
if ok1 && ok2 {
return fA.Union(fB)
} else if ok1 || ok2 {
return nil
}
anyA, ok1 := a.(Any)
anyB, ok2 := b.(Any)
if ok1 {
return anyA.Merge(b)
}
if ok2 {
return anyB.Merge(a)
}
if Compare(a, b) == 0 {
return a
}
return NewAny(a, b)
} | go | func Or(a, b Type) Type {
if a == nil {
return b
} else if b == nil {
return a
}
fA, ok1 := a.(*Function)
fB, ok2 := b.(*Function)
if ok1 && ok2 {
return fA.Union(fB)
} else if ok1 || ok2 {
return nil
}
anyA, ok1 := a.(Any)
anyB, ok2 := b.(Any)
if ok1 {
return anyA.Merge(b)
}
if ok2 {
return anyB.Merge(a)
}
if Compare(a, b) == 0 {
return a
}
return NewAny(a, b)
} | [
"func",
"Or",
"(",
"a",
",",
"b",
"Type",
")",
"Type",
"{",
"if",
"a",
"==",
"nil",
"{",
"return",
"b",
"\n",
"}",
"else",
"if",
"b",
"==",
"nil",
"{",
"return",
"a",
"\n",
"}",
"\n",
"fA",
",",
"ok1",
":=",
"a",
".",
"(",
"*",
"Function",
")",
"\n",
"fB",
",",
"ok2",
":=",
"b",
".",
"(",
"*",
"Function",
")",
"\n",
"if",
"ok1",
"&&",
"ok2",
"{",
"return",
"fA",
".",
"Union",
"(",
"fB",
")",
"\n",
"}",
"else",
"if",
"ok1",
"||",
"ok2",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"anyA",
",",
"ok1",
":=",
"a",
".",
"(",
"Any",
")",
"\n",
"anyB",
",",
"ok2",
":=",
"b",
".",
"(",
"Any",
")",
"\n",
"if",
"ok1",
"{",
"return",
"anyA",
".",
"Merge",
"(",
"b",
")",
"\n",
"}",
"\n",
"if",
"ok2",
"{",
"return",
"anyB",
".",
"Merge",
"(",
"a",
")",
"\n",
"}",
"\n",
"if",
"Compare",
"(",
"a",
",",
"b",
")",
"==",
"0",
"{",
"return",
"a",
"\n",
"}",
"\n",
"return",
"NewAny",
"(",
"a",
",",
"b",
")",
"\n",
"}"
] | // Or returns a type that represents the union of a and b. If one type is a
// superset of the other, the superset is returned unchanged. | [
"Or",
"returns",
"a",
"type",
"that",
"represents",
"the",
"union",
"of",
"a",
"and",
"b",
".",
"If",
"one",
"type",
"is",
"a",
"superset",
"of",
"the",
"other",
"the",
"superset",
"is",
"returned",
"unchanged",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L623-L648 | train |
open-policy-agent/opa | types/types.go | Select | func Select(a Type, x interface{}) Type {
switch a := a.(type) {
case *Array:
n, ok := x.(json.Number)
if !ok {
return nil
}
pos, err := n.Int64()
if err != nil {
return nil
}
return a.Select(int(pos))
case *Object:
return a.Select(x)
case *Set:
tpe := TypeOf(x)
if Compare(a.of, tpe) == 0 {
return a.of
}
if any, ok := a.of.(Any); ok {
if any.Contains(tpe) {
return tpe
}
}
return nil
case Any:
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
// TODO(tsandall): test nil/nil
tpe = Or(Select(a[i], x), tpe)
}
return tpe
default:
return nil
}
} | go | func Select(a Type, x interface{}) Type {
switch a := a.(type) {
case *Array:
n, ok := x.(json.Number)
if !ok {
return nil
}
pos, err := n.Int64()
if err != nil {
return nil
}
return a.Select(int(pos))
case *Object:
return a.Select(x)
case *Set:
tpe := TypeOf(x)
if Compare(a.of, tpe) == 0 {
return a.of
}
if any, ok := a.of.(Any); ok {
if any.Contains(tpe) {
return tpe
}
}
return nil
case Any:
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
// TODO(tsandall): test nil/nil
tpe = Or(Select(a[i], x), tpe)
}
return tpe
default:
return nil
}
} | [
"func",
"Select",
"(",
"a",
"Type",
",",
"x",
"interface",
"{",
"}",
")",
"Type",
"{",
"switch",
"a",
":=",
"a",
".",
"(",
"type",
")",
"{",
"case",
"*",
"Array",
":",
"n",
",",
"ok",
":=",
"x",
".",
"(",
"json",
".",
"Number",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"pos",
",",
"err",
":=",
"n",
".",
"Int64",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"a",
".",
"Select",
"(",
"int",
"(",
"pos",
")",
")",
"\n",
"case",
"*",
"Object",
":",
"return",
"a",
".",
"Select",
"(",
"x",
")",
"\n",
"case",
"*",
"Set",
":",
"tpe",
":=",
"TypeOf",
"(",
"x",
")",
"\n",
"if",
"Compare",
"(",
"a",
".",
"of",
",",
"tpe",
")",
"==",
"0",
"{",
"return",
"a",
".",
"of",
"\n",
"}",
"\n",
"if",
"any",
",",
"ok",
":=",
"a",
".",
"of",
".",
"(",
"Any",
")",
";",
"ok",
"{",
"if",
"any",
".",
"Contains",
"(",
"tpe",
")",
"{",
"return",
"tpe",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"case",
"Any",
":",
"if",
"Compare",
"(",
"a",
",",
"A",
")",
"==",
"0",
"{",
"return",
"A",
"\n",
"}",
"\n",
"var",
"tpe",
"Type",
"\n",
"for",
"i",
":=",
"range",
"a",
"{",
"// TODO(tsandall): test nil/nil",
"tpe",
"=",
"Or",
"(",
"Select",
"(",
"a",
"[",
"i",
"]",
",",
"x",
")",
",",
"tpe",
")",
"\n",
"}",
"\n",
"return",
"tpe",
"\n",
"default",
":",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // Select returns a property or item of a. | [
"Select",
"returns",
"a",
"property",
"or",
"item",
"of",
"a",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L651-L689 | train |
open-policy-agent/opa | types/types.go | Keys | func Keys(a Type) Type {
switch a := a.(type) {
case *Array:
return N
case *Object:
var tpe Type
for _, k := range a.Keys() {
tpe = Or(tpe, TypeOf(k))
}
if a.dynamic != nil {
tpe = Or(tpe, a.dynamic.Key)
}
return tpe
case *Set:
return a.of
case Any:
// TODO(tsandall): ditto test
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
tpe = Or(Keys(a[i]), tpe)
}
return tpe
}
return nil
} | go | func Keys(a Type) Type {
switch a := a.(type) {
case *Array:
return N
case *Object:
var tpe Type
for _, k := range a.Keys() {
tpe = Or(tpe, TypeOf(k))
}
if a.dynamic != nil {
tpe = Or(tpe, a.dynamic.Key)
}
return tpe
case *Set:
return a.of
case Any:
// TODO(tsandall): ditto test
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
tpe = Or(Keys(a[i]), tpe)
}
return tpe
}
return nil
} | [
"func",
"Keys",
"(",
"a",
"Type",
")",
"Type",
"{",
"switch",
"a",
":=",
"a",
".",
"(",
"type",
")",
"{",
"case",
"*",
"Array",
":",
"return",
"N",
"\n",
"case",
"*",
"Object",
":",
"var",
"tpe",
"Type",
"\n",
"for",
"_",
",",
"k",
":=",
"range",
"a",
".",
"Keys",
"(",
")",
"{",
"tpe",
"=",
"Or",
"(",
"tpe",
",",
"TypeOf",
"(",
"k",
")",
")",
"\n",
"}",
"\n",
"if",
"a",
".",
"dynamic",
"!=",
"nil",
"{",
"tpe",
"=",
"Or",
"(",
"tpe",
",",
"a",
".",
"dynamic",
".",
"Key",
")",
"\n",
"}",
"\n",
"return",
"tpe",
"\n",
"case",
"*",
"Set",
":",
"return",
"a",
".",
"of",
"\n",
"case",
"Any",
":",
"// TODO(tsandall): ditto test",
"if",
"Compare",
"(",
"a",
",",
"A",
")",
"==",
"0",
"{",
"return",
"A",
"\n",
"}",
"\n",
"var",
"tpe",
"Type",
"\n",
"for",
"i",
":=",
"range",
"a",
"{",
"tpe",
"=",
"Or",
"(",
"Keys",
"(",
"a",
"[",
"i",
"]",
")",
",",
"tpe",
")",
"\n",
"}",
"\n",
"return",
"tpe",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Keys returns the type of keys that can be enumerated for a. For arrays, the
// keys are always number types, for objects the keys are always string types,
// and for sets the keys are always the type of the set element. | [
"Keys",
"returns",
"the",
"type",
"of",
"keys",
"that",
"can",
"be",
"enumerated",
"for",
"a",
".",
"For",
"arrays",
"the",
"keys",
"are",
"always",
"number",
"types",
"for",
"objects",
"the",
"keys",
"are",
"always",
"string",
"types",
"and",
"for",
"sets",
"the",
"keys",
"are",
"always",
"the",
"type",
"of",
"the",
"set",
"element",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L694-L721 | train |
open-policy-agent/opa | types/types.go | Values | func Values(a Type) Type {
switch a := a.(type) {
case *Array:
var tpe Type
for i := range a.static {
tpe = Or(tpe, a.static[i])
}
return Or(tpe, a.dynamic)
case *Object:
var tpe Type
for _, v := range a.static {
tpe = Or(tpe, v.Value)
}
if a.dynamic != nil {
tpe = Or(tpe, a.dynamic.Value)
}
return tpe
case *Set:
return a.of
case Any:
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
tpe = Or(Values(a[i]), tpe)
}
return tpe
}
return nil
} | go | func Values(a Type) Type {
switch a := a.(type) {
case *Array:
var tpe Type
for i := range a.static {
tpe = Or(tpe, a.static[i])
}
return Or(tpe, a.dynamic)
case *Object:
var tpe Type
for _, v := range a.static {
tpe = Or(tpe, v.Value)
}
if a.dynamic != nil {
tpe = Or(tpe, a.dynamic.Value)
}
return tpe
case *Set:
return a.of
case Any:
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
tpe = Or(Values(a[i]), tpe)
}
return tpe
}
return nil
} | [
"func",
"Values",
"(",
"a",
"Type",
")",
"Type",
"{",
"switch",
"a",
":=",
"a",
".",
"(",
"type",
")",
"{",
"case",
"*",
"Array",
":",
"var",
"tpe",
"Type",
"\n",
"for",
"i",
":=",
"range",
"a",
".",
"static",
"{",
"tpe",
"=",
"Or",
"(",
"tpe",
",",
"a",
".",
"static",
"[",
"i",
"]",
")",
"\n",
"}",
"\n",
"return",
"Or",
"(",
"tpe",
",",
"a",
".",
"dynamic",
")",
"\n",
"case",
"*",
"Object",
":",
"var",
"tpe",
"Type",
"\n",
"for",
"_",
",",
"v",
":=",
"range",
"a",
".",
"static",
"{",
"tpe",
"=",
"Or",
"(",
"tpe",
",",
"v",
".",
"Value",
")",
"\n",
"}",
"\n",
"if",
"a",
".",
"dynamic",
"!=",
"nil",
"{",
"tpe",
"=",
"Or",
"(",
"tpe",
",",
"a",
".",
"dynamic",
".",
"Value",
")",
"\n",
"}",
"\n",
"return",
"tpe",
"\n",
"case",
"*",
"Set",
":",
"return",
"a",
".",
"of",
"\n",
"case",
"Any",
":",
"if",
"Compare",
"(",
"a",
",",
"A",
")",
"==",
"0",
"{",
"return",
"A",
"\n",
"}",
"\n",
"var",
"tpe",
"Type",
"\n",
"for",
"i",
":=",
"range",
"a",
"{",
"tpe",
"=",
"Or",
"(",
"Values",
"(",
"a",
"[",
"i",
"]",
")",
",",
"tpe",
")",
"\n",
"}",
"\n",
"return",
"tpe",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Values returns the type of values that can be enumerated for a. | [
"Values",
"returns",
"the",
"type",
"of",
"values",
"that",
"can",
"be",
"enumerated",
"for",
"a",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L724-L754 | train |
open-policy-agent/opa | types/types.go | Nil | func Nil(a Type) bool {
switch a := a.(type) {
case nil:
return true
case *Function:
for i := range a.args {
if Nil(a.args[i]) {
return true
}
}
return Nil(a.result)
case *Array:
for i := range a.static {
if Nil(a.static[i]) {
return true
}
}
if a.dynamic != nil {
return Nil(a.dynamic)
}
case *Object:
for i := range a.static {
if Nil(a.static[i].Value) {
return true
}
}
if a.dynamic != nil {
return Nil(a.dynamic.Key) || Nil(a.dynamic.Value)
}
case *Set:
return Nil(a.of)
}
return false
} | go | func Nil(a Type) bool {
switch a := a.(type) {
case nil:
return true
case *Function:
for i := range a.args {
if Nil(a.args[i]) {
return true
}
}
return Nil(a.result)
case *Array:
for i := range a.static {
if Nil(a.static[i]) {
return true
}
}
if a.dynamic != nil {
return Nil(a.dynamic)
}
case *Object:
for i := range a.static {
if Nil(a.static[i].Value) {
return true
}
}
if a.dynamic != nil {
return Nil(a.dynamic.Key) || Nil(a.dynamic.Value)
}
case *Set:
return Nil(a.of)
}
return false
} | [
"func",
"Nil",
"(",
"a",
"Type",
")",
"bool",
"{",
"switch",
"a",
":=",
"a",
".",
"(",
"type",
")",
"{",
"case",
"nil",
":",
"return",
"true",
"\n",
"case",
"*",
"Function",
":",
"for",
"i",
":=",
"range",
"a",
".",
"args",
"{",
"if",
"Nil",
"(",
"a",
".",
"args",
"[",
"i",
"]",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"Nil",
"(",
"a",
".",
"result",
")",
"\n",
"case",
"*",
"Array",
":",
"for",
"i",
":=",
"range",
"a",
".",
"static",
"{",
"if",
"Nil",
"(",
"a",
".",
"static",
"[",
"i",
"]",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"a",
".",
"dynamic",
"!=",
"nil",
"{",
"return",
"Nil",
"(",
"a",
".",
"dynamic",
")",
"\n",
"}",
"\n",
"case",
"*",
"Object",
":",
"for",
"i",
":=",
"range",
"a",
".",
"static",
"{",
"if",
"Nil",
"(",
"a",
".",
"static",
"[",
"i",
"]",
".",
"Value",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"a",
".",
"dynamic",
"!=",
"nil",
"{",
"return",
"Nil",
"(",
"a",
".",
"dynamic",
".",
"Key",
")",
"||",
"Nil",
"(",
"a",
".",
"dynamic",
".",
"Value",
")",
"\n",
"}",
"\n",
"case",
"*",
"Set",
":",
"return",
"Nil",
"(",
"a",
".",
"of",
")",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // Nil returns true if a's type is unknown. | [
"Nil",
"returns",
"true",
"if",
"a",
"s",
"type",
"is",
"unknown",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L757-L790 | train |
open-policy-agent/opa | types/types.go | TypeOf | func TypeOf(x interface{}) Type {
switch x := x.(type) {
case nil:
return NewNull()
case bool:
return B
case string:
return S
case json.Number:
return N
case map[interface{}]interface{}:
static := make([]*StaticProperty, 0, len(x))
for k, v := range x {
static = append(static, NewStaticProperty(k, TypeOf(v)))
}
return NewObject(static, nil)
case []interface{}:
static := make([]Type, len(x))
for i := range x {
static[i] = TypeOf(x[i])
}
return NewArray(static, nil)
}
panic("unreachable")
} | go | func TypeOf(x interface{}) Type {
switch x := x.(type) {
case nil:
return NewNull()
case bool:
return B
case string:
return S
case json.Number:
return N
case map[interface{}]interface{}:
static := make([]*StaticProperty, 0, len(x))
for k, v := range x {
static = append(static, NewStaticProperty(k, TypeOf(v)))
}
return NewObject(static, nil)
case []interface{}:
static := make([]Type, len(x))
for i := range x {
static[i] = TypeOf(x[i])
}
return NewArray(static, nil)
}
panic("unreachable")
} | [
"func",
"TypeOf",
"(",
"x",
"interface",
"{",
"}",
")",
"Type",
"{",
"switch",
"x",
":=",
"x",
".",
"(",
"type",
")",
"{",
"case",
"nil",
":",
"return",
"NewNull",
"(",
")",
"\n",
"case",
"bool",
":",
"return",
"B",
"\n",
"case",
"string",
":",
"return",
"S",
"\n",
"case",
"json",
".",
"Number",
":",
"return",
"N",
"\n",
"case",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
":",
"static",
":=",
"make",
"(",
"[",
"]",
"*",
"StaticProperty",
",",
"0",
",",
"len",
"(",
"x",
")",
")",
"\n",
"for",
"k",
",",
"v",
":=",
"range",
"x",
"{",
"static",
"=",
"append",
"(",
"static",
",",
"NewStaticProperty",
"(",
"k",
",",
"TypeOf",
"(",
"v",
")",
")",
")",
"\n",
"}",
"\n",
"return",
"NewObject",
"(",
"static",
",",
"nil",
")",
"\n",
"case",
"[",
"]",
"interface",
"{",
"}",
":",
"static",
":=",
"make",
"(",
"[",
"]",
"Type",
",",
"len",
"(",
"x",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"x",
"{",
"static",
"[",
"i",
"]",
"=",
"TypeOf",
"(",
"x",
"[",
"i",
"]",
")",
"\n",
"}",
"\n",
"return",
"NewArray",
"(",
"static",
",",
"nil",
")",
"\n",
"}",
"\n",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}"
] | // TypeOf returns the type of the Golang native value. | [
"TypeOf",
"returns",
"the",
"type",
"of",
"the",
"Golang",
"native",
"value",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/types/types.go#L793-L817 | train |
open-policy-agent/opa | ast/compilehelper.go | CompileModules | func CompileModules(modules map[string]string) (*Compiler, error) {
parsed := make(map[string]*Module, len(modules))
for f, module := range modules {
if pm, err := ParseModule(f, module); err != nil {
return nil, err
} else if pm != nil {
parsed[f] = pm
}
}
compiler := NewCompiler()
compiler.Compile(parsed)
if compiler.Failed() {
return nil, compiler.Errors
}
return compiler, nil
} | go | func CompileModules(modules map[string]string) (*Compiler, error) {
parsed := make(map[string]*Module, len(modules))
for f, module := range modules {
if pm, err := ParseModule(f, module); err != nil {
return nil, err
} else if pm != nil {
parsed[f] = pm
}
}
compiler := NewCompiler()
compiler.Compile(parsed)
if compiler.Failed() {
return nil, compiler.Errors
}
return compiler, nil
} | [
"func",
"CompileModules",
"(",
"modules",
"map",
"[",
"string",
"]",
"string",
")",
"(",
"*",
"Compiler",
",",
"error",
")",
"{",
"parsed",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"Module",
",",
"len",
"(",
"modules",
")",
")",
"\n\n",
"for",
"f",
",",
"module",
":=",
"range",
"modules",
"{",
"if",
"pm",
",",
"err",
":=",
"ParseModule",
"(",
"f",
",",
"module",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"else",
"if",
"pm",
"!=",
"nil",
"{",
"parsed",
"[",
"f",
"]",
"=",
"pm",
"\n",
"}",
"\n",
"}",
"\n\n",
"compiler",
":=",
"NewCompiler",
"(",
")",
"\n",
"compiler",
".",
"Compile",
"(",
"parsed",
")",
"\n\n",
"if",
"compiler",
".",
"Failed",
"(",
")",
"{",
"return",
"nil",
",",
"compiler",
".",
"Errors",
"\n",
"}",
"\n\n",
"return",
"compiler",
",",
"nil",
"\n",
"}"
] | // CompileModules takes a set of Rego modules represented as strings and
// compiles them for evaluation. The keys of the map are used as filenames. | [
"CompileModules",
"takes",
"a",
"set",
"of",
"Rego",
"modules",
"represented",
"as",
"strings",
"and",
"compiles",
"them",
"for",
"evaluation",
".",
"The",
"keys",
"of",
"the",
"map",
"are",
"used",
"as",
"filenames",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/ast/compilehelper.go#L9-L29 | train |
open-policy-agent/opa | ast/compilehelper.go | MustCompileModules | func MustCompileModules(modules map[string]string) *Compiler {
compiler, err := CompileModules(modules)
if err != nil {
panic(err)
}
return compiler
} | go | func MustCompileModules(modules map[string]string) *Compiler {
compiler, err := CompileModules(modules)
if err != nil {
panic(err)
}
return compiler
} | [
"func",
"MustCompileModules",
"(",
"modules",
"map",
"[",
"string",
"]",
"string",
")",
"*",
"Compiler",
"{",
"compiler",
",",
"err",
":=",
"CompileModules",
"(",
"modules",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"compiler",
"\n",
"}"
] | // MustCompileModules compiles a set of Rego modules represented as strings. If
// the compilation process fails, this function panics. | [
"MustCompileModules",
"compiles",
"a",
"set",
"of",
"Rego",
"modules",
"represented",
"as",
"strings",
".",
"If",
"the",
"compilation",
"process",
"fails",
"this",
"function",
"panics",
"."
] | 589105aff580587c1b27b87df7123105af3e212a | https://github.com/open-policy-agent/opa/blob/589105aff580587c1b27b87df7123105af3e212a/ast/compilehelper.go#L33-L41 | train |
libp2p/go-libp2p | config/security.go | SecurityConstructor | func SecurityConstructor(sec interface{}) (SecC, error) {
// Already constructed?
if t, ok := sec.(security.Transport); ok {
return func(_ host.Host) (security.Transport, error) {
return t, nil
}, nil
}
ctor, err := makeConstructor(sec, securityType, securityArgTypes)
if err != nil {
return nil, err
}
return func(h host.Host) (security.Transport, error) {
t, err := ctor(h, nil)
if err != nil {
return nil, err
}
return t.(security.Transport), nil
}, nil
} | go | func SecurityConstructor(sec interface{}) (SecC, error) {
// Already constructed?
if t, ok := sec.(security.Transport); ok {
return func(_ host.Host) (security.Transport, error) {
return t, nil
}, nil
}
ctor, err := makeConstructor(sec, securityType, securityArgTypes)
if err != nil {
return nil, err
}
return func(h host.Host) (security.Transport, error) {
t, err := ctor(h, nil)
if err != nil {
return nil, err
}
return t.(security.Transport), nil
}, nil
} | [
"func",
"SecurityConstructor",
"(",
"sec",
"interface",
"{",
"}",
")",
"(",
"SecC",
",",
"error",
")",
"{",
"// Already constructed?",
"if",
"t",
",",
"ok",
":=",
"sec",
".",
"(",
"security",
".",
"Transport",
")",
";",
"ok",
"{",
"return",
"func",
"(",
"_",
"host",
".",
"Host",
")",
"(",
"security",
".",
"Transport",
",",
"error",
")",
"{",
"return",
"t",
",",
"nil",
"\n",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"ctor",
",",
"err",
":=",
"makeConstructor",
"(",
"sec",
",",
"securityType",
",",
"securityArgTypes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"func",
"(",
"h",
"host",
".",
"Host",
")",
"(",
"security",
".",
"Transport",
",",
"error",
")",
"{",
"t",
",",
"err",
":=",
"ctor",
"(",
"h",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"t",
".",
"(",
"security",
".",
"Transport",
")",
",",
"nil",
"\n",
"}",
",",
"nil",
"\n",
"}"
] | // SecurityConstructor creates a security constructor from the passed parameter
// using reflection. | [
"SecurityConstructor",
"creates",
"a",
"security",
"constructor",
"from",
"the",
"passed",
"parameter",
"using",
"reflection",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/config/security.go#L30-L49 | train |
libp2p/go-libp2p | p2p/protocol/identify/obsaddr.go | Addrs | func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) {
oas.RLock()
defer oas.RUnlock()
if len(oas.addrs) == 0 {
return nil
}
now := time.Now()
for _, observedAddrs := range oas.addrs {
for _, a := range observedAddrs {
if now.Sub(a.LastSeen) <= oas.ttl && a.activated(oas.ttl) {
addrs = append(addrs, a.Addr)
}
}
}
return addrs
} | go | func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) {
oas.RLock()
defer oas.RUnlock()
if len(oas.addrs) == 0 {
return nil
}
now := time.Now()
for _, observedAddrs := range oas.addrs {
for _, a := range observedAddrs {
if now.Sub(a.LastSeen) <= oas.ttl && a.activated(oas.ttl) {
addrs = append(addrs, a.Addr)
}
}
}
return addrs
} | [
"func",
"(",
"oas",
"*",
"ObservedAddrSet",
")",
"Addrs",
"(",
")",
"(",
"addrs",
"[",
"]",
"ma",
".",
"Multiaddr",
")",
"{",
"oas",
".",
"RLock",
"(",
")",
"\n",
"defer",
"oas",
".",
"RUnlock",
"(",
")",
"\n\n",
"if",
"len",
"(",
"oas",
".",
"addrs",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"now",
":=",
"time",
".",
"Now",
"(",
")",
"\n",
"for",
"_",
",",
"observedAddrs",
":=",
"range",
"oas",
".",
"addrs",
"{",
"for",
"_",
",",
"a",
":=",
"range",
"observedAddrs",
"{",
"if",
"now",
".",
"Sub",
"(",
"a",
".",
"LastSeen",
")",
"<=",
"oas",
".",
"ttl",
"&&",
"a",
".",
"activated",
"(",
"oas",
".",
"ttl",
")",
"{",
"addrs",
"=",
"append",
"(",
"addrs",
",",
"a",
".",
"Addr",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"addrs",
"\n",
"}"
] | // Addrs return all activated observed addresses | [
"Addrs",
"return",
"all",
"activated",
"observed",
"addresses"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/protocol/identify/obsaddr.go#L94-L111 | train |
libp2p/go-libp2p | p2p/protocol/identify/id.go | HasConsistentTransport | func HasConsistentTransport(a ma.Multiaddr, green []ma.Multiaddr) bool {
protosMatch := func(a, b []ma.Protocol) bool {
if len(a) != len(b) {
return false
}
for i, p := range a {
if b[i].Code != p.Code {
return false
}
}
return true
}
protos := a.Protocols()
for _, ga := range green {
if protosMatch(protos, ga.Protocols()) {
return true
}
}
return false
} | go | func HasConsistentTransport(a ma.Multiaddr, green []ma.Multiaddr) bool {
protosMatch := func(a, b []ma.Protocol) bool {
if len(a) != len(b) {
return false
}
for i, p := range a {
if b[i].Code != p.Code {
return false
}
}
return true
}
protos := a.Protocols()
for _, ga := range green {
if protosMatch(protos, ga.Protocols()) {
return true
}
}
return false
} | [
"func",
"HasConsistentTransport",
"(",
"a",
"ma",
".",
"Multiaddr",
",",
"green",
"[",
"]",
"ma",
".",
"Multiaddr",
")",
"bool",
"{",
"protosMatch",
":=",
"func",
"(",
"a",
",",
"b",
"[",
"]",
"ma",
".",
"Protocol",
")",
"bool",
"{",
"if",
"len",
"(",
"a",
")",
"!=",
"len",
"(",
"b",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"for",
"i",
",",
"p",
":=",
"range",
"a",
"{",
"if",
"b",
"[",
"i",
"]",
".",
"Code",
"!=",
"p",
".",
"Code",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}",
"\n\n",
"protos",
":=",
"a",
".",
"Protocols",
"(",
")",
"\n\n",
"for",
"_",
",",
"ga",
":=",
"range",
"green",
"{",
"if",
"protosMatch",
"(",
"protos",
",",
"ga",
".",
"Protocols",
"(",
")",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // HasConsistentTransport returns true if the address 'a' shares a
// protocol set with any address in the green set. This is used
// to check if a given address might be one of the addresses a peer is
// listening on. | [
"HasConsistentTransport",
"returns",
"true",
"if",
"the",
"address",
"a",
"shares",
"a",
"protocol",
"set",
"with",
"any",
"address",
"in",
"the",
"green",
"set",
".",
"This",
"is",
"used",
"to",
"check",
"if",
"a",
"given",
"address",
"might",
"be",
"one",
"of",
"the",
"addresses",
"a",
"peer",
"is",
"listening",
"on",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/protocol/identify/id.go#L385-L408 | train |
libp2p/go-libp2p | p2p/host/basic/basic_host.go | newConnHandler | func (h *BasicHost) newConnHandler(c inet.Conn) {
// Clear protocols on connecting to new peer to avoid issues caused
// by misremembering protocols between reconnects
h.Peerstore().SetProtocols(c.RemotePeer())
h.ids.IdentifyConn(c)
} | go | func (h *BasicHost) newConnHandler(c inet.Conn) {
// Clear protocols on connecting to new peer to avoid issues caused
// by misremembering protocols between reconnects
h.Peerstore().SetProtocols(c.RemotePeer())
h.ids.IdentifyConn(c)
} | [
"func",
"(",
"h",
"*",
"BasicHost",
")",
"newConnHandler",
"(",
"c",
"inet",
".",
"Conn",
")",
"{",
"// Clear protocols on connecting to new peer to avoid issues caused",
"// by misremembering protocols between reconnects",
"h",
".",
"Peerstore",
"(",
")",
".",
"SetProtocols",
"(",
"c",
".",
"RemotePeer",
"(",
")",
")",
"\n",
"h",
".",
"ids",
".",
"IdentifyConn",
"(",
"c",
")",
"\n",
"}"
] | // newConnHandler is the remote-opened conn handler for inet.Network | [
"newConnHandler",
"is",
"the",
"remote",
"-",
"opened",
"conn",
"handler",
"for",
"inet",
".",
"Network"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/host/basic/basic_host.go#L224-L229 | train |
libp2p/go-libp2p | p2p/host/basic/basic_host.go | SetStreamHandlerMatch | func (h *BasicHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler inet.StreamHandler) {
h.Mux().AddHandlerWithFunc(string(pid), m, func(p string, rwc io.ReadWriteCloser) error {
is := rwc.(inet.Stream)
is.SetProtocol(protocol.ID(p))
handler(is)
return nil
})
} | go | func (h *BasicHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler inet.StreamHandler) {
h.Mux().AddHandlerWithFunc(string(pid), m, func(p string, rwc io.ReadWriteCloser) error {
is := rwc.(inet.Stream)
is.SetProtocol(protocol.ID(p))
handler(is)
return nil
})
} | [
"func",
"(",
"h",
"*",
"BasicHost",
")",
"SetStreamHandlerMatch",
"(",
"pid",
"protocol",
".",
"ID",
",",
"m",
"func",
"(",
"string",
")",
"bool",
",",
"handler",
"inet",
".",
"StreamHandler",
")",
"{",
"h",
".",
"Mux",
"(",
")",
".",
"AddHandlerWithFunc",
"(",
"string",
"(",
"pid",
")",
",",
"m",
",",
"func",
"(",
"p",
"string",
",",
"rwc",
"io",
".",
"ReadWriteCloser",
")",
"error",
"{",
"is",
":=",
"rwc",
".",
"(",
"inet",
".",
"Stream",
")",
"\n",
"is",
".",
"SetProtocol",
"(",
"protocol",
".",
"ID",
"(",
"p",
")",
")",
"\n",
"handler",
"(",
"is",
")",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] | // SetStreamHandlerMatch sets the protocol handler on the Host's Mux
// using a matching function to do protocol comparisons | [
"SetStreamHandlerMatch",
"sets",
"the",
"protocol",
"handler",
"on",
"the",
"Host",
"s",
"Mux",
"using",
"a",
"matching",
"function",
"to",
"do",
"protocol",
"comparisons"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/host/basic/basic_host.go#L381-L388 | train |
libp2p/go-libp2p | p2p/host/basic/basic_host.go | RemoveStreamHandler | func (h *BasicHost) RemoveStreamHandler(pid protocol.ID) {
h.Mux().RemoveHandler(string(pid))
} | go | func (h *BasicHost) RemoveStreamHandler(pid protocol.ID) {
h.Mux().RemoveHandler(string(pid))
} | [
"func",
"(",
"h",
"*",
"BasicHost",
")",
"RemoveStreamHandler",
"(",
"pid",
"protocol",
".",
"ID",
")",
"{",
"h",
".",
"Mux",
"(",
")",
".",
"RemoveHandler",
"(",
"string",
"(",
"pid",
")",
")",
"\n",
"}"
] | // RemoveStreamHandler returns .. | [
"RemoveStreamHandler",
"returns",
".."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/host/basic/basic_host.go#L391-L393 | train |
libp2p/go-libp2p | p2p/host/basic/basic_host.go | dialPeer | func (h *BasicHost) dialPeer(ctx context.Context, p peer.ID) error {
log.Debugf("host %s dialing %s", h.ID(), p)
c, err := h.Network().DialPeer(ctx, p)
if err != nil {
return err
}
// Clear protocols on connecting to new peer to avoid issues caused
// by misremembering protocols between reconnects
h.Peerstore().SetProtocols(p)
// identify the connection before returning.
done := make(chan struct{})
go func() {
h.ids.IdentifyConn(c)
close(done)
}()
// respect don contexteone
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
log.Debugf("host %s finished dialing %s", h.ID(), p)
return nil
} | go | func (h *BasicHost) dialPeer(ctx context.Context, p peer.ID) error {
log.Debugf("host %s dialing %s", h.ID(), p)
c, err := h.Network().DialPeer(ctx, p)
if err != nil {
return err
}
// Clear protocols on connecting to new peer to avoid issues caused
// by misremembering protocols between reconnects
h.Peerstore().SetProtocols(p)
// identify the connection before returning.
done := make(chan struct{})
go func() {
h.ids.IdentifyConn(c)
close(done)
}()
// respect don contexteone
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
log.Debugf("host %s finished dialing %s", h.ID(), p)
return nil
} | [
"func",
"(",
"h",
"*",
"BasicHost",
")",
"dialPeer",
"(",
"ctx",
"context",
".",
"Context",
",",
"p",
"peer",
".",
"ID",
")",
"error",
"{",
"log",
".",
"Debugf",
"(",
"\"",
"\"",
",",
"h",
".",
"ID",
"(",
")",
",",
"p",
")",
"\n",
"c",
",",
"err",
":=",
"h",
".",
"Network",
"(",
")",
".",
"DialPeer",
"(",
"ctx",
",",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"// Clear protocols on connecting to new peer to avoid issues caused",
"// by misremembering protocols between reconnects",
"h",
".",
"Peerstore",
"(",
")",
".",
"SetProtocols",
"(",
"p",
")",
"\n\n",
"// identify the connection before returning.",
"done",
":=",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"h",
".",
"ids",
".",
"IdentifyConn",
"(",
"c",
")",
"\n",
"close",
"(",
"done",
")",
"\n",
"}",
"(",
")",
"\n\n",
"// respect don contexteone",
"select",
"{",
"case",
"<-",
"done",
":",
"case",
"<-",
"ctx",
".",
"Done",
"(",
")",
":",
"return",
"ctx",
".",
"Err",
"(",
")",
"\n",
"}",
"\n\n",
"log",
".",
"Debugf",
"(",
"\"",
"\"",
",",
"h",
".",
"ID",
"(",
")",
",",
"p",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // dialPeer opens a connection to peer, and makes sure to identify
// the connection once it has been opened. | [
"dialPeer",
"opens",
"a",
"connection",
"to",
"peer",
"and",
"makes",
"sure",
"to",
"identify",
"the",
"connection",
"once",
"it",
"has",
"been",
"opened",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/host/basic/basic_host.go#L523-L550 | train |
libp2p/go-libp2p | p2p/host/basic/basic_host.go | dedupAddrs | func dedupAddrs(addrs []ma.Multiaddr) (uniqueAddrs []ma.Multiaddr) {
exists := make(map[string]bool)
for _, addr := range addrs {
k := string(addr.Bytes())
if exists[k] {
continue
}
exists[k] = true
uniqueAddrs = append(uniqueAddrs, addr)
}
return uniqueAddrs
} | go | func dedupAddrs(addrs []ma.Multiaddr) (uniqueAddrs []ma.Multiaddr) {
exists := make(map[string]bool)
for _, addr := range addrs {
k := string(addr.Bytes())
if exists[k] {
continue
}
exists[k] = true
uniqueAddrs = append(uniqueAddrs, addr)
}
return uniqueAddrs
} | [
"func",
"dedupAddrs",
"(",
"addrs",
"[",
"]",
"ma",
".",
"Multiaddr",
")",
"(",
"uniqueAddrs",
"[",
"]",
"ma",
".",
"Multiaddr",
")",
"{",
"exists",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"for",
"_",
",",
"addr",
":=",
"range",
"addrs",
"{",
"k",
":=",
"string",
"(",
"addr",
".",
"Bytes",
"(",
")",
")",
"\n",
"if",
"exists",
"[",
"k",
"]",
"{",
"continue",
"\n",
"}",
"\n",
"exists",
"[",
"k",
"]",
"=",
"true",
"\n",
"uniqueAddrs",
"=",
"append",
"(",
"uniqueAddrs",
",",
"addr",
")",
"\n",
"}",
"\n",
"return",
"uniqueAddrs",
"\n",
"}"
] | // mergeAddrs merges input address lists, leave only unique addresses | [
"mergeAddrs",
"merges",
"input",
"address",
"lists",
"leave",
"only",
"unique",
"addresses"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/host/basic/basic_host.go#L563-L574 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | newPeernet | func newPeernet(ctx context.Context, m *mocknet, p peer.ID, ps pstore.Peerstore) (*peernet, error) {
n := &peernet{
mocknet: m,
peer: p,
ps: ps,
connsByPeer: map[peer.ID]map[*conn]struct{}{},
connsByLink: map[*link]map[*conn]struct{}{},
notifs: make(map[inet.Notifiee]struct{}),
}
n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
return n, nil
} | go | func newPeernet(ctx context.Context, m *mocknet, p peer.ID, ps pstore.Peerstore) (*peernet, error) {
n := &peernet{
mocknet: m,
peer: p,
ps: ps,
connsByPeer: map[peer.ID]map[*conn]struct{}{},
connsByLink: map[*link]map[*conn]struct{}{},
notifs: make(map[inet.Notifiee]struct{}),
}
n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
return n, nil
} | [
"func",
"newPeernet",
"(",
"ctx",
"context",
".",
"Context",
",",
"m",
"*",
"mocknet",
",",
"p",
"peer",
".",
"ID",
",",
"ps",
"pstore",
".",
"Peerstore",
")",
"(",
"*",
"peernet",
",",
"error",
")",
"{",
"n",
":=",
"&",
"peernet",
"{",
"mocknet",
":",
"m",
",",
"peer",
":",
"p",
",",
"ps",
":",
"ps",
",",
"connsByPeer",
":",
"map",
"[",
"peer",
".",
"ID",
"]",
"map",
"[",
"*",
"conn",
"]",
"struct",
"{",
"}",
"{",
"}",
",",
"connsByLink",
":",
"map",
"[",
"*",
"link",
"]",
"map",
"[",
"*",
"conn",
"]",
"struct",
"{",
"}",
"{",
"}",
",",
"notifs",
":",
"make",
"(",
"map",
"[",
"inet",
".",
"Notifiee",
"]",
"struct",
"{",
"}",
")",
",",
"}",
"\n\n",
"n",
".",
"proc",
"=",
"goprocessctx",
".",
"WithContextAndTeardown",
"(",
"ctx",
",",
"n",
".",
"teardown",
")",
"\n",
"return",
"n",
",",
"nil",
"\n",
"}"
] | // newPeernet constructs a new peernet | [
"newPeernet",
"constructs",
"a",
"new",
"peernet"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L42-L57 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | allConns | func (pn *peernet) allConns() []*conn {
pn.RLock()
var cs []*conn
for _, csl := range pn.connsByPeer {
for c := range csl {
cs = append(cs, c)
}
}
pn.RUnlock()
return cs
} | go | func (pn *peernet) allConns() []*conn {
pn.RLock()
var cs []*conn
for _, csl := range pn.connsByPeer {
for c := range csl {
cs = append(cs, c)
}
}
pn.RUnlock()
return cs
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"allConns",
"(",
")",
"[",
"]",
"*",
"conn",
"{",
"pn",
".",
"RLock",
"(",
")",
"\n",
"var",
"cs",
"[",
"]",
"*",
"conn",
"\n",
"for",
"_",
",",
"csl",
":=",
"range",
"pn",
".",
"connsByPeer",
"{",
"for",
"c",
":=",
"range",
"csl",
"{",
"cs",
"=",
"append",
"(",
"cs",
",",
"c",
")",
"\n",
"}",
"\n",
"}",
"\n",
"pn",
".",
"RUnlock",
"(",
")",
"\n",
"return",
"cs",
"\n",
"}"
] | // allConns returns all the connections between this peer and others | [
"allConns",
"returns",
"all",
"the",
"connections",
"between",
"this",
"peer",
"and",
"others"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L69-L79 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | handleNewStream | func (pn *peernet) handleNewStream(s inet.Stream) {
pn.RLock()
handler := pn.streamHandler
pn.RUnlock()
if handler != nil {
go handler(s)
}
} | go | func (pn *peernet) handleNewStream(s inet.Stream) {
pn.RLock()
handler := pn.streamHandler
pn.RUnlock()
if handler != nil {
go handler(s)
}
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"handleNewStream",
"(",
"s",
"inet",
".",
"Stream",
")",
"{",
"pn",
".",
"RLock",
"(",
")",
"\n",
"handler",
":=",
"pn",
".",
"streamHandler",
"\n",
"pn",
".",
"RUnlock",
"(",
")",
"\n",
"if",
"handler",
"!=",
"nil",
"{",
"go",
"handler",
"(",
"s",
")",
"\n",
"}",
"\n",
"}"
] | // handleNewStream is an internal function to trigger the client's handler | [
"handleNewStream",
"is",
"an",
"internal",
"function",
"to",
"trigger",
"the",
"client",
"s",
"handler"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L95-L102 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | handleNewConn | func (pn *peernet) handleNewConn(c inet.Conn) {
pn.RLock()
handler := pn.connHandler
pn.RUnlock()
if handler != nil {
go handler(c)
}
} | go | func (pn *peernet) handleNewConn(c inet.Conn) {
pn.RLock()
handler := pn.connHandler
pn.RUnlock()
if handler != nil {
go handler(c)
}
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"handleNewConn",
"(",
"c",
"inet",
".",
"Conn",
")",
"{",
"pn",
".",
"RLock",
"(",
")",
"\n",
"handler",
":=",
"pn",
".",
"connHandler",
"\n",
"pn",
".",
"RUnlock",
"(",
")",
"\n",
"if",
"handler",
"!=",
"nil",
"{",
"go",
"handler",
"(",
"c",
")",
"\n",
"}",
"\n",
"}"
] | // handleNewConn is an internal function to trigger the client's handler | [
"handleNewConn",
"is",
"an",
"internal",
"function",
"to",
"trigger",
"the",
"client",
"s",
"handler"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L105-L112 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | DialPeer | func (pn *peernet) DialPeer(ctx context.Context, p peer.ID) (inet.Conn, error) {
return pn.connect(p)
} | go | func (pn *peernet) DialPeer(ctx context.Context, p peer.ID) (inet.Conn, error) {
return pn.connect(p)
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"DialPeer",
"(",
"ctx",
"context",
".",
"Context",
",",
"p",
"peer",
".",
"ID",
")",
"(",
"inet",
".",
"Conn",
",",
"error",
")",
"{",
"return",
"pn",
".",
"connect",
"(",
"p",
")",
"\n",
"}"
] | // DialPeer attempts to establish a connection to a given peer.
// Respects the context. | [
"DialPeer",
"attempts",
"to",
"establish",
"a",
"connection",
"to",
"a",
"given",
"peer",
".",
"Respects",
"the",
"context",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L116-L118 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | addConn | func (pn *peernet) addConn(c *conn) {
pn.Lock()
defer pn.Unlock()
cs, found := pn.connsByPeer[c.RemotePeer()]
if !found {
cs = map[*conn]struct{}{}
pn.connsByPeer[c.RemotePeer()] = cs
}
pn.connsByPeer[c.RemotePeer()][c] = struct{}{}
cs, found = pn.connsByLink[c.link]
if !found {
cs = map[*conn]struct{}{}
pn.connsByLink[c.link] = cs
}
pn.connsByLink[c.link][c] = struct{}{}
} | go | func (pn *peernet) addConn(c *conn) {
pn.Lock()
defer pn.Unlock()
cs, found := pn.connsByPeer[c.RemotePeer()]
if !found {
cs = map[*conn]struct{}{}
pn.connsByPeer[c.RemotePeer()] = cs
}
pn.connsByPeer[c.RemotePeer()][c] = struct{}{}
cs, found = pn.connsByLink[c.link]
if !found {
cs = map[*conn]struct{}{}
pn.connsByLink[c.link] = cs
}
pn.connsByLink[c.link][c] = struct{}{}
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"addConn",
"(",
"c",
"*",
"conn",
")",
"{",
"pn",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pn",
".",
"Unlock",
"(",
")",
"\n\n",
"cs",
",",
"found",
":=",
"pn",
".",
"connsByPeer",
"[",
"c",
".",
"RemotePeer",
"(",
")",
"]",
"\n",
"if",
"!",
"found",
"{",
"cs",
"=",
"map",
"[",
"*",
"conn",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n",
"pn",
".",
"connsByPeer",
"[",
"c",
".",
"RemotePeer",
"(",
")",
"]",
"=",
"cs",
"\n",
"}",
"\n",
"pn",
".",
"connsByPeer",
"[",
"c",
".",
"RemotePeer",
"(",
")",
"]",
"[",
"c",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"cs",
",",
"found",
"=",
"pn",
".",
"connsByLink",
"[",
"c",
".",
"link",
"]",
"\n",
"if",
"!",
"found",
"{",
"cs",
"=",
"map",
"[",
"*",
"conn",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n",
"pn",
".",
"connsByLink",
"[",
"c",
".",
"link",
"]",
"=",
"cs",
"\n",
"}",
"\n",
"pn",
".",
"connsByLink",
"[",
"c",
".",
"link",
"]",
"[",
"c",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}"
] | // addConn constructs and adds a connection
// to given remote peer over given link | [
"addConn",
"constructs",
"and",
"adds",
"a",
"connection",
"to",
"given",
"remote",
"peer",
"over",
"given",
"link"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L180-L197 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | removeConn | func (pn *peernet) removeConn(c *conn) {
pn.Lock()
defer pn.Unlock()
cs, found := pn.connsByLink[c.link]
if !found || len(cs) < 1 {
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %p", c.link))
}
delete(cs, c)
cs, found = pn.connsByPeer[c.remote]
if !found {
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %v", c.remote))
}
delete(cs, c)
} | go | func (pn *peernet) removeConn(c *conn) {
pn.Lock()
defer pn.Unlock()
cs, found := pn.connsByLink[c.link]
if !found || len(cs) < 1 {
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %p", c.link))
}
delete(cs, c)
cs, found = pn.connsByPeer[c.remote]
if !found {
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %v", c.remote))
}
delete(cs, c)
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"removeConn",
"(",
"c",
"*",
"conn",
")",
"{",
"pn",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pn",
".",
"Unlock",
"(",
")",
"\n\n",
"cs",
",",
"found",
":=",
"pn",
".",
"connsByLink",
"[",
"c",
".",
"link",
"]",
"\n",
"if",
"!",
"found",
"||",
"len",
"(",
"cs",
")",
"<",
"1",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"c",
".",
"link",
")",
")",
"\n",
"}",
"\n",
"delete",
"(",
"cs",
",",
"c",
")",
"\n\n",
"cs",
",",
"found",
"=",
"pn",
".",
"connsByPeer",
"[",
"c",
".",
"remote",
"]",
"\n",
"if",
"!",
"found",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"c",
".",
"remote",
")",
")",
"\n",
"}",
"\n",
"delete",
"(",
"cs",
",",
"c",
")",
"\n",
"}"
] | // removeConn removes a given conn | [
"removeConn",
"removes",
"a",
"given",
"conn"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L200-L215 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | Peers | func (pn *peernet) Peers() []peer.ID {
pn.RLock()
defer pn.RUnlock()
peers := make([]peer.ID, 0, len(pn.connsByPeer))
for _, cs := range pn.connsByPeer {
for c := range cs {
peers = append(peers, c.remote)
break
}
}
return peers
} | go | func (pn *peernet) Peers() []peer.ID {
pn.RLock()
defer pn.RUnlock()
peers := make([]peer.ID, 0, len(pn.connsByPeer))
for _, cs := range pn.connsByPeer {
for c := range cs {
peers = append(peers, c.remote)
break
}
}
return peers
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"Peers",
"(",
")",
"[",
"]",
"peer",
".",
"ID",
"{",
"pn",
".",
"RLock",
"(",
")",
"\n",
"defer",
"pn",
".",
"RUnlock",
"(",
")",
"\n\n",
"peers",
":=",
"make",
"(",
"[",
"]",
"peer",
".",
"ID",
",",
"0",
",",
"len",
"(",
"pn",
".",
"connsByPeer",
")",
")",
"\n",
"for",
"_",
",",
"cs",
":=",
"range",
"pn",
".",
"connsByPeer",
"{",
"for",
"c",
":=",
"range",
"cs",
"{",
"peers",
"=",
"append",
"(",
"peers",
",",
"c",
".",
"remote",
")",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"peers",
"\n",
"}"
] | // Peers returns the connected peers | [
"Peers",
"returns",
"the",
"connected",
"peers"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L228-L240 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | Conns | func (pn *peernet) Conns() []inet.Conn {
pn.RLock()
defer pn.RUnlock()
out := make([]inet.Conn, 0, len(pn.connsByPeer))
for _, cs := range pn.connsByPeer {
for c := range cs {
out = append(out, c)
}
}
return out
} | go | func (pn *peernet) Conns() []inet.Conn {
pn.RLock()
defer pn.RUnlock()
out := make([]inet.Conn, 0, len(pn.connsByPeer))
for _, cs := range pn.connsByPeer {
for c := range cs {
out = append(out, c)
}
}
return out
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"Conns",
"(",
")",
"[",
"]",
"inet",
".",
"Conn",
"{",
"pn",
".",
"RLock",
"(",
")",
"\n",
"defer",
"pn",
".",
"RUnlock",
"(",
")",
"\n\n",
"out",
":=",
"make",
"(",
"[",
"]",
"inet",
".",
"Conn",
",",
"0",
",",
"len",
"(",
"pn",
".",
"connsByPeer",
")",
")",
"\n",
"for",
"_",
",",
"cs",
":=",
"range",
"pn",
".",
"connsByPeer",
"{",
"for",
"c",
":=",
"range",
"cs",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"c",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"out",
"\n",
"}"
] | // Conns returns all the connections of this peer | [
"Conns",
"returns",
"all",
"the",
"connections",
"of",
"this",
"peer"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L243-L254 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | ClosePeer | func (pn *peernet) ClosePeer(p peer.ID) error {
pn.RLock()
cs, found := pn.connsByPeer[p]
if !found {
pn.RUnlock()
return nil
}
var conns []*conn
for c := range cs {
conns = append(conns, c)
}
pn.RUnlock()
for _, c := range conns {
c.Close()
}
return nil
} | go | func (pn *peernet) ClosePeer(p peer.ID) error {
pn.RLock()
cs, found := pn.connsByPeer[p]
if !found {
pn.RUnlock()
return nil
}
var conns []*conn
for c := range cs {
conns = append(conns, c)
}
pn.RUnlock()
for _, c := range conns {
c.Close()
}
return nil
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"ClosePeer",
"(",
"p",
"peer",
".",
"ID",
")",
"error",
"{",
"pn",
".",
"RLock",
"(",
")",
"\n",
"cs",
",",
"found",
":=",
"pn",
".",
"connsByPeer",
"[",
"p",
"]",
"\n",
"if",
"!",
"found",
"{",
"pn",
".",
"RUnlock",
"(",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n\n",
"var",
"conns",
"[",
"]",
"*",
"conn",
"\n",
"for",
"c",
":=",
"range",
"cs",
"{",
"conns",
"=",
"append",
"(",
"conns",
",",
"c",
")",
"\n",
"}",
"\n",
"pn",
".",
"RUnlock",
"(",
")",
"\n",
"for",
"_",
",",
"c",
":=",
"range",
"conns",
"{",
"c",
".",
"Close",
"(",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // ClosePeer connections to peer | [
"ClosePeer",
"connections",
"to",
"peer"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L273-L290 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | Listen | func (pn *peernet) Listen(addrs ...ma.Multiaddr) error {
pn.Peerstore().AddAddrs(pn.LocalPeer(), addrs, pstore.PermanentAddrTTL)
return nil
} | go | func (pn *peernet) Listen(addrs ...ma.Multiaddr) error {
pn.Peerstore().AddAddrs(pn.LocalPeer(), addrs, pstore.PermanentAddrTTL)
return nil
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"Listen",
"(",
"addrs",
"...",
"ma",
".",
"Multiaddr",
")",
"error",
"{",
"pn",
".",
"Peerstore",
"(",
")",
".",
"AddAddrs",
"(",
"pn",
".",
"LocalPeer",
"(",
")",
",",
"addrs",
",",
"pstore",
".",
"PermanentAddrTTL",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // Listen tells the network to start listening on given multiaddrs. | [
"Listen",
"tells",
"the",
"network",
"to",
"start",
"listening",
"on",
"given",
"multiaddrs",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L300-L303 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | ListenAddresses | func (pn *peernet) ListenAddresses() []ma.Multiaddr {
return pn.Peerstore().Addrs(pn.LocalPeer())
} | go | func (pn *peernet) ListenAddresses() []ma.Multiaddr {
return pn.Peerstore().Addrs(pn.LocalPeer())
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"ListenAddresses",
"(",
")",
"[",
"]",
"ma",
".",
"Multiaddr",
"{",
"return",
"pn",
".",
"Peerstore",
"(",
")",
".",
"Addrs",
"(",
"pn",
".",
"LocalPeer",
"(",
")",
")",
"\n",
"}"
] | // ListenAddresses returns a list of addresses at which this network listens. | [
"ListenAddresses",
"returns",
"a",
"list",
"of",
"addresses",
"at",
"which",
"this",
"network",
"listens",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L306-L308 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | Connectedness | func (pn *peernet) Connectedness(p peer.ID) inet.Connectedness {
pn.Lock()
defer pn.Unlock()
cs, found := pn.connsByPeer[p]
if found && len(cs) > 0 {
return inet.Connected
}
return inet.NotConnected
} | go | func (pn *peernet) Connectedness(p peer.ID) inet.Connectedness {
pn.Lock()
defer pn.Unlock()
cs, found := pn.connsByPeer[p]
if found && len(cs) > 0 {
return inet.Connected
}
return inet.NotConnected
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"Connectedness",
"(",
"p",
"peer",
".",
"ID",
")",
"inet",
".",
"Connectedness",
"{",
"pn",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pn",
".",
"Unlock",
"(",
")",
"\n\n",
"cs",
",",
"found",
":=",
"pn",
".",
"connsByPeer",
"[",
"p",
"]",
"\n",
"if",
"found",
"&&",
"len",
"(",
"cs",
")",
">",
"0",
"{",
"return",
"inet",
".",
"Connected",
"\n",
"}",
"\n",
"return",
"inet",
".",
"NotConnected",
"\n",
"}"
] | // Connectedness returns a state signaling connection capabilities
// For now only returns Connecter || NotConnected. Expand into more later. | [
"Connectedness",
"returns",
"a",
"state",
"signaling",
"connection",
"capabilities",
"For",
"now",
"only",
"returns",
"Connecter",
"||",
"NotConnected",
".",
"Expand",
"into",
"more",
"later",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L319-L328 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | NewStream | func (pn *peernet) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error) {
c, err := pn.DialPeer(ctx, p)
if err != nil {
return nil, err
}
return c.NewStream()
} | go | func (pn *peernet) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error) {
c, err := pn.DialPeer(ctx, p)
if err != nil {
return nil, err
}
return c.NewStream()
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"NewStream",
"(",
"ctx",
"context",
".",
"Context",
",",
"p",
"peer",
".",
"ID",
")",
"(",
"inet",
".",
"Stream",
",",
"error",
")",
"{",
"c",
",",
"err",
":=",
"pn",
".",
"DialPeer",
"(",
"ctx",
",",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"c",
".",
"NewStream",
"(",
")",
"\n",
"}"
] | // NewStream returns a new stream to given peer p.
// If there is no connection to p, attempts to create one. | [
"NewStream",
"returns",
"a",
"new",
"stream",
"to",
"given",
"peer",
"p",
".",
"If",
"there",
"is",
"no",
"connection",
"to",
"p",
"attempts",
"to",
"create",
"one",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L332-L338 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | SetStreamHandler | func (pn *peernet) SetStreamHandler(h inet.StreamHandler) {
pn.Lock()
pn.streamHandler = h
pn.Unlock()
} | go | func (pn *peernet) SetStreamHandler(h inet.StreamHandler) {
pn.Lock()
pn.streamHandler = h
pn.Unlock()
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"SetStreamHandler",
"(",
"h",
"inet",
".",
"StreamHandler",
")",
"{",
"pn",
".",
"Lock",
"(",
")",
"\n",
"pn",
".",
"streamHandler",
"=",
"h",
"\n",
"pn",
".",
"Unlock",
"(",
")",
"\n",
"}"
] | // SetStreamHandler sets the new stream handler on the Network.
// This operation is threadsafe. | [
"SetStreamHandler",
"sets",
"the",
"new",
"stream",
"handler",
"on",
"the",
"Network",
".",
"This",
"operation",
"is",
"threadsafe",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L342-L346 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | SetConnHandler | func (pn *peernet) SetConnHandler(h inet.ConnHandler) {
pn.Lock()
pn.connHandler = h
pn.Unlock()
} | go | func (pn *peernet) SetConnHandler(h inet.ConnHandler) {
pn.Lock()
pn.connHandler = h
pn.Unlock()
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"SetConnHandler",
"(",
"h",
"inet",
".",
"ConnHandler",
")",
"{",
"pn",
".",
"Lock",
"(",
")",
"\n",
"pn",
".",
"connHandler",
"=",
"h",
"\n",
"pn",
".",
"Unlock",
"(",
")",
"\n",
"}"
] | // SetConnHandler sets the new conn handler on the Network.
// This operation is threadsafe. | [
"SetConnHandler",
"sets",
"the",
"new",
"conn",
"handler",
"on",
"the",
"Network",
".",
"This",
"operation",
"is",
"threadsafe",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L350-L354 | train |
libp2p/go-libp2p | p2p/net/mock/mock_peernet.go | StopNotify | func (pn *peernet) StopNotify(f inet.Notifiee) {
pn.notifmu.Lock()
delete(pn.notifs, f)
pn.notifmu.Unlock()
} | go | func (pn *peernet) StopNotify(f inet.Notifiee) {
pn.notifmu.Lock()
delete(pn.notifs, f)
pn.notifmu.Unlock()
} | [
"func",
"(",
"pn",
"*",
"peernet",
")",
"StopNotify",
"(",
"f",
"inet",
".",
"Notifiee",
")",
"{",
"pn",
".",
"notifmu",
".",
"Lock",
"(",
")",
"\n",
"delete",
"(",
"pn",
".",
"notifs",
",",
"f",
")",
"\n",
"pn",
".",
"notifmu",
".",
"Unlock",
"(",
")",
"\n",
"}"
] | // StopNotify unregisters Notifiee from receiving signals | [
"StopNotify",
"unregisters",
"Notifiee",
"from",
"receiving",
"signals"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_peernet.go#L364-L368 | train |
libp2p/go-libp2p | p2p/net/mock/mock_net.go | linksMapGet | func (mn *mocknet) linksMapGet(p1, p2 peer.ID) map[*link]struct{} {
l1, found := mn.links[p1]
if !found {
mn.links[p1] = map[peer.ID]map[*link]struct{}{}
l1 = mn.links[p1] // so we make sure it's there.
}
l2, found := l1[p2]
if !found {
m := map[*link]struct{}{}
l1[p2] = m
l2 = l1[p2]
}
return l2
} | go | func (mn *mocknet) linksMapGet(p1, p2 peer.ID) map[*link]struct{} {
l1, found := mn.links[p1]
if !found {
mn.links[p1] = map[peer.ID]map[*link]struct{}{}
l1 = mn.links[p1] // so we make sure it's there.
}
l2, found := l1[p2]
if !found {
m := map[*link]struct{}{}
l1[p2] = m
l2 = l1[p2]
}
return l2
} | [
"func",
"(",
"mn",
"*",
"mocknet",
")",
"linksMapGet",
"(",
"p1",
",",
"p2",
"peer",
".",
"ID",
")",
"map",
"[",
"*",
"link",
"]",
"struct",
"{",
"}",
"{",
"l1",
",",
"found",
":=",
"mn",
".",
"links",
"[",
"p1",
"]",
"\n",
"if",
"!",
"found",
"{",
"mn",
".",
"links",
"[",
"p1",
"]",
"=",
"map",
"[",
"peer",
".",
"ID",
"]",
"map",
"[",
"*",
"link",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n",
"l1",
"=",
"mn",
".",
"links",
"[",
"p1",
"]",
"// so we make sure it's there.",
"\n",
"}",
"\n\n",
"l2",
",",
"found",
":=",
"l1",
"[",
"p2",
"]",
"\n",
"if",
"!",
"found",
"{",
"m",
":=",
"map",
"[",
"*",
"link",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n",
"l1",
"[",
"p2",
"]",
"=",
"m",
"\n",
"l2",
"=",
"l1",
"[",
"p2",
"]",
"\n",
"}",
"\n\n",
"return",
"l2",
"\n",
"}"
] | // get from the links map. and lazily contruct. | [
"get",
"from",
"the",
"links",
"map",
".",
"and",
"lazily",
"contruct",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock_net.go#L288-L304 | train |
libp2p/go-libp2p | config/reflection_magic.go | checkReturnType | func checkReturnType(fnType, tptType reflect.Type) error {
switch fnType.NumOut() {
case 2:
if fnType.Out(1) != errorType {
return fmt.Errorf("expected (optional) second return value from transport constructor to be an error")
}
fallthrough
case 1:
if !fnType.Out(0).Implements(tptType) {
return fmt.Errorf("transport constructor returns %s which doesn't implement %s", fnType.Out(0), tptType)
}
default:
return fmt.Errorf("expected transport constructor to return a transport and, optionally, an error")
}
return nil
} | go | func checkReturnType(fnType, tptType reflect.Type) error {
switch fnType.NumOut() {
case 2:
if fnType.Out(1) != errorType {
return fmt.Errorf("expected (optional) second return value from transport constructor to be an error")
}
fallthrough
case 1:
if !fnType.Out(0).Implements(tptType) {
return fmt.Errorf("transport constructor returns %s which doesn't implement %s", fnType.Out(0), tptType)
}
default:
return fmt.Errorf("expected transport constructor to return a transport and, optionally, an error")
}
return nil
} | [
"func",
"checkReturnType",
"(",
"fnType",
",",
"tptType",
"reflect",
".",
"Type",
")",
"error",
"{",
"switch",
"fnType",
".",
"NumOut",
"(",
")",
"{",
"case",
"2",
":",
"if",
"fnType",
".",
"Out",
"(",
"1",
")",
"!=",
"errorType",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"fallthrough",
"\n",
"case",
"1",
":",
"if",
"!",
"fnType",
".",
"Out",
"(",
"0",
")",
".",
"Implements",
"(",
"tptType",
")",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"fnType",
".",
"Out",
"(",
"0",
")",
",",
"tptType",
")",
"\n",
"}",
"\n",
"default",
":",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // checks if a function returns either the specified type or the specified type
// and an error. | [
"checks",
"if",
"a",
"function",
"returns",
"either",
"the",
"specified",
"type",
"or",
"the",
"specified",
"type",
"and",
"an",
"error",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/config/reflection_magic.go#L16-L32 | train |
libp2p/go-libp2p | config/reflection_magic.go | callConstructor | func callConstructor(c reflect.Value, args []reflect.Value) (interface{}, error) {
val, err := handleReturnValue(c.Call(args))
if err != nil {
name := runtime.FuncForPC(c.Pointer()).Name()
if name != "" {
// makes debugging easier
return nil, fmt.Errorf("transport constructor %s failed: %s", name, err)
}
}
return val, err
} | go | func callConstructor(c reflect.Value, args []reflect.Value) (interface{}, error) {
val, err := handleReturnValue(c.Call(args))
if err != nil {
name := runtime.FuncForPC(c.Pointer()).Name()
if name != "" {
// makes debugging easier
return nil, fmt.Errorf("transport constructor %s failed: %s", name, err)
}
}
return val, err
} | [
"func",
"callConstructor",
"(",
"c",
"reflect",
".",
"Value",
",",
"args",
"[",
"]",
"reflect",
".",
"Value",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"val",
",",
"err",
":=",
"handleReturnValue",
"(",
"c",
".",
"Call",
"(",
"args",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"name",
":=",
"runtime",
".",
"FuncForPC",
"(",
"c",
".",
"Pointer",
"(",
")",
")",
".",
"Name",
"(",
")",
"\n",
"if",
"name",
"!=",
"\"",
"\"",
"{",
"// makes debugging easier",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"name",
",",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"val",
",",
"err",
"\n",
"}"
] | // calls the transport constructor and annotates the error with the name of the constructor. | [
"calls",
"the",
"transport",
"constructor",
"and",
"annotates",
"the",
"error",
"with",
"the",
"name",
"of",
"the",
"constructor",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/config/reflection_magic.go#L67-L77 | train |
libp2p/go-libp2p | config/reflection_magic.go | makeConstructor | func makeConstructor(
tpt interface{},
tptType reflect.Type,
argTypes map[reflect.Type]constructor,
) (func(host.Host, *tptu.Upgrader) (interface{}, error), error) {
v := reflect.ValueOf(tpt)
// avoid panicing on nil/zero value.
if v == (reflect.Value{}) {
return nil, fmt.Errorf("expected a transport or transport constructor, got a %T", tpt)
}
t := v.Type()
if t.Kind() != reflect.Func {
return nil, fmt.Errorf("expected a transport or transport constructor, got a %T", tpt)
}
if err := checkReturnType(t, tptType); err != nil {
return nil, err
}
argConstructors, err := makeArgumentConstructors(t, argTypes)
if err != nil {
return nil, err
}
return func(h host.Host, u *tptu.Upgrader) (interface{}, error) {
arguments := make([]reflect.Value, len(argConstructors))
for i, makeArg := range argConstructors {
arguments[i] = reflect.ValueOf(makeArg(h, u))
}
return callConstructor(v, arguments)
}, nil
} | go | func makeConstructor(
tpt interface{},
tptType reflect.Type,
argTypes map[reflect.Type]constructor,
) (func(host.Host, *tptu.Upgrader) (interface{}, error), error) {
v := reflect.ValueOf(tpt)
// avoid panicing on nil/zero value.
if v == (reflect.Value{}) {
return nil, fmt.Errorf("expected a transport or transport constructor, got a %T", tpt)
}
t := v.Type()
if t.Kind() != reflect.Func {
return nil, fmt.Errorf("expected a transport or transport constructor, got a %T", tpt)
}
if err := checkReturnType(t, tptType); err != nil {
return nil, err
}
argConstructors, err := makeArgumentConstructors(t, argTypes)
if err != nil {
return nil, err
}
return func(h host.Host, u *tptu.Upgrader) (interface{}, error) {
arguments := make([]reflect.Value, len(argConstructors))
for i, makeArg := range argConstructors {
arguments[i] = reflect.ValueOf(makeArg(h, u))
}
return callConstructor(v, arguments)
}, nil
} | [
"func",
"makeConstructor",
"(",
"tpt",
"interface",
"{",
"}",
",",
"tptType",
"reflect",
".",
"Type",
",",
"argTypes",
"map",
"[",
"reflect",
".",
"Type",
"]",
"constructor",
",",
")",
"(",
"func",
"(",
"host",
".",
"Host",
",",
"*",
"tptu",
".",
"Upgrader",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
",",
"error",
")",
"{",
"v",
":=",
"reflect",
".",
"ValueOf",
"(",
"tpt",
")",
"\n",
"// avoid panicing on nil/zero value.",
"if",
"v",
"==",
"(",
"reflect",
".",
"Value",
"{",
"}",
")",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"tpt",
")",
"\n",
"}",
"\n",
"t",
":=",
"v",
".",
"Type",
"(",
")",
"\n",
"if",
"t",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Func",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"tpt",
")",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"checkReturnType",
"(",
"t",
",",
"tptType",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"argConstructors",
",",
"err",
":=",
"makeArgumentConstructors",
"(",
"t",
",",
"argTypes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"func",
"(",
"h",
"host",
".",
"Host",
",",
"u",
"*",
"tptu",
".",
"Upgrader",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"arguments",
":=",
"make",
"(",
"[",
"]",
"reflect",
".",
"Value",
",",
"len",
"(",
"argConstructors",
")",
")",
"\n",
"for",
"i",
",",
"makeArg",
":=",
"range",
"argConstructors",
"{",
"arguments",
"[",
"i",
"]",
"=",
"reflect",
".",
"ValueOf",
"(",
"makeArg",
"(",
"h",
",",
"u",
")",
")",
"\n",
"}",
"\n",
"return",
"callConstructor",
"(",
"v",
",",
"arguments",
")",
"\n",
"}",
",",
"nil",
"\n",
"}"
] | // makes a transport constructor. | [
"makes",
"a",
"transport",
"constructor",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/config/reflection_magic.go#L95-L126 | train |
libp2p/go-libp2p | p2p/net/mock/ratelimiter.go | UpdateBandwidth | func (r *RateLimiter) UpdateBandwidth(bandwidth float64) {
r.lock.Lock()
defer r.lock.Unlock()
// Convert bandwidth from bytes/second to bytes/nanosecond
b := bandwidth / float64(time.Second)
r.bandwidth = b
// Reset allowance
r.allowance = 0
r.maxAllowance = bandwidth
r.lastUpdate = time.Now()
} | go | func (r *RateLimiter) UpdateBandwidth(bandwidth float64) {
r.lock.Lock()
defer r.lock.Unlock()
// Convert bandwidth from bytes/second to bytes/nanosecond
b := bandwidth / float64(time.Second)
r.bandwidth = b
// Reset allowance
r.allowance = 0
r.maxAllowance = bandwidth
r.lastUpdate = time.Now()
} | [
"func",
"(",
"r",
"*",
"RateLimiter",
")",
"UpdateBandwidth",
"(",
"bandwidth",
"float64",
")",
"{",
"r",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"r",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"// Convert bandwidth from bytes/second to bytes/nanosecond",
"b",
":=",
"bandwidth",
"/",
"float64",
"(",
"time",
".",
"Second",
")",
"\n",
"r",
".",
"bandwidth",
"=",
"b",
"\n",
"// Reset allowance",
"r",
".",
"allowance",
"=",
"0",
"\n",
"r",
".",
"maxAllowance",
"=",
"bandwidth",
"\n",
"r",
".",
"lastUpdate",
"=",
"time",
".",
"Now",
"(",
")",
"\n",
"}"
] | // Changes bandwidth of a RateLimiter and resets its allowance | [
"Changes",
"bandwidth",
"of",
"a",
"RateLimiter",
"and",
"resets",
"its",
"allowance"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/ratelimiter.go#L33-L43 | train |
libp2p/go-libp2p | p2p/net/mock/ratelimiter.go | Limit | func (r *RateLimiter) Limit(dataSize int) time.Duration {
r.lock.Lock()
defer r.lock.Unlock()
// update time
var duration time.Duration = time.Duration(0)
if r.bandwidth == 0 {
return duration
}
current := time.Now()
elapsedTime := current.Sub(r.lastUpdate)
r.lastUpdate = current
allowance := r.allowance + float64(elapsedTime)*r.bandwidth
// allowance can't exceed bandwidth
if allowance > r.maxAllowance {
allowance = r.maxAllowance
}
allowance -= float64(dataSize)
if allowance < 0 {
// sleep until allowance is back to 0
duration = time.Duration(-allowance / r.bandwidth)
// rate limiting was applied, record stats
r.count++
r.duration += duration
}
r.allowance = allowance
return duration
} | go | func (r *RateLimiter) Limit(dataSize int) time.Duration {
r.lock.Lock()
defer r.lock.Unlock()
// update time
var duration time.Duration = time.Duration(0)
if r.bandwidth == 0 {
return duration
}
current := time.Now()
elapsedTime := current.Sub(r.lastUpdate)
r.lastUpdate = current
allowance := r.allowance + float64(elapsedTime)*r.bandwidth
// allowance can't exceed bandwidth
if allowance > r.maxAllowance {
allowance = r.maxAllowance
}
allowance -= float64(dataSize)
if allowance < 0 {
// sleep until allowance is back to 0
duration = time.Duration(-allowance / r.bandwidth)
// rate limiting was applied, record stats
r.count++
r.duration += duration
}
r.allowance = allowance
return duration
} | [
"func",
"(",
"r",
"*",
"RateLimiter",
")",
"Limit",
"(",
"dataSize",
"int",
")",
"time",
".",
"Duration",
"{",
"r",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"r",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"// update time",
"var",
"duration",
"time",
".",
"Duration",
"=",
"time",
".",
"Duration",
"(",
"0",
")",
"\n",
"if",
"r",
".",
"bandwidth",
"==",
"0",
"{",
"return",
"duration",
"\n",
"}",
"\n",
"current",
":=",
"time",
".",
"Now",
"(",
")",
"\n",
"elapsedTime",
":=",
"current",
".",
"Sub",
"(",
"r",
".",
"lastUpdate",
")",
"\n",
"r",
".",
"lastUpdate",
"=",
"current",
"\n\n",
"allowance",
":=",
"r",
".",
"allowance",
"+",
"float64",
"(",
"elapsedTime",
")",
"*",
"r",
".",
"bandwidth",
"\n",
"// allowance can't exceed bandwidth",
"if",
"allowance",
">",
"r",
".",
"maxAllowance",
"{",
"allowance",
"=",
"r",
".",
"maxAllowance",
"\n",
"}",
"\n\n",
"allowance",
"-=",
"float64",
"(",
"dataSize",
")",
"\n",
"if",
"allowance",
"<",
"0",
"{",
"// sleep until allowance is back to 0",
"duration",
"=",
"time",
".",
"Duration",
"(",
"-",
"allowance",
"/",
"r",
".",
"bandwidth",
")",
"\n",
"// rate limiting was applied, record stats",
"r",
".",
"count",
"++",
"\n",
"r",
".",
"duration",
"+=",
"duration",
"\n",
"}",
"\n\n",
"r",
".",
"allowance",
"=",
"allowance",
"\n",
"return",
"duration",
"\n",
"}"
] | // Returns how long to wait before sending data with length 'dataSize' bytes | [
"Returns",
"how",
"long",
"to",
"wait",
"before",
"sending",
"data",
"with",
"length",
"dataSize",
"bytes"
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/ratelimiter.go#L46-L75 | train |
libp2p/go-libp2p | p2p/host/relay/relay.go | Advertise | func Advertise(ctx context.Context, advertise discovery.Advertiser) {
go func() {
select {
case <-time.After(AdvertiseBootDelay):
discovery.Advertise(ctx, advertise, RelayRendezvous)
case <-ctx.Done():
}
}()
} | go | func Advertise(ctx context.Context, advertise discovery.Advertiser) {
go func() {
select {
case <-time.After(AdvertiseBootDelay):
discovery.Advertise(ctx, advertise, RelayRendezvous)
case <-ctx.Done():
}
}()
} | [
"func",
"Advertise",
"(",
"ctx",
"context",
".",
"Context",
",",
"advertise",
"discovery",
".",
"Advertiser",
")",
"{",
"go",
"func",
"(",
")",
"{",
"select",
"{",
"case",
"<-",
"time",
".",
"After",
"(",
"AdvertiseBootDelay",
")",
":",
"discovery",
".",
"Advertise",
"(",
"ctx",
",",
"advertise",
",",
"RelayRendezvous",
")",
"\n",
"case",
"<-",
"ctx",
".",
"Done",
"(",
")",
":",
"}",
"\n",
"}",
"(",
")",
"\n",
"}"
] | // Advertise advertises this node as a libp2p relay. | [
"Advertise",
"advertises",
"this",
"node",
"as",
"a",
"libp2p",
"relay",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/host/relay/relay.go#L17-L25 | train |
libp2p/go-libp2p | p2p/host/relay/relay.go | Filter | func Filter(addrs []ma.Multiaddr) []ma.Multiaddr {
raddrs := make([]ma.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
if isRelayAddr(addr) {
continue
}
raddrs = append(raddrs, addr)
}
return raddrs
} | go | func Filter(addrs []ma.Multiaddr) []ma.Multiaddr {
raddrs := make([]ma.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
if isRelayAddr(addr) {
continue
}
raddrs = append(raddrs, addr)
}
return raddrs
} | [
"func",
"Filter",
"(",
"addrs",
"[",
"]",
"ma",
".",
"Multiaddr",
")",
"[",
"]",
"ma",
".",
"Multiaddr",
"{",
"raddrs",
":=",
"make",
"(",
"[",
"]",
"ma",
".",
"Multiaddr",
",",
"0",
",",
"len",
"(",
"addrs",
")",
")",
"\n",
"for",
"_",
",",
"addr",
":=",
"range",
"addrs",
"{",
"if",
"isRelayAddr",
"(",
"addr",
")",
"{",
"continue",
"\n",
"}",
"\n",
"raddrs",
"=",
"append",
"(",
"raddrs",
",",
"addr",
")",
"\n",
"}",
"\n",
"return",
"raddrs",
"\n",
"}"
] | // Filter filters out all relay addresses. | [
"Filter",
"filters",
"out",
"all",
"relay",
"addresses",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/host/relay/relay.go#L28-L37 | train |
libp2p/go-libp2p | config/muxer.go | MuxerConstructor | func MuxerConstructor(m interface{}) (MuxC, error) {
// Already constructed?
if t, ok := m.(mux.Transport); ok {
return func(_ host.Host) (mux.Transport, error) {
return t, nil
}, nil
}
ctor, err := makeConstructor(m, muxType, muxArgTypes)
if err != nil {
return nil, err
}
return func(h host.Host) (mux.Transport, error) {
t, err := ctor(h, nil)
if err != nil {
return nil, err
}
return t.(mux.Transport), nil
}, nil
} | go | func MuxerConstructor(m interface{}) (MuxC, error) {
// Already constructed?
if t, ok := m.(mux.Transport); ok {
return func(_ host.Host) (mux.Transport, error) {
return t, nil
}, nil
}
ctor, err := makeConstructor(m, muxType, muxArgTypes)
if err != nil {
return nil, err
}
return func(h host.Host) (mux.Transport, error) {
t, err := ctor(h, nil)
if err != nil {
return nil, err
}
return t.(mux.Transport), nil
}, nil
} | [
"func",
"MuxerConstructor",
"(",
"m",
"interface",
"{",
"}",
")",
"(",
"MuxC",
",",
"error",
")",
"{",
"// Already constructed?",
"if",
"t",
",",
"ok",
":=",
"m",
".",
"(",
"mux",
".",
"Transport",
")",
";",
"ok",
"{",
"return",
"func",
"(",
"_",
"host",
".",
"Host",
")",
"(",
"mux",
".",
"Transport",
",",
"error",
")",
"{",
"return",
"t",
",",
"nil",
"\n",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"ctor",
",",
"err",
":=",
"makeConstructor",
"(",
"m",
",",
"muxType",
",",
"muxArgTypes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"func",
"(",
"h",
"host",
".",
"Host",
")",
"(",
"mux",
".",
"Transport",
",",
"error",
")",
"{",
"t",
",",
"err",
":=",
"ctor",
"(",
"h",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"t",
".",
"(",
"mux",
".",
"Transport",
")",
",",
"nil",
"\n",
"}",
",",
"nil",
"\n",
"}"
] | // MuxerConstructor creates a multiplex constructor from the passed parameter
// using reflection. | [
"MuxerConstructor",
"creates",
"a",
"multiplex",
"constructor",
"from",
"the",
"passed",
"parameter",
"using",
"reflection",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/config/muxer.go#L25-L44 | train |
libp2p/go-libp2p | p2p/net/mock/mock.go | WithNPeers | func WithNPeers(ctx context.Context, n int) (Mocknet, error) {
m := New(ctx)
for i := 0; i < n; i++ {
if _, err := m.GenPeer(); err != nil {
return nil, err
}
}
return m, nil
} | go | func WithNPeers(ctx context.Context, n int) (Mocknet, error) {
m := New(ctx)
for i := 0; i < n; i++ {
if _, err := m.GenPeer(); err != nil {
return nil, err
}
}
return m, nil
} | [
"func",
"WithNPeers",
"(",
"ctx",
"context",
".",
"Context",
",",
"n",
"int",
")",
"(",
"Mocknet",
",",
"error",
")",
"{",
"m",
":=",
"New",
"(",
"ctx",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
"{",
"if",
"_",
",",
"err",
":=",
"m",
".",
"GenPeer",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"m",
",",
"nil",
"\n",
"}"
] | // WithNPeers constructs a Mocknet with N peers. | [
"WithNPeers",
"constructs",
"a",
"Mocknet",
"with",
"N",
"peers",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock.go#L12-L20 | train |
libp2p/go-libp2p | p2p/net/mock/mock.go | FullMeshConnected | func FullMeshConnected(ctx context.Context, n int) (Mocknet, error) {
m, err := FullMeshLinked(ctx, n)
if err != nil {
return nil, err
}
err = m.ConnectAllButSelf()
if err != nil {
return nil, err
}
return m, nil
} | go | func FullMeshConnected(ctx context.Context, n int) (Mocknet, error) {
m, err := FullMeshLinked(ctx, n)
if err != nil {
return nil, err
}
err = m.ConnectAllButSelf()
if err != nil {
return nil, err
}
return m, nil
} | [
"func",
"FullMeshConnected",
"(",
"ctx",
"context",
".",
"Context",
",",
"n",
"int",
")",
"(",
"Mocknet",
",",
"error",
")",
"{",
"m",
",",
"err",
":=",
"FullMeshLinked",
"(",
"ctx",
",",
"n",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"m",
".",
"ConnectAllButSelf",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"m",
",",
"nil",
"\n",
"}"
] | // FullMeshConnected constructs a Mocknet with full mesh of Connections.
// This means that all the peers have dialed and are ready to talk to
// each other. | [
"FullMeshConnected",
"constructs",
"a",
"Mocknet",
"with",
"full",
"mesh",
"of",
"Connections",
".",
"This",
"means",
"that",
"all",
"the",
"peers",
"have",
"dialed",
"and",
"are",
"ready",
"to",
"talk",
"to",
"each",
"other",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/p2p/net/mock/mock.go#L41-L52 | train |
libp2p/go-libp2p | options.go | ListenAddrs | func ListenAddrs(addrs ...ma.Multiaddr) Option {
return func(cfg *Config) error {
cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...)
return nil
}
} | go | func ListenAddrs(addrs ...ma.Multiaddr) Option {
return func(cfg *Config) error {
cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...)
return nil
}
} | [
"func",
"ListenAddrs",
"(",
"addrs",
"...",
"ma",
".",
"Multiaddr",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"cfg",
".",
"ListenAddrs",
"=",
"append",
"(",
"cfg",
".",
"ListenAddrs",
",",
"addrs",
"...",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // ListenAddrs configures libp2p to listen on the given addresses. | [
"ListenAddrs",
"configures",
"libp2p",
"to",
"listen",
"on",
"the",
"given",
"addresses",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L39-L44 | train |
libp2p/go-libp2p | options.go | Peerstore | func Peerstore(ps pstore.Peerstore) Option {
return func(cfg *Config) error {
if cfg.Peerstore != nil {
return fmt.Errorf("cannot specify multiple peerstore options")
}
cfg.Peerstore = ps
return nil
}
} | go | func Peerstore(ps pstore.Peerstore) Option {
return func(cfg *Config) error {
if cfg.Peerstore != nil {
return fmt.Errorf("cannot specify multiple peerstore options")
}
cfg.Peerstore = ps
return nil
}
} | [
"func",
"Peerstore",
"(",
"ps",
"pstore",
".",
"Peerstore",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"if",
"cfg",
".",
"Peerstore",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"cfg",
".",
"Peerstore",
"=",
"ps",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // Peerstore configures libp2p to use the given peerstore. | [
"Peerstore",
"configures",
"libp2p",
"to",
"use",
"the",
"given",
"peerstore",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L135-L144 | train |
libp2p/go-libp2p | options.go | PrivateNetwork | func PrivateNetwork(prot pnet.Protector) Option {
return func(cfg *Config) error {
if cfg.Protector != nil {
return fmt.Errorf("cannot specify multiple private network options")
}
cfg.Protector = prot
return nil
}
} | go | func PrivateNetwork(prot pnet.Protector) Option {
return func(cfg *Config) error {
if cfg.Protector != nil {
return fmt.Errorf("cannot specify multiple private network options")
}
cfg.Protector = prot
return nil
}
} | [
"func",
"PrivateNetwork",
"(",
"prot",
"pnet",
".",
"Protector",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"if",
"cfg",
".",
"Protector",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"cfg",
".",
"Protector",
"=",
"prot",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // PrivateNetwork configures libp2p to use the given private network protector. | [
"PrivateNetwork",
"configures",
"libp2p",
"to",
"use",
"the",
"given",
"private",
"network",
"protector",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L147-L156 | train |
libp2p/go-libp2p | options.go | BandwidthReporter | func BandwidthReporter(rep metrics.Reporter) Option {
return func(cfg *Config) error {
if cfg.Reporter != nil {
return fmt.Errorf("cannot specify multiple bandwidth reporter options")
}
cfg.Reporter = rep
return nil
}
} | go | func BandwidthReporter(rep metrics.Reporter) Option {
return func(cfg *Config) error {
if cfg.Reporter != nil {
return fmt.Errorf("cannot specify multiple bandwidth reporter options")
}
cfg.Reporter = rep
return nil
}
} | [
"func",
"BandwidthReporter",
"(",
"rep",
"metrics",
".",
"Reporter",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"if",
"cfg",
".",
"Reporter",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"cfg",
".",
"Reporter",
"=",
"rep",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // BandwidthReporter configures libp2p to use the given bandwidth reporter. | [
"BandwidthReporter",
"configures",
"libp2p",
"to",
"use",
"the",
"given",
"bandwidth",
"reporter",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L159-L168 | train |
libp2p/go-libp2p | options.go | Identity | func Identity(sk crypto.PrivKey) Option {
return func(cfg *Config) error {
if cfg.PeerKey != nil {
return fmt.Errorf("cannot specify multiple identities")
}
cfg.PeerKey = sk
return nil
}
} | go | func Identity(sk crypto.PrivKey) Option {
return func(cfg *Config) error {
if cfg.PeerKey != nil {
return fmt.Errorf("cannot specify multiple identities")
}
cfg.PeerKey = sk
return nil
}
} | [
"func",
"Identity",
"(",
"sk",
"crypto",
".",
"PrivKey",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"if",
"cfg",
".",
"PeerKey",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"cfg",
".",
"PeerKey",
"=",
"sk",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // Identity configures libp2p to use the given private key to identify itself. | [
"Identity",
"configures",
"libp2p",
"to",
"use",
"the",
"given",
"private",
"key",
"to",
"identify",
"itself",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L171-L180 | train |
libp2p/go-libp2p | options.go | ConnectionManager | func ConnectionManager(connman ifconnmgr.ConnManager) Option {
return func(cfg *Config) error {
if cfg.ConnManager != nil {
return fmt.Errorf("cannot specify multiple connection managers")
}
cfg.ConnManager = connman
return nil
}
} | go | func ConnectionManager(connman ifconnmgr.ConnManager) Option {
return func(cfg *Config) error {
if cfg.ConnManager != nil {
return fmt.Errorf("cannot specify multiple connection managers")
}
cfg.ConnManager = connman
return nil
}
} | [
"func",
"ConnectionManager",
"(",
"connman",
"ifconnmgr",
".",
"ConnManager",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"if",
"cfg",
".",
"ConnManager",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"cfg",
".",
"ConnManager",
"=",
"connman",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // ConnectionManager configures libp2p to use the given connection manager. | [
"ConnectionManager",
"configures",
"libp2p",
"to",
"use",
"the",
"given",
"connection",
"manager",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L183-L191 | train |
libp2p/go-libp2p | options.go | AddrsFactory | func AddrsFactory(factory config.AddrsFactory) Option {
return func(cfg *Config) error {
if cfg.AddrsFactory != nil {
return fmt.Errorf("cannot specify multiple address factories")
}
cfg.AddrsFactory = factory
return nil
}
} | go | func AddrsFactory(factory config.AddrsFactory) Option {
return func(cfg *Config) error {
if cfg.AddrsFactory != nil {
return fmt.Errorf("cannot specify multiple address factories")
}
cfg.AddrsFactory = factory
return nil
}
} | [
"func",
"AddrsFactory",
"(",
"factory",
"config",
".",
"AddrsFactory",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"if",
"cfg",
".",
"AddrsFactory",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"cfg",
".",
"AddrsFactory",
"=",
"factory",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // AddrsFactory configures libp2p to use the given address factory. | [
"AddrsFactory",
"configures",
"libp2p",
"to",
"use",
"the",
"given",
"address",
"factory",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L194-L202 | train |
libp2p/go-libp2p | options.go | DisableRelay | func DisableRelay() Option {
return func(cfg *Config) error {
cfg.RelayCustom = true
cfg.Relay = false
return nil
}
} | go | func DisableRelay() Option {
return func(cfg *Config) error {
cfg.RelayCustom = true
cfg.Relay = false
return nil
}
} | [
"func",
"DisableRelay",
"(",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"cfg",
".",
"RelayCustom",
"=",
"true",
"\n",
"cfg",
".",
"Relay",
"=",
"false",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // DisableRelay configures libp2p to disable the relay transport. | [
"DisableRelay",
"configures",
"libp2p",
"to",
"disable",
"the",
"relay",
"transport",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L220-L226 | train |
libp2p/go-libp2p | options.go | FilterAddresses | func FilterAddresses(addrs ...*net.IPNet) Option {
return func(cfg *Config) error {
if cfg.Filters == nil {
cfg.Filters = filter.NewFilters()
}
for _, addr := range addrs {
cfg.Filters.AddDialFilter(addr)
}
return nil
}
} | go | func FilterAddresses(addrs ...*net.IPNet) Option {
return func(cfg *Config) error {
if cfg.Filters == nil {
cfg.Filters = filter.NewFilters()
}
for _, addr := range addrs {
cfg.Filters.AddDialFilter(addr)
}
return nil
}
} | [
"func",
"FilterAddresses",
"(",
"addrs",
"...",
"*",
"net",
".",
"IPNet",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"if",
"cfg",
".",
"Filters",
"==",
"nil",
"{",
"cfg",
".",
"Filters",
"=",
"filter",
".",
"NewFilters",
"(",
")",
"\n",
"}",
"\n",
"for",
"_",
",",
"addr",
":=",
"range",
"addrs",
"{",
"cfg",
".",
"Filters",
".",
"AddDialFilter",
"(",
"addr",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // FilterAddresses configures libp2p to never dial nor accept connections from
// the given addresses. | [
"FilterAddresses",
"configures",
"libp2p",
"to",
"never",
"dial",
"nor",
"accept",
"connections",
"from",
"the",
"given",
"addresses",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L249-L259 | train |
libp2p/go-libp2p | options.go | Ping | func Ping(enable bool) Option {
return func(cfg *Config) error {
cfg.DisablePing = !enable
return nil
}
} | go | func Ping(enable bool) Option {
return func(cfg *Config) error {
cfg.DisablePing = !enable
return nil
}
} | [
"func",
"Ping",
"(",
"enable",
"bool",
")",
"Option",
"{",
"return",
"func",
"(",
"cfg",
"*",
"Config",
")",
"error",
"{",
"cfg",
".",
"DisablePing",
"=",
"!",
"enable",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // Ping will configure libp2p to support the ping service; enable by default. | [
"Ping",
"will",
"configure",
"libp2p",
"to",
"support",
"the",
"ping",
"service",
";",
"enable",
"by",
"default",
"."
] | bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344 | https://github.com/libp2p/go-libp2p/blob/bd0f8953cf5cb16e1f9d0a420ad20ebc08ffd344/options.go#L280-L285 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.