id
int32 0
167k
| repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
sequence | docstring
stringlengths 6
2.61k
| docstring_tokens
sequence | sha
stringlengths 40
40
| url
stringlengths 85
252
|
---|---|---|---|---|---|---|---|---|---|---|---|
3,200 | Jumpscale/go-raml | codegen/python/security.go | generate | func (ps *pythonSecurity) generate(dir string) error {
filename := filepath.Join(dir, ps.FileName)
return commons.GenerateFile(ps, ps.template, ps.templateName, filename, true)
} | go | func (ps *pythonSecurity) generate(dir string) error {
filename := filepath.Join(dir, ps.FileName)
return commons.GenerateFile(ps, ps.template, ps.templateName, filename, true)
} | [
"func",
"(",
"ps",
"*",
"pythonSecurity",
")",
"generate",
"(",
"dir",
"string",
")",
"error",
"{",
"filename",
":=",
"filepath",
".",
"Join",
"(",
"dir",
",",
"ps",
".",
"FileName",
")",
"\n",
"return",
"commons",
".",
"GenerateFile",
"(",
"ps",
",",
"ps",
".",
"template",
",",
"ps",
".",
"templateName",
",",
"filename",
",",
"true",
")",
"\n",
"}"
] | // generate security scheme representation in python.
// security scheme is generated as a middleware | [
"generate",
"security",
"scheme",
"representation",
"in",
"python",
".",
"security",
"scheme",
"is",
"generated",
"as",
"a",
"middleware"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/python/security.go#L88-L91 |
3,201 | Jumpscale/go-raml | codegen/nim/server.go | NewServer | func NewServer(apiDef *raml.APIDefinition, apiDocsDir, dir string) *Server {
return &Server{
Title: apiDef.Title,
APIDef: apiDef,
apiDocsDir: apiDocsDir,
Dir: dir,
}
} | go | func NewServer(apiDef *raml.APIDefinition, apiDocsDir, dir string) *Server {
return &Server{
Title: apiDef.Title,
APIDef: apiDef,
apiDocsDir: apiDocsDir,
Dir: dir,
}
} | [
"func",
"NewServer",
"(",
"apiDef",
"*",
"raml",
".",
"APIDefinition",
",",
"apiDocsDir",
",",
"dir",
"string",
")",
"*",
"Server",
"{",
"return",
"&",
"Server",
"{",
"Title",
":",
"apiDef",
".",
"Title",
",",
"APIDef",
":",
"apiDef",
",",
"apiDocsDir",
":",
"apiDocsDir",
",",
"Dir",
":",
"dir",
",",
"}",
"\n",
"}"
] | // NewServer creates a new Nim server | [
"NewServer",
"creates",
"a",
"new",
"Nim",
"server"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/server.go#L20-L27 |
3,202 | Jumpscale/go-raml | codegen/nim/server.go | generateMain | func (s *Server) generateMain() error {
filename := filepath.Join(s.Dir, "main.nim")
return commons.GenerateFile(s, "./templates/nim/server_main_nim.tmpl", "server_main_nim", filename, true)
} | go | func (s *Server) generateMain() error {
filename := filepath.Join(s.Dir, "main.nim")
return commons.GenerateFile(s, "./templates/nim/server_main_nim.tmpl", "server_main_nim", filename, true)
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"generateMain",
"(",
")",
"error",
"{",
"filename",
":=",
"filepath",
".",
"Join",
"(",
"s",
".",
"Dir",
",",
"\"",
"\"",
")",
"\n",
"return",
"commons",
".",
"GenerateFile",
"(",
"s",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"filename",
",",
"true",
")",
"\n",
"}"
] | // main generates main file | [
"main",
"generates",
"main",
"file"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/server.go#L66-L69 |
3,203 | Jumpscale/go-raml | codegen/nim/server.go | generateSecurity | func (s *Server) generateSecurity() error {
if !s.needJWT() {
return nil
}
// libjwt
if err := commons.GenerateFile(s, "./templates/nim/libjwt_nim.tmpl", "libjwt_nim", filepath.Join(s.Dir, "libjwt.nim"), true); err != nil {
return err
}
// itsyouonline integration
return commons.GenerateFile(s, "./templates/nim/oauth2_jwt_nim.tmpl", "oauth2_jwt_nim", filepath.Join(s.Dir, "oauth2_jwt.nim"), true)
} | go | func (s *Server) generateSecurity() error {
if !s.needJWT() {
return nil
}
// libjwt
if err := commons.GenerateFile(s, "./templates/nim/libjwt_nim.tmpl", "libjwt_nim", filepath.Join(s.Dir, "libjwt.nim"), true); err != nil {
return err
}
// itsyouonline integration
return commons.GenerateFile(s, "./templates/nim/oauth2_jwt_nim.tmpl", "oauth2_jwt_nim", filepath.Join(s.Dir, "oauth2_jwt.nim"), true)
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"generateSecurity",
"(",
")",
"error",
"{",
"if",
"!",
"s",
".",
"needJWT",
"(",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"// libjwt",
"if",
"err",
":=",
"commons",
".",
"GenerateFile",
"(",
"s",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"filepath",
".",
"Join",
"(",
"s",
".",
"Dir",
",",
"\"",
"\"",
")",
",",
"true",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"// itsyouonline integration",
"return",
"commons",
".",
"GenerateFile",
"(",
"s",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"filepath",
".",
"Join",
"(",
"s",
".",
"Dir",
",",
"\"",
"\"",
")",
",",
"true",
")",
"\n",
"}"
] | // generates all needed security files
// we currently only support itsyou.online oauth2 jwt token | [
"generates",
"all",
"needed",
"security",
"files",
"we",
"currently",
"only",
"support",
"itsyou",
".",
"online",
"oauth2",
"jwt",
"token"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/server.go#L73-L84 |
3,204 | Jumpscale/go-raml | codegen/nim/server.go | Imports | func (s *Server) Imports() []string {
imports := map[string]struct{}{}
for _, r := range s.Resources {
imports[r.apiName()] = struct{}{}
}
return commons.MapToSortedStrings(imports)
} | go | func (s *Server) Imports() []string {
imports := map[string]struct{}{}
for _, r := range s.Resources {
imports[r.apiName()] = struct{}{}
}
return commons.MapToSortedStrings(imports)
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"Imports",
"(",
")",
"[",
"]",
"string",
"{",
"imports",
":=",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"for",
"_",
",",
"r",
":=",
"range",
"s",
".",
"Resources",
"{",
"imports",
"[",
"r",
".",
"apiName",
"(",
")",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"return",
"commons",
".",
"MapToSortedStrings",
"(",
"imports",
")",
"\n",
"}"
] | // Imports returns array of modules that need to be imported by server's main file | [
"Imports",
"returns",
"array",
"of",
"modules",
"that",
"need",
"to",
"be",
"imported",
"by",
"server",
"s",
"main",
"file"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/server.go#L87-L94 |
3,205 | Jumpscale/go-raml | codegen/nim/server.go | needJWT | func (s *Server) needJWT() bool {
for _, r := range s.Resources {
if r.NeedJWT() {
return true
}
}
return false
} | go | func (s *Server) needJWT() bool {
for _, r := range s.Resources {
if r.NeedJWT() {
return true
}
}
return false
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"needJWT",
"(",
")",
"bool",
"{",
"for",
"_",
",",
"r",
":=",
"range",
"s",
".",
"Resources",
"{",
"if",
"r",
".",
"NeedJWT",
"(",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // check if this server need to have jwt lib | [
"check",
"if",
"this",
"server",
"need",
"to",
"have",
"jwt",
"lib"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/server.go#L97-L104 |
3,206 | Jumpscale/go-raml | codegen/nim/resource.go | Imports | func (r *resource) Imports() []string {
ip := map[string]struct{}{}
for _, m := range r.Methods {
var names []string
if m.ReqBody != "" {
names = append(names, m.ReqBody)
}
if m.RespBody != "" {
names = append(names, m.RespBody)
}
for _, name := range names {
if tipe, ok := objectRegistered(name); ok {
ip[tipe] = struct{}{}
}
}
}
return commons.MapToSortedStrings(ip)
} | go | func (r *resource) Imports() []string {
ip := map[string]struct{}{}
for _, m := range r.Methods {
var names []string
if m.ReqBody != "" {
names = append(names, m.ReqBody)
}
if m.RespBody != "" {
names = append(names, m.RespBody)
}
for _, name := range names {
if tipe, ok := objectRegistered(name); ok {
ip[tipe] = struct{}{}
}
}
}
return commons.MapToSortedStrings(ip)
} | [
"func",
"(",
"r",
"*",
"resource",
")",
"Imports",
"(",
")",
"[",
"]",
"string",
"{",
"ip",
":=",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"for",
"_",
",",
"m",
":=",
"range",
"r",
".",
"Methods",
"{",
"var",
"names",
"[",
"]",
"string",
"\n",
"if",
"m",
".",
"ReqBody",
"!=",
"\"",
"\"",
"{",
"names",
"=",
"append",
"(",
"names",
",",
"m",
".",
"ReqBody",
")",
"\n",
"}",
"\n",
"if",
"m",
".",
"RespBody",
"!=",
"\"",
"\"",
"{",
"names",
"=",
"append",
"(",
"names",
",",
"m",
".",
"RespBody",
")",
"\n",
"}",
"\n",
"for",
"_",
",",
"name",
":=",
"range",
"names",
"{",
"if",
"tipe",
",",
"ok",
":=",
"objectRegistered",
"(",
"name",
")",
";",
"ok",
"{",
"ip",
"[",
"tipe",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"commons",
".",
"MapToSortedStrings",
"(",
"ip",
")",
"\n",
"}"
] | // get array of all imported modules | [
"get",
"array",
"of",
"all",
"imported",
"modules"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/resource.go#L40-L58 |
3,207 | Jumpscale/go-raml | codegen/nim/resource.go | generate | func (r *resource) generate(dir string) error {
filename := filepath.Join(dir, r.apiName()+".nim")
return commons.GenerateFile(r, "./templates/nim/server_resources_api_nim.tmpl", "server_resources_api_nim", filename, true)
} | go | func (r *resource) generate(dir string) error {
filename := filepath.Join(dir, r.apiName()+".nim")
return commons.GenerateFile(r, "./templates/nim/server_resources_api_nim.tmpl", "server_resources_api_nim", filename, true)
} | [
"func",
"(",
"r",
"*",
"resource",
")",
"generate",
"(",
"dir",
"string",
")",
"error",
"{",
"filename",
":=",
"filepath",
".",
"Join",
"(",
"dir",
",",
"r",
".",
"apiName",
"(",
")",
"+",
"\"",
"\"",
")",
"\n",
"return",
"commons",
".",
"GenerateFile",
"(",
"r",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"filename",
",",
"true",
")",
"\n",
"}"
] | // generate server resource API implementation | [
"generate",
"server",
"resource",
"API",
"implementation"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/resource.go#L61-L64 |
3,208 | Jumpscale/go-raml | codegen/nim/resource.go | NeedJWT | func (r *resource) NeedJWT() bool {
for _, m := range r.Methods {
if m.Secured() {
return true
}
}
return false
} | go | func (r *resource) NeedJWT() bool {
for _, m := range r.Methods {
if m.Secured() {
return true
}
}
return false
} | [
"func",
"(",
"r",
"*",
"resource",
")",
"NeedJWT",
"(",
")",
"bool",
"{",
"for",
"_",
",",
"m",
":=",
"range",
"r",
".",
"Methods",
"{",
"if",
"m",
".",
"Secured",
"(",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // NeedJWT returns true if this resource need JWT Library | [
"NeedJWT",
"returns",
"true",
"if",
"this",
"resource",
"need",
"JWT",
"Library"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/resource.go#L72-L79 |
3,209 | Jumpscale/go-raml | codegen/resource/resources.go | New | func New(apiDef *raml.APIDefinition, r *raml.Resource, endpoint string, sortMethod bool) Resource {
name := func() string {
if r.DisplayName == "" {
return commons.NormalizeIdentifier(strings.Title(commons.NormalizeURI(endpoint)))
}
return strings.Title(commons.DisplayNameToFuncName(r.DisplayName))
}()
res := Resource{
Endpoint: endpoint,
APIDef: apiDef,
Name: name,
Resource: r,
}
res.generateMethods(r)
if sortMethod {
sort.Sort(byEndpoint(res.Methods))
}
return res
} | go | func New(apiDef *raml.APIDefinition, r *raml.Resource, endpoint string, sortMethod bool) Resource {
name := func() string {
if r.DisplayName == "" {
return commons.NormalizeIdentifier(strings.Title(commons.NormalizeURI(endpoint)))
}
return strings.Title(commons.DisplayNameToFuncName(r.DisplayName))
}()
res := Resource{
Endpoint: endpoint,
APIDef: apiDef,
Name: name,
Resource: r,
}
res.generateMethods(r)
if sortMethod {
sort.Sort(byEndpoint(res.Methods))
}
return res
} | [
"func",
"New",
"(",
"apiDef",
"*",
"raml",
".",
"APIDefinition",
",",
"r",
"*",
"raml",
".",
"Resource",
",",
"endpoint",
"string",
",",
"sortMethod",
"bool",
")",
"Resource",
"{",
"name",
":=",
"func",
"(",
")",
"string",
"{",
"if",
"r",
".",
"DisplayName",
"==",
"\"",
"\"",
"{",
"return",
"commons",
".",
"NormalizeIdentifier",
"(",
"strings",
".",
"Title",
"(",
"commons",
".",
"NormalizeURI",
"(",
"endpoint",
")",
")",
")",
"\n",
"}",
"\n",
"return",
"strings",
".",
"Title",
"(",
"commons",
".",
"DisplayNameToFuncName",
"(",
"r",
".",
"DisplayName",
")",
")",
"\n",
"}",
"(",
")",
"\n\n",
"res",
":=",
"Resource",
"{",
"Endpoint",
":",
"endpoint",
",",
"APIDef",
":",
"apiDef",
",",
"Name",
":",
"name",
",",
"Resource",
":",
"r",
",",
"}",
"\n\n",
"res",
".",
"generateMethods",
"(",
"r",
")",
"\n",
"if",
"sortMethod",
"{",
"sort",
".",
"Sort",
"(",
"byEndpoint",
"(",
"res",
".",
"Methods",
")",
")",
"\n",
"}",
"\n",
"return",
"res",
"\n",
"}"
] | // New creates a resource definition | [
"New",
"creates",
"a",
"resource",
"definition"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/resource/resources.go#L27-L47 |
3,210 | Jumpscale/go-raml | codegen/resource/resources.go | addMethod | func (rd *Resource) addMethod(r *raml.Resource, m *raml.Method, methodName string) {
if m == nil {
return
}
rd.Methods = append(rd.Methods, newMethod(r, rd, m, methodName))
} | go | func (rd *Resource) addMethod(r *raml.Resource, m *raml.Method, methodName string) {
if m == nil {
return
}
rd.Methods = append(rd.Methods, newMethod(r, rd, m, methodName))
} | [
"func",
"(",
"rd",
"*",
"Resource",
")",
"addMethod",
"(",
"r",
"*",
"raml",
".",
"Resource",
",",
"m",
"*",
"raml",
".",
"Method",
",",
"methodName",
"string",
")",
"{",
"if",
"m",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"rd",
".",
"Methods",
"=",
"append",
"(",
"rd",
".",
"Methods",
",",
"newMethod",
"(",
"r",
",",
"rd",
",",
"m",
",",
"methodName",
")",
")",
"\n",
"}"
] | // add a method to resource definition | [
"add",
"a",
"method",
"to",
"resource",
"definition"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/resource/resources.go#L50-L55 |
3,211 | Jumpscale/go-raml | codegen/resource/resources.go | generateMethods | func (rd *Resource) generateMethods(r *raml.Resource) {
rd.addMethod(r, r.Get, "Get")
rd.addMethod(r, r.Post, "Post")
rd.addMethod(r, r.Put, "Put")
rd.addMethod(r, r.Patch, "Patch")
rd.addMethod(r, r.Delete, "Delete")
rd.addMethod(r, r.Options, "Options")
for _, v := range r.Nested {
rd.generateMethods(v)
}
} | go | func (rd *Resource) generateMethods(r *raml.Resource) {
rd.addMethod(r, r.Get, "Get")
rd.addMethod(r, r.Post, "Post")
rd.addMethod(r, r.Put, "Put")
rd.addMethod(r, r.Patch, "Patch")
rd.addMethod(r, r.Delete, "Delete")
rd.addMethod(r, r.Options, "Options")
for _, v := range r.Nested {
rd.generateMethods(v)
}
} | [
"func",
"(",
"rd",
"*",
"Resource",
")",
"generateMethods",
"(",
"r",
"*",
"raml",
".",
"Resource",
")",
"{",
"rd",
".",
"addMethod",
"(",
"r",
",",
"r",
".",
"Get",
",",
"\"",
"\"",
")",
"\n",
"rd",
".",
"addMethod",
"(",
"r",
",",
"r",
".",
"Post",
",",
"\"",
"\"",
")",
"\n",
"rd",
".",
"addMethod",
"(",
"r",
",",
"r",
".",
"Put",
",",
"\"",
"\"",
")",
"\n",
"rd",
".",
"addMethod",
"(",
"r",
",",
"r",
".",
"Patch",
",",
"\"",
"\"",
")",
"\n",
"rd",
".",
"addMethod",
"(",
"r",
",",
"r",
".",
"Delete",
",",
"\"",
"\"",
")",
"\n",
"rd",
".",
"addMethod",
"(",
"r",
",",
"r",
".",
"Options",
",",
"\"",
"\"",
")",
"\n\n",
"for",
"_",
",",
"v",
":=",
"range",
"r",
".",
"Nested",
"{",
"rd",
".",
"generateMethods",
"(",
"v",
")",
"\n",
"}",
"\n",
"}"
] | // GenerateMethods generates all methods of a resource recursively | [
"GenerateMethods",
"generates",
"all",
"methods",
"of",
"a",
"resource",
"recursively"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/resource/resources.go#L58-L69 |
3,212 | Jumpscale/go-raml | codegen/resource/resources.go | _getResourceParams | func _getResourceParams(r *raml.Resource, params []string) []string {
if r == nil {
return params
}
matches := reResourceParam.FindAllString(r.URI, -1)
for _, match := range matches {
matchEscaped := commons.NormalizeIdentifier(match[1 : len(match)-1])
params = append(params, matchEscaped)
}
return _getResourceParams(r.Parent, params)
} | go | func _getResourceParams(r *raml.Resource, params []string) []string {
if r == nil {
return params
}
matches := reResourceParam.FindAllString(r.URI, -1)
for _, match := range matches {
matchEscaped := commons.NormalizeIdentifier(match[1 : len(match)-1])
params = append(params, matchEscaped)
}
return _getResourceParams(r.Parent, params)
} | [
"func",
"_getResourceParams",
"(",
"r",
"*",
"raml",
".",
"Resource",
",",
"params",
"[",
"]",
"string",
")",
"[",
"]",
"string",
"{",
"if",
"r",
"==",
"nil",
"{",
"return",
"params",
"\n",
"}",
"\n\n",
"matches",
":=",
"reResourceParam",
".",
"FindAllString",
"(",
"r",
".",
"URI",
",",
"-",
"1",
")",
"\n",
"for",
"_",
",",
"match",
":=",
"range",
"matches",
"{",
"matchEscaped",
":=",
"commons",
".",
"NormalizeIdentifier",
"(",
"match",
"[",
"1",
":",
"len",
"(",
"match",
")",
"-",
"1",
"]",
")",
"\n",
"params",
"=",
"append",
"(",
"params",
",",
"matchEscaped",
")",
"\n",
"}",
"\n\n",
"return",
"_getResourceParams",
"(",
"r",
".",
"Parent",
",",
"params",
")",
"\n",
"}"
] | // _getResourceParams is the recursive function of getResourceParams | [
"_getResourceParams",
"is",
"the",
"recursive",
"function",
"of",
"getResourceParams"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/resource/resources.go#L72-L84 |
3,213 | Jumpscale/go-raml | codegen/python/service.go | Imports | func (s service) Imports() []string {
if !s.UnmarshallResponse {
return nil
}
ipMap := map[string]struct{}{
"from .unmarshall_error import UnmarshallError": struct{}{},
"from .unhandled_api_error import UnhandledAPIError": struct{}{},
}
for _, m := range s.Methods {
for _, importLine := range m.imports() {
ipMap[importLine] = struct{}{}
}
}
return commons.MapToSortedStrings(ipMap)
} | go | func (s service) Imports() []string {
if !s.UnmarshallResponse {
return nil
}
ipMap := map[string]struct{}{
"from .unmarshall_error import UnmarshallError": struct{}{},
"from .unhandled_api_error import UnhandledAPIError": struct{}{},
}
for _, m := range s.Methods {
for _, importLine := range m.imports() {
ipMap[importLine] = struct{}{}
}
}
return commons.MapToSortedStrings(ipMap)
} | [
"func",
"(",
"s",
"service",
")",
"Imports",
"(",
")",
"[",
"]",
"string",
"{",
"if",
"!",
"s",
".",
"UnmarshallResponse",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"ipMap",
":=",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
"{",
"\"",
"\"",
":",
"struct",
"{",
"}",
"{",
"}",
",",
"\"",
"\"",
":",
"struct",
"{",
"}",
"{",
"}",
",",
"}",
"\n",
"for",
"_",
",",
"m",
":=",
"range",
"s",
".",
"Methods",
"{",
"for",
"_",
",",
"importLine",
":=",
"range",
"m",
".",
"imports",
"(",
")",
"{",
"ipMap",
"[",
"importLine",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"commons",
".",
"MapToSortedStrings",
"(",
"ipMap",
")",
"\n",
"}"
] | // Imports returns slice of packages
// need to be imported by this service | [
"Imports",
"returns",
"slice",
"of",
"packages",
"need",
"to",
"be",
"imported",
"by",
"this",
"service"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/python/service.go#L33-L48 |
3,214 | Jumpscale/go-raml | codegen/python/service.go | filename | func (s service) filename(dir string) string {
return filepath.Join(dir, s.FilenameNoExt) + ".py"
} | go | func (s service) filename(dir string) string {
return filepath.Join(dir, s.FilenameNoExt) + ".py"
} | [
"func",
"(",
"s",
"service",
")",
"filename",
"(",
"dir",
"string",
")",
"string",
"{",
"return",
"filepath",
".",
"Join",
"(",
"dir",
",",
"s",
".",
"FilenameNoExt",
")",
"+",
"\"",
"\"",
"\n",
"}"
] | // filename returns generated filename of this service | [
"filename",
"returns",
"generated",
"filename",
"of",
"this",
"service"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/python/service.go#L51-L53 |
3,215 | Jumpscale/go-raml | raml/types.go | TypeString | func (p Property) TypeString() string {
switch p.Type.(type) {
case string:
return p.Type.(string)
case Type:
if p._type == nil {
panic(fmt.Errorf("property '%v' has no parent type", p.Name))
}
return p._type.Name + p.Name
default:
return "string"
}
} | go | func (p Property) TypeString() string {
switch p.Type.(type) {
case string:
return p.Type.(string)
case Type:
if p._type == nil {
panic(fmt.Errorf("property '%v' has no parent type", p.Name))
}
return p._type.Name + p.Name
default:
return "string"
}
} | [
"func",
"(",
"p",
"Property",
")",
"TypeString",
"(",
")",
"string",
"{",
"switch",
"p",
".",
"Type",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"return",
"p",
".",
"Type",
".",
"(",
"string",
")",
"\n",
"case",
"Type",
":",
"if",
"p",
".",
"_type",
"==",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"p",
".",
"Name",
")",
")",
"\n",
"}",
"\n",
"return",
"p",
".",
"_type",
".",
"Name",
"+",
"p",
".",
"Name",
"\n",
"default",
":",
"return",
"\"",
"\"",
"\n",
"}",
"\n",
"}"
] | // TypeString returns string representation
// of the property's type | [
"TypeString",
"returns",
"string",
"representation",
"of",
"the",
"property",
"s",
"type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L277-L289 |
3,216 | Jumpscale/go-raml | raml/types.go | IsArray | func (p Property) IsArray() bool {
return p.Type == arrayType || strings.HasSuffix(p.TypeString(), "[]")
} | go | func (p Property) IsArray() bool {
return p.Type == arrayType || strings.HasSuffix(p.TypeString(), "[]")
} | [
"func",
"(",
"p",
"Property",
")",
"IsArray",
"(",
")",
"bool",
"{",
"return",
"p",
".",
"Type",
"==",
"arrayType",
"||",
"strings",
".",
"HasSuffix",
"(",
"p",
".",
"TypeString",
"(",
")",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // IsArray returns true if it is an array | [
"IsArray",
"returns",
"true",
"if",
"it",
"is",
"an",
"array"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L303-L305 |
3,217 | Jumpscale/go-raml | raml/types.go | ArrayType | func (p Property) ArrayType() string {
if p.Type == arrayType {
return p.Items.Type
}
return strings.TrimSuffix(p.TypeString(), "[]")
} | go | func (p Property) ArrayType() string {
if p.Type == arrayType {
return p.Items.Type
}
return strings.TrimSuffix(p.TypeString(), "[]")
} | [
"func",
"(",
"p",
"Property",
")",
"ArrayType",
"(",
")",
"string",
"{",
"if",
"p",
".",
"Type",
"==",
"arrayType",
"{",
"return",
"p",
".",
"Items",
".",
"Type",
"\n",
"}",
"\n",
"return",
"strings",
".",
"TrimSuffix",
"(",
"p",
".",
"TypeString",
"(",
")",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // ArrayType returns the type of the array | [
"ArrayType",
"returns",
"the",
"type",
"of",
"the",
"array"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L318-L323 |
3,218 | Jumpscale/go-raml | raml/types.go | GetProperty | func (t *Type) GetProperty(name string) Property {
propInterface, ok := t.Properties[name]
if !ok {
panic(fmt.Errorf("property %v not exist", name))
}
prop := toProperty(name, propInterface)
prop._type = t
return prop
} | go | func (t *Type) GetProperty(name string) Property {
propInterface, ok := t.Properties[name]
if !ok {
panic(fmt.Errorf("property %v not exist", name))
}
prop := toProperty(name, propInterface)
prop._type = t
return prop
} | [
"func",
"(",
"t",
"*",
"Type",
")",
"GetProperty",
"(",
"name",
"string",
")",
"Property",
"{",
"propInterface",
",",
"ok",
":=",
"t",
".",
"Properties",
"[",
"name",
"]",
"\n",
"if",
"!",
"ok",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"name",
")",
")",
"\n",
"}",
"\n\n",
"prop",
":=",
"toProperty",
"(",
"name",
",",
"propInterface",
")",
"\n",
"prop",
".",
"_type",
"=",
"t",
"\n\n",
"return",
"prop",
"\n",
"}"
] | // GetProperty returns property with given name | [
"GetProperty",
"returns",
"property",
"with",
"given",
"name"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L457-L467 |
3,219 | Jumpscale/go-raml | raml/types.go | IsBuiltin | func (t Type) IsBuiltin() bool {
_, ok := scalarTypes[t.TypeString()]
return ok
} | go | func (t Type) IsBuiltin() bool {
_, ok := scalarTypes[t.TypeString()]
return ok
} | [
"func",
"(",
"t",
"Type",
")",
"IsBuiltin",
"(",
")",
"bool",
"{",
"_",
",",
"ok",
":=",
"scalarTypes",
"[",
"t",
".",
"TypeString",
"(",
")",
"]",
"\n",
"return",
"ok",
"\n",
"}"
] | // IsBuiltin if a type is an RAML builtin type. | [
"IsBuiltin",
"if",
"a",
"type",
"is",
"an",
"RAML",
"builtin",
"type",
"."
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L470-L473 |
3,220 | Jumpscale/go-raml | raml/types.go | Parents | func (t Type) Parents() []string {
if parents, ok := t.MultipleInheritance(); ok {
return parents
}
if parent, ok := t.SingleInheritance(); ok {
return []string{parent}
}
return []string{}
} | go | func (t Type) Parents() []string {
if parents, ok := t.MultipleInheritance(); ok {
return parents
}
if parent, ok := t.SingleInheritance(); ok {
return []string{parent}
}
return []string{}
} | [
"func",
"(",
"t",
"Type",
")",
"Parents",
"(",
")",
"[",
"]",
"string",
"{",
"if",
"parents",
",",
"ok",
":=",
"t",
".",
"MultipleInheritance",
"(",
")",
";",
"ok",
"{",
"return",
"parents",
"\n",
"}",
"\n\n",
"if",
"parent",
",",
"ok",
":=",
"t",
".",
"SingleInheritance",
"(",
")",
";",
"ok",
"{",
"return",
"[",
"]",
"string",
"{",
"parent",
"}",
"\n",
"}",
"\n",
"return",
"[",
"]",
"string",
"{",
"}",
"\n",
"}"
] | // Parents returns parents of this Type.
// "object" is not considered a parent | [
"Parents",
"returns",
"parents",
"of",
"this",
"Type",
".",
"object",
"is",
"not",
"considered",
"a",
"parent"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L477-L486 |
3,221 | Jumpscale/go-raml | raml/types.go | IsJSONType | func (t Type) IsJSONType() bool {
tStr := t.TypeString()
tStr = strings.TrimSpace(tStr)
return strings.HasPrefix(tStr, "{") && strings.HasSuffix(tStr, "}")
} | go | func (t Type) IsJSONType() bool {
tStr := t.TypeString()
tStr = strings.TrimSpace(tStr)
return strings.HasPrefix(tStr, "{") && strings.HasSuffix(tStr, "}")
} | [
"func",
"(",
"t",
"Type",
")",
"IsJSONType",
"(",
")",
"bool",
"{",
"tStr",
":=",
"t",
".",
"TypeString",
"(",
")",
"\n",
"tStr",
"=",
"strings",
".",
"TrimSpace",
"(",
"tStr",
")",
"\n",
"return",
"strings",
".",
"HasPrefix",
"(",
"tStr",
",",
"\"",
"\"",
")",
"&&",
"strings",
".",
"HasSuffix",
"(",
"tStr",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // IsJSONType true if this Type
// has JSON scheme that defines it's type | [
"IsJSONType",
"true",
"if",
"this",
"Type",
"has",
"JSON",
"scheme",
"that",
"defines",
"it",
"s",
"type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L490-L494 |
3,222 | Jumpscale/go-raml | raml/types.go | MultipleInheritance | func (t Type) MultipleInheritance() ([]string, bool) {
if t.IsJSONType() {
return nil, false
}
tStr := t.TypeString()
if !strings.HasPrefix(tStr, "[") || !strings.HasSuffix(tStr, "]") {
return nil, false
}
tStr = strings.TrimPrefix(strings.TrimSuffix(tStr, "]"), "[")
splitted := strings.Split(tStr, ",")
return splitted, len(splitted) > 1
} | go | func (t Type) MultipleInheritance() ([]string, bool) {
if t.IsJSONType() {
return nil, false
}
tStr := t.TypeString()
if !strings.HasPrefix(tStr, "[") || !strings.HasSuffix(tStr, "]") {
return nil, false
}
tStr = strings.TrimPrefix(strings.TrimSuffix(tStr, "]"), "[")
splitted := strings.Split(tStr, ",")
return splitted, len(splitted) > 1
} | [
"func",
"(",
"t",
"Type",
")",
"MultipleInheritance",
"(",
")",
"(",
"[",
"]",
"string",
",",
"bool",
")",
"{",
"if",
"t",
".",
"IsJSONType",
"(",
")",
"{",
"return",
"nil",
",",
"false",
"\n",
"}",
"\n",
"tStr",
":=",
"t",
".",
"TypeString",
"(",
")",
"\n",
"if",
"!",
"strings",
".",
"HasPrefix",
"(",
"tStr",
",",
"\"",
"\"",
")",
"||",
"!",
"strings",
".",
"HasSuffix",
"(",
"tStr",
",",
"\"",
"\"",
")",
"{",
"return",
"nil",
",",
"false",
"\n",
"}",
"\n",
"tStr",
"=",
"strings",
".",
"TrimPrefix",
"(",
"strings",
".",
"TrimSuffix",
"(",
"tStr",
",",
"\"",
"\"",
")",
",",
"\"",
"\"",
")",
"\n",
"splitted",
":=",
"strings",
".",
"Split",
"(",
"tStr",
",",
"\"",
"\"",
")",
"\n",
"return",
"splitted",
",",
"len",
"(",
"splitted",
")",
">",
"1",
"\n",
"}"
] | // MultipleInheritance returns all types inherited by this type | [
"MultipleInheritance",
"returns",
"all",
"types",
"inherited",
"by",
"this",
"type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L517-L528 |
3,223 | Jumpscale/go-raml | raml/types.go | ArrayType | func (t Type) ArrayType() string {
if t.TypeString() == "array" {
return interfaceToString(t.Items)
}
return strings.TrimSuffix(t.TypeString(), "[]")
} | go | func (t Type) ArrayType() string {
if t.TypeString() == "array" {
return interfaceToString(t.Items)
}
return strings.TrimSuffix(t.TypeString(), "[]")
} | [
"func",
"(",
"t",
"Type",
")",
"ArrayType",
"(",
")",
"string",
"{",
"if",
"t",
".",
"TypeString",
"(",
")",
"==",
"\"",
"\"",
"{",
"return",
"interfaceToString",
"(",
"t",
".",
"Items",
")",
"\n",
"}",
"\n",
"return",
"strings",
".",
"TrimSuffix",
"(",
"t",
".",
"TypeString",
"(",
")",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // ArrayType returns type of the array | [
"ArrayType",
"returns",
"type",
"of",
"the",
"array"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L579-L584 |
3,224 | Jumpscale/go-raml | raml/types.go | IsBidimensiArray | func (t Type) IsBidimensiArray() bool {
if t.IsJSONType() {
return false
}
return strings.HasSuffix(t.TypeString(), "[][]")
} | go | func (t Type) IsBidimensiArray() bool {
if t.IsJSONType() {
return false
}
return strings.HasSuffix(t.TypeString(), "[][]")
} | [
"func",
"(",
"t",
"Type",
")",
"IsBidimensiArray",
"(",
")",
"bool",
"{",
"if",
"t",
".",
"IsJSONType",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"strings",
".",
"HasSuffix",
"(",
"t",
".",
"TypeString",
"(",
")",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // IsBidimensiArray returns true
// if it is a bidimensional array | [
"IsBidimensiArray",
"returns",
"true",
"if",
"it",
"is",
"a",
"bidimensional",
"array"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L588-L593 |
3,225 | Jumpscale/go-raml | raml/types.go | Union | func (t Type) Union() ([]string, bool) {
if !t.IsUnion() {
return nil, false
}
tips := []string{}
for _, ut := range strings.Split(t.TypeString(), "|") {
tips = append(tips, strings.TrimSpace(ut))
}
return tips, true
} | go | func (t Type) Union() ([]string, bool) {
if !t.IsUnion() {
return nil, false
}
tips := []string{}
for _, ut := range strings.Split(t.TypeString(), "|") {
tips = append(tips, strings.TrimSpace(ut))
}
return tips, true
} | [
"func",
"(",
"t",
"Type",
")",
"Union",
"(",
")",
"(",
"[",
"]",
"string",
",",
"bool",
")",
"{",
"if",
"!",
"t",
".",
"IsUnion",
"(",
")",
"{",
"return",
"nil",
",",
"false",
"\n",
"}",
"\n",
"tips",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"_",
",",
"ut",
":=",
"range",
"strings",
".",
"Split",
"(",
"t",
".",
"TypeString",
"(",
")",
",",
"\"",
"\"",
")",
"{",
"tips",
"=",
"append",
"(",
"tips",
",",
"strings",
".",
"TrimSpace",
"(",
"ut",
")",
")",
"\n",
"}",
"\n",
"return",
"tips",
",",
"true",
"\n",
"}"
] | // Union returns union type of this type | [
"Union",
"returns",
"union",
"type",
"of",
"this",
"type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L617-L626 |
3,226 | Jumpscale/go-raml | raml/types.go | IsAlias | func (t Type) IsAlias() bool {
if t.IsMultipleInheritance() || t.IsArray() || t.IsUnion() || t.TypeString() == "object" {
return false
}
return t.TypeString() != "" && len(t.Properties) == 0
} | go | func (t Type) IsAlias() bool {
if t.IsMultipleInheritance() || t.IsArray() || t.IsUnion() || t.TypeString() == "object" {
return false
}
return t.TypeString() != "" && len(t.Properties) == 0
} | [
"func",
"(",
"t",
"Type",
")",
"IsAlias",
"(",
")",
"bool",
"{",
"if",
"t",
".",
"IsMultipleInheritance",
"(",
")",
"||",
"t",
".",
"IsArray",
"(",
")",
"||",
"t",
".",
"IsUnion",
"(",
")",
"||",
"t",
".",
"TypeString",
"(",
")",
"==",
"\"",
"\"",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"t",
".",
"TypeString",
"(",
")",
"!=",
"\"",
"\"",
"&&",
"len",
"(",
"t",
".",
"Properties",
")",
"==",
"0",
"\n",
"}"
] | // IsAlias returns true if this Type is
// alias of another Type | [
"IsAlias",
"returns",
"true",
"if",
"this",
"Type",
"is",
"alias",
"of",
"another",
"Type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L630-L635 |
3,227 | Jumpscale/go-raml | raml/types.go | postProcess | func (t *Type) postProcess(name string, apiDef *APIDefinition) error {
t.Name = name
t._apiDef = apiDef
if t.IsJSONType() {
return t.postProcessJSONSchema()
}
// process type in properties
for name := range t.Properties {
t.parseOptionalProperty(name)
t.createTypeFromPropProperty(name, apiDef)
t.createTypeFromPropItems(name, apiDef)
}
return nil
} | go | func (t *Type) postProcess(name string, apiDef *APIDefinition) error {
t.Name = name
t._apiDef = apiDef
if t.IsJSONType() {
return t.postProcessJSONSchema()
}
// process type in properties
for name := range t.Properties {
t.parseOptionalProperty(name)
t.createTypeFromPropProperty(name, apiDef)
t.createTypeFromPropItems(name, apiDef)
}
return nil
} | [
"func",
"(",
"t",
"*",
"Type",
")",
"postProcess",
"(",
"name",
"string",
",",
"apiDef",
"*",
"APIDefinition",
")",
"error",
"{",
"t",
".",
"Name",
"=",
"name",
"\n",
"t",
".",
"_apiDef",
"=",
"apiDef",
"\n\n",
"if",
"t",
".",
"IsJSONType",
"(",
")",
"{",
"return",
"t",
".",
"postProcessJSONSchema",
"(",
")",
"\n",
"}",
"\n\n",
"// process type in properties",
"for",
"name",
":=",
"range",
"t",
".",
"Properties",
"{",
"t",
".",
"parseOptionalProperty",
"(",
"name",
")",
"\n",
"t",
".",
"createTypeFromPropProperty",
"(",
"name",
",",
"apiDef",
")",
"\n",
"t",
".",
"createTypeFromPropItems",
"(",
"name",
",",
"apiDef",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // see if the 'Type' field is a JSON schema | [
"see",
"if",
"the",
"Type",
"field",
"is",
"a",
"JSON",
"schema"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L638-L653 |
3,228 | Jumpscale/go-raml | raml/types.go | parseOptionalProperty | func (t *Type) parseOptionalProperty(name string) {
if !strings.HasSuffix(name, "?") {
return
}
newName := strings.TrimSuffix(name, "?")
p := t.Properties[name]
// we will modify both property name
// and content, so we delete it
delete(t.Properties, name)
switch p.(type) {
case string:
// if it is a simple string
// convert it to map style property
newProp := map[interface{}]interface{}{}
newProp["type"] = p
newProp["required"] = false
t.Properties[newName] = newProp
case map[interface{}]interface{}:
// already in map style property
propMap := p.(map[interface{}]interface{})
propMap["required"] = false
t.Properties[newName] = propMap
default:
log.Fatalf("unexpeced property type: %v", p)
}
} | go | func (t *Type) parseOptionalProperty(name string) {
if !strings.HasSuffix(name, "?") {
return
}
newName := strings.TrimSuffix(name, "?")
p := t.Properties[name]
// we will modify both property name
// and content, so we delete it
delete(t.Properties, name)
switch p.(type) {
case string:
// if it is a simple string
// convert it to map style property
newProp := map[interface{}]interface{}{}
newProp["type"] = p
newProp["required"] = false
t.Properties[newName] = newProp
case map[interface{}]interface{}:
// already in map style property
propMap := p.(map[interface{}]interface{})
propMap["required"] = false
t.Properties[newName] = propMap
default:
log.Fatalf("unexpeced property type: %v", p)
}
} | [
"func",
"(",
"t",
"*",
"Type",
")",
"parseOptionalProperty",
"(",
"name",
"string",
")",
"{",
"if",
"!",
"strings",
".",
"HasSuffix",
"(",
"name",
",",
"\"",
"\"",
")",
"{",
"return",
"\n",
"}",
"\n",
"newName",
":=",
"strings",
".",
"TrimSuffix",
"(",
"name",
",",
"\"",
"\"",
")",
"\n\n",
"p",
":=",
"t",
".",
"Properties",
"[",
"name",
"]",
"\n\n",
"// we will modify both property name",
"// and content, so we delete it",
"delete",
"(",
"t",
".",
"Properties",
",",
"name",
")",
"\n\n",
"switch",
"p",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"// if it is a simple string",
"// convert it to map style property",
"newProp",
":=",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
"{",
"}",
"\n",
"newProp",
"[",
"\"",
"\"",
"]",
"=",
"p",
"\n",
"newProp",
"[",
"\"",
"\"",
"]",
"=",
"false",
"\n",
"t",
".",
"Properties",
"[",
"newName",
"]",
"=",
"newProp",
"\n\n",
"case",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
":",
"// already in map style property",
"propMap",
":=",
"p",
".",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
"\n",
"propMap",
"[",
"\"",
"\"",
"]",
"=",
"false",
"\n",
"t",
".",
"Properties",
"[",
"newName",
"]",
"=",
"propMap",
"\n",
"default",
":",
"log",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"p",
")",
"\n",
"}",
"\n",
"}"
] | // parse property with `?` suffix as optional property | [
"parse",
"property",
"with",
"?",
"suffix",
"as",
"optional",
"property"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L656-L685 |
3,229 | Jumpscale/go-raml | raml/types.go | createTypeFromPropItems | func (t *Type) createTypeFromPropItems(name string, apiDef *APIDefinition) {
p := t.Properties[name]
// propMap is this properties as map
propMap, ok := p.(map[interface{}]interface{})
if !ok {
return
}
// only process the array
tip, ok := propMap["type"]
if !ok || tip != "array" {
return
}
// only process if it has 'items' field
itemsIf, ok := propMap["items"]
if !ok {
return
}
// check it's validity
items, ok := itemsIf.(map[interface{}]interface{})
if !ok {
return
}
// to define new type, it needs to have 'properties' field
props, ok := items["properties"].(map[interface{}]interface{})
if !ok { // doesn't define new type, no problem, we can simply return
return
}
newName := t.Name + name + "Item"
created := apiDef.createType(newName, tip, props)
delete(items, "properties")
items["type"] = newName
propMap["items"] = items
t.Properties[name] = propMap
if created {
createdType := apiDef.Types[newName]
createdType.postProcess(newName, apiDef)
}
} | go | func (t *Type) createTypeFromPropItems(name string, apiDef *APIDefinition) {
p := t.Properties[name]
// propMap is this properties as map
propMap, ok := p.(map[interface{}]interface{})
if !ok {
return
}
// only process the array
tip, ok := propMap["type"]
if !ok || tip != "array" {
return
}
// only process if it has 'items' field
itemsIf, ok := propMap["items"]
if !ok {
return
}
// check it's validity
items, ok := itemsIf.(map[interface{}]interface{})
if !ok {
return
}
// to define new type, it needs to have 'properties' field
props, ok := items["properties"].(map[interface{}]interface{})
if !ok { // doesn't define new type, no problem, we can simply return
return
}
newName := t.Name + name + "Item"
created := apiDef.createType(newName, tip, props)
delete(items, "properties")
items["type"] = newName
propMap["items"] = items
t.Properties[name] = propMap
if created {
createdType := apiDef.Types[newName]
createdType.postProcess(newName, apiDef)
}
} | [
"func",
"(",
"t",
"*",
"Type",
")",
"createTypeFromPropItems",
"(",
"name",
"string",
",",
"apiDef",
"*",
"APIDefinition",
")",
"{",
"p",
":=",
"t",
".",
"Properties",
"[",
"name",
"]",
"\n\n",
"// propMap is this properties as map",
"propMap",
",",
"ok",
":=",
"p",
".",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"\n",
"}",
"\n\n",
"// only process the array",
"tip",
",",
"ok",
":=",
"propMap",
"[",
"\"",
"\"",
"]",
"\n",
"if",
"!",
"ok",
"||",
"tip",
"!=",
"\"",
"\"",
"{",
"return",
"\n",
"}",
"\n\n",
"// only process if it has 'items' field",
"itemsIf",
",",
"ok",
":=",
"propMap",
"[",
"\"",
"\"",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"\n",
"}",
"\n\n",
"// check it's validity",
"items",
",",
"ok",
":=",
"itemsIf",
".",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"\n",
"}",
"\n\n",
"// to define new type, it needs to have 'properties' field",
"props",
",",
"ok",
":=",
"items",
"[",
"\"",
"\"",
"]",
".",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
"\n",
"if",
"!",
"ok",
"{",
"// doesn't define new type, no problem, we can simply return",
"return",
"\n",
"}",
"\n",
"newName",
":=",
"t",
".",
"Name",
"+",
"name",
"+",
"\"",
"\"",
"\n",
"created",
":=",
"apiDef",
".",
"createType",
"(",
"newName",
",",
"tip",
",",
"props",
")",
"\n\n",
"delete",
"(",
"items",
",",
"\"",
"\"",
")",
"\n",
"items",
"[",
"\"",
"\"",
"]",
"=",
"newName",
"\n",
"propMap",
"[",
"\"",
"\"",
"]",
"=",
"items",
"\n\n",
"t",
".",
"Properties",
"[",
"name",
"]",
"=",
"propMap",
"\n\n",
"if",
"created",
"{",
"createdType",
":=",
"apiDef",
".",
"Types",
"[",
"newName",
"]",
"\n",
"createdType",
".",
"postProcess",
"(",
"newName",
",",
"apiDef",
")",
"\n",
"}",
"\n",
"}"
] | // create type from item with inline type definition | [
"create",
"type",
"from",
"item",
"with",
"inline",
"type",
"definition"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L688-L733 |
3,230 | Jumpscale/go-raml | raml/types.go | createTypeFromPropProperty | func (t *Type) createTypeFromPropProperty(name string, apiDef *APIDefinition) {
p := t.Properties[name]
// only process map[interface]interface{}
propMap, ok := p.(map[interface{}]interface{})
if !ok {
return
}
// only process if it has 'properties' field
propsIf, ok := propMap["properties"]
if !ok {
return
}
// check validity of the properties
props, ok := propsIf.(map[interface{}]interface{})
if !ok {
panic("inline properties expect properties in type:map[string]interface{}")
}
newName := t.Name + name
created := apiDef.createType(newName, propMap["type"], props)
propMap["type"] = newName
// delete the 'properties' field
delete(propMap, "properties")
t.Properties[name] = propMap
// post process the created type
if created {
createdType := apiDef.Types[newName]
createdType.postProcess(newName, apiDef)
}
} | go | func (t *Type) createTypeFromPropProperty(name string, apiDef *APIDefinition) {
p := t.Properties[name]
// only process map[interface]interface{}
propMap, ok := p.(map[interface{}]interface{})
if !ok {
return
}
// only process if it has 'properties' field
propsIf, ok := propMap["properties"]
if !ok {
return
}
// check validity of the properties
props, ok := propsIf.(map[interface{}]interface{})
if !ok {
panic("inline properties expect properties in type:map[string]interface{}")
}
newName := t.Name + name
created := apiDef.createType(newName, propMap["type"], props)
propMap["type"] = newName
// delete the 'properties' field
delete(propMap, "properties")
t.Properties[name] = propMap
// post process the created type
if created {
createdType := apiDef.Types[newName]
createdType.postProcess(newName, apiDef)
}
} | [
"func",
"(",
"t",
"*",
"Type",
")",
"createTypeFromPropProperty",
"(",
"name",
"string",
",",
"apiDef",
"*",
"APIDefinition",
")",
"{",
"p",
":=",
"t",
".",
"Properties",
"[",
"name",
"]",
"\n",
"// only process map[interface]interface{}",
"propMap",
",",
"ok",
":=",
"p",
".",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"\n",
"}",
"\n\n",
"// only process if it has 'properties' field",
"propsIf",
",",
"ok",
":=",
"propMap",
"[",
"\"",
"\"",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"\n",
"}",
"\n\n",
"// check validity of the properties",
"props",
",",
"ok",
":=",
"propsIf",
".",
"(",
"map",
"[",
"interface",
"{",
"}",
"]",
"interface",
"{",
"}",
")",
"\n",
"if",
"!",
"ok",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"newName",
":=",
"t",
".",
"Name",
"+",
"name",
"\n",
"created",
":=",
"apiDef",
".",
"createType",
"(",
"newName",
",",
"propMap",
"[",
"\"",
"\"",
"]",
",",
"props",
")",
"\n\n",
"propMap",
"[",
"\"",
"\"",
"]",
"=",
"newName",
"\n\n",
"// delete the 'properties' field",
"delete",
"(",
"propMap",
",",
"\"",
"\"",
")",
"\n\n",
"t",
".",
"Properties",
"[",
"name",
"]",
"=",
"propMap",
"\n\n",
"// post process the created type",
"if",
"created",
"{",
"createdType",
":=",
"apiDef",
".",
"Types",
"[",
"newName",
"]",
"\n",
"createdType",
".",
"postProcess",
"(",
"newName",
",",
"apiDef",
")",
"\n",
"}",
"\n\n",
"}"
] | // create type from property's property | [
"create",
"type",
"from",
"property",
"s",
"property"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L736-L772 |
3,231 | Jumpscale/go-raml | raml/types.go | GetProperty | func (bp BodiesProperty) GetProperty(name string) Property {
p, ok := bp.Properties[name]
if !ok {
panic(fmt.Errorf("can't find property name %v", name))
}
return toProperty(name, p)
} | go | func (bp BodiesProperty) GetProperty(name string) Property {
p, ok := bp.Properties[name]
if !ok {
panic(fmt.Errorf("can't find property name %v", name))
}
return toProperty(name, p)
} | [
"func",
"(",
"bp",
"BodiesProperty",
")",
"GetProperty",
"(",
"name",
"string",
")",
"Property",
"{",
"p",
",",
"ok",
":=",
"bp",
".",
"Properties",
"[",
"name",
"]",
"\n",
"if",
"!",
"ok",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"name",
")",
")",
"\n",
"}",
"\n",
"return",
"toProperty",
"(",
"name",
",",
"p",
")",
"\n",
"}"
] | // GetProperty gets property with given name
// from a bodies | [
"GetProperty",
"gets",
"property",
"with",
"given",
"name",
"from",
"a",
"bodies"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/types.go#L811-L817 |
3,232 | Jumpscale/go-raml | raml/errors.go | convertYAMLError | func convertYAMLError(yamlError string) string {
if strings.Contains(yamlError, "cannot unmarshal") {
yamlErrorParts := strings.Split(yamlError, " ")
if len(yamlErrorParts) >= 7 {
fmt.Println(yamlError)
var ok bool
var source string
var target string
var targetName string
line := yamlErrorParts[1]
line = line[:len(line)-1]
// TODO: support more complex types:
// map[string]raml.NamedParameter -->
// detect map, format to:
// "mapping of %s to %s", ramlTypeNames["string"], ramlTypeNames["raml.NamedParameter"]
// if "string" is not found, use the key, i.e. "string" in this case.
// so the output would be:
// mapping of string to named parameter
// TODO: instead of having string in the key of some mappings,
// perhaps use a type alias:
// type Name string
// map[Name]NamedParameter
// would output:
// mapping of name string to named parameter
if source, ok = yamlTypeToName[yamlErrorParts[4]]; !ok {
source = yamlErrorParts[4]
}
fmt.Println("source: ", source)
if source == "string" {
source = fmt.Sprintf("string (got %s)", yamlErrorParts[5])
target = yamlErrorParts[7]
} else {
target = yamlErrorParts[6]
}
if targetName, ok = ramlTypeNames[target]; !ok {
targetName = target
}
target, _ = ramlTypes[target]
return fmt.Sprintf("line %s: %s cannot be of "+
"type %s, must be %s", line, targetName, source, target)
}
}
// Otherwise
return fmt.Sprintf("YAML error, %s", yamlError)
} | go | func convertYAMLError(yamlError string) string {
if strings.Contains(yamlError, "cannot unmarshal") {
yamlErrorParts := strings.Split(yamlError, " ")
if len(yamlErrorParts) >= 7 {
fmt.Println(yamlError)
var ok bool
var source string
var target string
var targetName string
line := yamlErrorParts[1]
line = line[:len(line)-1]
// TODO: support more complex types:
// map[string]raml.NamedParameter -->
// detect map, format to:
// "mapping of %s to %s", ramlTypeNames["string"], ramlTypeNames["raml.NamedParameter"]
// if "string" is not found, use the key, i.e. "string" in this case.
// so the output would be:
// mapping of string to named parameter
// TODO: instead of having string in the key of some mappings,
// perhaps use a type alias:
// type Name string
// map[Name]NamedParameter
// would output:
// mapping of name string to named parameter
if source, ok = yamlTypeToName[yamlErrorParts[4]]; !ok {
source = yamlErrorParts[4]
}
fmt.Println("source: ", source)
if source == "string" {
source = fmt.Sprintf("string (got %s)", yamlErrorParts[5])
target = yamlErrorParts[7]
} else {
target = yamlErrorParts[6]
}
if targetName, ok = ramlTypeNames[target]; !ok {
targetName = target
}
target, _ = ramlTypes[target]
return fmt.Sprintf("line %s: %s cannot be of "+
"type %s, must be %s", line, targetName, source, target)
}
}
// Otherwise
return fmt.Sprintf("YAML error, %s", yamlError)
} | [
"func",
"convertYAMLError",
"(",
"yamlError",
"string",
")",
"string",
"{",
"if",
"strings",
".",
"Contains",
"(",
"yamlError",
",",
"\"",
"\"",
")",
"{",
"yamlErrorParts",
":=",
"strings",
".",
"Split",
"(",
"yamlError",
",",
"\"",
"\"",
")",
"\n\n",
"if",
"len",
"(",
"yamlErrorParts",
")",
">=",
"7",
"{",
"fmt",
".",
"Println",
"(",
"yamlError",
")",
"\n\n",
"var",
"ok",
"bool",
"\n",
"var",
"source",
"string",
"\n",
"var",
"target",
"string",
"\n",
"var",
"targetName",
"string",
"\n",
"line",
":=",
"yamlErrorParts",
"[",
"1",
"]",
"\n",
"line",
"=",
"line",
"[",
":",
"len",
"(",
"line",
")",
"-",
"1",
"]",
"\n\n",
"// TODO: support more complex types:",
"// map[string]raml.NamedParameter -->",
"// detect map, format to:",
"// \"mapping of %s to %s\", ramlTypeNames[\"string\"], ramlTypeNames[\"raml.NamedParameter\"]",
"// if \"string\" is not found, use the key, i.e. \"string\" in this case.",
"// so the output would be:",
"// mapping of string to named parameter",
"// TODO: instead of having string in the key of some mappings,",
"// perhaps use a type alias:",
"// type Name string",
"// map[Name]NamedParameter",
"// would output:",
"// mapping of name string to named parameter",
"if",
"source",
",",
"ok",
"=",
"yamlTypeToName",
"[",
"yamlErrorParts",
"[",
"4",
"]",
"]",
";",
"!",
"ok",
"{",
"source",
"=",
"yamlErrorParts",
"[",
"4",
"]",
"\n",
"}",
"\n",
"fmt",
".",
"Println",
"(",
"\"",
"\"",
",",
"source",
")",
"\n\n",
"if",
"source",
"==",
"\"",
"\"",
"{",
"source",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"yamlErrorParts",
"[",
"5",
"]",
")",
"\n",
"target",
"=",
"yamlErrorParts",
"[",
"7",
"]",
"\n",
"}",
"else",
"{",
"target",
"=",
"yamlErrorParts",
"[",
"6",
"]",
"\n\n",
"}",
"\n",
"if",
"targetName",
",",
"ok",
"=",
"ramlTypeNames",
"[",
"target",
"]",
";",
"!",
"ok",
"{",
"targetName",
"=",
"target",
"\n",
"}",
"\n\n",
"target",
",",
"_",
"=",
"ramlTypes",
"[",
"target",
"]",
"\n\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
"+",
"\"",
"\"",
",",
"line",
",",
"targetName",
",",
"source",
",",
"target",
")",
"\n\n",
"}",
"\n",
"}",
"\n\n",
"// Otherwise",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"yamlError",
")",
"\n",
"}"
] | // Convert a YAML error string into RAML error string, with more context | [
"Convert",
"a",
"YAML",
"error",
"string",
"into",
"RAML",
"error",
"string",
"with",
"more",
"context"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/raml/errors.go#L65-L123 |
3,233 | Jumpscale/go-raml | codegen/security/security.go | New | func New(ss raml.SecurityScheme, name, packageName string) Security {
sd := Security{
SecurityScheme: &ss,
}
sd.Name = SecuritySchemeName(name)
sd.PackageName = packageName
// assign header, if any
for k, v := range sd.DescribedBy.Headers {
header := v
header.Name = string(k)
sd.Headers = append(sd.Headers, &header)
}
sort.SliceStable(sd.Headers, func(i, j int) bool { return sd.Headers[i].Name < sd.Headers[j].Name })
// assign query params if any
for k, v := range sd.DescribedBy.QueryParameters {
queryParam := v
queryParam.Name = string(k)
sd.QueryParams = append(sd.QueryParams, &queryParam)
}
sort.SliceStable(sd.QueryParams, func(i, j int) bool { return sd.QueryParams[i].Name < sd.QueryParams[j].Name })
return sd
} | go | func New(ss raml.SecurityScheme, name, packageName string) Security {
sd := Security{
SecurityScheme: &ss,
}
sd.Name = SecuritySchemeName(name)
sd.PackageName = packageName
// assign header, if any
for k, v := range sd.DescribedBy.Headers {
header := v
header.Name = string(k)
sd.Headers = append(sd.Headers, &header)
}
sort.SliceStable(sd.Headers, func(i, j int) bool { return sd.Headers[i].Name < sd.Headers[j].Name })
// assign query params if any
for k, v := range sd.DescribedBy.QueryParameters {
queryParam := v
queryParam.Name = string(k)
sd.QueryParams = append(sd.QueryParams, &queryParam)
}
sort.SliceStable(sd.QueryParams, func(i, j int) bool { return sd.QueryParams[i].Name < sd.QueryParams[j].Name })
return sd
} | [
"func",
"New",
"(",
"ss",
"raml",
".",
"SecurityScheme",
",",
"name",
",",
"packageName",
"string",
")",
"Security",
"{",
"sd",
":=",
"Security",
"{",
"SecurityScheme",
":",
"&",
"ss",
",",
"}",
"\n",
"sd",
".",
"Name",
"=",
"SecuritySchemeName",
"(",
"name",
")",
"\n",
"sd",
".",
"PackageName",
"=",
"packageName",
"\n\n",
"// assign header, if any",
"for",
"k",
",",
"v",
":=",
"range",
"sd",
".",
"DescribedBy",
".",
"Headers",
"{",
"header",
":=",
"v",
"\n",
"header",
".",
"Name",
"=",
"string",
"(",
"k",
")",
"\n",
"sd",
".",
"Headers",
"=",
"append",
"(",
"sd",
".",
"Headers",
",",
"&",
"header",
")",
"\n",
"}",
"\n",
"sort",
".",
"SliceStable",
"(",
"sd",
".",
"Headers",
",",
"func",
"(",
"i",
",",
"j",
"int",
")",
"bool",
"{",
"return",
"sd",
".",
"Headers",
"[",
"i",
"]",
".",
"Name",
"<",
"sd",
".",
"Headers",
"[",
"j",
"]",
".",
"Name",
"}",
")",
"\n\n",
"// assign query params if any",
"for",
"k",
",",
"v",
":=",
"range",
"sd",
".",
"DescribedBy",
".",
"QueryParameters",
"{",
"queryParam",
":=",
"v",
"\n",
"queryParam",
".",
"Name",
"=",
"string",
"(",
"k",
")",
"\n",
"sd",
".",
"QueryParams",
"=",
"append",
"(",
"sd",
".",
"QueryParams",
",",
"&",
"queryParam",
")",
"\n",
"}",
"\n",
"sort",
".",
"SliceStable",
"(",
"sd",
".",
"QueryParams",
",",
"func",
"(",
"i",
",",
"j",
"int",
")",
"bool",
"{",
"return",
"sd",
".",
"QueryParams",
"[",
"i",
"]",
".",
"Name",
"<",
"sd",
".",
"QueryParams",
"[",
"j",
"]",
".",
"Name",
"}",
")",
"\n\n",
"return",
"sd",
"\n",
"}"
] | // New creates a security struct | [
"New",
"creates",
"a",
"security",
"struct"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/security/security.go#L29-L53 |
3,234 | Jumpscale/go-raml | codegen/security/security.go | Supported | func Supported(ss raml.SecurityScheme) bool {
switch ss.Type {
case Oauth2:
_, ok := ss.Settings["accessTokenUri"]
return ok
default:
return false
}
} | go | func Supported(ss raml.SecurityScheme) bool {
switch ss.Type {
case Oauth2:
_, ok := ss.Settings["accessTokenUri"]
return ok
default:
return false
}
} | [
"func",
"Supported",
"(",
"ss",
"raml",
".",
"SecurityScheme",
")",
"bool",
"{",
"switch",
"ss",
".",
"Type",
"{",
"case",
"Oauth2",
":",
"_",
",",
"ok",
":=",
"ss",
".",
"Settings",
"[",
"\"",
"\"",
"]",
"\n",
"return",
"ok",
"\n",
"default",
":",
"return",
"false",
"\n",
"}",
"\n",
"}"
] | // Supported returns true if the security scheme is supported by go-raml | [
"Supported",
"returns",
"true",
"if",
"the",
"security",
"scheme",
"is",
"supported",
"by",
"go",
"-",
"raml"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/security/security.go#L56-L64 |
3,235 | Jumpscale/go-raml | codegen/security/security.go | GetMethodSecuredBy | func GetMethodSecuredBy(apiDef *raml.APIDefinition, r *raml.Resource, m *raml.Method) []raml.DefinitionChoice {
// get the secured by
securedBy := func() []raml.DefinitionChoice {
if len(m.SecuredBy) > 0 {
return m.SecuredBy
} else if sb := FindResourceSecuredBy(r); len(sb) > 0 {
return sb
}
return apiDef.SecuredBy
}()
// filter only security scheme we supported
var filtered []raml.DefinitionChoice
for _, sb := range securedBy {
ss, ok := apiDef.SecuritySchemes[sb.Name]
if !ok {
continue
}
if !Supported(ss) {
continue
}
filtered = append(filtered, sb)
}
return filtered
} | go | func GetMethodSecuredBy(apiDef *raml.APIDefinition, r *raml.Resource, m *raml.Method) []raml.DefinitionChoice {
// get the secured by
securedBy := func() []raml.DefinitionChoice {
if len(m.SecuredBy) > 0 {
return m.SecuredBy
} else if sb := FindResourceSecuredBy(r); len(sb) > 0 {
return sb
}
return apiDef.SecuredBy
}()
// filter only security scheme we supported
var filtered []raml.DefinitionChoice
for _, sb := range securedBy {
ss, ok := apiDef.SecuritySchemes[sb.Name]
if !ok {
continue
}
if !Supported(ss) {
continue
}
filtered = append(filtered, sb)
}
return filtered
} | [
"func",
"GetMethodSecuredBy",
"(",
"apiDef",
"*",
"raml",
".",
"APIDefinition",
",",
"r",
"*",
"raml",
".",
"Resource",
",",
"m",
"*",
"raml",
".",
"Method",
")",
"[",
"]",
"raml",
".",
"DefinitionChoice",
"{",
"// get the secured by",
"securedBy",
":=",
"func",
"(",
")",
"[",
"]",
"raml",
".",
"DefinitionChoice",
"{",
"if",
"len",
"(",
"m",
".",
"SecuredBy",
")",
">",
"0",
"{",
"return",
"m",
".",
"SecuredBy",
"\n",
"}",
"else",
"if",
"sb",
":=",
"FindResourceSecuredBy",
"(",
"r",
")",
";",
"len",
"(",
"sb",
")",
">",
"0",
"{",
"return",
"sb",
"\n",
"}",
"\n",
"return",
"apiDef",
".",
"SecuredBy",
"\n",
"}",
"(",
")",
"\n\n",
"// filter only security scheme we supported",
"var",
"filtered",
"[",
"]",
"raml",
".",
"DefinitionChoice",
"\n",
"for",
"_",
",",
"sb",
":=",
"range",
"securedBy",
"{",
"ss",
",",
"ok",
":=",
"apiDef",
".",
"SecuritySchemes",
"[",
"sb",
".",
"Name",
"]",
"\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"!",
"Supported",
"(",
"ss",
")",
"{",
"continue",
"\n",
"}",
"\n",
"filtered",
"=",
"append",
"(",
"filtered",
",",
"sb",
")",
"\n",
"}",
"\n",
"return",
"filtered",
"\n",
"}"
] | // GetMethodSecuredBy get SecuredBy field of a method | [
"GetMethodSecuredBy",
"get",
"SecuredBy",
"field",
"of",
"a",
"method"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/security/security.go#L67-L91 |
3,236 | Jumpscale/go-raml | codegen/security/security.go | GetQuotedScopes | func GetQuotedScopes(ss raml.DefinitionChoice) ([]string, error) {
var quoted []string
scopes, err := getSecurityScopes(ss)
if err != nil {
return quoted, err
}
for _, s := range scopes {
quoted = append(quoted, fmt.Sprintf(`"%v"`, s))
}
return quoted, nil
} | go | func GetQuotedScopes(ss raml.DefinitionChoice) ([]string, error) {
var quoted []string
scopes, err := getSecurityScopes(ss)
if err != nil {
return quoted, err
}
for _, s := range scopes {
quoted = append(quoted, fmt.Sprintf(`"%v"`, s))
}
return quoted, nil
} | [
"func",
"GetQuotedScopes",
"(",
"ss",
"raml",
".",
"DefinitionChoice",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"var",
"quoted",
"[",
"]",
"string",
"\n",
"scopes",
",",
"err",
":=",
"getSecurityScopes",
"(",
"ss",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"quoted",
",",
"err",
"\n",
"}",
"\n",
"for",
"_",
",",
"s",
":=",
"range",
"scopes",
"{",
"quoted",
"=",
"append",
"(",
"quoted",
",",
"fmt",
".",
"Sprintf",
"(",
"`\"%v\"`",
",",
"s",
")",
")",
"\n",
"}",
"\n",
"return",
"quoted",
",",
"nil",
"\n",
"}"
] | // get array of security scopes in the form of quoted string | [
"get",
"array",
"of",
"security",
"scopes",
"in",
"the",
"form",
"of",
"quoted",
"string"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/security/security.go#L94-L104 |
3,237 | Jumpscale/go-raml | codegen/security/security.go | FindResourceSecuredBy | func FindResourceSecuredBy(r *raml.Resource) []raml.DefinitionChoice {
if len(r.SecuredBy) > 0 {
return r.SecuredBy
}
if r.Parent == nil {
return []raml.DefinitionChoice{}
}
return FindResourceSecuredBy(r.Parent)
} | go | func FindResourceSecuredBy(r *raml.Resource) []raml.DefinitionChoice {
if len(r.SecuredBy) > 0 {
return r.SecuredBy
}
if r.Parent == nil {
return []raml.DefinitionChoice{}
}
return FindResourceSecuredBy(r.Parent)
} | [
"func",
"FindResourceSecuredBy",
"(",
"r",
"*",
"raml",
".",
"Resource",
")",
"[",
"]",
"raml",
".",
"DefinitionChoice",
"{",
"if",
"len",
"(",
"r",
".",
"SecuredBy",
")",
">",
"0",
"{",
"return",
"r",
".",
"SecuredBy",
"\n",
"}",
"\n",
"if",
"r",
".",
"Parent",
"==",
"nil",
"{",
"return",
"[",
"]",
"raml",
".",
"DefinitionChoice",
"{",
"}",
"\n",
"}",
"\n",
"return",
"FindResourceSecuredBy",
"(",
"r",
".",
"Parent",
")",
"\n",
"}"
] | // find resource's securedBy recursively | [
"find",
"resource",
"s",
"securedBy",
"recursively"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/security/security.go#L149-L157 |
3,238 | Jumpscale/go-raml | codegen/nim/method.go | JesterEndpoint | func (m method) JesterEndpoint() string {
e := strings.Replace(m.Endpoint, "{", "@", -1)
return strings.Replace(e, "}", "", -1)
} | go | func (m method) JesterEndpoint() string {
e := strings.Replace(m.Endpoint, "{", "@", -1)
return strings.Replace(e, "}", "", -1)
} | [
"func",
"(",
"m",
"method",
")",
"JesterEndpoint",
"(",
")",
"string",
"{",
"e",
":=",
"strings",
".",
"Replace",
"(",
"m",
".",
"Endpoint",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"-",
"1",
")",
"\n",
"return",
"strings",
".",
"Replace",
"(",
"e",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"-",
"1",
")",
"\n",
"}"
] | // JesterEndpoint returns endpoint in jester format | [
"JesterEndpoint",
"returns",
"endpoint",
"in",
"jester",
"format"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/method.go#L64-L67 |
3,239 | Jumpscale/go-raml | codegen/nim/method.go | ServerProcParams | func (m method) ServerProcParams() string {
var params []string
for _, p := range cr.GetResourceParams(m.Resource) {
params = append(params, p+": string")
}
params = append(params, "req: Request")
return strings.Join(params, ", ")
} | go | func (m method) ServerProcParams() string {
var params []string
for _, p := range cr.GetResourceParams(m.Resource) {
params = append(params, p+": string")
}
params = append(params, "req: Request")
return strings.Join(params, ", ")
} | [
"func",
"(",
"m",
"method",
")",
"ServerProcParams",
"(",
")",
"string",
"{",
"var",
"params",
"[",
"]",
"string",
"\n\n",
"for",
"_",
",",
"p",
":=",
"range",
"cr",
".",
"GetResourceParams",
"(",
"m",
".",
"Resource",
")",
"{",
"params",
"=",
"append",
"(",
"params",
",",
"p",
"+",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"params",
"=",
"append",
"(",
"params",
",",
"\"",
"\"",
")",
"\n",
"return",
"strings",
".",
"Join",
"(",
"params",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // ProcParams are params of this jester endpoint handler | [
"ProcParams",
"are",
"params",
"of",
"this",
"jester",
"endpoint",
"handler"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/method.go#L70-L79 |
3,240 | Jumpscale/go-raml | codegen/nim/method.go | ServerCallParams | func (m method) ServerCallParams() string {
var params []string
for _, p := range cr.GetResourceParams(m.Resource) {
params = append(params, fmt.Sprintf(`@"%v"`, p))
}
params = append(params, "request")
return strings.Join(params, ", ")
} | go | func (m method) ServerCallParams() string {
var params []string
for _, p := range cr.GetResourceParams(m.Resource) {
params = append(params, fmt.Sprintf(`@"%v"`, p))
}
params = append(params, "request")
return strings.Join(params, ", ")
} | [
"func",
"(",
"m",
"method",
")",
"ServerCallParams",
"(",
")",
"string",
"{",
"var",
"params",
"[",
"]",
"string",
"\n\n",
"for",
"_",
",",
"p",
":=",
"range",
"cr",
".",
"GetResourceParams",
"(",
"m",
".",
"Resource",
")",
"{",
"params",
"=",
"append",
"(",
"params",
",",
"fmt",
".",
"Sprintf",
"(",
"`@\"%v\"`",
",",
"p",
")",
")",
"\n",
"}",
"\n\n",
"params",
"=",
"append",
"(",
"params",
",",
"\"",
"\"",
")",
"\n",
"return",
"strings",
".",
"Join",
"(",
"params",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // CallParams are params when we call jester handler | [
"CallParams",
"are",
"params",
"when",
"we",
"call",
"jester",
"handler"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/method.go#L82-L91 |
3,241 | Jumpscale/go-raml | codegen/nim/method.go | ClientCallParams | func (m method) ClientCallParams() string {
params := []string{
m.ResourcePath,
fmt.Sprintf(`"%v"`, m.Verb()),
}
if m.ReqBody != "" {
params = append(params, "$$reqBody")
}
params = append(params, "queryParams=queryParams")
return strings.Join(params, ", ")
} | go | func (m method) ClientCallParams() string {
params := []string{
m.ResourcePath,
fmt.Sprintf(`"%v"`, m.Verb()),
}
if m.ReqBody != "" {
params = append(params, "$$reqBody")
}
params = append(params, "queryParams=queryParams")
return strings.Join(params, ", ")
} | [
"func",
"(",
"m",
"method",
")",
"ClientCallParams",
"(",
")",
"string",
"{",
"params",
":=",
"[",
"]",
"string",
"{",
"m",
".",
"ResourcePath",
",",
"fmt",
".",
"Sprintf",
"(",
"`\"%v\"`",
",",
"m",
".",
"Verb",
"(",
")",
")",
",",
"}",
"\n",
"if",
"m",
".",
"ReqBody",
"!=",
"\"",
"\"",
"{",
"params",
"=",
"append",
"(",
"params",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"params",
"=",
"append",
"(",
"params",
",",
"\"",
"\"",
")",
"\n\n",
"return",
"strings",
".",
"Join",
"(",
"params",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // ClientCallParams are params when we call jester handler | [
"ClientCallParams",
"are",
"params",
"when",
"we",
"call",
"jester",
"handler"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/method.go#L117-L128 |
3,242 | Jumpscale/go-raml | codegen/nim/method.go | SecurityScopes | func (m method) SecurityScopes() string {
if len(m.SecuredBy) == 0 {
return ""
}
s, err := security.GetQuotedScopes(m.SecuredBy[0])
if err != nil {
log.Fatalf("failed to get security scopes of method `%v %v` err = %v", m.Verb(), m.ResourcePath, err)
}
return strings.Join(s, ", ")
} | go | func (m method) SecurityScopes() string {
if len(m.SecuredBy) == 0 {
return ""
}
s, err := security.GetQuotedScopes(m.SecuredBy[0])
if err != nil {
log.Fatalf("failed to get security scopes of method `%v %v` err = %v", m.Verb(), m.ResourcePath, err)
}
return strings.Join(s, ", ")
} | [
"func",
"(",
"m",
"method",
")",
"SecurityScopes",
"(",
")",
"string",
"{",
"if",
"len",
"(",
"m",
".",
"SecuredBy",
")",
"==",
"0",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n",
"s",
",",
"err",
":=",
"security",
".",
"GetQuotedScopes",
"(",
"m",
".",
"SecuredBy",
"[",
"0",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"log",
".",
"Fatalf",
"(",
"\"",
"\"",
",",
"m",
".",
"Verb",
"(",
")",
",",
"m",
".",
"ResourcePath",
",",
"err",
")",
"\n",
"}",
"\n",
"return",
"strings",
".",
"Join",
"(",
"s",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // SecurityScopes retuns security scopes of a method as single string | [
"SecurityScopes",
"retuns",
"security",
"scopes",
"of",
"a",
"method",
"as",
"single",
"string"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/method.go#L143-L152 |
3,243 | Jumpscale/go-raml | codegen/nim/method.go | formatProcName | func formatProcName(fullURI string) string {
// remove leading `/`
fullURI = fullURI[1:]
// When meet `/{`
// - replace it with `By`
// - make uppercase the first char after `/{`
spl := strings.Split(fullURI, "/{")
tmp := []string{}
for i, v := range spl {
if i != 0 {
v = strings.Title(v)
}
tmp = append(tmp, v)
}
name := strings.Join(tmp, "By")
// when meet `/`
// - make uppercase the first char after `/`
// - remove the `/`
spl = strings.Split(name, "/")
tmp = []string{}
for i, v := range spl {
if i != 0 {
v = strings.Title(v)
}
tmp = append(tmp, v)
}
return strings.Join(tmp, "")
} | go | func formatProcName(fullURI string) string {
// remove leading `/`
fullURI = fullURI[1:]
// When meet `/{`
// - replace it with `By`
// - make uppercase the first char after `/{`
spl := strings.Split(fullURI, "/{")
tmp := []string{}
for i, v := range spl {
if i != 0 {
v = strings.Title(v)
}
tmp = append(tmp, v)
}
name := strings.Join(tmp, "By")
// when meet `/`
// - make uppercase the first char after `/`
// - remove the `/`
spl = strings.Split(name, "/")
tmp = []string{}
for i, v := range spl {
if i != 0 {
v = strings.Title(v)
}
tmp = append(tmp, v)
}
return strings.Join(tmp, "")
} | [
"func",
"formatProcName",
"(",
"fullURI",
"string",
")",
"string",
"{",
"// remove leading `/`",
"fullURI",
"=",
"fullURI",
"[",
"1",
":",
"]",
"\n\n",
"// When meet `/{`",
"// - replace it with `By`",
"// - make uppercase the first char after `/{`",
"spl",
":=",
"strings",
".",
"Split",
"(",
"fullURI",
",",
"\"",
"\"",
")",
"\n",
"tmp",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"i",
",",
"v",
":=",
"range",
"spl",
"{",
"if",
"i",
"!=",
"0",
"{",
"v",
"=",
"strings",
".",
"Title",
"(",
"v",
")",
"\n",
"}",
"\n",
"tmp",
"=",
"append",
"(",
"tmp",
",",
"v",
")",
"\n",
"}",
"\n",
"name",
":=",
"strings",
".",
"Join",
"(",
"tmp",
",",
"\"",
"\"",
")",
"\n\n",
"// when meet `/`",
"// - make uppercase the first char after `/`",
"// - remove the `/`",
"spl",
"=",
"strings",
".",
"Split",
"(",
"name",
",",
"\"",
"\"",
")",
"\n",
"tmp",
"=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"i",
",",
"v",
":=",
"range",
"spl",
"{",
"if",
"i",
"!=",
"0",
"{",
"v",
"=",
"strings",
".",
"Title",
"(",
"v",
")",
"\n",
"}",
"\n",
"tmp",
"=",
"append",
"(",
"tmp",
",",
"v",
")",
"\n",
"}",
"\n\n",
"return",
"strings",
".",
"Join",
"(",
"tmp",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // format Nim proc name from complete URI | [
"format",
"Nim",
"proc",
"name",
"from",
"complete",
"URI"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/nim/method.go#L185-L215 |
3,244 | Jumpscale/go-raml | codegen/python/types.go | toPythonType | func toPythonType(t string) *pythonType {
tip, ok := typeMap[t]
if !ok {
return nil
}
pt := pythonType{
name: tip,
}
switch t {
case "datetime":
pt.importModule = "datetime"
pt.importName = "datetime"
case "uuid":
pt.importModule = "uuid"
pt.importName = "UUID"
case "string":
pt.importModule = "six"
pt.importName = "string_types"
}
return &pt
} | go | func toPythonType(t string) *pythonType {
tip, ok := typeMap[t]
if !ok {
return nil
}
pt := pythonType{
name: tip,
}
switch t {
case "datetime":
pt.importModule = "datetime"
pt.importName = "datetime"
case "uuid":
pt.importModule = "uuid"
pt.importName = "UUID"
case "string":
pt.importModule = "six"
pt.importName = "string_types"
}
return &pt
} | [
"func",
"toPythonType",
"(",
"t",
"string",
")",
"*",
"pythonType",
"{",
"tip",
",",
"ok",
":=",
"typeMap",
"[",
"t",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"pt",
":=",
"pythonType",
"{",
"name",
":",
"tip",
",",
"}",
"\n\n",
"switch",
"t",
"{",
"case",
"\"",
"\"",
":",
"pt",
".",
"importModule",
"=",
"\"",
"\"",
"\n",
"pt",
".",
"importName",
"=",
"\"",
"\"",
"\n",
"case",
"\"",
"\"",
":",
"pt",
".",
"importModule",
"=",
"\"",
"\"",
"\n",
"pt",
".",
"importName",
"=",
"\"",
"\"",
"\n",
"case",
"\"",
"\"",
":",
"pt",
".",
"importModule",
"=",
"\"",
"\"",
"\n",
"pt",
".",
"importName",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"return",
"&",
"pt",
"\n",
"}"
] | // convert raml type to python type
// also returns the needed import | [
"convert",
"raml",
"type",
"to",
"python",
"type",
"also",
"returns",
"the",
"needed",
"import"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/python/types.go#L31-L52 |
3,245 | Jumpscale/go-raml | codegen/golang/server_method.go | setup | func (gm *serverMethod) setup(apiDef *raml.APIDefinition, r *raml.Resource, rd *resource.Resource, methodName string) error {
gm.MethodName = strings.Title(serverMethodName(gm.Endpoint, gm.DisplayName, methodName, rd.Name))
// setting middlewares
middlewares := []string{}
// security middlewares
for _, v := range gm.SecuredBy {
if !security.ValidateScheme(v.Name, apiDef) {
continue
}
// oauth2 middleware
m, err := getOauth2MwrHandler(v)
if err != nil {
return err
}
middlewares = append(middlewares, m)
}
gm.Middlewares = strings.Join(middlewares, ", ")
return nil
} | go | func (gm *serverMethod) setup(apiDef *raml.APIDefinition, r *raml.Resource, rd *resource.Resource, methodName string) error {
gm.MethodName = strings.Title(serverMethodName(gm.Endpoint, gm.DisplayName, methodName, rd.Name))
// setting middlewares
middlewares := []string{}
// security middlewares
for _, v := range gm.SecuredBy {
if !security.ValidateScheme(v.Name, apiDef) {
continue
}
// oauth2 middleware
m, err := getOauth2MwrHandler(v)
if err != nil {
return err
}
middlewares = append(middlewares, m)
}
gm.Middlewares = strings.Join(middlewares, ", ")
return nil
} | [
"func",
"(",
"gm",
"*",
"serverMethod",
")",
"setup",
"(",
"apiDef",
"*",
"raml",
".",
"APIDefinition",
",",
"r",
"*",
"raml",
".",
"Resource",
",",
"rd",
"*",
"resource",
".",
"Resource",
",",
"methodName",
"string",
")",
"error",
"{",
"gm",
".",
"MethodName",
"=",
"strings",
".",
"Title",
"(",
"serverMethodName",
"(",
"gm",
".",
"Endpoint",
",",
"gm",
".",
"DisplayName",
",",
"methodName",
",",
"rd",
".",
"Name",
")",
")",
"\n\n",
"// setting middlewares",
"middlewares",
":=",
"[",
"]",
"string",
"{",
"}",
"\n\n",
"// security middlewares",
"for",
"_",
",",
"v",
":=",
"range",
"gm",
".",
"SecuredBy",
"{",
"if",
"!",
"security",
".",
"ValidateScheme",
"(",
"v",
".",
"Name",
",",
"apiDef",
")",
"{",
"continue",
"\n",
"}",
"\n",
"// oauth2 middleware",
"m",
",",
"err",
":=",
"getOauth2MwrHandler",
"(",
"v",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"middlewares",
"=",
"append",
"(",
"middlewares",
",",
"m",
")",
"\n",
"}",
"\n\n",
"gm",
".",
"Middlewares",
"=",
"strings",
".",
"Join",
"(",
"middlewares",
",",
"\"",
"\"",
")",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // setup go server method, initializes all needed variables | [
"setup",
"go",
"server",
"method",
"initializes",
"all",
"needed",
"variables"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/golang/server_method.go#L50-L72 |
3,246 | Jumpscale/go-raml | codegen/golang/server_method.go | libImported | func (gm serverMethod) libImported(rootImportPath string) map[string]struct{} {
libs := map[string]struct{}{}
// req body
if lib := libImportPath(rootImportPath, gm.reqBody, globLibRootURLs); lib != "" {
libs[lib] = struct{}{}
}
// resp body
if lib := libImportPath(rootImportPath, gm.firstSuccessRespBodyRawType(), globLibRootURLs); lib != "" {
libs[lib] = struct{}{}
}
return libs
} | go | func (gm serverMethod) libImported(rootImportPath string) map[string]struct{} {
libs := map[string]struct{}{}
// req body
if lib := libImportPath(rootImportPath, gm.reqBody, globLibRootURLs); lib != "" {
libs[lib] = struct{}{}
}
// resp body
if lib := libImportPath(rootImportPath, gm.firstSuccessRespBodyRawType(), globLibRootURLs); lib != "" {
libs[lib] = struct{}{}
}
return libs
} | [
"func",
"(",
"gm",
"serverMethod",
")",
"libImported",
"(",
"rootImportPath",
"string",
")",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
"{",
"libs",
":=",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"// req body",
"if",
"lib",
":=",
"libImportPath",
"(",
"rootImportPath",
",",
"gm",
".",
"reqBody",
",",
"globLibRootURLs",
")",
";",
"lib",
"!=",
"\"",
"\"",
"{",
"libs",
"[",
"lib",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n\n",
"// resp body",
"if",
"lib",
":=",
"libImportPath",
"(",
"rootImportPath",
",",
"gm",
".",
"firstSuccessRespBodyRawType",
"(",
")",
",",
"globLibRootURLs",
")",
";",
"lib",
"!=",
"\"",
"\"",
"{",
"libs",
"[",
"lib",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"return",
"libs",
"\n",
"}"
] | // return all libs imported by this method | [
"return",
"all",
"libs",
"imported",
"by",
"this",
"method"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/golang/server_method.go#L75-L88 |
3,247 | Jumpscale/go-raml | codegen/golang/server_method.go | Imports | func (gm serverMethod) Imports() []string {
ip := map[string]struct{}{
`"net/http"`: struct{}{},
}
if gm.firstSuccessRespBodyType() != "" || gm.reqBody != "" {
ip[`"encoding/json"`] = struct{}{}
}
if gm.needImportGoramlTypes() {
ip[`"`+globRootImportPath+"/types"+`"`] = struct{}{}
}
for lib := range gm.libImported(globRootImportPath) {
ip[lib] = struct{}{}
}
return commons.MapToSortedStrings(ip)
} | go | func (gm serverMethod) Imports() []string {
ip := map[string]struct{}{
`"net/http"`: struct{}{},
}
if gm.firstSuccessRespBodyType() != "" || gm.reqBody != "" {
ip[`"encoding/json"`] = struct{}{}
}
if gm.needImportGoramlTypes() {
ip[`"`+globRootImportPath+"/types"+`"`] = struct{}{}
}
for lib := range gm.libImported(globRootImportPath) {
ip[lib] = struct{}{}
}
return commons.MapToSortedStrings(ip)
} | [
"func",
"(",
"gm",
"serverMethod",
")",
"Imports",
"(",
")",
"[",
"]",
"string",
"{",
"ip",
":=",
"map",
"[",
"string",
"]",
"struct",
"{",
"}",
"{",
"`\"net/http\"`",
":",
"struct",
"{",
"}",
"{",
"}",
",",
"}",
"\n",
"if",
"gm",
".",
"firstSuccessRespBodyType",
"(",
")",
"!=",
"\"",
"\"",
"||",
"gm",
".",
"reqBody",
"!=",
"\"",
"\"",
"{",
"ip",
"[",
"`\"encoding/json\"`",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"if",
"gm",
".",
"needImportGoramlTypes",
"(",
")",
"{",
"ip",
"[",
"`\"`",
"+",
"globRootImportPath",
"+",
"\"",
"\"",
"+",
"`\"`",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"for",
"lib",
":=",
"range",
"gm",
".",
"libImported",
"(",
"globRootImportPath",
")",
"{",
"ip",
"[",
"lib",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"return",
"commons",
".",
"MapToSortedStrings",
"(",
"ip",
")",
"\n",
"}"
] | // Imports return all packages needed
// by this method | [
"Imports",
"return",
"all",
"packages",
"needed",
"by",
"this",
"method"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/golang/server_method.go#L92-L106 |
3,248 | Jumpscale/go-raml | codegen/golang/server_method.go | ReqBodyNeedValidation | func (gm serverMethod) ReqBodyNeedValidation() bool {
// we can't use t.GetBuiltinType here because
// the reqBody type is already in Go type
getBuiltinType := func() string {
switch {
case strings.HasPrefix(gm.reqBody, "[][]"): // bidimensional array
return strings.TrimPrefix(gm.reqBody, "[][]")
case strings.HasPrefix(gm.reqBody, "[]"): // array
return strings.TrimPrefix(gm.reqBody, "[]")
default:
return gm.reqBody
}
}
t := raml.Type{
Type: getBuiltinType(),
}
return !t.IsBuiltin()
} | go | func (gm serverMethod) ReqBodyNeedValidation() bool {
// we can't use t.GetBuiltinType here because
// the reqBody type is already in Go type
getBuiltinType := func() string {
switch {
case strings.HasPrefix(gm.reqBody, "[][]"): // bidimensional array
return strings.TrimPrefix(gm.reqBody, "[][]")
case strings.HasPrefix(gm.reqBody, "[]"): // array
return strings.TrimPrefix(gm.reqBody, "[]")
default:
return gm.reqBody
}
}
t := raml.Type{
Type: getBuiltinType(),
}
return !t.IsBuiltin()
} | [
"func",
"(",
"gm",
"serverMethod",
")",
"ReqBodyNeedValidation",
"(",
")",
"bool",
"{",
"// we can't use t.GetBuiltinType here because",
"// the reqBody type is already in Go type",
"getBuiltinType",
":=",
"func",
"(",
")",
"string",
"{",
"switch",
"{",
"case",
"strings",
".",
"HasPrefix",
"(",
"gm",
".",
"reqBody",
",",
"\"",
"\"",
")",
":",
"// bidimensional array",
"return",
"strings",
".",
"TrimPrefix",
"(",
"gm",
".",
"reqBody",
",",
"\"",
"\"",
")",
"\n",
"case",
"strings",
".",
"HasPrefix",
"(",
"gm",
".",
"reqBody",
",",
"\"",
"\"",
")",
":",
"// array",
"return",
"strings",
".",
"TrimPrefix",
"(",
"gm",
".",
"reqBody",
",",
"\"",
"\"",
")",
"\n",
"default",
":",
"return",
"gm",
".",
"reqBody",
"\n",
"}",
"\n",
"}",
"\n",
"t",
":=",
"raml",
".",
"Type",
"{",
"Type",
":",
"getBuiltinType",
"(",
")",
",",
"}",
"\n\n",
"return",
"!",
"t",
".",
"IsBuiltin",
"(",
")",
"\n",
"}"
] | // true if req body need validation code | [
"true",
"if",
"req",
"body",
"need",
"validation",
"code"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/golang/server_method.go#L109-L127 |
3,249 | Jumpscale/go-raml | codegen/commons/type.go | IsBuiltinType | func IsBuiltinType(t interface{}) bool {
tip := raml.Type{
Type: t,
}
return tip.IsBuiltin()
} | go | func IsBuiltinType(t interface{}) bool {
tip := raml.Type{
Type: t,
}
return tip.IsBuiltin()
} | [
"func",
"IsBuiltinType",
"(",
"t",
"interface",
"{",
"}",
")",
"bool",
"{",
"tip",
":=",
"raml",
".",
"Type",
"{",
"Type",
":",
"t",
",",
"}",
"\n",
"return",
"tip",
".",
"IsBuiltin",
"(",
")",
"\n",
"}"
] | // IsBuiltinType returns true if the given type
// is builtin type | [
"IsBuiltinType",
"returns",
"true",
"if",
"the",
"given",
"type",
"is",
"builtin",
"type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/commons/type.go#L12-L17 |
3,250 | Jumpscale/go-raml | codegen/commons/type.go | GetBasicType | func GetBasicType(t string) string {
tip := raml.Type{
Type: t,
}
switch {
case tip.IsArray():
return tip.ArrayType()
case tip.IsBidimensiArray():
return tip.BidimensiArrayType()
default:
return t
}
} | go | func GetBasicType(t string) string {
tip := raml.Type{
Type: t,
}
switch {
case tip.IsArray():
return tip.ArrayType()
case tip.IsBidimensiArray():
return tip.BidimensiArrayType()
default:
return t
}
} | [
"func",
"GetBasicType",
"(",
"t",
"string",
")",
"string",
"{",
"tip",
":=",
"raml",
".",
"Type",
"{",
"Type",
":",
"t",
",",
"}",
"\n\n",
"switch",
"{",
"case",
"tip",
".",
"IsArray",
"(",
")",
":",
"return",
"tip",
".",
"ArrayType",
"(",
")",
"\n",
"case",
"tip",
".",
"IsBidimensiArray",
"(",
")",
":",
"return",
"tip",
".",
"BidimensiArrayType",
"(",
")",
"\n",
"default",
":",
"return",
"t",
"\n",
"}",
"\n",
"}"
] | // GetBasicType returns basic type of a basic or complex type | [
"GetBasicType",
"returns",
"basic",
"type",
"of",
"a",
"basic",
"or",
"complex",
"type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/commons/type.go#L20-L33 |
3,251 | Jumpscale/go-raml | codegen/commons/type.go | IsArrayType | func IsArrayType(t string) bool {
tip := raml.Type{
Type: t,
}
return tip.IsArray()
} | go | func IsArrayType(t string) bool {
tip := raml.Type{
Type: t,
}
return tip.IsArray()
} | [
"func",
"IsArrayType",
"(",
"t",
"string",
")",
"bool",
"{",
"tip",
":=",
"raml",
".",
"Type",
"{",
"Type",
":",
"t",
",",
"}",
"\n",
"return",
"tip",
".",
"IsArray",
"(",
")",
"\n",
"}"
] | // IsArrayType returns true if the given
// string is a RAML array | [
"IsArrayType",
"returns",
"true",
"if",
"the",
"given",
"string",
"is",
"a",
"RAML",
"array"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/commons/type.go#L37-L42 |
3,252 | Jumpscale/go-raml | codegen/capnp.go | GenerateCapnp | func GenerateCapnp(apiDef *raml.APIDefinition, dir, lang, pkg string) error {
return capnp.GenerateCapnp(apiDef, dir, lang, pkg)
} | go | func GenerateCapnp(apiDef *raml.APIDefinition, dir, lang, pkg string) error {
return capnp.GenerateCapnp(apiDef, dir, lang, pkg)
} | [
"func",
"GenerateCapnp",
"(",
"apiDef",
"*",
"raml",
".",
"APIDefinition",
",",
"dir",
",",
"lang",
",",
"pkg",
"string",
")",
"error",
"{",
"return",
"capnp",
".",
"GenerateCapnp",
"(",
"apiDef",
",",
"dir",
",",
"lang",
",",
"pkg",
")",
"\n",
"}"
] | // GenerateCapnp generates capnp schema from RAML specs | [
"GenerateCapnp",
"generates",
"capnp",
"schema",
"from",
"RAML",
"specs"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/capnp.go#L9-L11 |
3,253 | Jumpscale/go-raml | codegen/golang/types.go | convertToGoType | func convertToGoType(tip, items string) string {
// check for raml builtin type
if v, ok := typeMap[tip]; ok {
return v
}
if v := mapDate(tip); v != "" {
return v
}
// creates raml.Type object for this type,
// so we can use raml.Type methods
ramlType := raml.Type{
Type: tip,
Items: items,
}
// other types that need some processing
switch {
case ramlType.IsBidimensiArray():
return "[][]" + ramlType.BidimensiArrayType()
case ramlType.IsArray():
return "[]" + convertToGoType(ramlType.ArrayType(), "")
case ramlType.IsUnion():
return unionNewName(tip)
case ramlType.IsMultipleInheritance():
parents, _ := ramlType.MultipleInheritance()
return multipleInheritanceNewName(parents)
}
return commons.NormalizePkgName(tip)
} | go | func convertToGoType(tip, items string) string {
// check for raml builtin type
if v, ok := typeMap[tip]; ok {
return v
}
if v := mapDate(tip); v != "" {
return v
}
// creates raml.Type object for this type,
// so we can use raml.Type methods
ramlType := raml.Type{
Type: tip,
Items: items,
}
// other types that need some processing
switch {
case ramlType.IsBidimensiArray():
return "[][]" + ramlType.BidimensiArrayType()
case ramlType.IsArray():
return "[]" + convertToGoType(ramlType.ArrayType(), "")
case ramlType.IsUnion():
return unionNewName(tip)
case ramlType.IsMultipleInheritance():
parents, _ := ramlType.MultipleInheritance()
return multipleInheritanceNewName(parents)
}
return commons.NormalizePkgName(tip)
} | [
"func",
"convertToGoType",
"(",
"tip",
",",
"items",
"string",
")",
"string",
"{",
"// check for raml builtin type",
"if",
"v",
",",
"ok",
":=",
"typeMap",
"[",
"tip",
"]",
";",
"ok",
"{",
"return",
"v",
"\n",
"}",
"\n\n",
"if",
"v",
":=",
"mapDate",
"(",
"tip",
")",
";",
"v",
"!=",
"\"",
"\"",
"{",
"return",
"v",
"\n",
"}",
"\n\n",
"// creates raml.Type object for this type,",
"// so we can use raml.Type methods",
"ramlType",
":=",
"raml",
".",
"Type",
"{",
"Type",
":",
"tip",
",",
"Items",
":",
"items",
",",
"}",
"\n\n",
"// other types that need some processing",
"switch",
"{",
"case",
"ramlType",
".",
"IsBidimensiArray",
"(",
")",
":",
"return",
"\"",
"\"",
"+",
"ramlType",
".",
"BidimensiArrayType",
"(",
")",
"\n",
"case",
"ramlType",
".",
"IsArray",
"(",
")",
":",
"return",
"\"",
"\"",
"+",
"convertToGoType",
"(",
"ramlType",
".",
"ArrayType",
"(",
")",
",",
"\"",
"\"",
")",
"\n",
"case",
"ramlType",
".",
"IsUnion",
"(",
")",
":",
"return",
"unionNewName",
"(",
"tip",
")",
"\n",
"case",
"ramlType",
".",
"IsMultipleInheritance",
"(",
")",
":",
"parents",
",",
"_",
":=",
"ramlType",
".",
"MultipleInheritance",
"(",
")",
"\n",
"return",
"multipleInheritanceNewName",
"(",
"parents",
")",
"\n",
"}",
"\n",
"return",
"commons",
".",
"NormalizePkgName",
"(",
"tip",
")",
"\n",
"}"
] | // convert from raml type to go type | [
"convert",
"from",
"raml",
"type",
"to",
"go",
"type"
] | f151e1e143c47282b294fe70c5e56f113988ed10 | https://github.com/Jumpscale/go-raml/blob/f151e1e143c47282b294fe70c5e56f113988ed10/codegen/golang/types.go#L79-L109 |
3,254 | mcuadros/go-defaults | filler.go | Fill | func (f *Filler) Fill(variable interface{}) {
fields := f.getFields(variable)
f.SetDefaultValues(fields)
} | go | func (f *Filler) Fill(variable interface{}) {
fields := f.getFields(variable)
f.SetDefaultValues(fields)
} | [
"func",
"(",
"f",
"*",
"Filler",
")",
"Fill",
"(",
"variable",
"interface",
"{",
"}",
")",
"{",
"fields",
":=",
"f",
".",
"getFields",
"(",
"variable",
")",
"\n",
"f",
".",
"SetDefaultValues",
"(",
"fields",
")",
"\n",
"}"
] | // Fill apply all the functions contained on Filler, setting all the possible
// values | [
"Fill",
"apply",
"all",
"the",
"functions",
"contained",
"on",
"Filler",
"setting",
"all",
"the",
"possible",
"values"
] | e1c978be3307be96ef03a0bd25d719ab50d277e4 | https://github.com/mcuadros/go-defaults/blob/e1c978be3307be96ef03a0bd25d719ab50d277e4/filler.go#L28-L31 |
3,255 | mcuadros/go-defaults | filler.go | GetTypeHash | func GetTypeHash(t reflect.Type) TypeHash {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return TypeHash(fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()))
} | go | func GetTypeHash(t reflect.Type) TypeHash {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return TypeHash(fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()))
} | [
"func",
"GetTypeHash",
"(",
"t",
"reflect",
".",
"Type",
")",
"TypeHash",
"{",
"if",
"t",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"t",
"=",
"t",
".",
"Elem",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"TypeHash",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"t",
".",
"PkgPath",
"(",
")",
",",
"t",
".",
"Name",
"(",
")",
")",
")",
"\n",
"}"
] | // GetTypeHash returns the TypeHash for a given reflect.Type | [
"GetTypeHash",
"returns",
"the",
"TypeHash",
"for",
"a",
"given",
"reflect",
".",
"Type"
] | e1c978be3307be96ef03a0bd25d719ab50d277e4 | https://github.com/mcuadros/go-defaults/blob/e1c978be3307be96ef03a0bd25d719ab50d277e4/filler.go#L141-L147 |
3,256 | mitchellh/ioprogress | draw.go | DrawTerminalf | func DrawTerminalf(w io.Writer, f DrawTextFormatFunc) DrawFunc {
var maxLength int
return func(progress, total int64) error {
if progress == -1 && total == -1 {
_, err := fmt.Fprintf(w, "\n")
return err
}
// Make sure we pad it to the max length we've ever drawn so that
// we don't have trailing characters.
line := f(progress, total)
if len(line) < maxLength {
line = fmt.Sprintf(
"%s%s",
line,
strings.Repeat(" ", maxLength-len(line)))
}
maxLength = len(line)
_, err := fmt.Fprint(w, line+"\r")
return err
}
} | go | func DrawTerminalf(w io.Writer, f DrawTextFormatFunc) DrawFunc {
var maxLength int
return func(progress, total int64) error {
if progress == -1 && total == -1 {
_, err := fmt.Fprintf(w, "\n")
return err
}
// Make sure we pad it to the max length we've ever drawn so that
// we don't have trailing characters.
line := f(progress, total)
if len(line) < maxLength {
line = fmt.Sprintf(
"%s%s",
line,
strings.Repeat(" ", maxLength-len(line)))
}
maxLength = len(line)
_, err := fmt.Fprint(w, line+"\r")
return err
}
} | [
"func",
"DrawTerminalf",
"(",
"w",
"io",
".",
"Writer",
",",
"f",
"DrawTextFormatFunc",
")",
"DrawFunc",
"{",
"var",
"maxLength",
"int",
"\n\n",
"return",
"func",
"(",
"progress",
",",
"total",
"int64",
")",
"error",
"{",
"if",
"progress",
"==",
"-",
"1",
"&&",
"total",
"==",
"-",
"1",
"{",
"_",
",",
"err",
":=",
"fmt",
".",
"Fprintf",
"(",
"w",
",",
"\"",
"\\n",
"\"",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n\n",
"// Make sure we pad it to the max length we've ever drawn so that",
"// we don't have trailing characters.",
"line",
":=",
"f",
"(",
"progress",
",",
"total",
")",
"\n",
"if",
"len",
"(",
"line",
")",
"<",
"maxLength",
"{",
"line",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"line",
",",
"strings",
".",
"Repeat",
"(",
"\"",
"\"",
",",
"maxLength",
"-",
"len",
"(",
"line",
")",
")",
")",
"\n",
"}",
"\n",
"maxLength",
"=",
"len",
"(",
"line",
")",
"\n\n",
"_",
",",
"err",
":=",
"fmt",
".",
"Fprint",
"(",
"w",
",",
"line",
"+",
"\"",
"\\r",
"\"",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}"
] | // DrawTerminalf returns a DrawFunc that draws a progress bar to an io.Writer
// that is formatted with the given formatting function. | [
"DrawTerminalf",
"returns",
"a",
"DrawFunc",
"that",
"draws",
"a",
"progress",
"bar",
"to",
"an",
"io",
".",
"Writer",
"that",
"is",
"formatted",
"with",
"the",
"given",
"formatting",
"function",
"."
] | 6a23b12fa88ea53e63d9b49f148fe6b29c30af6d | https://github.com/mitchellh/ioprogress/blob/6a23b12fa88ea53e63d9b49f148fe6b29c30af6d/draw.go#L33-L56 |
3,257 | mitchellh/ioprogress | draw.go | DrawTextFormatBytes | func DrawTextFormatBytes(progress, total int64) string {
return fmt.Sprintf("%s/%s", byteUnitStr(progress), byteUnitStr(total))
} | go | func DrawTextFormatBytes(progress, total int64) string {
return fmt.Sprintf("%s/%s", byteUnitStr(progress), byteUnitStr(total))
} | [
"func",
"DrawTextFormatBytes",
"(",
"progress",
",",
"total",
"int64",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"byteUnitStr",
"(",
"progress",
")",
",",
"byteUnitStr",
"(",
"total",
")",
")",
"\n",
"}"
] | // DrawTextFormatBytes is a DrawTextFormatFunc that formats the progress
// and total into human-friendly byte formats. | [
"DrawTextFormatBytes",
"is",
"a",
"DrawTextFormatFunc",
"that",
"formats",
"the",
"progress",
"and",
"total",
"into",
"human",
"-",
"friendly",
"byte",
"formats",
"."
] | 6a23b12fa88ea53e63d9b49f148fe6b29c30af6d | https://github.com/mitchellh/ioprogress/blob/6a23b12fa88ea53e63d9b49f148fe6b29c30af6d/draw.go#L62-L64 |
3,258 | blackjack/webcam | webcam.go | Open | func Open(path string) (*Webcam, error) {
handle, err := unix.Open(path, unix.O_RDWR|unix.O_NONBLOCK, 0666)
fd := uintptr(handle)
if fd < 0 || err != nil {
return nil, err
}
supportsVideoCapture, supportsVideoStreaming, err := checkCapabilities(fd)
if err != nil {
return nil, err
}
if !supportsVideoCapture {
return nil, errors.New("Not a video capture device")
}
if !supportsVideoStreaming {
return nil, errors.New("Device does not support the streaming I/O method")
}
w := new(Webcam)
w.fd = uintptr(fd)
w.bufcount = 256
return w, nil
} | go | func Open(path string) (*Webcam, error) {
handle, err := unix.Open(path, unix.O_RDWR|unix.O_NONBLOCK, 0666)
fd := uintptr(handle)
if fd < 0 || err != nil {
return nil, err
}
supportsVideoCapture, supportsVideoStreaming, err := checkCapabilities(fd)
if err != nil {
return nil, err
}
if !supportsVideoCapture {
return nil, errors.New("Not a video capture device")
}
if !supportsVideoStreaming {
return nil, errors.New("Device does not support the streaming I/O method")
}
w := new(Webcam)
w.fd = uintptr(fd)
w.bufcount = 256
return w, nil
} | [
"func",
"Open",
"(",
"path",
"string",
")",
"(",
"*",
"Webcam",
",",
"error",
")",
"{",
"handle",
",",
"err",
":=",
"unix",
".",
"Open",
"(",
"path",
",",
"unix",
".",
"O_RDWR",
"|",
"unix",
".",
"O_NONBLOCK",
",",
"0666",
")",
"\n",
"fd",
":=",
"uintptr",
"(",
"handle",
")",
"\n\n",
"if",
"fd",
"<",
"0",
"||",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"supportsVideoCapture",
",",
"supportsVideoStreaming",
",",
"err",
":=",
"checkCapabilities",
"(",
"fd",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"!",
"supportsVideoCapture",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"supportsVideoStreaming",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"w",
":=",
"new",
"(",
"Webcam",
")",
"\n",
"w",
".",
"fd",
"=",
"uintptr",
"(",
"fd",
")",
"\n",
"w",
".",
"bufcount",
"=",
"256",
"\n",
"return",
"w",
",",
"nil",
"\n",
"}"
] | // Open a webcam with a given path
// Checks if device is a v4l2 device and if it is
// capable to stream video | [
"Open",
"a",
"webcam",
"with",
"a",
"given",
"path",
"Checks",
"if",
"device",
"is",
"a",
"v4l2",
"device",
"and",
"if",
"it",
"is",
"capable",
"to",
"stream",
"video"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L32-L59 |
3,259 | blackjack/webcam | webcam.go | GetSupportedFrameSizes | func (w *Webcam) GetSupportedFrameSizes(f PixelFormat) []FrameSize {
result := make([]FrameSize, 0)
var index uint32
var err error
for index = 0; err == nil; index++ {
s, err := getFrameSize(w.fd, index, uint32(f))
if err != nil {
break
}
result = append(result, s)
}
return result
} | go | func (w *Webcam) GetSupportedFrameSizes(f PixelFormat) []FrameSize {
result := make([]FrameSize, 0)
var index uint32
var err error
for index = 0; err == nil; index++ {
s, err := getFrameSize(w.fd, index, uint32(f))
if err != nil {
break
}
result = append(result, s)
}
return result
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"GetSupportedFrameSizes",
"(",
"f",
"PixelFormat",
")",
"[",
"]",
"FrameSize",
"{",
"result",
":=",
"make",
"(",
"[",
"]",
"FrameSize",
",",
"0",
")",
"\n\n",
"var",
"index",
"uint32",
"\n",
"var",
"err",
"error",
"\n\n",
"for",
"index",
"=",
"0",
";",
"err",
"==",
"nil",
";",
"index",
"++",
"{",
"s",
",",
"err",
":=",
"getFrameSize",
"(",
"w",
".",
"fd",
",",
"index",
",",
"uint32",
"(",
"f",
")",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"break",
"\n",
"}",
"\n\n",
"result",
"=",
"append",
"(",
"result",
",",
"s",
")",
"\n",
"}",
"\n\n",
"return",
"result",
"\n",
"}"
] | // Returns supported frame sizes for a given image format | [
"Returns",
"supported",
"frame",
"sizes",
"for",
"a",
"given",
"image",
"format"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L89-L106 |
3,260 | blackjack/webcam | webcam.go | SetImageFormat | func (w *Webcam) SetImageFormat(f PixelFormat, width, height uint32) (PixelFormat, uint32, uint32, error) {
code := uint32(f)
cw := width
ch := height
err := setImageFormat(w.fd, &code, &width, &height)
if err != nil {
return 0, 0, 0, err
} else {
return PixelFormat(code), cw, ch, nil
}
} | go | func (w *Webcam) SetImageFormat(f PixelFormat, width, height uint32) (PixelFormat, uint32, uint32, error) {
code := uint32(f)
cw := width
ch := height
err := setImageFormat(w.fd, &code, &width, &height)
if err != nil {
return 0, 0, 0, err
} else {
return PixelFormat(code), cw, ch, nil
}
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"SetImageFormat",
"(",
"f",
"PixelFormat",
",",
"width",
",",
"height",
"uint32",
")",
"(",
"PixelFormat",
",",
"uint32",
",",
"uint32",
",",
"error",
")",
"{",
"code",
":=",
"uint32",
"(",
"f",
")",
"\n",
"cw",
":=",
"width",
"\n",
"ch",
":=",
"height",
"\n\n",
"err",
":=",
"setImageFormat",
"(",
"w",
".",
"fd",
",",
"&",
"code",
",",
"&",
"width",
",",
"&",
"height",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"0",
",",
"0",
",",
"err",
"\n",
"}",
"else",
"{",
"return",
"PixelFormat",
"(",
"code",
")",
",",
"cw",
",",
"ch",
",",
"nil",
"\n",
"}",
"\n",
"}"
] | // Sets desired image format and frame size
// Note, that device driver can change that values.
// Resulting values are returned by a function
// alongside with an error if any | [
"Sets",
"desired",
"image",
"format",
"and",
"frame",
"size",
"Note",
"that",
"device",
"driver",
"can",
"change",
"that",
"values",
".",
"Resulting",
"values",
"are",
"returned",
"by",
"a",
"function",
"alongside",
"with",
"an",
"error",
"if",
"any"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L112-L125 |
3,261 | blackjack/webcam | webcam.go | SetBufferCount | func (w *Webcam) SetBufferCount(count uint32) error {
if w.streaming {
return errors.New("Cannot set buffer count when streaming")
}
w.bufcount = count
return nil
} | go | func (w *Webcam) SetBufferCount(count uint32) error {
if w.streaming {
return errors.New("Cannot set buffer count when streaming")
}
w.bufcount = count
return nil
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"SetBufferCount",
"(",
"count",
"uint32",
")",
"error",
"{",
"if",
"w",
".",
"streaming",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"w",
".",
"bufcount",
"=",
"count",
"\n",
"return",
"nil",
"\n",
"}"
] | // Set the number of frames to be buffered.
// Not allowed if streaming is already on. | [
"Set",
"the",
"number",
"of",
"frames",
"to",
"be",
"buffered",
".",
"Not",
"allowed",
"if",
"streaming",
"is",
"already",
"on",
"."
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L129-L135 |
3,262 | blackjack/webcam | webcam.go | GetControls | func (w *Webcam) GetControls() map[ControlID]Control {
cmap := make(map[ControlID]Control)
for _, c := range queryControls(w.fd) {
cmap[ControlID(c.id)] = Control{c.name, c.min, c.max}
}
return cmap
} | go | func (w *Webcam) GetControls() map[ControlID]Control {
cmap := make(map[ControlID]Control)
for _, c := range queryControls(w.fd) {
cmap[ControlID(c.id)] = Control{c.name, c.min, c.max}
}
return cmap
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"GetControls",
"(",
")",
"map",
"[",
"ControlID",
"]",
"Control",
"{",
"cmap",
":=",
"make",
"(",
"map",
"[",
"ControlID",
"]",
"Control",
")",
"\n",
"for",
"_",
",",
"c",
":=",
"range",
"queryControls",
"(",
"w",
".",
"fd",
")",
"{",
"cmap",
"[",
"ControlID",
"(",
"c",
".",
"id",
")",
"]",
"=",
"Control",
"{",
"c",
".",
"name",
",",
"c",
".",
"min",
",",
"c",
".",
"max",
"}",
"\n",
"}",
"\n",
"return",
"cmap",
"\n",
"}"
] | // Get a map of available controls. | [
"Get",
"a",
"map",
"of",
"available",
"controls",
"."
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L138-L144 |
3,263 | blackjack/webcam | webcam.go | GetControl | func (w *Webcam) GetControl(id ControlID) (int32, error) {
return getControl(w.fd, uint32(id))
} | go | func (w *Webcam) GetControl(id ControlID) (int32, error) {
return getControl(w.fd, uint32(id))
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"GetControl",
"(",
"id",
"ControlID",
")",
"(",
"int32",
",",
"error",
")",
"{",
"return",
"getControl",
"(",
"w",
".",
"fd",
",",
"uint32",
"(",
"id",
")",
")",
"\n",
"}"
] | // Get the value of a control. | [
"Get",
"the",
"value",
"of",
"a",
"control",
"."
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L147-L149 |
3,264 | blackjack/webcam | webcam.go | SetControl | func (w *Webcam) SetControl(id ControlID, value int32) error {
return setControl(w.fd, uint32(id), value)
} | go | func (w *Webcam) SetControl(id ControlID, value int32) error {
return setControl(w.fd, uint32(id), value)
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"SetControl",
"(",
"id",
"ControlID",
",",
"value",
"int32",
")",
"error",
"{",
"return",
"setControl",
"(",
"w",
".",
"fd",
",",
"uint32",
"(",
"id",
")",
",",
"value",
")",
"\n",
"}"
] | // Set a control. | [
"Set",
"a",
"control",
"."
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L152-L154 |
3,265 | blackjack/webcam | webcam.go | StartStreaming | func (w *Webcam) StartStreaming() error {
if w.streaming {
return errors.New("Already streaming")
}
err := mmapRequestBuffers(w.fd, &w.bufcount)
if err != nil {
return errors.New("Failed to map request buffers: " + string(err.Error()))
}
w.buffers = make([][]byte, w.bufcount, w.bufcount)
for index, _ := range w.buffers {
var length uint32
buffer, err := mmapQueryBuffer(w.fd, uint32(index), &length)
if err != nil {
return errors.New("Failed to map memory: " + string(err.Error()))
}
w.buffers[index] = buffer
}
for index, _ := range w.buffers {
err := mmapEnqueueBuffer(w.fd, uint32(index))
if err != nil {
return errors.New("Failed to enqueue buffer: " + string(err.Error()))
}
}
err = startStreaming(w.fd)
if err != nil {
return errors.New("Failed to start streaming: " + string(err.Error()))
}
w.streaming = true
return nil
} | go | func (w *Webcam) StartStreaming() error {
if w.streaming {
return errors.New("Already streaming")
}
err := mmapRequestBuffers(w.fd, &w.bufcount)
if err != nil {
return errors.New("Failed to map request buffers: " + string(err.Error()))
}
w.buffers = make([][]byte, w.bufcount, w.bufcount)
for index, _ := range w.buffers {
var length uint32
buffer, err := mmapQueryBuffer(w.fd, uint32(index), &length)
if err != nil {
return errors.New("Failed to map memory: " + string(err.Error()))
}
w.buffers[index] = buffer
}
for index, _ := range w.buffers {
err := mmapEnqueueBuffer(w.fd, uint32(index))
if err != nil {
return errors.New("Failed to enqueue buffer: " + string(err.Error()))
}
}
err = startStreaming(w.fd)
if err != nil {
return errors.New("Failed to start streaming: " + string(err.Error()))
}
w.streaming = true
return nil
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"StartStreaming",
"(",
")",
"error",
"{",
"if",
"w",
".",
"streaming",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"err",
":=",
"mmapRequestBuffers",
"(",
"w",
".",
"fd",
",",
"&",
"w",
".",
"bufcount",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
"+",
"string",
"(",
"err",
".",
"Error",
"(",
")",
")",
")",
"\n",
"}",
"\n\n",
"w",
".",
"buffers",
"=",
"make",
"(",
"[",
"]",
"[",
"]",
"byte",
",",
"w",
".",
"bufcount",
",",
"w",
".",
"bufcount",
")",
"\n",
"for",
"index",
",",
"_",
":=",
"range",
"w",
".",
"buffers",
"{",
"var",
"length",
"uint32",
"\n\n",
"buffer",
",",
"err",
":=",
"mmapQueryBuffer",
"(",
"w",
".",
"fd",
",",
"uint32",
"(",
"index",
")",
",",
"&",
"length",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
"+",
"string",
"(",
"err",
".",
"Error",
"(",
")",
")",
")",
"\n",
"}",
"\n\n",
"w",
".",
"buffers",
"[",
"index",
"]",
"=",
"buffer",
"\n",
"}",
"\n\n",
"for",
"index",
",",
"_",
":=",
"range",
"w",
".",
"buffers",
"{",
"err",
":=",
"mmapEnqueueBuffer",
"(",
"w",
".",
"fd",
",",
"uint32",
"(",
"index",
")",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
"+",
"string",
"(",
"err",
".",
"Error",
"(",
")",
")",
")",
"\n",
"}",
"\n\n",
"}",
"\n\n",
"err",
"=",
"startStreaming",
"(",
"w",
".",
"fd",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
"+",
"string",
"(",
"err",
".",
"Error",
"(",
")",
")",
")",
"\n",
"}",
"\n",
"w",
".",
"streaming",
"=",
"true",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // Start streaming process | [
"Start",
"streaming",
"process"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L157-L199 |
3,266 | blackjack/webcam | webcam.go | ReadFrame | func (w *Webcam) ReadFrame() ([]byte, error) {
result, index, err := w.GetFrame()
if err == nil {
w.ReleaseFrame(index)
}
return result, err
} | go | func (w *Webcam) ReadFrame() ([]byte, error) {
result, index, err := w.GetFrame()
if err == nil {
w.ReleaseFrame(index)
}
return result, err
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"ReadFrame",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"result",
",",
"index",
",",
"err",
":=",
"w",
".",
"GetFrame",
"(",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"w",
".",
"ReleaseFrame",
"(",
"index",
")",
"\n",
"}",
"\n",
"return",
"result",
",",
"err",
"\n",
"}"
] | // Read a single frame from the webcam
// If frame cannot be read at the moment
// function will return empty slice | [
"Read",
"a",
"single",
"frame",
"from",
"the",
"webcam",
"If",
"frame",
"cannot",
"be",
"read",
"at",
"the",
"moment",
"function",
"will",
"return",
"empty",
"slice"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L204-L210 |
3,267 | blackjack/webcam | webcam.go | GetFrame | func (w *Webcam) GetFrame() ([]byte, uint32, error) {
var index uint32
var length uint32
err := mmapDequeueBuffer(w.fd, &index, &length)
if err != nil {
return nil, 0, err
}
return w.buffers[int(index)][:length], index, nil
} | go | func (w *Webcam) GetFrame() ([]byte, uint32, error) {
var index uint32
var length uint32
err := mmapDequeueBuffer(w.fd, &index, &length)
if err != nil {
return nil, 0, err
}
return w.buffers[int(index)][:length], index, nil
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"GetFrame",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"uint32",
",",
"error",
")",
"{",
"var",
"index",
"uint32",
"\n",
"var",
"length",
"uint32",
"\n\n",
"err",
":=",
"mmapDequeueBuffer",
"(",
"w",
".",
"fd",
",",
"&",
"index",
",",
"&",
"length",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"0",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"w",
".",
"buffers",
"[",
"int",
"(",
"index",
")",
"]",
"[",
":",
"length",
"]",
",",
"index",
",",
"nil",
"\n\n",
"}"
] | // Get a single frame from the webcam and return the frame and
// the buffer index. To return the buffer, ReleaseFrame must be called.
// If frame cannot be read at the moment
// function will return empty slice | [
"Get",
"a",
"single",
"frame",
"from",
"the",
"webcam",
"and",
"return",
"the",
"frame",
"and",
"the",
"buffer",
"index",
".",
"To",
"return",
"the",
"buffer",
"ReleaseFrame",
"must",
"be",
"called",
".",
"If",
"frame",
"cannot",
"be",
"read",
"at",
"the",
"moment",
"function",
"will",
"return",
"empty",
"slice"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L216-L228 |
3,268 | blackjack/webcam | webcam.go | ReleaseFrame | func (w *Webcam) ReleaseFrame(index uint32) error {
return mmapEnqueueBuffer(w.fd, index)
} | go | func (w *Webcam) ReleaseFrame(index uint32) error {
return mmapEnqueueBuffer(w.fd, index)
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"ReleaseFrame",
"(",
"index",
"uint32",
")",
"error",
"{",
"return",
"mmapEnqueueBuffer",
"(",
"w",
".",
"fd",
",",
"index",
")",
"\n",
"}"
] | // Release the frame buffer that was obtained via GetFrame | [
"Release",
"the",
"frame",
"buffer",
"that",
"was",
"obtained",
"via",
"GetFrame"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L231-L233 |
3,269 | blackjack/webcam | webcam.go | WaitForFrame | func (w *Webcam) WaitForFrame(timeout uint32) error {
count, err := waitForFrame(w.fd, timeout)
if count < 0 || err != nil {
return err
} else if count == 0 {
return new(Timeout)
} else {
return nil
}
} | go | func (w *Webcam) WaitForFrame(timeout uint32) error {
count, err := waitForFrame(w.fd, timeout)
if count < 0 || err != nil {
return err
} else if count == 0 {
return new(Timeout)
} else {
return nil
}
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"WaitForFrame",
"(",
"timeout",
"uint32",
")",
"error",
"{",
"count",
",",
"err",
":=",
"waitForFrame",
"(",
"w",
".",
"fd",
",",
"timeout",
")",
"\n\n",
"if",
"count",
"<",
"0",
"||",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"else",
"if",
"count",
"==",
"0",
"{",
"return",
"new",
"(",
"Timeout",
")",
"\n",
"}",
"else",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] | // Wait until frame could be read | [
"Wait",
"until",
"frame",
"could",
"be",
"read"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L236-L247 |
3,270 | blackjack/webcam | webcam.go | Close | func (w *Webcam) Close() error {
if w.streaming {
w.StopStreaming()
}
err := unix.Close(int(w.fd))
return err
} | go | func (w *Webcam) Close() error {
if w.streaming {
w.StopStreaming()
}
err := unix.Close(int(w.fd))
return err
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"Close",
"(",
")",
"error",
"{",
"if",
"w",
".",
"streaming",
"{",
"w",
".",
"StopStreaming",
"(",
")",
"\n",
"}",
"\n\n",
"err",
":=",
"unix",
".",
"Close",
"(",
"int",
"(",
"w",
".",
"fd",
")",
")",
"\n\n",
"return",
"err",
"\n",
"}"
] | // Close the device | [
"Close",
"the",
"device"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L265-L273 |
3,271 | blackjack/webcam | webcam.go | SetAutoWhiteBalance | func (w *Webcam) SetAutoWhiteBalance(val bool) error {
v := int32(0)
if val {
v = 1
}
return setControl(w.fd, V4L2_CID_AUTO_WHITE_BALANCE, v)
} | go | func (w *Webcam) SetAutoWhiteBalance(val bool) error {
v := int32(0)
if val {
v = 1
}
return setControl(w.fd, V4L2_CID_AUTO_WHITE_BALANCE, v)
} | [
"func",
"(",
"w",
"*",
"Webcam",
")",
"SetAutoWhiteBalance",
"(",
"val",
"bool",
")",
"error",
"{",
"v",
":=",
"int32",
"(",
"0",
")",
"\n",
"if",
"val",
"{",
"v",
"=",
"1",
"\n",
"}",
"\n",
"return",
"setControl",
"(",
"w",
".",
"fd",
",",
"V4L2_CID_AUTO_WHITE_BALANCE",
",",
"v",
")",
"\n",
"}"
] | // Sets automatic white balance correction | [
"Sets",
"automatic",
"white",
"balance",
"correction"
] | 6cd3de4f4861b86bac33b0568a1c061b8aebac3c | https://github.com/blackjack/webcam/blob/6cd3de4f4861b86bac33b0568a1c061b8aebac3c/webcam.go#L276-L282 |
3,272 | dveselov/go-libreofficekit | libreofficekit.go | TwipsToPixels | func TwipsToPixels(twips int, dpi int) int {
return int(float32(twips) / 1440.0 * float32(dpi))
} | go | func TwipsToPixels(twips int, dpi int) int {
return int(float32(twips) / 1440.0 * float32(dpi))
} | [
"func",
"TwipsToPixels",
"(",
"twips",
"int",
",",
"dpi",
"int",
")",
"int",
"{",
"return",
"int",
"(",
"float32",
"(",
"twips",
")",
"/",
"1440.0",
"*",
"float32",
"(",
"dpi",
")",
")",
"\n",
"}"
] | // TwipsToPixels converts given twips to pixels with given dpi | [
"TwipsToPixels",
"converts",
"given",
"twips",
"to",
"pixels",
"with",
"given",
"dpi"
] | 2bac0eacb65b9f8e4fe21572308e7dcf625a4275 | https://github.com/dveselov/go-libreofficekit/blob/2bac0eacb65b9f8e4fe21572308e7dcf625a4275/libreofficekit.go#L19-L21 |
3,273 | dveselov/go-libreofficekit | libreofficekit.go | PixelsToTwips | func PixelsToTwips(pixels int, dpi int) int {
return int((float32(pixels) / float32(dpi)) * 1440.0)
} | go | func PixelsToTwips(pixels int, dpi int) int {
return int((float32(pixels) / float32(dpi)) * 1440.0)
} | [
"func",
"PixelsToTwips",
"(",
"pixels",
"int",
",",
"dpi",
"int",
")",
"int",
"{",
"return",
"int",
"(",
"(",
"float32",
"(",
"pixels",
")",
"/",
"float32",
"(",
"dpi",
")",
")",
"*",
"1440.0",
")",
"\n",
"}"
] | // PixelsToTwips is like TwipsToPixels, but to another way | [
"PixelsToTwips",
"is",
"like",
"TwipsToPixels",
"but",
"to",
"another",
"way"
] | 2bac0eacb65b9f8e4fe21572308e7dcf625a4275 | https://github.com/dveselov/go-libreofficekit/blob/2bac0eacb65b9f8e4fe21572308e7dcf625a4275/libreofficekit.go#L24-L26 |
3,274 | dveselov/go-libreofficekit | libreofficekit.go | GetError | func (office *Office) GetError() string {
message := C.get_error(office.handle)
return C.GoString(message)
} | go | func (office *Office) GetError() string {
message := C.get_error(office.handle)
return C.GoString(message)
} | [
"func",
"(",
"office",
"*",
"Office",
")",
"GetError",
"(",
")",
"string",
"{",
"message",
":=",
"C",
".",
"get_error",
"(",
"office",
".",
"handle",
")",
"\n",
"return",
"C",
".",
"GoString",
"(",
"message",
")",
"\n",
"}"
] | // GetError returns last happened error message in human-readable format | [
"GetError",
"returns",
"last",
"happened",
"error",
"message",
"in",
"human",
"-",
"readable",
"format"
] | 2bac0eacb65b9f8e4fe21572308e7dcf625a4275 | https://github.com/dveselov/go-libreofficekit/blob/2bac0eacb65b9f8e4fe21572308e7dcf625a4275/libreofficekit.go#L67-L70 |
3,275 | dveselov/go-libreofficekit | libreofficekit.go | LoadDocument | func (office *Office) LoadDocument(path string) (*Document, error) {
document := new(Document)
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
handle := C.document_load(office.handle, cPath)
if handle == nil {
return nil, fmt.Errorf("Failed to load document")
}
document.handle = handle
return document, nil
} | go | func (office *Office) LoadDocument(path string) (*Document, error) {
document := new(Document)
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
handle := C.document_load(office.handle, cPath)
if handle == nil {
return nil, fmt.Errorf("Failed to load document")
}
document.handle = handle
return document, nil
} | [
"func",
"(",
"office",
"*",
"Office",
")",
"LoadDocument",
"(",
"path",
"string",
")",
"(",
"*",
"Document",
",",
"error",
")",
"{",
"document",
":=",
"new",
"(",
"Document",
")",
"\n",
"cPath",
":=",
"C",
".",
"CString",
"(",
"path",
")",
"\n",
"defer",
"C",
".",
"free",
"(",
"unsafe",
".",
"Pointer",
"(",
"cPath",
")",
")",
"\n",
"handle",
":=",
"C",
".",
"document_load",
"(",
"office",
".",
"handle",
",",
"cPath",
")",
"\n",
"if",
"handle",
"==",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"document",
".",
"handle",
"=",
"handle",
"\n",
"return",
"document",
",",
"nil",
"\n",
"}"
] | // LoadDocument return Document or error, if LibreOffice fails to open document at provided path.
// Actual error message can be retrieved by office.GetError method | [
"LoadDocument",
"return",
"Document",
"or",
"error",
"if",
"LibreOffice",
"fails",
"to",
"open",
"document",
"at",
"provided",
"path",
".",
"Actual",
"error",
"message",
"can",
"be",
"retrieved",
"by",
"office",
".",
"GetError",
"method"
] | 2bac0eacb65b9f8e4fe21572308e7dcf625a4275 | https://github.com/dveselov/go-libreofficekit/blob/2bac0eacb65b9f8e4fe21572308e7dcf625a4275/libreofficekit.go#L74-L84 |
3,276 | dveselov/go-libreofficekit | libreofficekit.go | SetPart | func (document *Document) SetPart(part int) {
C.set_document_part(document.handle, C.int(part))
} | go | func (document *Document) SetPart(part int) {
C.set_document_part(document.handle, C.int(part))
} | [
"func",
"(",
"document",
"*",
"Document",
")",
"SetPart",
"(",
"part",
"int",
")",
"{",
"C",
".",
"set_document_part",
"(",
"document",
".",
"handle",
",",
"C",
".",
"int",
"(",
"part",
")",
")",
"\n",
"}"
] | // SetPart updates current part of document | [
"SetPart",
"updates",
"current",
"part",
"of",
"document"
] | 2bac0eacb65b9f8e4fe21572308e7dcf625a4275 | https://github.com/dveselov/go-libreofficekit/blob/2bac0eacb65b9f8e4fe21572308e7dcf625a4275/libreofficekit.go#L138-L140 |
3,277 | dveselov/go-libreofficekit | libreofficekit.go | InitializeForRendering | func (document *Document) InitializeForRendering(arguments string) {
cArguments := C.CString(arguments)
defer C.free(unsafe.Pointer(cArguments))
C.initialize_for_rendering(document.handle, cArguments)
} | go | func (document *Document) InitializeForRendering(arguments string) {
cArguments := C.CString(arguments)
defer C.free(unsafe.Pointer(cArguments))
C.initialize_for_rendering(document.handle, cArguments)
} | [
"func",
"(",
"document",
"*",
"Document",
")",
"InitializeForRendering",
"(",
"arguments",
"string",
")",
"{",
"cArguments",
":=",
"C",
".",
"CString",
"(",
"arguments",
")",
"\n",
"defer",
"C",
".",
"free",
"(",
"unsafe",
".",
"Pointer",
"(",
"cArguments",
")",
")",
"\n",
"C",
".",
"initialize_for_rendering",
"(",
"document",
".",
"handle",
",",
"cArguments",
")",
"\n",
"}"
] | // InitializeForRendering must be called before performing any rendering-related actions | [
"InitializeForRendering",
"must",
"be",
"called",
"before",
"performing",
"any",
"rendering",
"-",
"related",
"actions"
] | 2bac0eacb65b9f8e4fe21572308e7dcf625a4275 | https://github.com/dveselov/go-libreofficekit/blob/2bac0eacb65b9f8e4fe21572308e7dcf625a4275/libreofficekit.go#L160-L164 |
3,278 | dveselov/go-libreofficekit | libreofficekit.go | GetPartPageRectangles | func (document *Document) GetPartPageRectangles() []image.Rectangle {
var rectangles []image.Rectangle
rawRectangles := C.GoString(C.get_part_page_rectangles(document.handle))
pageRectangles := strings.Split(rawRectangles, ";")
for _, points := range pageRectangles {
var intPoints []int
strPoints := strings.Split(points, ",")
for _, point := range strPoints {
point = strings.Trim(point, " ")
i, _ := strconv.Atoi(point)
intPoints = append(intPoints, i)
}
x0, y0 := intPoints[0], intPoints[1]
x1, y1 := x0+intPoints[2], y0+intPoints[3]
rectangles = append(rectangles, image.Rect(x0, y0, x1, y1))
}
return rectangles
} | go | func (document *Document) GetPartPageRectangles() []image.Rectangle {
var rectangles []image.Rectangle
rawRectangles := C.GoString(C.get_part_page_rectangles(document.handle))
pageRectangles := strings.Split(rawRectangles, ";")
for _, points := range pageRectangles {
var intPoints []int
strPoints := strings.Split(points, ",")
for _, point := range strPoints {
point = strings.Trim(point, " ")
i, _ := strconv.Atoi(point)
intPoints = append(intPoints, i)
}
x0, y0 := intPoints[0], intPoints[1]
x1, y1 := x0+intPoints[2], y0+intPoints[3]
rectangles = append(rectangles, image.Rect(x0, y0, x1, y1))
}
return rectangles
} | [
"func",
"(",
"document",
"*",
"Document",
")",
"GetPartPageRectangles",
"(",
")",
"[",
"]",
"image",
".",
"Rectangle",
"{",
"var",
"rectangles",
"[",
"]",
"image",
".",
"Rectangle",
"\n",
"rawRectangles",
":=",
"C",
".",
"GoString",
"(",
"C",
".",
"get_part_page_rectangles",
"(",
"document",
".",
"handle",
")",
")",
"\n",
"pageRectangles",
":=",
"strings",
".",
"Split",
"(",
"rawRectangles",
",",
"\"",
"\"",
")",
"\n",
"for",
"_",
",",
"points",
":=",
"range",
"pageRectangles",
"{",
"var",
"intPoints",
"[",
"]",
"int",
"\n",
"strPoints",
":=",
"strings",
".",
"Split",
"(",
"points",
",",
"\"",
"\"",
")",
"\n",
"for",
"_",
",",
"point",
":=",
"range",
"strPoints",
"{",
"point",
"=",
"strings",
".",
"Trim",
"(",
"point",
",",
"\"",
"\"",
")",
"\n",
"i",
",",
"_",
":=",
"strconv",
".",
"Atoi",
"(",
"point",
")",
"\n",
"intPoints",
"=",
"append",
"(",
"intPoints",
",",
"i",
")",
"\n",
"}",
"\n",
"x0",
",",
"y0",
":=",
"intPoints",
"[",
"0",
"]",
",",
"intPoints",
"[",
"1",
"]",
"\n",
"x1",
",",
"y1",
":=",
"x0",
"+",
"intPoints",
"[",
"2",
"]",
",",
"y0",
"+",
"intPoints",
"[",
"3",
"]",
"\n",
"rectangles",
"=",
"append",
"(",
"rectangles",
",",
"image",
".",
"Rect",
"(",
"x0",
",",
"y0",
",",
"x1",
",",
"y1",
")",
")",
"\n",
"}",
"\n",
"return",
"rectangles",
"\n",
"}"
] | // GetPartPageRectangles array of image.Rectangle, with actually TextDocument page
// rectangles. Useful, when rendering text document page-by-page. | [
"GetPartPageRectangles",
"array",
"of",
"image",
".",
"Rectangle",
"with",
"actually",
"TextDocument",
"page",
"rectangles",
".",
"Useful",
"when",
"rendering",
"text",
"document",
"page",
"-",
"by",
"-",
"page",
"."
] | 2bac0eacb65b9f8e4fe21572308e7dcf625a4275 | https://github.com/dveselov/go-libreofficekit/blob/2bac0eacb65b9f8e4fe21572308e7dcf625a4275/libreofficekit.go#L224-L241 |
3,279 | armon/go-proxyproto | protocol.go | NewConn | func NewConn(conn net.Conn, timeout time.Duration) *Conn {
pConn := &Conn{
bufReader: bufio.NewReader(conn),
conn: conn,
proxyHeaderTimeout: timeout,
}
return pConn
} | go | func NewConn(conn net.Conn, timeout time.Duration) *Conn {
pConn := &Conn{
bufReader: bufio.NewReader(conn),
conn: conn,
proxyHeaderTimeout: timeout,
}
return pConn
} | [
"func",
"NewConn",
"(",
"conn",
"net",
".",
"Conn",
",",
"timeout",
"time",
".",
"Duration",
")",
"*",
"Conn",
"{",
"pConn",
":=",
"&",
"Conn",
"{",
"bufReader",
":",
"bufio",
".",
"NewReader",
"(",
"conn",
")",
",",
"conn",
":",
"conn",
",",
"proxyHeaderTimeout",
":",
"timeout",
",",
"}",
"\n",
"return",
"pConn",
"\n",
"}"
] | // NewConn is used to wrap a net.Conn that may be speaking
// the proxy protocol into a proxyproto.Conn | [
"NewConn",
"is",
"used",
"to",
"wrap",
"a",
"net",
".",
"Conn",
"that",
"may",
"be",
"speaking",
"the",
"proxy",
"protocol",
"into",
"a",
"proxyproto",
".",
"Conn"
] | 68259f75880e8bcc207a49de82466957646af353 | https://github.com/armon/go-proxyproto/blob/68259f75880e8bcc207a49de82466957646af353/protocol.go#L101-L108 |
3,280 | armon/go-proxyproto | protocol.go | Read | func (p *Conn) Read(b []byte) (int, error) {
var err error
p.once.Do(func() { err = p.checkPrefix() })
if err != nil {
return 0, err
}
return p.bufReader.Read(b)
} | go | func (p *Conn) Read(b []byte) (int, error) {
var err error
p.once.Do(func() { err = p.checkPrefix() })
if err != nil {
return 0, err
}
return p.bufReader.Read(b)
} | [
"func",
"(",
"p",
"*",
"Conn",
")",
"Read",
"(",
"b",
"[",
"]",
"byte",
")",
"(",
"int",
",",
"error",
")",
"{",
"var",
"err",
"error",
"\n",
"p",
".",
"once",
".",
"Do",
"(",
"func",
"(",
")",
"{",
"err",
"=",
"p",
".",
"checkPrefix",
"(",
")",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"err",
"\n",
"}",
"\n",
"return",
"p",
".",
"bufReader",
".",
"Read",
"(",
"b",
")",
"\n",
"}"
] | // Read is check for the proxy protocol header when doing
// the initial scan. If there is an error parsing the header,
// it is returned and the socket is closed. | [
"Read",
"is",
"check",
"for",
"the",
"proxy",
"protocol",
"header",
"when",
"doing",
"the",
"initial",
"scan",
".",
"If",
"there",
"is",
"an",
"error",
"parsing",
"the",
"header",
"it",
"is",
"returned",
"and",
"the",
"socket",
"is",
"closed",
"."
] | 68259f75880e8bcc207a49de82466957646af353 | https://github.com/armon/go-proxyproto/blob/68259f75880e8bcc207a49de82466957646af353/protocol.go#L113-L120 |
3,281 | multiformats/go-multiaddr | varint.go | ReadVarintCode | func ReadVarintCode(buf []byte) (int, int, error) {
num, n := binary.Uvarint(buf)
if n < 0 {
return 0, 0, fmt.Errorf("varints larger than uint64 not yet supported")
}
return int(num), n, nil
} | go | func ReadVarintCode(buf []byte) (int, int, error) {
num, n := binary.Uvarint(buf)
if n < 0 {
return 0, 0, fmt.Errorf("varints larger than uint64 not yet supported")
}
return int(num), n, nil
} | [
"func",
"ReadVarintCode",
"(",
"buf",
"[",
"]",
"byte",
")",
"(",
"int",
",",
"int",
",",
"error",
")",
"{",
"num",
",",
"n",
":=",
"binary",
".",
"Uvarint",
"(",
"buf",
")",
"\n",
"if",
"n",
"<",
"0",
"{",
"return",
"0",
",",
"0",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"int",
"(",
"num",
")",
",",
"n",
",",
"nil",
"\n",
"}"
] | // ReadVarintCode reads a varint code from the beginning of buf.
// returns the code, and the number of bytes read. | [
"ReadVarintCode",
"reads",
"a",
"varint",
"code",
"from",
"the",
"beginning",
"of",
"buf",
".",
"returns",
"the",
"code",
"and",
"the",
"number",
"of",
"bytes",
"read",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/varint.go#L38-L44 |
3,282 | multiformats/go-multiaddr | multiaddr.go | NewMultiaddrBytes | func NewMultiaddrBytes(b []byte) (a Multiaddr, err error) {
defer func() {
if e := recover(); e != nil {
log.Printf("Panic in NewMultiaddrBytes on input %q: %s", b, e)
err = fmt.Errorf("%v", e)
}
}()
if err := validateBytes(b); err != nil {
return nil, err
}
return &multiaddr{bytes: b}, nil
} | go | func NewMultiaddrBytes(b []byte) (a Multiaddr, err error) {
defer func() {
if e := recover(); e != nil {
log.Printf("Panic in NewMultiaddrBytes on input %q: %s", b, e)
err = fmt.Errorf("%v", e)
}
}()
if err := validateBytes(b); err != nil {
return nil, err
}
return &multiaddr{bytes: b}, nil
} | [
"func",
"NewMultiaddrBytes",
"(",
"b",
"[",
"]",
"byte",
")",
"(",
"a",
"Multiaddr",
",",
"err",
"error",
")",
"{",
"defer",
"func",
"(",
")",
"{",
"if",
"e",
":=",
"recover",
"(",
")",
";",
"e",
"!=",
"nil",
"{",
"log",
".",
"Printf",
"(",
"\"",
"\"",
",",
"b",
",",
"e",
")",
"\n",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"e",
")",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n\n",
"if",
"err",
":=",
"validateBytes",
"(",
"b",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"multiaddr",
"{",
"bytes",
":",
"b",
"}",
",",
"nil",
"\n",
"}"
] | // NewMultiaddrBytes initializes a Multiaddr from a byte representation.
// It validates it as an input string. | [
"NewMultiaddrBytes",
"initializes",
"a",
"Multiaddr",
"from",
"a",
"byte",
"representation",
".",
"It",
"validates",
"it",
"as",
"an",
"input",
"string",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/multiaddr.go#L33-L46 |
3,283 | multiformats/go-multiaddr | multiaddr.go | Equal | func (m *multiaddr) Equal(m2 Multiaddr) bool {
return bytes.Equal(m.bytes, m2.Bytes())
} | go | func (m *multiaddr) Equal(m2 Multiaddr) bool {
return bytes.Equal(m.bytes, m2.Bytes())
} | [
"func",
"(",
"m",
"*",
"multiaddr",
")",
"Equal",
"(",
"m2",
"Multiaddr",
")",
"bool",
"{",
"return",
"bytes",
".",
"Equal",
"(",
"m",
".",
"bytes",
",",
"m2",
".",
"Bytes",
"(",
")",
")",
"\n",
"}"
] | // Equal tests whether two multiaddrs are equal | [
"Equal",
"tests",
"whether",
"two",
"multiaddrs",
"are",
"equal"
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/multiaddr.go#L49-L51 |
3,284 | multiformats/go-multiaddr | multiaddr.go | String | func (m *multiaddr) String() string {
s, err := bytesToString(m.bytes)
if err != nil {
panic(fmt.Errorf("multiaddr failed to convert back to string. corrupted? %s", err))
}
return s
} | go | func (m *multiaddr) String() string {
s, err := bytesToString(m.bytes)
if err != nil {
panic(fmt.Errorf("multiaddr failed to convert back to string. corrupted? %s", err))
}
return s
} | [
"func",
"(",
"m",
"*",
"multiaddr",
")",
"String",
"(",
")",
"string",
"{",
"s",
",",
"err",
":=",
"bytesToString",
"(",
"m",
".",
"bytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // String returns the string representation of a Multiaddr | [
"String",
"returns",
"the",
"string",
"representation",
"of",
"a",
"Multiaddr"
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/multiaddr.go#L61-L67 |
3,285 | multiformats/go-multiaddr | multiaddr.go | Protocols | func (m *multiaddr) Protocols() []Protocol {
ps := make([]Protocol, 0, 8)
b := m.bytes
for len(b) > 0 {
code, n, err := ReadVarintCode(b)
if err != nil {
panic(err)
}
p := ProtocolWithCode(code)
if p.Code == 0 {
// this is a panic (and not returning err) because this should've been
// caught on constructing the Multiaddr
panic(fmt.Errorf("no protocol with code %d", b[0]))
}
ps = append(ps, p)
b = b[n:]
n, size, err := sizeForAddr(p, b)
if err != nil {
panic(err)
}
b = b[n+size:]
}
return ps
} | go | func (m *multiaddr) Protocols() []Protocol {
ps := make([]Protocol, 0, 8)
b := m.bytes
for len(b) > 0 {
code, n, err := ReadVarintCode(b)
if err != nil {
panic(err)
}
p := ProtocolWithCode(code)
if p.Code == 0 {
// this is a panic (and not returning err) because this should've been
// caught on constructing the Multiaddr
panic(fmt.Errorf("no protocol with code %d", b[0]))
}
ps = append(ps, p)
b = b[n:]
n, size, err := sizeForAddr(p, b)
if err != nil {
panic(err)
}
b = b[n+size:]
}
return ps
} | [
"func",
"(",
"m",
"*",
"multiaddr",
")",
"Protocols",
"(",
")",
"[",
"]",
"Protocol",
"{",
"ps",
":=",
"make",
"(",
"[",
"]",
"Protocol",
",",
"0",
",",
"8",
")",
"\n",
"b",
":=",
"m",
".",
"bytes",
"\n",
"for",
"len",
"(",
"b",
")",
">",
"0",
"{",
"code",
",",
"n",
",",
"err",
":=",
"ReadVarintCode",
"(",
"b",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"p",
":=",
"ProtocolWithCode",
"(",
"code",
")",
"\n",
"if",
"p",
".",
"Code",
"==",
"0",
"{",
"// this is a panic (and not returning err) because this should've been",
"// caught on constructing the Multiaddr",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"b",
"[",
"0",
"]",
")",
")",
"\n",
"}",
"\n",
"ps",
"=",
"append",
"(",
"ps",
",",
"p",
")",
"\n",
"b",
"=",
"b",
"[",
"n",
":",
"]",
"\n\n",
"n",
",",
"size",
",",
"err",
":=",
"sizeForAddr",
"(",
"p",
",",
"b",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"b",
"=",
"b",
"[",
"n",
"+",
"size",
":",
"]",
"\n",
"}",
"\n",
"return",
"ps",
"\n",
"}"
] | // Protocols returns the list of protocols this Multiaddr has.
// will panic in case we access bytes incorrectly. | [
"Protocols",
"returns",
"the",
"list",
"of",
"protocols",
"this",
"Multiaddr",
"has",
".",
"will",
"panic",
"in",
"case",
"we",
"access",
"bytes",
"incorrectly",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/multiaddr.go#L111-L137 |
3,286 | multiformats/go-multiaddr | multiaddr.go | Encapsulate | func (m *multiaddr) Encapsulate(o Multiaddr) Multiaddr {
mb := m.bytes
ob := o.Bytes()
b := make([]byte, len(mb)+len(ob))
copy(b, mb)
copy(b[len(mb):], ob)
return &multiaddr{bytes: b}
} | go | func (m *multiaddr) Encapsulate(o Multiaddr) Multiaddr {
mb := m.bytes
ob := o.Bytes()
b := make([]byte, len(mb)+len(ob))
copy(b, mb)
copy(b[len(mb):], ob)
return &multiaddr{bytes: b}
} | [
"func",
"(",
"m",
"*",
"multiaddr",
")",
"Encapsulate",
"(",
"o",
"Multiaddr",
")",
"Multiaddr",
"{",
"mb",
":=",
"m",
".",
"bytes",
"\n",
"ob",
":=",
"o",
".",
"Bytes",
"(",
")",
"\n\n",
"b",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"len",
"(",
"mb",
")",
"+",
"len",
"(",
"ob",
")",
")",
"\n",
"copy",
"(",
"b",
",",
"mb",
")",
"\n",
"copy",
"(",
"b",
"[",
"len",
"(",
"mb",
")",
":",
"]",
",",
"ob",
")",
"\n",
"return",
"&",
"multiaddr",
"{",
"bytes",
":",
"b",
"}",
"\n",
"}"
] | // Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr | [
"Encapsulate",
"wraps",
"a",
"given",
"Multiaddr",
"returning",
"the",
"resulting",
"joined",
"Multiaddr"
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/multiaddr.go#L140-L148 |
3,287 | multiformats/go-multiaddr | multiaddr.go | Decapsulate | func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
s1 := m.String()
s2 := o.String()
i := strings.LastIndex(s1, s2)
if i < 0 {
// if multiaddr not contained, returns a copy.
cpy := make([]byte, len(m.bytes))
copy(cpy, m.bytes)
return &multiaddr{bytes: cpy}
}
ma, err := NewMultiaddr(s1[:i])
if err != nil {
panic("Multiaddr.Decapsulate incorrect byte boundaries.")
}
return ma
} | go | func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
s1 := m.String()
s2 := o.String()
i := strings.LastIndex(s1, s2)
if i < 0 {
// if multiaddr not contained, returns a copy.
cpy := make([]byte, len(m.bytes))
copy(cpy, m.bytes)
return &multiaddr{bytes: cpy}
}
ma, err := NewMultiaddr(s1[:i])
if err != nil {
panic("Multiaddr.Decapsulate incorrect byte boundaries.")
}
return ma
} | [
"func",
"(",
"m",
"*",
"multiaddr",
")",
"Decapsulate",
"(",
"o",
"Multiaddr",
")",
"Multiaddr",
"{",
"s1",
":=",
"m",
".",
"String",
"(",
")",
"\n",
"s2",
":=",
"o",
".",
"String",
"(",
")",
"\n",
"i",
":=",
"strings",
".",
"LastIndex",
"(",
"s1",
",",
"s2",
")",
"\n",
"if",
"i",
"<",
"0",
"{",
"// if multiaddr not contained, returns a copy.",
"cpy",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"len",
"(",
"m",
".",
"bytes",
")",
")",
"\n",
"copy",
"(",
"cpy",
",",
"m",
".",
"bytes",
")",
"\n",
"return",
"&",
"multiaddr",
"{",
"bytes",
":",
"cpy",
"}",
"\n",
"}",
"\n\n",
"ma",
",",
"err",
":=",
"NewMultiaddr",
"(",
"s1",
"[",
":",
"i",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"ma",
"\n",
"}"
] | // Decapsulate unwraps Multiaddr up until the given Multiaddr is found. | [
"Decapsulate",
"unwraps",
"Multiaddr",
"up",
"until",
"the",
"given",
"Multiaddr",
"is",
"found",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/multiaddr.go#L151-L167 |
3,288 | multiformats/go-multiaddr | util.go | Split | func Split(m Multiaddr) []Multiaddr {
if _, ok := m.(*Component); ok {
return []Multiaddr{m}
}
var addrs []Multiaddr
ForEach(m, func(c Component) bool {
addrs = append(addrs, &c)
return true
})
return addrs
} | go | func Split(m Multiaddr) []Multiaddr {
if _, ok := m.(*Component); ok {
return []Multiaddr{m}
}
var addrs []Multiaddr
ForEach(m, func(c Component) bool {
addrs = append(addrs, &c)
return true
})
return addrs
} | [
"func",
"Split",
"(",
"m",
"Multiaddr",
")",
"[",
"]",
"Multiaddr",
"{",
"if",
"_",
",",
"ok",
":=",
"m",
".",
"(",
"*",
"Component",
")",
";",
"ok",
"{",
"return",
"[",
"]",
"Multiaddr",
"{",
"m",
"}",
"\n",
"}",
"\n",
"var",
"addrs",
"[",
"]",
"Multiaddr",
"\n",
"ForEach",
"(",
"m",
",",
"func",
"(",
"c",
"Component",
")",
"bool",
"{",
"addrs",
"=",
"append",
"(",
"addrs",
",",
"&",
"c",
")",
"\n",
"return",
"true",
"\n",
"}",
")",
"\n",
"return",
"addrs",
"\n",
"}"
] | // Split returns the sub-address portions of a multiaddr. | [
"Split",
"returns",
"the",
"sub",
"-",
"address",
"portions",
"of",
"a",
"multiaddr",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/util.go#L6-L16 |
3,289 | multiformats/go-multiaddr | util.go | Join | func Join(ms ...Multiaddr) Multiaddr {
switch len(ms) {
case 0:
// empty multiaddr, unfortunately, we have callers that rely on
// this contract.
return &multiaddr{}
case 1:
return ms[0]
}
length := 0
bs := make([][]byte, len(ms))
for i, m := range ms {
bs[i] = m.Bytes()
length += len(bs[i])
}
bidx := 0
b := make([]byte, length)
for _, mb := range bs {
bidx += copy(b[bidx:], mb)
}
return &multiaddr{bytes: b}
} | go | func Join(ms ...Multiaddr) Multiaddr {
switch len(ms) {
case 0:
// empty multiaddr, unfortunately, we have callers that rely on
// this contract.
return &multiaddr{}
case 1:
return ms[0]
}
length := 0
bs := make([][]byte, len(ms))
for i, m := range ms {
bs[i] = m.Bytes()
length += len(bs[i])
}
bidx := 0
b := make([]byte, length)
for _, mb := range bs {
bidx += copy(b[bidx:], mb)
}
return &multiaddr{bytes: b}
} | [
"func",
"Join",
"(",
"ms",
"...",
"Multiaddr",
")",
"Multiaddr",
"{",
"switch",
"len",
"(",
"ms",
")",
"{",
"case",
"0",
":",
"// empty multiaddr, unfortunately, we have callers that rely on",
"// this contract.",
"return",
"&",
"multiaddr",
"{",
"}",
"\n",
"case",
"1",
":",
"return",
"ms",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"length",
":=",
"0",
"\n",
"bs",
":=",
"make",
"(",
"[",
"]",
"[",
"]",
"byte",
",",
"len",
"(",
"ms",
")",
")",
"\n",
"for",
"i",
",",
"m",
":=",
"range",
"ms",
"{",
"bs",
"[",
"i",
"]",
"=",
"m",
".",
"Bytes",
"(",
")",
"\n",
"length",
"+=",
"len",
"(",
"bs",
"[",
"i",
"]",
")",
"\n",
"}",
"\n\n",
"bidx",
":=",
"0",
"\n",
"b",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"length",
")",
"\n",
"for",
"_",
",",
"mb",
":=",
"range",
"bs",
"{",
"bidx",
"+=",
"copy",
"(",
"b",
"[",
"bidx",
":",
"]",
",",
"mb",
")",
"\n",
"}",
"\n",
"return",
"&",
"multiaddr",
"{",
"bytes",
":",
"b",
"}",
"\n",
"}"
] | // Join returns a combination of addresses. | [
"Join",
"returns",
"a",
"combination",
"of",
"addresses",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/util.go#L19-L42 |
3,290 | multiformats/go-multiaddr | util.go | Cast | func Cast(b []byte) Multiaddr {
m, err := NewMultiaddrBytes(b)
if err != nil {
panic(fmt.Errorf("multiaddr failed to parse: %s", err))
}
return m
} | go | func Cast(b []byte) Multiaddr {
m, err := NewMultiaddrBytes(b)
if err != nil {
panic(fmt.Errorf("multiaddr failed to parse: %s", err))
}
return m
} | [
"func",
"Cast",
"(",
"b",
"[",
"]",
"byte",
")",
"Multiaddr",
"{",
"m",
",",
"err",
":=",
"NewMultiaddrBytes",
"(",
"b",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"return",
"m",
"\n",
"}"
] | // Cast re-casts a byte slice as a multiaddr. will panic if it fails to parse. | [
"Cast",
"re",
"-",
"casts",
"a",
"byte",
"slice",
"as",
"a",
"multiaddr",
".",
"will",
"panic",
"if",
"it",
"fails",
"to",
"parse",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/util.go#L45-L51 |
3,291 | multiformats/go-multiaddr | util.go | StringCast | func StringCast(s string) Multiaddr {
m, err := NewMultiaddr(s)
if err != nil {
panic(fmt.Errorf("multiaddr failed to parse: %s", err))
}
return m
} | go | func StringCast(s string) Multiaddr {
m, err := NewMultiaddr(s)
if err != nil {
panic(fmt.Errorf("multiaddr failed to parse: %s", err))
}
return m
} | [
"func",
"StringCast",
"(",
"s",
"string",
")",
"Multiaddr",
"{",
"m",
",",
"err",
":=",
"NewMultiaddr",
"(",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"return",
"m",
"\n",
"}"
] | // StringCast like Cast, but parses a string. Will also panic if it fails to parse. | [
"StringCast",
"like",
"Cast",
"but",
"parses",
"a",
"string",
".",
"Will",
"also",
"panic",
"if",
"it",
"fails",
"to",
"parse",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/util.go#L54-L60 |
3,292 | multiformats/go-multiaddr | util.go | SplitFirst | func SplitFirst(m Multiaddr) (*Component, Multiaddr) {
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
return c, nil
}
b := m.Bytes()
if len(b) == 0 {
return nil, nil
}
n, c, err := readComponent(b)
if err != nil {
panic(err)
}
if len(b) == n {
return &c, nil
}
return &c, &multiaddr{b[n:]}
} | go | func SplitFirst(m Multiaddr) (*Component, Multiaddr) {
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
return c, nil
}
b := m.Bytes()
if len(b) == 0 {
return nil, nil
}
n, c, err := readComponent(b)
if err != nil {
panic(err)
}
if len(b) == n {
return &c, nil
}
return &c, &multiaddr{b[n:]}
} | [
"func",
"SplitFirst",
"(",
"m",
"Multiaddr",
")",
"(",
"*",
"Component",
",",
"Multiaddr",
")",
"{",
"// Shortcut if we already have a component",
"if",
"c",
",",
"ok",
":=",
"m",
".",
"(",
"*",
"Component",
")",
";",
"ok",
"{",
"return",
"c",
",",
"nil",
"\n",
"}",
"\n\n",
"b",
":=",
"m",
".",
"Bytes",
"(",
")",
"\n",
"if",
"len",
"(",
"b",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n",
"n",
",",
"c",
",",
"err",
":=",
"readComponent",
"(",
"b",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"b",
")",
"==",
"n",
"{",
"return",
"&",
"c",
",",
"nil",
"\n",
"}",
"\n",
"return",
"&",
"c",
",",
"&",
"multiaddr",
"{",
"b",
"[",
"n",
":",
"]",
"}",
"\n",
"}"
] | // SplitFirst returns the first component and the rest of the multiaddr. | [
"SplitFirst",
"returns",
"the",
"first",
"component",
"and",
"the",
"rest",
"of",
"the",
"multiaddr",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/util.go#L63-L81 |
3,293 | multiformats/go-multiaddr | util.go | SplitLast | func SplitLast(m Multiaddr) (Multiaddr, *Component) {
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
return nil, c
}
b := m.Bytes()
if len(b) == 0 {
return nil, nil
}
var (
c Component
err error
offset int
)
for {
var n int
n, c, err = readComponent(b[offset:])
if err != nil {
panic(err)
}
if len(b) == n+offset {
// Reached end
if offset == 0 {
// Only one component
return nil, &c
}
return &multiaddr{b[:offset]}, &c
}
offset += n
}
} | go | func SplitLast(m Multiaddr) (Multiaddr, *Component) {
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
return nil, c
}
b := m.Bytes()
if len(b) == 0 {
return nil, nil
}
var (
c Component
err error
offset int
)
for {
var n int
n, c, err = readComponent(b[offset:])
if err != nil {
panic(err)
}
if len(b) == n+offset {
// Reached end
if offset == 0 {
// Only one component
return nil, &c
}
return &multiaddr{b[:offset]}, &c
}
offset += n
}
} | [
"func",
"SplitLast",
"(",
"m",
"Multiaddr",
")",
"(",
"Multiaddr",
",",
"*",
"Component",
")",
"{",
"// Shortcut if we already have a component",
"if",
"c",
",",
"ok",
":=",
"m",
".",
"(",
"*",
"Component",
")",
";",
"ok",
"{",
"return",
"nil",
",",
"c",
"\n",
"}",
"\n\n",
"b",
":=",
"m",
".",
"Bytes",
"(",
")",
"\n",
"if",
"len",
"(",
"b",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n\n",
"var",
"(",
"c",
"Component",
"\n",
"err",
"error",
"\n",
"offset",
"int",
"\n",
")",
"\n",
"for",
"{",
"var",
"n",
"int",
"\n",
"n",
",",
"c",
",",
"err",
"=",
"readComponent",
"(",
"b",
"[",
"offset",
":",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"b",
")",
"==",
"n",
"+",
"offset",
"{",
"// Reached end",
"if",
"offset",
"==",
"0",
"{",
"// Only one component",
"return",
"nil",
",",
"&",
"c",
"\n",
"}",
"\n",
"return",
"&",
"multiaddr",
"{",
"b",
"[",
":",
"offset",
"]",
"}",
",",
"&",
"c",
"\n",
"}",
"\n",
"offset",
"+=",
"n",
"\n",
"}",
"\n",
"}"
] | // SplitLast returns the rest of the multiaddr and the last component. | [
"SplitLast",
"returns",
"the",
"rest",
"of",
"the",
"multiaddr",
"and",
"the",
"last",
"component",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/util.go#L84-L116 |
3,294 | multiformats/go-multiaddr | protocol.go | ProtocolsWithString | func ProtocolsWithString(s string) ([]Protocol, error) {
s = strings.Trim(s, "/")
sp := strings.Split(s, "/")
if len(sp) == 0 {
return nil, nil
}
t := make([]Protocol, len(sp))
for i, name := range sp {
p := ProtocolWithName(name)
if p.Code == 0 {
return nil, fmt.Errorf("no protocol with name: %s", name)
}
t[i] = p
}
return t, nil
} | go | func ProtocolsWithString(s string) ([]Protocol, error) {
s = strings.Trim(s, "/")
sp := strings.Split(s, "/")
if len(sp) == 0 {
return nil, nil
}
t := make([]Protocol, len(sp))
for i, name := range sp {
p := ProtocolWithName(name)
if p.Code == 0 {
return nil, fmt.Errorf("no protocol with name: %s", name)
}
t[i] = p
}
return t, nil
} | [
"func",
"ProtocolsWithString",
"(",
"s",
"string",
")",
"(",
"[",
"]",
"Protocol",
",",
"error",
")",
"{",
"s",
"=",
"strings",
".",
"Trim",
"(",
"s",
",",
"\"",
"\"",
")",
"\n",
"sp",
":=",
"strings",
".",
"Split",
"(",
"s",
",",
"\"",
"\"",
")",
"\n",
"if",
"len",
"(",
"sp",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n\n",
"t",
":=",
"make",
"(",
"[",
"]",
"Protocol",
",",
"len",
"(",
"sp",
")",
")",
"\n",
"for",
"i",
",",
"name",
":=",
"range",
"sp",
"{",
"p",
":=",
"ProtocolWithName",
"(",
"name",
")",
"\n",
"if",
"p",
".",
"Code",
"==",
"0",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"name",
")",
"\n",
"}",
"\n",
"t",
"[",
"i",
"]",
"=",
"p",
"\n",
"}",
"\n",
"return",
"t",
",",
"nil",
"\n",
"}"
] | // ProtocolsWithString returns a slice of protocols matching given string. | [
"ProtocolsWithString",
"returns",
"a",
"slice",
"of",
"protocols",
"matching",
"given",
"string",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/protocol.go#L113-L129 |
3,295 | multiformats/go-multiaddr | component.go | writeTo | func (c *Component) writeTo(b *strings.Builder) {
b.WriteByte('/')
b.WriteString(c.protocol.Name)
value := c.Value()
if len(value) == 0 {
return
}
if !(c.protocol.Path && value[0] == '/') {
b.WriteByte('/')
}
b.WriteString(value)
} | go | func (c *Component) writeTo(b *strings.Builder) {
b.WriteByte('/')
b.WriteString(c.protocol.Name)
value := c.Value()
if len(value) == 0 {
return
}
if !(c.protocol.Path && value[0] == '/') {
b.WriteByte('/')
}
b.WriteString(value)
} | [
"func",
"(",
"c",
"*",
"Component",
")",
"writeTo",
"(",
"b",
"*",
"strings",
".",
"Builder",
")",
"{",
"b",
".",
"WriteByte",
"(",
"'/'",
")",
"\n",
"b",
".",
"WriteString",
"(",
"c",
".",
"protocol",
".",
"Name",
")",
"\n",
"value",
":=",
"c",
".",
"Value",
"(",
")",
"\n",
"if",
"len",
"(",
"value",
")",
"==",
"0",
"{",
"return",
"\n",
"}",
"\n",
"if",
"!",
"(",
"c",
".",
"protocol",
".",
"Path",
"&&",
"value",
"[",
"0",
"]",
"==",
"'/'",
")",
"{",
"b",
".",
"WriteByte",
"(",
"'/'",
")",
"\n",
"}",
"\n",
"b",
".",
"WriteString",
"(",
"value",
")",
"\n",
"}"
] | // writeTo is an efficient, private function for string-formatting a multiaddr.
// Trust me, we tend to allocate a lot when doing this. | [
"writeTo",
"is",
"an",
"efficient",
"private",
"function",
"for",
"string",
"-",
"formatting",
"a",
"multiaddr",
".",
"Trust",
"me",
"we",
"tend",
"to",
"allocate",
"a",
"lot",
"when",
"doing",
"this",
"."
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/component.go#L125-L136 |
3,296 | multiformats/go-multiaddr | component.go | NewComponent | func NewComponent(protocol, value string) (*Component, error) {
p := ProtocolWithName(protocol)
if p.Code == 0 {
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
}
if p.Transcoder != nil {
bts, err := p.Transcoder.StringToBytes(value)
if err != nil {
return nil, err
}
return newComponent(p, bts), nil
} else if value != "" {
return nil, fmt.Errorf("protocol %s doesn't take a value", p.Name)
}
return newComponent(p, nil), nil
// TODO: handle path /?
} | go | func NewComponent(protocol, value string) (*Component, error) {
p := ProtocolWithName(protocol)
if p.Code == 0 {
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
}
if p.Transcoder != nil {
bts, err := p.Transcoder.StringToBytes(value)
if err != nil {
return nil, err
}
return newComponent(p, bts), nil
} else if value != "" {
return nil, fmt.Errorf("protocol %s doesn't take a value", p.Name)
}
return newComponent(p, nil), nil
// TODO: handle path /?
} | [
"func",
"NewComponent",
"(",
"protocol",
",",
"value",
"string",
")",
"(",
"*",
"Component",
",",
"error",
")",
"{",
"p",
":=",
"ProtocolWithName",
"(",
"protocol",
")",
"\n",
"if",
"p",
".",
"Code",
"==",
"0",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"protocol",
")",
"\n",
"}",
"\n",
"if",
"p",
".",
"Transcoder",
"!=",
"nil",
"{",
"bts",
",",
"err",
":=",
"p",
".",
"Transcoder",
".",
"StringToBytes",
"(",
"value",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"newComponent",
"(",
"p",
",",
"bts",
")",
",",
"nil",
"\n",
"}",
"else",
"if",
"value",
"!=",
"\"",
"\"",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"p",
".",
"Name",
")",
"\n",
"}",
"\n",
"return",
"newComponent",
"(",
"p",
",",
"nil",
")",
",",
"nil",
"\n",
"// TODO: handle path /?",
"}"
] | // NewComponent constructs a new multiaddr component | [
"NewComponent",
"constructs",
"a",
"new",
"multiaddr",
"component"
] | e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee | https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/component.go#L139-L155 |
3,297 | sebdah/goldie | goldie.go | compare | func compare(name string, data interface{}, actualData []byte) error {
expectedDataTmpl, err := ioutil.ReadFile(goldenFileName(name))
if err != nil {
if os.IsNotExist(err) {
return newErrFixtureNotFound()
}
return fmt.Errorf("Expected %s to be nil", err.Error())
}
tmpl, err := template.New("test").Parse(string(expectedDataTmpl))
if err != nil {
return fmt.Errorf("Expected %s to be nil", err.Error())
}
var expectedData bytes.Buffer
err = tmpl.Execute(&expectedData, data)
if err != nil {
return fmt.Errorf("Expected %s to be nil", err.Error())
}
if !bytes.Equal(actualData, expectedData.Bytes()) {
return newErrFixtureMismatch(
fmt.Sprintf("Result did not match the golden fixture.\n"+
"Expected: %s\n"+
"Got: %s",
string(expectedData.Bytes()),
string(actualData)))
}
return nil
} | go | func compare(name string, data interface{}, actualData []byte) error {
expectedDataTmpl, err := ioutil.ReadFile(goldenFileName(name))
if err != nil {
if os.IsNotExist(err) {
return newErrFixtureNotFound()
}
return fmt.Errorf("Expected %s to be nil", err.Error())
}
tmpl, err := template.New("test").Parse(string(expectedDataTmpl))
if err != nil {
return fmt.Errorf("Expected %s to be nil", err.Error())
}
var expectedData bytes.Buffer
err = tmpl.Execute(&expectedData, data)
if err != nil {
return fmt.Errorf("Expected %s to be nil", err.Error())
}
if !bytes.Equal(actualData, expectedData.Bytes()) {
return newErrFixtureMismatch(
fmt.Sprintf("Result did not match the golden fixture.\n"+
"Expected: %s\n"+
"Got: %s",
string(expectedData.Bytes()),
string(actualData)))
}
return nil
} | [
"func",
"compare",
"(",
"name",
"string",
",",
"data",
"interface",
"{",
"}",
",",
"actualData",
"[",
"]",
"byte",
")",
"error",
"{",
"expectedDataTmpl",
",",
"err",
":=",
"ioutil",
".",
"ReadFile",
"(",
"goldenFileName",
"(",
"name",
")",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"os",
".",
"IsNotExist",
"(",
"err",
")",
"{",
"return",
"newErrFixtureNotFound",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n\n",
"tmpl",
",",
"err",
":=",
"template",
".",
"New",
"(",
"\"",
"\"",
")",
".",
"Parse",
"(",
"string",
"(",
"expectedDataTmpl",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n\n",
"var",
"expectedData",
"bytes",
".",
"Buffer",
"\n",
"err",
"=",
"tmpl",
".",
"Execute",
"(",
"&",
"expectedData",
",",
"data",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"bytes",
".",
"Equal",
"(",
"actualData",
",",
"expectedData",
".",
"Bytes",
"(",
")",
")",
"{",
"return",
"newErrFixtureMismatch",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\\n",
"\"",
"+",
"\"",
"\\n",
"\"",
"+",
"\"",
"\"",
",",
"string",
"(",
"expectedData",
".",
"Bytes",
"(",
")",
")",
",",
"string",
"(",
"actualData",
")",
")",
")",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // compare is reading the golden fixture file and compare the stored data with
// the actual data. | [
"compare",
"is",
"reading",
"the",
"golden",
"fixture",
"file",
"and",
"compare",
"the",
"stored",
"data",
"with",
"the",
"actual",
"data",
"."
] | 629351c67c535d23e79162543d4ed21e32449d68 | https://github.com/sebdah/goldie/blob/629351c67c535d23e79162543d4ed21e32449d68/goldie.go#L101-L133 |
3,298 | sebdah/goldie | goldie.go | ensureDir | func ensureDir(loc string) error {
s, err := os.Stat(loc)
switch {
case err != nil && os.IsNotExist(err):
// the location does not exist, so make directories to there
return os.MkdirAll(loc, DirPerms)
case err == nil && !s.IsDir():
return newErrFixtureDirectoryIsFile(loc)
}
return err
} | go | func ensureDir(loc string) error {
s, err := os.Stat(loc)
switch {
case err != nil && os.IsNotExist(err):
// the location does not exist, so make directories to there
return os.MkdirAll(loc, DirPerms)
case err == nil && !s.IsDir():
return newErrFixtureDirectoryIsFile(loc)
}
return err
} | [
"func",
"ensureDir",
"(",
"loc",
"string",
")",
"error",
"{",
"s",
",",
"err",
":=",
"os",
".",
"Stat",
"(",
"loc",
")",
"\n",
"switch",
"{",
"case",
"err",
"!=",
"nil",
"&&",
"os",
".",
"IsNotExist",
"(",
"err",
")",
":",
"// the location does not exist, so make directories to there",
"return",
"os",
".",
"MkdirAll",
"(",
"loc",
",",
"DirPerms",
")",
"\n",
"case",
"err",
"==",
"nil",
"&&",
"!",
"s",
".",
"IsDir",
"(",
")",
":",
"return",
"newErrFixtureDirectoryIsFile",
"(",
"loc",
")",
"\n",
"}",
"\n\n",
"return",
"err",
"\n",
"}"
] | // ensureDir will create the fixture folder if it does not already exist. | [
"ensureDir",
"will",
"create",
"the",
"fixture",
"folder",
"if",
"it",
"does",
"not",
"already",
"exist",
"."
] | 629351c67c535d23e79162543d4ed21e32449d68 | https://github.com/sebdah/goldie/blob/629351c67c535d23e79162543d4ed21e32449d68/goldie.go#L136-L147 |
3,299 | sebdah/goldie | goldie.go | goldenFileName | func goldenFileName(name string) string {
return filepath.Join(FixtureDir, fmt.Sprintf("%s%s", name, FileNameSuffix))
} | go | func goldenFileName(name string) string {
return filepath.Join(FixtureDir, fmt.Sprintf("%s%s", name, FileNameSuffix))
} | [
"func",
"goldenFileName",
"(",
"name",
"string",
")",
"string",
"{",
"return",
"filepath",
".",
"Join",
"(",
"FixtureDir",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"name",
",",
"FileNameSuffix",
")",
")",
"\n",
"}"
] | // goldenFileName simply returns the file name of the golden file fixture. | [
"goldenFileName",
"simply",
"returns",
"the",
"file",
"name",
"of",
"the",
"golden",
"file",
"fixture",
"."
] | 629351c67c535d23e79162543d4ed21e32449d68 | https://github.com/sebdah/goldie/blob/629351c67c535d23e79162543d4ed21e32449d68/goldie.go#L150-L152 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.