repo
stringlengths 5
67
| path
stringlengths 4
218
| func_name
stringlengths 0
151
| original_string
stringlengths 52
373k
| language
stringclasses 6
values | code
stringlengths 52
373k
| code_tokens
listlengths 10
512
| docstring
stringlengths 3
47.2k
| docstring_tokens
listlengths 3
234
| sha
stringlengths 40
40
| url
stringlengths 85
339
| partition
stringclasses 3
values |
|---|---|---|---|---|---|---|---|---|---|---|---|
naoina/genmai
|
genmai.go
|
Join
|
func (db *DB) Join(table interface{}) *JoinCondition {
return (&JoinCondition{db: db}).Join(table)
}
|
go
|
func (db *DB) Join(table interface{}) *JoinCondition {
return (&JoinCondition{db: db}).Join(table)
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Join",
"(",
"table",
"interface",
"{",
"}",
")",
"*",
"JoinCondition",
"{",
"return",
"(",
"&",
"JoinCondition",
"{",
"db",
":",
"db",
"}",
")",
".",
"Join",
"(",
"table",
")",
"\n",
"}"
] |
// Join returns a new JoinCondition of "JOIN" clause.
|
[
"Join",
"returns",
"a",
"new",
"JoinCondition",
"of",
"JOIN",
"clause",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L162-L164
|
test
|
naoina/genmai
|
genmai.go
|
Count
|
func (db *DB) Count(column ...interface{}) *Function {
switch len(column) {
case 0, 1:
// do nothing.
default:
panic(fmt.Errorf("Count: a number of argument must be 0 or 1, got %v", len(column)))
}
return &Function{
Name: "COUNT",
Args: column,
}
}
|
go
|
func (db *DB) Count(column ...interface{}) *Function {
switch len(column) {
case 0, 1:
// do nothing.
default:
panic(fmt.Errorf("Count: a number of argument must be 0 or 1, got %v", len(column)))
}
return &Function{
Name: "COUNT",
Args: column,
}
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Count",
"(",
"column",
"...",
"interface",
"{",
"}",
")",
"*",
"Function",
"{",
"switch",
"len",
"(",
"column",
")",
"{",
"case",
"0",
",",
"1",
":",
"default",
":",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"Count: a number of argument must be 0 or 1, got %v\"",
",",
"len",
"(",
"column",
")",
")",
")",
"\n",
"}",
"\n",
"return",
"&",
"Function",
"{",
"Name",
":",
"\"COUNT\"",
",",
"Args",
":",
"column",
",",
"}",
"\n",
"}"
] |
// Count returns "COUNT" function.
|
[
"Count",
"returns",
"COUNT",
"function",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L171-L182
|
test
|
naoina/genmai
|
genmai.go
|
Update
|
func (db *DB) Update(obj interface{}) (affected int64, err error) {
rv, rtype, tableName, err := db.tableValueOf("Update", obj)
if err != nil {
return -1, err
}
if hook, ok := obj.(BeforeUpdater); ok {
if err := hook.BeforeUpdate(); err != nil {
return -1, err
}
}
fieldIndexes := db.collectFieldIndexes(rtype, nil)
pkIdx := db.findPKIndex(rtype, nil)
if len(pkIdx) < 1 {
return -1, fmt.Errorf(`Update: fields of struct doesn't have primary key: "pk" struct tag must be specified for update`)
}
sets := make([]string, len(fieldIndexes))
var args []interface{}
for i, index := range fieldIndexes {
col := db.columnFromTag(rtype.FieldByIndex(index))
sets[i] = fmt.Sprintf("%s = %s", db.dialect.Quote(col), db.dialect.PlaceHolder(i))
args = append(args, rv.FieldByIndex(index).Interface())
}
query := fmt.Sprintf("UPDATE %s SET %s WHERE %s = %s",
db.dialect.Quote(tableName),
strings.Join(sets, ", "),
db.dialect.Quote(db.columnFromTag(rtype.FieldByIndex(pkIdx))),
db.dialect.PlaceHolder(len(fieldIndexes)))
args = append(args, rv.FieldByIndex(pkIdx).Interface())
stmt, err := db.prepare(query, args...)
if err != nil {
return -1, err
}
defer stmt.Close()
result, err := stmt.Exec(args...)
if err != nil {
return -1, err
}
affected, _ = result.RowsAffected()
if hook, ok := obj.(AfterUpdater); ok {
if err := hook.AfterUpdate(); err != nil {
return affected, err
}
}
return affected, nil
}
|
go
|
func (db *DB) Update(obj interface{}) (affected int64, err error) {
rv, rtype, tableName, err := db.tableValueOf("Update", obj)
if err != nil {
return -1, err
}
if hook, ok := obj.(BeforeUpdater); ok {
if err := hook.BeforeUpdate(); err != nil {
return -1, err
}
}
fieldIndexes := db.collectFieldIndexes(rtype, nil)
pkIdx := db.findPKIndex(rtype, nil)
if len(pkIdx) < 1 {
return -1, fmt.Errorf(`Update: fields of struct doesn't have primary key: "pk" struct tag must be specified for update`)
}
sets := make([]string, len(fieldIndexes))
var args []interface{}
for i, index := range fieldIndexes {
col := db.columnFromTag(rtype.FieldByIndex(index))
sets[i] = fmt.Sprintf("%s = %s", db.dialect.Quote(col), db.dialect.PlaceHolder(i))
args = append(args, rv.FieldByIndex(index).Interface())
}
query := fmt.Sprintf("UPDATE %s SET %s WHERE %s = %s",
db.dialect.Quote(tableName),
strings.Join(sets, ", "),
db.dialect.Quote(db.columnFromTag(rtype.FieldByIndex(pkIdx))),
db.dialect.PlaceHolder(len(fieldIndexes)))
args = append(args, rv.FieldByIndex(pkIdx).Interface())
stmt, err := db.prepare(query, args...)
if err != nil {
return -1, err
}
defer stmt.Close()
result, err := stmt.Exec(args...)
if err != nil {
return -1, err
}
affected, _ = result.RowsAffected()
if hook, ok := obj.(AfterUpdater); ok {
if err := hook.AfterUpdate(); err != nil {
return affected, err
}
}
return affected, nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Update",
"(",
"obj",
"interface",
"{",
"}",
")",
"(",
"affected",
"int64",
",",
"err",
"error",
")",
"{",
"rv",
",",
"rtype",
",",
"tableName",
",",
"err",
":=",
"db",
".",
"tableValueOf",
"(",
"\"Update\"",
",",
"obj",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"if",
"hook",
",",
"ok",
":=",
"obj",
".",
"(",
"BeforeUpdater",
")",
";",
"ok",
"{",
"if",
"err",
":=",
"hook",
".",
"BeforeUpdate",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"fieldIndexes",
":=",
"db",
".",
"collectFieldIndexes",
"(",
"rtype",
",",
"nil",
")",
"\n",
"pkIdx",
":=",
"db",
".",
"findPKIndex",
"(",
"rtype",
",",
"nil",
")",
"\n",
"if",
"len",
"(",
"pkIdx",
")",
"<",
"1",
"{",
"return",
"-",
"1",
",",
"fmt",
".",
"Errorf",
"(",
"`Update: fields of struct doesn't have primary key: \"pk\" struct tag must be specified for update`",
")",
"\n",
"}",
"\n",
"sets",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"fieldIndexes",
")",
")",
"\n",
"var",
"args",
"[",
"]",
"interface",
"{",
"}",
"\n",
"for",
"i",
",",
"index",
":=",
"range",
"fieldIndexes",
"{",
"col",
":=",
"db",
".",
"columnFromTag",
"(",
"rtype",
".",
"FieldByIndex",
"(",
"index",
")",
")",
"\n",
"sets",
"[",
"i",
"]",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"%s = %s\"",
",",
"db",
".",
"dialect",
".",
"Quote",
"(",
"col",
")",
",",
"db",
".",
"dialect",
".",
"PlaceHolder",
"(",
"i",
")",
")",
"\n",
"args",
"=",
"append",
"(",
"args",
",",
"rv",
".",
"FieldByIndex",
"(",
"index",
")",
".",
"Interface",
"(",
")",
")",
"\n",
"}",
"\n",
"query",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"UPDATE %s SET %s WHERE %s = %s\"",
",",
"db",
".",
"dialect",
".",
"Quote",
"(",
"tableName",
")",
",",
"strings",
".",
"Join",
"(",
"sets",
",",
"\", \"",
")",
",",
"db",
".",
"dialect",
".",
"Quote",
"(",
"db",
".",
"columnFromTag",
"(",
"rtype",
".",
"FieldByIndex",
"(",
"pkIdx",
")",
")",
")",
",",
"db",
".",
"dialect",
".",
"PlaceHolder",
"(",
"len",
"(",
"fieldIndexes",
")",
")",
")",
"\n",
"args",
"=",
"append",
"(",
"args",
",",
"rv",
".",
"FieldByIndex",
"(",
"pkIdx",
")",
".",
"Interface",
"(",
")",
")",
"\n",
"stmt",
",",
"err",
":=",
"db",
".",
"prepare",
"(",
"query",
",",
"args",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"defer",
"stmt",
".",
"Close",
"(",
")",
"\n",
"result",
",",
"err",
":=",
"stmt",
".",
"Exec",
"(",
"args",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"affected",
",",
"_",
"=",
"result",
".",
"RowsAffected",
"(",
")",
"\n",
"if",
"hook",
",",
"ok",
":=",
"obj",
".",
"(",
"AfterUpdater",
")",
";",
"ok",
"{",
"if",
"err",
":=",
"hook",
".",
"AfterUpdate",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"affected",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"affected",
",",
"nil",
"\n",
"}"
] |
// Update updates the one record.
// The obj must be struct, and must have field that specified "pk" struct tag.
// Update will try to update record which searched by value of primary key in obj.
// Update returns the number of rows affected by an update.
|
[
"Update",
"updates",
"the",
"one",
"record",
".",
"The",
"obj",
"must",
"be",
"struct",
"and",
"must",
"have",
"field",
"that",
"specified",
"pk",
"struct",
"tag",
".",
"Update",
"will",
"try",
"to",
"update",
"record",
"which",
"searched",
"by",
"value",
"of",
"primary",
"key",
"in",
"obj",
".",
"Update",
"returns",
"the",
"number",
"of",
"rows",
"affected",
"by",
"an",
"update",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L298-L342
|
test
|
naoina/genmai
|
genmai.go
|
Delete
|
func (db *DB) Delete(obj interface{}) (affected int64, err error) {
objs, rtype, tableName, err := db.tableObjs("Delete", obj)
if err != nil {
return -1, err
}
if len(objs) < 1 {
return 0, nil
}
for _, obj := range objs {
if hook, ok := obj.(BeforeDeleter); ok {
if err := hook.BeforeDelete(); err != nil {
return -1, err
}
}
}
pkIdx := db.findPKIndex(rtype, nil)
if len(pkIdx) < 1 {
return -1, fmt.Errorf(`Delete: fields of struct doesn't have primary key: "pk" struct tag must be specified for delete`)
}
var args []interface{}
for _, obj := range objs {
rv := reflect.Indirect(reflect.ValueOf(obj))
args = append(args, rv.FieldByIndex(pkIdx).Interface())
}
holders := make([]string, len(args))
for i := 0; i < len(holders); i++ {
holders[i] = db.dialect.PlaceHolder(i)
}
query := fmt.Sprintf("DELETE FROM %s WHERE %s IN (%s)",
db.dialect.Quote(tableName),
db.dialect.Quote(db.columnFromTag(rtype.FieldByIndex(pkIdx))),
strings.Join(holders, ", "))
stmt, err := db.prepare(query, args...)
if err != nil {
return -1, err
}
defer stmt.Close()
result, err := stmt.Exec(args...)
if err != nil {
return -1, err
}
affected, _ = result.RowsAffected()
for _, obj := range objs {
if hook, ok := obj.(AfterDeleter); ok {
if err := hook.AfterDelete(); err != nil {
return affected, err
}
}
}
return affected, nil
}
|
go
|
func (db *DB) Delete(obj interface{}) (affected int64, err error) {
objs, rtype, tableName, err := db.tableObjs("Delete", obj)
if err != nil {
return -1, err
}
if len(objs) < 1 {
return 0, nil
}
for _, obj := range objs {
if hook, ok := obj.(BeforeDeleter); ok {
if err := hook.BeforeDelete(); err != nil {
return -1, err
}
}
}
pkIdx := db.findPKIndex(rtype, nil)
if len(pkIdx) < 1 {
return -1, fmt.Errorf(`Delete: fields of struct doesn't have primary key: "pk" struct tag must be specified for delete`)
}
var args []interface{}
for _, obj := range objs {
rv := reflect.Indirect(reflect.ValueOf(obj))
args = append(args, rv.FieldByIndex(pkIdx).Interface())
}
holders := make([]string, len(args))
for i := 0; i < len(holders); i++ {
holders[i] = db.dialect.PlaceHolder(i)
}
query := fmt.Sprintf("DELETE FROM %s WHERE %s IN (%s)",
db.dialect.Quote(tableName),
db.dialect.Quote(db.columnFromTag(rtype.FieldByIndex(pkIdx))),
strings.Join(holders, ", "))
stmt, err := db.prepare(query, args...)
if err != nil {
return -1, err
}
defer stmt.Close()
result, err := stmt.Exec(args...)
if err != nil {
return -1, err
}
affected, _ = result.RowsAffected()
for _, obj := range objs {
if hook, ok := obj.(AfterDeleter); ok {
if err := hook.AfterDelete(); err != nil {
return affected, err
}
}
}
return affected, nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Delete",
"(",
"obj",
"interface",
"{",
"}",
")",
"(",
"affected",
"int64",
",",
"err",
"error",
")",
"{",
"objs",
",",
"rtype",
",",
"tableName",
",",
"err",
":=",
"db",
".",
"tableObjs",
"(",
"\"Delete\"",
",",
"obj",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"if",
"len",
"(",
"objs",
")",
"<",
"1",
"{",
"return",
"0",
",",
"nil",
"\n",
"}",
"\n",
"for",
"_",
",",
"obj",
":=",
"range",
"objs",
"{",
"if",
"hook",
",",
"ok",
":=",
"obj",
".",
"(",
"BeforeDeleter",
")",
";",
"ok",
"{",
"if",
"err",
":=",
"hook",
".",
"BeforeDelete",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"pkIdx",
":=",
"db",
".",
"findPKIndex",
"(",
"rtype",
",",
"nil",
")",
"\n",
"if",
"len",
"(",
"pkIdx",
")",
"<",
"1",
"{",
"return",
"-",
"1",
",",
"fmt",
".",
"Errorf",
"(",
"`Delete: fields of struct doesn't have primary key: \"pk\" struct tag must be specified for delete`",
")",
"\n",
"}",
"\n",
"var",
"args",
"[",
"]",
"interface",
"{",
"}",
"\n",
"for",
"_",
",",
"obj",
":=",
"range",
"objs",
"{",
"rv",
":=",
"reflect",
".",
"Indirect",
"(",
"reflect",
".",
"ValueOf",
"(",
"obj",
")",
")",
"\n",
"args",
"=",
"append",
"(",
"args",
",",
"rv",
".",
"FieldByIndex",
"(",
"pkIdx",
")",
".",
"Interface",
"(",
")",
")",
"\n",
"}",
"\n",
"holders",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"args",
")",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"holders",
")",
";",
"i",
"++",
"{",
"holders",
"[",
"i",
"]",
"=",
"db",
".",
"dialect",
".",
"PlaceHolder",
"(",
"i",
")",
"\n",
"}",
"\n",
"query",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"DELETE FROM %s WHERE %s IN (%s)\"",
",",
"db",
".",
"dialect",
".",
"Quote",
"(",
"tableName",
")",
",",
"db",
".",
"dialect",
".",
"Quote",
"(",
"db",
".",
"columnFromTag",
"(",
"rtype",
".",
"FieldByIndex",
"(",
"pkIdx",
")",
")",
")",
",",
"strings",
".",
"Join",
"(",
"holders",
",",
"\", \"",
")",
")",
"\n",
"stmt",
",",
"err",
":=",
"db",
".",
"prepare",
"(",
"query",
",",
"args",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"defer",
"stmt",
".",
"Close",
"(",
")",
"\n",
"result",
",",
"err",
":=",
"stmt",
".",
"Exec",
"(",
"args",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"-",
"1",
",",
"err",
"\n",
"}",
"\n",
"affected",
",",
"_",
"=",
"result",
".",
"RowsAffected",
"(",
")",
"\n",
"for",
"_",
",",
"obj",
":=",
"range",
"objs",
"{",
"if",
"hook",
",",
"ok",
":=",
"obj",
".",
"(",
"AfterDeleter",
")",
";",
"ok",
"{",
"if",
"err",
":=",
"hook",
".",
"AfterDelete",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"affected",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"affected",
",",
"nil",
"\n",
"}"
] |
// Delete deletes the records from database table.
// The obj must be pointer to struct or slice of struct, and must have field that specified "pk" struct tag.
// Delete will try to delete record which searched by value of primary key in obj.
// Delete returns teh number of rows affected by a delete.
|
[
"Delete",
"deletes",
"the",
"records",
"from",
"database",
"table",
".",
"The",
"obj",
"must",
"be",
"pointer",
"to",
"struct",
"or",
"slice",
"of",
"struct",
"and",
"must",
"have",
"field",
"that",
"specified",
"pk",
"struct",
"tag",
".",
"Delete",
"will",
"try",
"to",
"delete",
"record",
"which",
"searched",
"by",
"value",
"of",
"primary",
"key",
"in",
"obj",
".",
"Delete",
"returns",
"teh",
"number",
"of",
"rows",
"affected",
"by",
"a",
"delete",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L432-L482
|
test
|
naoina/genmai
|
genmai.go
|
Begin
|
func (db *DB) Begin() error {
tx, err := db.db.Begin()
if err != nil {
return err
}
db.m.Lock()
defer db.m.Unlock()
db.tx = tx
return nil
}
|
go
|
func (db *DB) Begin() error {
tx, err := db.db.Begin()
if err != nil {
return err
}
db.m.Lock()
defer db.m.Unlock()
db.tx = tx
return nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Begin",
"(",
")",
"error",
"{",
"tx",
",",
"err",
":=",
"db",
".",
"db",
".",
"Begin",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"db",
".",
"m",
".",
"Lock",
"(",
")",
"\n",
"defer",
"db",
".",
"m",
".",
"Unlock",
"(",
")",
"\n",
"db",
".",
"tx",
"=",
"tx",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Begin starts a transaction.
|
[
"Begin",
"starts",
"a",
"transaction",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L485-L494
|
test
|
naoina/genmai
|
genmai.go
|
Commit
|
func (db *DB) Commit() error {
db.m.Lock()
defer db.m.Unlock()
if db.tx == nil {
return ErrTxDone
}
err := db.tx.Commit()
db.tx = nil
return err
}
|
go
|
func (db *DB) Commit() error {
db.m.Lock()
defer db.m.Unlock()
if db.tx == nil {
return ErrTxDone
}
err := db.tx.Commit()
db.tx = nil
return err
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Commit",
"(",
")",
"error",
"{",
"db",
".",
"m",
".",
"Lock",
"(",
")",
"\n",
"defer",
"db",
".",
"m",
".",
"Unlock",
"(",
")",
"\n",
"if",
"db",
".",
"tx",
"==",
"nil",
"{",
"return",
"ErrTxDone",
"\n",
"}",
"\n",
"err",
":=",
"db",
".",
"tx",
".",
"Commit",
"(",
")",
"\n",
"db",
".",
"tx",
"=",
"nil",
"\n",
"return",
"err",
"\n",
"}"
] |
// Commit commits the transaction.
// If Begin still not called, or Commit or Rollback already called, Commit returns ErrTxDone.
|
[
"Commit",
"commits",
"the",
"transaction",
".",
"If",
"Begin",
"still",
"not",
"called",
"or",
"Commit",
"or",
"Rollback",
"already",
"called",
"Commit",
"returns",
"ErrTxDone",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L498-L507
|
test
|
naoina/genmai
|
genmai.go
|
Quote
|
func (db *DB) Quote(s string) string {
return db.dialect.Quote(s)
}
|
go
|
func (db *DB) Quote(s string) string {
return db.dialect.Quote(s)
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Quote",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"db",
".",
"dialect",
".",
"Quote",
"(",
"s",
")",
"\n",
"}"
] |
// Quote returns a quoted s.
// It is for a column name, not a value.
|
[
"Quote",
"returns",
"a",
"quoted",
"s",
".",
"It",
"is",
"for",
"a",
"column",
"name",
"not",
"a",
"value",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L544-L546
|
test
|
naoina/genmai
|
genmai.go
|
SetLogOutput
|
func (db *DB) SetLogOutput(w io.Writer) {
if w == nil {
db.logger = defaultLogger
} else {
db.logger = &templateLogger{w: w, t: defaultLoggerTemplate}
}
}
|
go
|
func (db *DB) SetLogOutput(w io.Writer) {
if w == nil {
db.logger = defaultLogger
} else {
db.logger = &templateLogger{w: w, t: defaultLoggerTemplate}
}
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"SetLogOutput",
"(",
"w",
"io",
".",
"Writer",
")",
"{",
"if",
"w",
"==",
"nil",
"{",
"db",
".",
"logger",
"=",
"defaultLogger",
"\n",
"}",
"else",
"{",
"db",
".",
"logger",
"=",
"&",
"templateLogger",
"{",
"w",
":",
"w",
",",
"t",
":",
"defaultLoggerTemplate",
"}",
"\n",
"}",
"\n",
"}"
] |
// SetLogOutput sets output destination for logging.
// If w is nil, it unsets output of logging.
|
[
"SetLogOutput",
"sets",
"output",
"destination",
"for",
"logging",
".",
"If",
"w",
"is",
"nil",
"it",
"unsets",
"output",
"of",
"logging",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L555-L561
|
test
|
naoina/genmai
|
genmai.go
|
selectToSlice
|
func (db *DB) selectToSlice(rows *sql.Rows, t reflect.Type) (reflect.Value, error) {
columns, err := rows.Columns()
if err != nil {
return reflect.Value{}, err
}
t = t.Elem()
ptrN := 0
for ; t.Kind() == reflect.Ptr; ptrN++ {
t = t.Elem()
}
fieldIndexes := make([][]int, len(columns))
for i, column := range columns {
index := db.fieldIndexByName(t, column, nil)
if len(index) < 1 {
return reflect.Value{}, fmt.Errorf("`%v` field isn't defined in %v or embedded struct", stringutil.ToUpperCamelCase(column), t)
}
fieldIndexes[i] = index
}
dest := make([]interface{}, len(columns))
var result []reflect.Value
for rows.Next() {
v := reflect.New(t).Elem()
for i, index := range fieldIndexes {
field := v.FieldByIndex(index)
dest[i] = field.Addr().Interface()
}
if err := rows.Scan(dest...); err != nil {
return reflect.Value{}, err
}
result = append(result, v)
}
if err := rows.Err(); err != nil {
return reflect.Value{}, err
}
for i := 0; i < ptrN; i++ {
t = reflect.PtrTo(t)
}
slice := reflect.MakeSlice(reflect.SliceOf(t), len(result), len(result))
for i, v := range result {
for j := 0; j < ptrN; j++ {
v = v.Addr()
}
slice.Index(i).Set(v)
}
return slice, nil
}
|
go
|
func (db *DB) selectToSlice(rows *sql.Rows, t reflect.Type) (reflect.Value, error) {
columns, err := rows.Columns()
if err != nil {
return reflect.Value{}, err
}
t = t.Elem()
ptrN := 0
for ; t.Kind() == reflect.Ptr; ptrN++ {
t = t.Elem()
}
fieldIndexes := make([][]int, len(columns))
for i, column := range columns {
index := db.fieldIndexByName(t, column, nil)
if len(index) < 1 {
return reflect.Value{}, fmt.Errorf("`%v` field isn't defined in %v or embedded struct", stringutil.ToUpperCamelCase(column), t)
}
fieldIndexes[i] = index
}
dest := make([]interface{}, len(columns))
var result []reflect.Value
for rows.Next() {
v := reflect.New(t).Elem()
for i, index := range fieldIndexes {
field := v.FieldByIndex(index)
dest[i] = field.Addr().Interface()
}
if err := rows.Scan(dest...); err != nil {
return reflect.Value{}, err
}
result = append(result, v)
}
if err := rows.Err(); err != nil {
return reflect.Value{}, err
}
for i := 0; i < ptrN; i++ {
t = reflect.PtrTo(t)
}
slice := reflect.MakeSlice(reflect.SliceOf(t), len(result), len(result))
for i, v := range result {
for j := 0; j < ptrN; j++ {
v = v.Addr()
}
slice.Index(i).Set(v)
}
return slice, nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"selectToSlice",
"(",
"rows",
"*",
"sql",
".",
"Rows",
",",
"t",
"reflect",
".",
"Type",
")",
"(",
"reflect",
".",
"Value",
",",
"error",
")",
"{",
"columns",
",",
"err",
":=",
"rows",
".",
"Columns",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"reflect",
".",
"Value",
"{",
"}",
",",
"err",
"\n",
"}",
"\n",
"t",
"=",
"t",
".",
"Elem",
"(",
")",
"\n",
"ptrN",
":=",
"0",
"\n",
"for",
";",
"t",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
";",
"ptrN",
"++",
"{",
"t",
"=",
"t",
".",
"Elem",
"(",
")",
"\n",
"}",
"\n",
"fieldIndexes",
":=",
"make",
"(",
"[",
"]",
"[",
"]",
"int",
",",
"len",
"(",
"columns",
")",
")",
"\n",
"for",
"i",
",",
"column",
":=",
"range",
"columns",
"{",
"index",
":=",
"db",
".",
"fieldIndexByName",
"(",
"t",
",",
"column",
",",
"nil",
")",
"\n",
"if",
"len",
"(",
"index",
")",
"<",
"1",
"{",
"return",
"reflect",
".",
"Value",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"`%v` field isn't defined in %v or embedded struct\"",
",",
"stringutil",
".",
"ToUpperCamelCase",
"(",
"column",
")",
",",
"t",
")",
"\n",
"}",
"\n",
"fieldIndexes",
"[",
"i",
"]",
"=",
"index",
"\n",
"}",
"\n",
"dest",
":=",
"make",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"len",
"(",
"columns",
")",
")",
"\n",
"var",
"result",
"[",
"]",
"reflect",
".",
"Value",
"\n",
"for",
"rows",
".",
"Next",
"(",
")",
"{",
"v",
":=",
"reflect",
".",
"New",
"(",
"t",
")",
".",
"Elem",
"(",
")",
"\n",
"for",
"i",
",",
"index",
":=",
"range",
"fieldIndexes",
"{",
"field",
":=",
"v",
".",
"FieldByIndex",
"(",
"index",
")",
"\n",
"dest",
"[",
"i",
"]",
"=",
"field",
".",
"Addr",
"(",
")",
".",
"Interface",
"(",
")",
"\n",
"}",
"\n",
"if",
"err",
":=",
"rows",
".",
"Scan",
"(",
"dest",
"...",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"reflect",
".",
"Value",
"{",
"}",
",",
"err",
"\n",
"}",
"\n",
"result",
"=",
"append",
"(",
"result",
",",
"v",
")",
"\n",
"}",
"\n",
"if",
"err",
":=",
"rows",
".",
"Err",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"reflect",
".",
"Value",
"{",
"}",
",",
"err",
"\n",
"}",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"ptrN",
";",
"i",
"++",
"{",
"t",
"=",
"reflect",
".",
"PtrTo",
"(",
"t",
")",
"\n",
"}",
"\n",
"slice",
":=",
"reflect",
".",
"MakeSlice",
"(",
"reflect",
".",
"SliceOf",
"(",
"t",
")",
",",
"len",
"(",
"result",
")",
",",
"len",
"(",
"result",
")",
")",
"\n",
"for",
"i",
",",
"v",
":=",
"range",
"result",
"{",
"for",
"j",
":=",
"0",
";",
"j",
"<",
"ptrN",
";",
"j",
"++",
"{",
"v",
"=",
"v",
".",
"Addr",
"(",
")",
"\n",
"}",
"\n",
"slice",
".",
"Index",
"(",
"i",
")",
".",
"Set",
"(",
"v",
")",
"\n",
"}",
"\n",
"return",
"slice",
",",
"nil",
"\n",
"}"
] |
// selectToSlice returns a slice value fetched from rows.
|
[
"selectToSlice",
"returns",
"a",
"slice",
"value",
"fetched",
"from",
"rows",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L580-L625
|
test
|
naoina/genmai
|
genmai.go
|
selectToValue
|
func (db *DB) selectToValue(rows *sql.Rows, t reflect.Type) (reflect.Value, error) {
ptrN := 0
for ; t.Kind() == reflect.Ptr; ptrN++ {
t = t.Elem()
}
dest := reflect.New(t).Elem()
if rows.Next() {
if err := rows.Scan(dest.Addr().Interface()); err != nil {
return reflect.Value{}, err
}
}
for i := 0; i < ptrN; i++ {
dest = dest.Addr()
}
return dest, nil
}
|
go
|
func (db *DB) selectToValue(rows *sql.Rows, t reflect.Type) (reflect.Value, error) {
ptrN := 0
for ; t.Kind() == reflect.Ptr; ptrN++ {
t = t.Elem()
}
dest := reflect.New(t).Elem()
if rows.Next() {
if err := rows.Scan(dest.Addr().Interface()); err != nil {
return reflect.Value{}, err
}
}
for i := 0; i < ptrN; i++ {
dest = dest.Addr()
}
return dest, nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"selectToValue",
"(",
"rows",
"*",
"sql",
".",
"Rows",
",",
"t",
"reflect",
".",
"Type",
")",
"(",
"reflect",
".",
"Value",
",",
"error",
")",
"{",
"ptrN",
":=",
"0",
"\n",
"for",
";",
"t",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
";",
"ptrN",
"++",
"{",
"t",
"=",
"t",
".",
"Elem",
"(",
")",
"\n",
"}",
"\n",
"dest",
":=",
"reflect",
".",
"New",
"(",
"t",
")",
".",
"Elem",
"(",
")",
"\n",
"if",
"rows",
".",
"Next",
"(",
")",
"{",
"if",
"err",
":=",
"rows",
".",
"Scan",
"(",
"dest",
".",
"Addr",
"(",
")",
".",
"Interface",
"(",
")",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"reflect",
".",
"Value",
"{",
"}",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"ptrN",
";",
"i",
"++",
"{",
"dest",
"=",
"dest",
".",
"Addr",
"(",
")",
"\n",
"}",
"\n",
"return",
"dest",
",",
"nil",
"\n",
"}"
] |
// selectToValue returns a single value fetched from rows.
|
[
"selectToValue",
"returns",
"a",
"single",
"value",
"fetched",
"from",
"rows",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L628-L643
|
test
|
naoina/genmai
|
genmai.go
|
fieldIndexByName
|
func (db *DB) fieldIndexByName(t reflect.Type, name string, index []int) []int {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if candidate := db.columnFromTag(field); candidate == name {
return append(index, i)
}
if field.Anonymous {
if idx := db.fieldIndexByName(field.Type, name, append(index, i)); len(idx) > 0 {
return append(index, idx...)
}
}
}
return nil
}
|
go
|
func (db *DB) fieldIndexByName(t reflect.Type, name string, index []int) []int {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if candidate := db.columnFromTag(field); candidate == name {
return append(index, i)
}
if field.Anonymous {
if idx := db.fieldIndexByName(field.Type, name, append(index, i)); len(idx) > 0 {
return append(index, idx...)
}
}
}
return nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"fieldIndexByName",
"(",
"t",
"reflect",
".",
"Type",
",",
"name",
"string",
",",
"index",
"[",
"]",
"int",
")",
"[",
"]",
"int",
"{",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"t",
".",
"NumField",
"(",
")",
";",
"i",
"++",
"{",
"field",
":=",
"t",
".",
"Field",
"(",
"i",
")",
"\n",
"if",
"candidate",
":=",
"db",
".",
"columnFromTag",
"(",
"field",
")",
";",
"candidate",
"==",
"name",
"{",
"return",
"append",
"(",
"index",
",",
"i",
")",
"\n",
"}",
"\n",
"if",
"field",
".",
"Anonymous",
"{",
"if",
"idx",
":=",
"db",
".",
"fieldIndexByName",
"(",
"field",
".",
"Type",
",",
"name",
",",
"append",
"(",
"index",
",",
"i",
")",
")",
";",
"len",
"(",
"idx",
")",
">",
"0",
"{",
"return",
"append",
"(",
"index",
",",
"idx",
"...",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// fieldIndexByName returns the nested field corresponding to the index sequence.
|
[
"fieldIndexByName",
"returns",
"the",
"nested",
"field",
"corresponding",
"to",
"the",
"index",
"sequence",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L646-L659
|
test
|
naoina/genmai
|
genmai.go
|
columns
|
func (db *DB) columns(tableName string, columns []interface{}) string {
if len(columns) == 0 {
return ColumnName(db.dialect, tableName, "*")
}
names := make([]string, len(columns))
for i, col := range columns {
switch c := col.(type) {
case Raw:
names[i] = fmt.Sprint(*c)
case string:
names[i] = ColumnName(db.dialect, tableName, c)
case *Distinct:
names[i] = fmt.Sprintf("DISTINCT %s", db.columns(tableName, ToInterfaceSlice(c.columns)))
default:
panic(fmt.Errorf("column name must be string, Raw or *Distinct, got %T", c))
}
}
return strings.Join(names, ", ")
}
|
go
|
func (db *DB) columns(tableName string, columns []interface{}) string {
if len(columns) == 0 {
return ColumnName(db.dialect, tableName, "*")
}
names := make([]string, len(columns))
for i, col := range columns {
switch c := col.(type) {
case Raw:
names[i] = fmt.Sprint(*c)
case string:
names[i] = ColumnName(db.dialect, tableName, c)
case *Distinct:
names[i] = fmt.Sprintf("DISTINCT %s", db.columns(tableName, ToInterfaceSlice(c.columns)))
default:
panic(fmt.Errorf("column name must be string, Raw or *Distinct, got %T", c))
}
}
return strings.Join(names, ", ")
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"columns",
"(",
"tableName",
"string",
",",
"columns",
"[",
"]",
"interface",
"{",
"}",
")",
"string",
"{",
"if",
"len",
"(",
"columns",
")",
"==",
"0",
"{",
"return",
"ColumnName",
"(",
"db",
".",
"dialect",
",",
"tableName",
",",
"\"*\"",
")",
"\n",
"}",
"\n",
"names",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"columns",
")",
")",
"\n",
"for",
"i",
",",
"col",
":=",
"range",
"columns",
"{",
"switch",
"c",
":=",
"col",
".",
"(",
"type",
")",
"{",
"case",
"Raw",
":",
"names",
"[",
"i",
"]",
"=",
"fmt",
".",
"Sprint",
"(",
"*",
"c",
")",
"\n",
"case",
"string",
":",
"names",
"[",
"i",
"]",
"=",
"ColumnName",
"(",
"db",
".",
"dialect",
",",
"tableName",
",",
"c",
")",
"\n",
"case",
"*",
"Distinct",
":",
"names",
"[",
"i",
"]",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"DISTINCT %s\"",
",",
"db",
".",
"columns",
"(",
"tableName",
",",
"ToInterfaceSlice",
"(",
"c",
".",
"columns",
")",
")",
")",
"\n",
"default",
":",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"column name must be string, Raw or *Distinct, got %T\"",
",",
"c",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"strings",
".",
"Join",
"(",
"names",
",",
"\", \"",
")",
"\n",
"}"
] |
// columns returns the comma-separated column name with quoted.
|
[
"columns",
"returns",
"the",
"comma",
"-",
"separated",
"column",
"name",
"with",
"quoted",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L708-L726
|
test
|
naoina/genmai
|
genmai.go
|
tagsFromField
|
func (db *DB) tagsFromField(field *reflect.StructField) (options []string) {
if db.hasSkipTag(field) {
return nil
}
for _, tag := range strings.Split(field.Tag.Get(dbTag), ",") {
if t := strings.ToLower(strings.TrimSpace(tag)); t != "" {
options = append(options, t)
}
}
return options
}
|
go
|
func (db *DB) tagsFromField(field *reflect.StructField) (options []string) {
if db.hasSkipTag(field) {
return nil
}
for _, tag := range strings.Split(field.Tag.Get(dbTag), ",") {
if t := strings.ToLower(strings.TrimSpace(tag)); t != "" {
options = append(options, t)
}
}
return options
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"tagsFromField",
"(",
"field",
"*",
"reflect",
".",
"StructField",
")",
"(",
"options",
"[",
"]",
"string",
")",
"{",
"if",
"db",
".",
"hasSkipTag",
"(",
"field",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"for",
"_",
",",
"tag",
":=",
"range",
"strings",
".",
"Split",
"(",
"field",
".",
"Tag",
".",
"Get",
"(",
"dbTag",
")",
",",
"\",\"",
")",
"{",
"if",
"t",
":=",
"strings",
".",
"ToLower",
"(",
"strings",
".",
"TrimSpace",
"(",
"tag",
")",
")",
";",
"t",
"!=",
"\"\"",
"{",
"options",
"=",
"append",
"(",
"options",
",",
"t",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"options",
"\n",
"}"
] |
// tagsFromField returns a slice of option strings.
|
[
"tagsFromField",
"returns",
"a",
"slice",
"of",
"option",
"strings",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L783-L793
|
test
|
naoina/genmai
|
genmai.go
|
hasSkipTag
|
func (db *DB) hasSkipTag(field *reflect.StructField) bool {
if field.Tag.Get(dbTag) == skipTag {
return true
}
return false
}
|
go
|
func (db *DB) hasSkipTag(field *reflect.StructField) bool {
if field.Tag.Get(dbTag) == skipTag {
return true
}
return false
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"hasSkipTag",
"(",
"field",
"*",
"reflect",
".",
"StructField",
")",
"bool",
"{",
"if",
"field",
".",
"Tag",
".",
"Get",
"(",
"dbTag",
")",
"==",
"skipTag",
"{",
"return",
"true",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// hasSkipTag returns whether the struct field has the "-" tag.
|
[
"hasSkipTag",
"returns",
"whether",
"the",
"struct",
"field",
"has",
"the",
"-",
"tag",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L796-L801
|
test
|
naoina/genmai
|
genmai.go
|
hasPKTag
|
func (db *DB) hasPKTag(field *reflect.StructField) bool {
for _, tag := range db.tagsFromField(field) {
if tag == "pk" {
return true
}
}
return false
}
|
go
|
func (db *DB) hasPKTag(field *reflect.StructField) bool {
for _, tag := range db.tagsFromField(field) {
if tag == "pk" {
return true
}
}
return false
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"hasPKTag",
"(",
"field",
"*",
"reflect",
".",
"StructField",
")",
"bool",
"{",
"for",
"_",
",",
"tag",
":=",
"range",
"db",
".",
"tagsFromField",
"(",
"field",
")",
"{",
"if",
"tag",
"==",
"\"pk\"",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// hasPKTag returns whether the struct field has the "pk" tag.
|
[
"hasPKTag",
"returns",
"whether",
"the",
"struct",
"field",
"has",
"the",
"pk",
"tag",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L804-L811
|
test
|
naoina/genmai
|
genmai.go
|
isAutoIncrementable
|
func (db *DB) isAutoIncrementable(field *reflect.StructField) bool {
switch field.Type.Kind() {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
}
return false
}
|
go
|
func (db *DB) isAutoIncrementable(field *reflect.StructField) bool {
switch field.Type.Kind() {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
}
return false
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"isAutoIncrementable",
"(",
"field",
"*",
"reflect",
".",
"StructField",
")",
"bool",
"{",
"switch",
"field",
".",
"Type",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Int",
",",
"reflect",
".",
"Int16",
",",
"reflect",
".",
"Int32",
",",
"reflect",
".",
"Int64",
",",
"reflect",
".",
"Uint",
",",
"reflect",
".",
"Uint16",
",",
"reflect",
".",
"Uint32",
",",
"reflect",
".",
"Uint64",
":",
"return",
"true",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// isAutoIncrementable returns whether the struct field is integer.
|
[
"isAutoIncrementable",
"returns",
"whether",
"the",
"struct",
"field",
"is",
"integer",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L814-L821
|
test
|
naoina/genmai
|
genmai.go
|
collectFieldIndexes
|
func (db *DB) collectFieldIndexes(typ reflect.Type, index []int) (indexes [][]int) {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if IsUnexportedField(field) {
continue
}
if !(db.hasSkipTag(&field) || (db.hasPKTag(&field) && db.isAutoIncrementable(&field))) {
tmp := make([]int, len(index)+1)
copy(tmp, index)
tmp[len(tmp)-1] = i
if field.Anonymous {
indexes = append(indexes, db.collectFieldIndexes(field.Type, tmp)...)
} else {
indexes = append(indexes, tmp)
}
}
}
return indexes
}
|
go
|
func (db *DB) collectFieldIndexes(typ reflect.Type, index []int) (indexes [][]int) {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if IsUnexportedField(field) {
continue
}
if !(db.hasSkipTag(&field) || (db.hasPKTag(&field) && db.isAutoIncrementable(&field))) {
tmp := make([]int, len(index)+1)
copy(tmp, index)
tmp[len(tmp)-1] = i
if field.Anonymous {
indexes = append(indexes, db.collectFieldIndexes(field.Type, tmp)...)
} else {
indexes = append(indexes, tmp)
}
}
}
return indexes
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"collectFieldIndexes",
"(",
"typ",
"reflect",
".",
"Type",
",",
"index",
"[",
"]",
"int",
")",
"(",
"indexes",
"[",
"]",
"[",
"]",
"int",
")",
"{",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"typ",
".",
"NumField",
"(",
")",
";",
"i",
"++",
"{",
"field",
":=",
"typ",
".",
"Field",
"(",
"i",
")",
"\n",
"if",
"IsUnexportedField",
"(",
"field",
")",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"!",
"(",
"db",
".",
"hasSkipTag",
"(",
"&",
"field",
")",
"||",
"(",
"db",
".",
"hasPKTag",
"(",
"&",
"field",
")",
"&&",
"db",
".",
"isAutoIncrementable",
"(",
"&",
"field",
")",
")",
")",
"{",
"tmp",
":=",
"make",
"(",
"[",
"]",
"int",
",",
"len",
"(",
"index",
")",
"+",
"1",
")",
"\n",
"copy",
"(",
"tmp",
",",
"index",
")",
"\n",
"tmp",
"[",
"len",
"(",
"tmp",
")",
"-",
"1",
"]",
"=",
"i",
"\n",
"if",
"field",
".",
"Anonymous",
"{",
"indexes",
"=",
"append",
"(",
"indexes",
",",
"db",
".",
"collectFieldIndexes",
"(",
"field",
".",
"Type",
",",
"tmp",
")",
"...",
")",
"\n",
"}",
"else",
"{",
"indexes",
"=",
"append",
"(",
"indexes",
",",
"tmp",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"indexes",
"\n",
"}"
] |
// collectFieldIndexes returns the indexes of field which doesn't have skip tag and pk tag.
|
[
"collectFieldIndexes",
"returns",
"the",
"indexes",
"of",
"field",
"which",
"doesn",
"t",
"have",
"skip",
"tag",
"and",
"pk",
"tag",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L824-L842
|
test
|
naoina/genmai
|
genmai.go
|
findPKIndex
|
func (db *DB) findPKIndex(typ reflect.Type, index []int) []int {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if IsUnexportedField(field) {
continue
}
if field.Anonymous {
if idx := db.findPKIndex(field.Type, append(index, i)); idx != nil {
return append(index, idx...)
}
continue
}
if db.hasPKTag(&field) {
return append(index, i)
}
}
return nil
}
|
go
|
func (db *DB) findPKIndex(typ reflect.Type, index []int) []int {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if IsUnexportedField(field) {
continue
}
if field.Anonymous {
if idx := db.findPKIndex(field.Type, append(index, i)); idx != nil {
return append(index, idx...)
}
continue
}
if db.hasPKTag(&field) {
return append(index, i)
}
}
return nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"findPKIndex",
"(",
"typ",
"reflect",
".",
"Type",
",",
"index",
"[",
"]",
"int",
")",
"[",
"]",
"int",
"{",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"typ",
".",
"NumField",
"(",
")",
";",
"i",
"++",
"{",
"field",
":=",
"typ",
".",
"Field",
"(",
"i",
")",
"\n",
"if",
"IsUnexportedField",
"(",
"field",
")",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"field",
".",
"Anonymous",
"{",
"if",
"idx",
":=",
"db",
".",
"findPKIndex",
"(",
"field",
".",
"Type",
",",
"append",
"(",
"index",
",",
"i",
")",
")",
";",
"idx",
"!=",
"nil",
"{",
"return",
"append",
"(",
"index",
",",
"idx",
"...",
")",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n",
"if",
"db",
".",
"hasPKTag",
"(",
"&",
"field",
")",
"{",
"return",
"append",
"(",
"index",
",",
"i",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// findPKIndex returns the nested field corresponding to the index sequence of field of primary key.
|
[
"findPKIndex",
"returns",
"the",
"nested",
"field",
"corresponding",
"to",
"the",
"index",
"sequence",
"of",
"field",
"of",
"primary",
"key",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L845-L862
|
test
|
naoina/genmai
|
genmai.go
|
sizeFromTag
|
func (db *DB) sizeFromTag(field *reflect.StructField) (size uint64, err error) {
if s := field.Tag.Get(dbSizeTag); s != "" {
size, err = strconv.ParseUint(s, 10, 64)
}
return size, err
}
|
go
|
func (db *DB) sizeFromTag(field *reflect.StructField) (size uint64, err error) {
if s := field.Tag.Get(dbSizeTag); s != "" {
size, err = strconv.ParseUint(s, 10, 64)
}
return size, err
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"sizeFromTag",
"(",
"field",
"*",
"reflect",
".",
"StructField",
")",
"(",
"size",
"uint64",
",",
"err",
"error",
")",
"{",
"if",
"s",
":=",
"field",
".",
"Tag",
".",
"Get",
"(",
"dbSizeTag",
")",
";",
"s",
"!=",
"\"\"",
"{",
"size",
",",
"err",
"=",
"strconv",
".",
"ParseUint",
"(",
"s",
",",
"10",
",",
"64",
")",
"\n",
"}",
"\n",
"return",
"size",
",",
"err",
"\n",
"}"
] |
// sizeFromTag returns a size from tag.
// If "size" tag specified to struct field, it will converted to uint64 and returns it.
// If it doesn't specify, it returns 0.
// If value of "size" tag cannot convert to uint64, it returns 0 and error.
|
[
"sizeFromTag",
"returns",
"a",
"size",
"from",
"tag",
".",
"If",
"size",
"tag",
"specified",
"to",
"struct",
"field",
"it",
"will",
"converted",
"to",
"uint64",
"and",
"returns",
"it",
".",
"If",
"it",
"doesn",
"t",
"specify",
"it",
"returns",
"0",
".",
"If",
"value",
"of",
"size",
"tag",
"cannot",
"convert",
"to",
"uint64",
"it",
"returns",
"0",
"and",
"error",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L868-L873
|
test
|
naoina/genmai
|
genmai.go
|
columnFromTag
|
func (db *DB) columnFromTag(field reflect.StructField) string {
col := field.Tag.Get(dbColumnTag)
if col == "" {
return stringutil.ToSnakeCase(field.Name)
}
return col
}
|
go
|
func (db *DB) columnFromTag(field reflect.StructField) string {
col := field.Tag.Get(dbColumnTag)
if col == "" {
return stringutil.ToSnakeCase(field.Name)
}
return col
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"columnFromTag",
"(",
"field",
"reflect",
".",
"StructField",
")",
"string",
"{",
"col",
":=",
"field",
".",
"Tag",
".",
"Get",
"(",
"dbColumnTag",
")",
"\n",
"if",
"col",
"==",
"\"\"",
"{",
"return",
"stringutil",
".",
"ToSnakeCase",
"(",
"field",
".",
"Name",
")",
"\n",
"}",
"\n",
"return",
"col",
"\n",
"}"
] |
// columnFromTag returns the column name.
// If "column" tag specified to struct field, returns it.
// Otherwise, it returns snake-cased field name as column name.
|
[
"columnFromTag",
"returns",
"the",
"column",
"name",
".",
"If",
"column",
"tag",
"specified",
"to",
"struct",
"field",
"returns",
"it",
".",
"Otherwise",
"it",
"returns",
"snake",
"-",
"cased",
"field",
"name",
"as",
"column",
"name",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L885-L891
|
test
|
naoina/genmai
|
genmai.go
|
defaultFromTag
|
func (db *DB) defaultFromTag(field *reflect.StructField) (string, error) {
def := field.Tag.Get(dbDefaultTag)
if def == "" {
return "", nil
}
switch field.Type.Kind() {
case reflect.Bool:
b, err := strconv.ParseBool(def)
if err != nil {
return "", err
}
return fmt.Sprintf("DEFAULT %v", db.dialect.FormatBool(b)), nil
}
return fmt.Sprintf("DEFAULT %v", def), nil
}
|
go
|
func (db *DB) defaultFromTag(field *reflect.StructField) (string, error) {
def := field.Tag.Get(dbDefaultTag)
if def == "" {
return "", nil
}
switch field.Type.Kind() {
case reflect.Bool:
b, err := strconv.ParseBool(def)
if err != nil {
return "", err
}
return fmt.Sprintf("DEFAULT %v", db.dialect.FormatBool(b)), nil
}
return fmt.Sprintf("DEFAULT %v", def), nil
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"defaultFromTag",
"(",
"field",
"*",
"reflect",
".",
"StructField",
")",
"(",
"string",
",",
"error",
")",
"{",
"def",
":=",
"field",
".",
"Tag",
".",
"Get",
"(",
"dbDefaultTag",
")",
"\n",
"if",
"def",
"==",
"\"\"",
"{",
"return",
"\"\"",
",",
"nil",
"\n",
"}",
"\n",
"switch",
"field",
".",
"Type",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Bool",
":",
"b",
",",
"err",
":=",
"strconv",
".",
"ParseBool",
"(",
"def",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"DEFAULT %v\"",
",",
"db",
".",
"dialect",
".",
"FormatBool",
"(",
"b",
")",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"DEFAULT %v\"",
",",
"def",
")",
",",
"nil",
"\n",
"}"
] |
// defaultFromTag returns a "DEFAULT ..." keyword.
// If "default" tag specified to struct field, it use as the default value.
// If it doesn't specify, it returns empty string.
|
[
"defaultFromTag",
"returns",
"a",
"DEFAULT",
"...",
"keyword",
".",
"If",
"default",
"tag",
"specified",
"to",
"struct",
"field",
"it",
"use",
"as",
"the",
"default",
"value",
".",
"If",
"it",
"doesn",
"t",
"specify",
"it",
"returns",
"empty",
"string",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L896-L910
|
test
|
naoina/genmai
|
genmai.go
|
Where
|
func (c *Condition) Where(cond interface{}, args ...interface{}) *Condition {
return c.appendQueryByCondOrExpr("Where", 0, Where, cond, args...)
}
|
go
|
func (c *Condition) Where(cond interface{}, args ...interface{}) *Condition {
return c.appendQueryByCondOrExpr("Where", 0, Where, cond, args...)
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"Where",
"(",
"cond",
"interface",
"{",
"}",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"*",
"Condition",
"{",
"return",
"c",
".",
"appendQueryByCondOrExpr",
"(",
"\"Where\"",
",",
"0",
",",
"Where",
",",
"cond",
",",
"args",
"...",
")",
"\n",
"}"
] |
// Where adds "WHERE" clause to the Condition and returns it for method chain.
|
[
"Where",
"adds",
"WHERE",
"clause",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1134-L1136
|
test
|
naoina/genmai
|
genmai.go
|
And
|
func (c *Condition) And(cond interface{}, args ...interface{}) *Condition {
return c.appendQueryByCondOrExpr("And", 100, And, cond, args...)
}
|
go
|
func (c *Condition) And(cond interface{}, args ...interface{}) *Condition {
return c.appendQueryByCondOrExpr("And", 100, And, cond, args...)
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"And",
"(",
"cond",
"interface",
"{",
"}",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"*",
"Condition",
"{",
"return",
"c",
".",
"appendQueryByCondOrExpr",
"(",
"\"And\"",
",",
"100",
",",
"And",
",",
"cond",
",",
"args",
"...",
")",
"\n",
"}"
] |
// And adds "AND" operator to the Condition and returns it for method chain.
|
[
"And",
"adds",
"AND",
"operator",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1139-L1141
|
test
|
naoina/genmai
|
genmai.go
|
Or
|
func (c *Condition) Or(cond interface{}, args ...interface{}) *Condition {
return c.appendQueryByCondOrExpr("Or", 100, Or, cond, args...)
}
|
go
|
func (c *Condition) Or(cond interface{}, args ...interface{}) *Condition {
return c.appendQueryByCondOrExpr("Or", 100, Or, cond, args...)
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"Or",
"(",
"cond",
"interface",
"{",
"}",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"*",
"Condition",
"{",
"return",
"c",
".",
"appendQueryByCondOrExpr",
"(",
"\"Or\"",
",",
"100",
",",
"Or",
",",
"cond",
",",
"args",
"...",
")",
"\n",
"}"
] |
// Or adds "OR" operator to the Condition and returns it for method chain.
|
[
"Or",
"adds",
"OR",
"operator",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1144-L1146
|
test
|
naoina/genmai
|
genmai.go
|
Like
|
func (c *Condition) Like(arg string) *Condition {
return c.appendQuery(100, Like, arg)
}
|
go
|
func (c *Condition) Like(arg string) *Condition {
return c.appendQuery(100, Like, arg)
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"Like",
"(",
"arg",
"string",
")",
"*",
"Condition",
"{",
"return",
"c",
".",
"appendQuery",
"(",
"100",
",",
"Like",
",",
"arg",
")",
"\n",
"}"
] |
// Like adds "LIKE" clause to the Condition and returns it for method chain.
|
[
"Like",
"adds",
"LIKE",
"clause",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1154-L1156
|
test
|
naoina/genmai
|
genmai.go
|
Between
|
func (c *Condition) Between(from, to interface{}) *Condition {
return c.appendQuery(100, Between, &between{from, to})
}
|
go
|
func (c *Condition) Between(from, to interface{}) *Condition {
return c.appendQuery(100, Between, &between{from, to})
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"Between",
"(",
"from",
",",
"to",
"interface",
"{",
"}",
")",
"*",
"Condition",
"{",
"return",
"c",
".",
"appendQuery",
"(",
"100",
",",
"Between",
",",
"&",
"between",
"{",
"from",
",",
"to",
"}",
")",
"\n",
"}"
] |
// Between adds "BETWEEN ... AND ..." clause to the Condition and returns it for method chain.
|
[
"Between",
"adds",
"BETWEEN",
"...",
"AND",
"...",
"clause",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1159-L1161
|
test
|
naoina/genmai
|
genmai.go
|
OrderBy
|
func (c *Condition) OrderBy(table, col interface{}, order ...interface{}) *Condition {
order = append([]interface{}{table, col}, order...)
orderbys := make([]orderBy, 0, 1)
for len(order) > 0 {
o, rest := order[0], order[1:]
if _, ok := o.(string); ok {
if len(rest) < 1 {
panic(fmt.Errorf("OrderBy: few arguments"))
}
// OrderBy("column", genmai.DESC)
orderbys = append(orderbys, c.orderBy(nil, o, rest[0]))
order = rest[1:]
continue
}
if len(rest) < 2 {
panic(fmt.Errorf("OrderBy: few arguments"))
}
// OrderBy(tbl{}, "column", genmai.DESC)
orderbys = append(orderbys, c.orderBy(o, rest[0], rest[1]))
order = rest[2:]
}
return c.appendQuery(300, OrderBy, orderbys)
}
|
go
|
func (c *Condition) OrderBy(table, col interface{}, order ...interface{}) *Condition {
order = append([]interface{}{table, col}, order...)
orderbys := make([]orderBy, 0, 1)
for len(order) > 0 {
o, rest := order[0], order[1:]
if _, ok := o.(string); ok {
if len(rest) < 1 {
panic(fmt.Errorf("OrderBy: few arguments"))
}
// OrderBy("column", genmai.DESC)
orderbys = append(orderbys, c.orderBy(nil, o, rest[0]))
order = rest[1:]
continue
}
if len(rest) < 2 {
panic(fmt.Errorf("OrderBy: few arguments"))
}
// OrderBy(tbl{}, "column", genmai.DESC)
orderbys = append(orderbys, c.orderBy(o, rest[0], rest[1]))
order = rest[2:]
}
return c.appendQuery(300, OrderBy, orderbys)
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"OrderBy",
"(",
"table",
",",
"col",
"interface",
"{",
"}",
",",
"order",
"...",
"interface",
"{",
"}",
")",
"*",
"Condition",
"{",
"order",
"=",
"append",
"(",
"[",
"]",
"interface",
"{",
"}",
"{",
"table",
",",
"col",
"}",
",",
"order",
"...",
")",
"\n",
"orderbys",
":=",
"make",
"(",
"[",
"]",
"orderBy",
",",
"0",
",",
"1",
")",
"\n",
"for",
"len",
"(",
"order",
")",
">",
"0",
"{",
"o",
",",
"rest",
":=",
"order",
"[",
"0",
"]",
",",
"order",
"[",
"1",
":",
"]",
"\n",
"if",
"_",
",",
"ok",
":=",
"o",
".",
"(",
"string",
")",
";",
"ok",
"{",
"if",
"len",
"(",
"rest",
")",
"<",
"1",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"OrderBy: few arguments\"",
")",
")",
"\n",
"}",
"\n",
"orderbys",
"=",
"append",
"(",
"orderbys",
",",
"c",
".",
"orderBy",
"(",
"nil",
",",
"o",
",",
"rest",
"[",
"0",
"]",
")",
")",
"\n",
"order",
"=",
"rest",
"[",
"1",
":",
"]",
"\n",
"continue",
"\n",
"}",
"\n",
"if",
"len",
"(",
"rest",
")",
"<",
"2",
"{",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"OrderBy: few arguments\"",
")",
")",
"\n",
"}",
"\n",
"orderbys",
"=",
"append",
"(",
"orderbys",
",",
"c",
".",
"orderBy",
"(",
"o",
",",
"rest",
"[",
"0",
"]",
",",
"rest",
"[",
"1",
"]",
")",
")",
"\n",
"order",
"=",
"rest",
"[",
"2",
":",
"]",
"\n",
"}",
"\n",
"return",
"c",
".",
"appendQuery",
"(",
"300",
",",
"OrderBy",
",",
"orderbys",
")",
"\n",
"}"
] |
// OrderBy adds "ORDER BY" clause to the Condition and returns it for method chain.
|
[
"OrderBy",
"adds",
"ORDER",
"BY",
"clause",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1174-L1196
|
test
|
naoina/genmai
|
genmai.go
|
Limit
|
func (c *Condition) Limit(lim int) *Condition {
return c.appendQuery(500, Limit, lim)
}
|
go
|
func (c *Condition) Limit(lim int) *Condition {
return c.appendQuery(500, Limit, lim)
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"Limit",
"(",
"lim",
"int",
")",
"*",
"Condition",
"{",
"return",
"c",
".",
"appendQuery",
"(",
"500",
",",
"Limit",
",",
"lim",
")",
"\n",
"}"
] |
// Limit adds "LIMIT" clause to the Condition and returns it for method chain.
|
[
"Limit",
"adds",
"LIMIT",
"clause",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1199-L1201
|
test
|
naoina/genmai
|
genmai.go
|
Offset
|
func (c *Condition) Offset(offset int) *Condition {
return c.appendQuery(700, Offset, offset)
}
|
go
|
func (c *Condition) Offset(offset int) *Condition {
return c.appendQuery(700, Offset, offset)
}
|
[
"func",
"(",
"c",
"*",
"Condition",
")",
"Offset",
"(",
"offset",
"int",
")",
"*",
"Condition",
"{",
"return",
"c",
".",
"appendQuery",
"(",
"700",
",",
"Offset",
",",
"offset",
")",
"\n",
"}"
] |
// Offset adds "OFFSET" clause to the Condition and returns it for method chain.
|
[
"Offset",
"adds",
"OFFSET",
"clause",
"to",
"the",
"Condition",
"and",
"returns",
"it",
"for",
"method",
"chain",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/genmai.go#L1204-L1206
|
test
|
naoina/genmai
|
log.go
|
SetFormat
|
func (l *templateLogger) SetFormat(format string) error {
l.m.Lock()
defer l.m.Unlock()
t, err := template.New("genmai").Parse(format)
if err != nil {
return err
}
l.t = t
return nil
}
|
go
|
func (l *templateLogger) SetFormat(format string) error {
l.m.Lock()
defer l.m.Unlock()
t, err := template.New("genmai").Parse(format)
if err != nil {
return err
}
l.t = t
return nil
}
|
[
"func",
"(",
"l",
"*",
"templateLogger",
")",
"SetFormat",
"(",
"format",
"string",
")",
"error",
"{",
"l",
".",
"m",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"m",
".",
"Unlock",
"(",
")",
"\n",
"t",
",",
"err",
":=",
"template",
".",
"New",
"(",
"\"genmai\"",
")",
".",
"Parse",
"(",
"format",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"l",
".",
"t",
"=",
"t",
"\n",
"return",
"nil",
"\n",
"}"
] |
// SetFormat sets the format for logging.
|
[
"SetFormat",
"sets",
"the",
"format",
"for",
"logging",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/log.go#L38-L47
|
test
|
naoina/genmai
|
log.go
|
Print
|
func (l *templateLogger) Print(start time.Time, query string, args ...interface{}) error {
if len(args) > 0 {
values := make([]string, len(args))
for i, arg := range args {
values[i] = fmt.Sprintf("%#v", arg)
}
query = fmt.Sprintf("%v; [%v]", query, strings.Join(values, ", "))
} else {
query = fmt.Sprintf("%s;", query)
}
data := map[string]interface{}{
"time": start,
"duration": fmt.Sprintf("%.2fms", now().Sub(start).Seconds()*float64(time.Microsecond)),
"query": query,
}
var buf bytes.Buffer
if err := l.t.Execute(&buf, data); err != nil {
return err
}
l.m.Lock()
defer l.m.Unlock()
if _, err := fmt.Fprintln(l.w, strings.TrimSuffix(buf.String(), "\n")); err != nil {
return err
}
return nil
}
|
go
|
func (l *templateLogger) Print(start time.Time, query string, args ...interface{}) error {
if len(args) > 0 {
values := make([]string, len(args))
for i, arg := range args {
values[i] = fmt.Sprintf("%#v", arg)
}
query = fmt.Sprintf("%v; [%v]", query, strings.Join(values, ", "))
} else {
query = fmt.Sprintf("%s;", query)
}
data := map[string]interface{}{
"time": start,
"duration": fmt.Sprintf("%.2fms", now().Sub(start).Seconds()*float64(time.Microsecond)),
"query": query,
}
var buf bytes.Buffer
if err := l.t.Execute(&buf, data); err != nil {
return err
}
l.m.Lock()
defer l.m.Unlock()
if _, err := fmt.Fprintln(l.w, strings.TrimSuffix(buf.String(), "\n")); err != nil {
return err
}
return nil
}
|
[
"func",
"(",
"l",
"*",
"templateLogger",
")",
"Print",
"(",
"start",
"time",
".",
"Time",
",",
"query",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"len",
"(",
"args",
")",
">",
"0",
"{",
"values",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"args",
")",
")",
"\n",
"for",
"i",
",",
"arg",
":=",
"range",
"args",
"{",
"values",
"[",
"i",
"]",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"%#v\"",
",",
"arg",
")",
"\n",
"}",
"\n",
"query",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"%v; [%v]\"",
",",
"query",
",",
"strings",
".",
"Join",
"(",
"values",
",",
"\", \"",
")",
")",
"\n",
"}",
"else",
"{",
"query",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"%s;\"",
",",
"query",
")",
"\n",
"}",
"\n",
"data",
":=",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
"{",
"\"time\"",
":",
"start",
",",
"\"duration\"",
":",
"fmt",
".",
"Sprintf",
"(",
"\"%.2fms\"",
",",
"now",
"(",
")",
".",
"Sub",
"(",
"start",
")",
".",
"Seconds",
"(",
")",
"*",
"float64",
"(",
"time",
".",
"Microsecond",
")",
")",
",",
"\"query\"",
":",
"query",
",",
"}",
"\n",
"var",
"buf",
"bytes",
".",
"Buffer",
"\n",
"if",
"err",
":=",
"l",
".",
"t",
".",
"Execute",
"(",
"&",
"buf",
",",
"data",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"l",
".",
"m",
".",
"Lock",
"(",
")",
"\n",
"defer",
"l",
".",
"m",
".",
"Unlock",
"(",
")",
"\n",
"if",
"_",
",",
"err",
":=",
"fmt",
".",
"Fprintln",
"(",
"l",
".",
"w",
",",
"strings",
".",
"TrimSuffix",
"(",
"buf",
".",
"String",
"(",
")",
",",
"\"\\n\"",
")",
")",
";",
"\\n",
"err",
"!=",
"nil",
"\n",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}"
] |
// Print outputs query log using format template.
// All arguments will be used to formatting.
|
[
"Print",
"outputs",
"query",
"log",
"using",
"format",
"template",
".",
"All",
"arguments",
"will",
"be",
"used",
"to",
"formatting",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/log.go#L51-L76
|
test
|
naoina/genmai
|
log.go
|
Print
|
func (l *nullLogger) Print(start time.Time, query string, args ...interface{}) error {
return nil
}
|
go
|
func (l *nullLogger) Print(start time.Time, query string, args ...interface{}) error {
return nil
}
|
[
"func",
"(",
"l",
"*",
"nullLogger",
")",
"Print",
"(",
"start",
"time",
".",
"Time",
",",
"query",
"string",
",",
"args",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"nil",
"\n",
"}"
] |
// Print is a dummy method.
|
[
"Print",
"is",
"a",
"dummy",
"method",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/log.go#L88-L90
|
test
|
naoina/genmai
|
dialect.go
|
Quote
|
func (d *MySQLDialect) Quote(s string) string {
return fmt.Sprintf("`%s`", strings.Replace(s, "`", "``", -1))
}
|
go
|
func (d *MySQLDialect) Quote(s string) string {
return fmt.Sprintf("`%s`", strings.Replace(s, "`", "``", -1))
}
|
[
"func",
"(",
"d",
"*",
"MySQLDialect",
")",
"Quote",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"`%s`\"",
",",
"strings",
".",
"Replace",
"(",
"s",
",",
"\"`\"",
",",
"\"``\"",
",",
"-",
"1",
")",
")",
"\n",
"}"
] |
// Quote returns a quoted s for a column name.
|
[
"Quote",
"returns",
"a",
"quoted",
"s",
"for",
"a",
"column",
"name",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/dialect.go#L138-L140
|
test
|
naoina/genmai
|
dialect.go
|
SQLType
|
func (d *PostgresDialect) SQLType(v interface{}, autoIncrement bool, size uint64) (name string, allowNull bool) {
switch v.(type) {
case bool:
return "boolean", false
case *bool, sql.NullBool:
return "boolean", true
case int8, int16, uint8, uint16:
return d.smallint(autoIncrement), false
case *int8, *int16, *uint8, *uint16:
return d.smallint(autoIncrement), true
case int, int32, uint, uint32:
return d.integer(autoIncrement), false
case *int, *int32, *uint, *uint32:
return d.integer(autoIncrement), true
case int64, uint64:
return d.bigint(autoIncrement), false
case *int64, *uint64, sql.NullInt64:
return d.bigint(autoIncrement), true
case string:
return d.varchar(size), false
case *string, sql.NullString:
return d.varchar(size), true
case []byte:
return "bytea", true
case time.Time:
return "timestamp with time zone", false
case *time.Time:
return "timestamp with time zone", true
case Rat:
return fmt.Sprintf("numeric(%d, %d)", decimalPrecision, decimalScale), false
case *Rat:
return fmt.Sprintf("numeric(%d, %d)", decimalPrecision, decimalScale), true
case Float32, Float64:
return "double precision", false
case *Float32, *Float64:
return "double precision", true
case float32, *float32, float64, *float64, sql.NullFloat64:
panic(ErrUsingFloatType)
}
panic(fmt.Errorf("PostgresDialect: unsupported SQL type: %T", v))
}
|
go
|
func (d *PostgresDialect) SQLType(v interface{}, autoIncrement bool, size uint64) (name string, allowNull bool) {
switch v.(type) {
case bool:
return "boolean", false
case *bool, sql.NullBool:
return "boolean", true
case int8, int16, uint8, uint16:
return d.smallint(autoIncrement), false
case *int8, *int16, *uint8, *uint16:
return d.smallint(autoIncrement), true
case int, int32, uint, uint32:
return d.integer(autoIncrement), false
case *int, *int32, *uint, *uint32:
return d.integer(autoIncrement), true
case int64, uint64:
return d.bigint(autoIncrement), false
case *int64, *uint64, sql.NullInt64:
return d.bigint(autoIncrement), true
case string:
return d.varchar(size), false
case *string, sql.NullString:
return d.varchar(size), true
case []byte:
return "bytea", true
case time.Time:
return "timestamp with time zone", false
case *time.Time:
return "timestamp with time zone", true
case Rat:
return fmt.Sprintf("numeric(%d, %d)", decimalPrecision, decimalScale), false
case *Rat:
return fmt.Sprintf("numeric(%d, %d)", decimalPrecision, decimalScale), true
case Float32, Float64:
return "double precision", false
case *Float32, *Float64:
return "double precision", true
case float32, *float32, float64, *float64, sql.NullFloat64:
panic(ErrUsingFloatType)
}
panic(fmt.Errorf("PostgresDialect: unsupported SQL type: %T", v))
}
|
[
"func",
"(",
"d",
"*",
"PostgresDialect",
")",
"SQLType",
"(",
"v",
"interface",
"{",
"}",
",",
"autoIncrement",
"bool",
",",
"size",
"uint64",
")",
"(",
"name",
"string",
",",
"allowNull",
"bool",
")",
"{",
"switch",
"v",
".",
"(",
"type",
")",
"{",
"case",
"bool",
":",
"return",
"\"boolean\"",
",",
"false",
"\n",
"case",
"*",
"bool",
",",
"sql",
".",
"NullBool",
":",
"return",
"\"boolean\"",
",",
"true",
"\n",
"case",
"int8",
",",
"int16",
",",
"uint8",
",",
"uint16",
":",
"return",
"d",
".",
"smallint",
"(",
"autoIncrement",
")",
",",
"false",
"\n",
"case",
"*",
"int8",
",",
"*",
"int16",
",",
"*",
"uint8",
",",
"*",
"uint16",
":",
"return",
"d",
".",
"smallint",
"(",
"autoIncrement",
")",
",",
"true",
"\n",
"case",
"int",
",",
"int32",
",",
"uint",
",",
"uint32",
":",
"return",
"d",
".",
"integer",
"(",
"autoIncrement",
")",
",",
"false",
"\n",
"case",
"*",
"int",
",",
"*",
"int32",
",",
"*",
"uint",
",",
"*",
"uint32",
":",
"return",
"d",
".",
"integer",
"(",
"autoIncrement",
")",
",",
"true",
"\n",
"case",
"int64",
",",
"uint64",
":",
"return",
"d",
".",
"bigint",
"(",
"autoIncrement",
")",
",",
"false",
"\n",
"case",
"*",
"int64",
",",
"*",
"uint64",
",",
"sql",
".",
"NullInt64",
":",
"return",
"d",
".",
"bigint",
"(",
"autoIncrement",
")",
",",
"true",
"\n",
"case",
"string",
":",
"return",
"d",
".",
"varchar",
"(",
"size",
")",
",",
"false",
"\n",
"case",
"*",
"string",
",",
"sql",
".",
"NullString",
":",
"return",
"d",
".",
"varchar",
"(",
"size",
")",
",",
"true",
"\n",
"case",
"[",
"]",
"byte",
":",
"return",
"\"bytea\"",
",",
"true",
"\n",
"case",
"time",
".",
"Time",
":",
"return",
"\"timestamp with time zone\"",
",",
"false",
"\n",
"case",
"*",
"time",
".",
"Time",
":",
"return",
"\"timestamp with time zone\"",
",",
"true",
"\n",
"case",
"Rat",
":",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"numeric(%d, %d)\"",
",",
"decimalPrecision",
",",
"decimalScale",
")",
",",
"false",
"\n",
"case",
"*",
"Rat",
":",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"numeric(%d, %d)\"",
",",
"decimalPrecision",
",",
"decimalScale",
")",
",",
"true",
"\n",
"case",
"Float32",
",",
"Float64",
":",
"return",
"\"double precision\"",
",",
"false",
"\n",
"case",
"*",
"Float32",
",",
"*",
"Float64",
":",
"return",
"\"double precision\"",
",",
"true",
"\n",
"case",
"float32",
",",
"*",
"float32",
",",
"float64",
",",
"*",
"float64",
",",
"sql",
".",
"NullFloat64",
":",
"panic",
"(",
"ErrUsingFloatType",
")",
"\n",
"}",
"\n",
"panic",
"(",
"fmt",
".",
"Errorf",
"(",
"\"PostgresDialect: unsupported SQL type: %T\"",
",",
"v",
")",
")",
"\n",
"}"
] |
// SQLType returns the SQL type of the v for PostgreSQL.
|
[
"SQLType",
"returns",
"the",
"SQL",
"type",
"of",
"the",
"v",
"for",
"PostgreSQL",
"."
] |
78583835e1e41e3938e1ddfffd7101f8ad27fae0
|
https://github.com/naoina/genmai/blob/78583835e1e41e3938e1ddfffd7101f8ad27fae0/dialect.go#L251-L291
|
test
|
goreleaser/archive
|
archive.go
|
New
|
func New(file *os.File) Archive {
if filepath.Ext(file.Name()) == ".zip" {
return zip.New(file)
}
return tar.New(file)
}
|
go
|
func New(file *os.File) Archive {
if filepath.Ext(file.Name()) == ".zip" {
return zip.New(file)
}
return tar.New(file)
}
|
[
"func",
"New",
"(",
"file",
"*",
"os",
".",
"File",
")",
"Archive",
"{",
"if",
"filepath",
".",
"Ext",
"(",
"file",
".",
"Name",
"(",
")",
")",
"==",
"\".zip\"",
"{",
"return",
"zip",
".",
"New",
"(",
"file",
")",
"\n",
"}",
"\n",
"return",
"tar",
".",
"New",
"(",
"file",
")",
"\n",
"}"
] |
// New archive
// If the exentions of the target file is .zip, the archive will be in the zip
// format, otherwise, it will be a tar.gz archive.
|
[
"New",
"archive",
"If",
"the",
"exentions",
"of",
"the",
"target",
"file",
"is",
".",
"zip",
"the",
"archive",
"will",
"be",
"in",
"the",
"zip",
"format",
"otherwise",
"it",
"will",
"be",
"a",
"tar",
".",
"gz",
"archive",
"."
] |
9c6b0c177751034bab579499b81c69993ddfe563
|
https://github.com/goreleaser/archive/blob/9c6b0c177751034bab579499b81c69993ddfe563/archive.go#L22-L27
|
test
|
hooklift/govix
|
host.go
|
Disconnect
|
func (h *Host) Disconnect() {
// TODO(c4milo): Return an error to the user given that this error
// may be thrown due to insufficient hardware resources
if h.handle == C.VIX_E_CANCELLED {
return
}
if &h.handle != nil &&
h.handle != C.VIX_INVALID_HANDLE {
C.VixHost_Disconnect(h.handle)
h.handle = C.VIX_INVALID_HANDLE
}
}
|
go
|
func (h *Host) Disconnect() {
// TODO(c4milo): Return an error to the user given that this error
// may be thrown due to insufficient hardware resources
if h.handle == C.VIX_E_CANCELLED {
return
}
if &h.handle != nil &&
h.handle != C.VIX_INVALID_HANDLE {
C.VixHost_Disconnect(h.handle)
h.handle = C.VIX_INVALID_HANDLE
}
}
|
[
"func",
"(",
"h",
"*",
"Host",
")",
"Disconnect",
"(",
")",
"{",
"if",
"h",
".",
"handle",
"==",
"C",
".",
"VIX_E_CANCELLED",
"{",
"return",
"\n",
"}",
"\n",
"if",
"&",
"h",
".",
"handle",
"!=",
"nil",
"&&",
"h",
".",
"handle",
"!=",
"C",
".",
"VIX_INVALID_HANDLE",
"{",
"C",
".",
"VixHost_Disconnect",
"(",
"h",
".",
"handle",
")",
"\n",
"h",
".",
"handle",
"=",
"C",
".",
"VIX_INVALID_HANDLE",
"\n",
"}",
"\n",
"}"
] |
// Disconnect destroys the state for a particular host instance.
//
// Call this function to disconnect the host. After you call this function the
// Host object is no longer valid and you should not longer use it.
// Similarly, you should not use any other object instances obtained from the
// Host object while it was connected.
//
// Since VMware Server 1.0
|
[
"Disconnect",
"destroys",
"the",
"state",
"for",
"a",
"particular",
"host",
"instance",
".",
"Call",
"this",
"function",
"to",
"disconnect",
"the",
"host",
".",
"After",
"you",
"call",
"this",
"function",
"the",
"Host",
"object",
"is",
"no",
"longer",
"valid",
"and",
"you",
"should",
"not",
"longer",
"use",
"it",
".",
"Similarly",
"you",
"should",
"not",
"use",
"any",
"other",
"object",
"instances",
"obtained",
"from",
"the",
"Host",
"object",
"while",
"it",
"was",
"connected",
".",
"Since",
"VMware",
"Server",
"1",
".",
"0"
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/host.go#L31-L44
|
test
|
hooklift/govix
|
network.go
|
nextNetworkAdapterID
|
func (v *VM) nextNetworkAdapterID(vmx map[string]string) int {
var nextID int
prefix := "ethernet"
for key := range vmx {
if strings.HasPrefix(key, prefix) {
ethN := strings.Split(key, ".")[0]
number, _ := strconv.Atoi(strings.Split(ethN, prefix)[1])
// If ethN is not present, its id is recycled
if vmx[ethN+".present"] == "FALSE" {
return number
}
if number > nextID {
nextID = number
}
}
}
nextID++
return nextID
}
|
go
|
func (v *VM) nextNetworkAdapterID(vmx map[string]string) int {
var nextID int
prefix := "ethernet"
for key := range vmx {
if strings.HasPrefix(key, prefix) {
ethN := strings.Split(key, ".")[0]
number, _ := strconv.Atoi(strings.Split(ethN, prefix)[1])
// If ethN is not present, its id is recycled
if vmx[ethN+".present"] == "FALSE" {
return number
}
if number > nextID {
nextID = number
}
}
}
nextID++
return nextID
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"nextNetworkAdapterID",
"(",
"vmx",
"map",
"[",
"string",
"]",
"string",
")",
"int",
"{",
"var",
"nextID",
"int",
"\n",
"prefix",
":=",
"\"ethernet\"",
"\n",
"for",
"key",
":=",
"range",
"vmx",
"{",
"if",
"strings",
".",
"HasPrefix",
"(",
"key",
",",
"prefix",
")",
"{",
"ethN",
":=",
"strings",
".",
"Split",
"(",
"key",
",",
"\".\"",
")",
"[",
"0",
"]",
"\n",
"number",
",",
"_",
":=",
"strconv",
".",
"Atoi",
"(",
"strings",
".",
"Split",
"(",
"ethN",
",",
"prefix",
")",
"[",
"1",
"]",
")",
"\n",
"if",
"vmx",
"[",
"ethN",
"+",
"\".present\"",
"]",
"==",
"\"FALSE\"",
"{",
"return",
"number",
"\n",
"}",
"\n",
"if",
"number",
">",
"nextID",
"{",
"nextID",
"=",
"number",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"nextID",
"++",
"\n",
"return",
"nextID",
"\n",
"}"
] |
// nextNetworkAdapterID returns the next available ethernet ID, reusing ids if
// the ethernet adapter has "present" equal to "FALSE"
|
[
"nextNetworkAdapterID",
"returns",
"the",
"next",
"available",
"ethernet",
"ID",
"reusing",
"ids",
"if",
"the",
"ethernet",
"adapter",
"has",
"present",
"equal",
"to",
"FALSE"
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/network.go#L305-L328
|
test
|
hooklift/govix
|
network.go
|
totalNetworkAdapters
|
func (v *VM) totalNetworkAdapters(vmx map[string]string) int {
var total int
prefix := "ethernet"
for key := range vmx {
if strings.HasPrefix(key, prefix) {
ethN := strings.Split(key, ".")[0]
number, _ := strconv.Atoi(strings.Split(ethN, prefix)[1])
if number > total {
total = number
}
}
}
return total
}
|
go
|
func (v *VM) totalNetworkAdapters(vmx map[string]string) int {
var total int
prefix := "ethernet"
for key := range vmx {
if strings.HasPrefix(key, prefix) {
ethN := strings.Split(key, ".")[0]
number, _ := strconv.Atoi(strings.Split(ethN, prefix)[1])
if number > total {
total = number
}
}
}
return total
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"totalNetworkAdapters",
"(",
"vmx",
"map",
"[",
"string",
"]",
"string",
")",
"int",
"{",
"var",
"total",
"int",
"\n",
"prefix",
":=",
"\"ethernet\"",
"\n",
"for",
"key",
":=",
"range",
"vmx",
"{",
"if",
"strings",
".",
"HasPrefix",
"(",
"key",
",",
"prefix",
")",
"{",
"ethN",
":=",
"strings",
".",
"Split",
"(",
"key",
",",
"\".\"",
")",
"[",
"0",
"]",
"\n",
"number",
",",
"_",
":=",
"strconv",
".",
"Atoi",
"(",
"strings",
".",
"Split",
"(",
"ethN",
",",
"prefix",
")",
"[",
"1",
"]",
")",
"\n",
"if",
"number",
">",
"total",
"{",
"total",
"=",
"number",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"total",
"\n",
"}"
] |
// totalNetworkAdapters returns the total number of network adapters in the VMX file.
|
[
"totalNetworkAdapters",
"returns",
"the",
"total",
"number",
"of",
"network",
"adapters",
"in",
"the",
"VMX",
"file",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/network.go#L331-L347
|
test
|
hooklift/govix
|
network.go
|
RemoveAllNetworkAdapters
|
func (v *VM) RemoveAllNetworkAdapters() error {
vmxPath, err := v.VmxPath()
if err != nil {
return err
}
vmx, err := readVmx(vmxPath)
if err != nil {
return err
}
for key := range vmx {
if strings.HasPrefix(key, "ethernet") {
delete(vmx, key)
}
}
return writeVmx(vmxPath, vmx)
}
|
go
|
func (v *VM) RemoveAllNetworkAdapters() error {
vmxPath, err := v.VmxPath()
if err != nil {
return err
}
vmx, err := readVmx(vmxPath)
if err != nil {
return err
}
for key := range vmx {
if strings.HasPrefix(key, "ethernet") {
delete(vmx, key)
}
}
return writeVmx(vmxPath, vmx)
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"RemoveAllNetworkAdapters",
"(",
")",
"error",
"{",
"vmxPath",
",",
"err",
":=",
"v",
".",
"VmxPath",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"vmx",
",",
"err",
":=",
"readVmx",
"(",
"vmxPath",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"for",
"key",
":=",
"range",
"vmx",
"{",
"if",
"strings",
".",
"HasPrefix",
"(",
"key",
",",
"\"ethernet\"",
")",
"{",
"delete",
"(",
"vmx",
",",
"key",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"writeVmx",
"(",
"vmxPath",
",",
"vmx",
")",
"\n",
"}"
] |
// RemoveAllNetworkAdapters deletes all network adapters from a VM.
|
[
"RemoveAllNetworkAdapters",
"deletes",
"all",
"network",
"adapters",
"from",
"a",
"VM",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/network.go#L350-L368
|
test
|
hooklift/govix
|
network.go
|
RemoveNetworkAdapter
|
func (v *VM) RemoveNetworkAdapter(adapter *NetworkAdapter) error {
isVMRunning, err := v.IsRunning()
if err != nil {
return err
}
if isVMRunning {
return &Error{
Operation: "vm.RemoveNetworkAdapter",
Code: 100000,
Text: "The VM has to be powered off in order to change its vmx settings",
}
}
vmxPath, err := v.VmxPath()
if err != nil {
return err
}
vmx, err := readVmx(vmxPath)
if err != nil {
return err
}
device := "ethernet" + adapter.ID
for key := range vmx {
if strings.HasPrefix(key, device) {
delete(vmx, key)
}
}
vmx[device+".present"] = "FALSE"
err = writeVmx(vmxPath, vmx)
if err != nil {
return err
}
return nil
}
|
go
|
func (v *VM) RemoveNetworkAdapter(adapter *NetworkAdapter) error {
isVMRunning, err := v.IsRunning()
if err != nil {
return err
}
if isVMRunning {
return &Error{
Operation: "vm.RemoveNetworkAdapter",
Code: 100000,
Text: "The VM has to be powered off in order to change its vmx settings",
}
}
vmxPath, err := v.VmxPath()
if err != nil {
return err
}
vmx, err := readVmx(vmxPath)
if err != nil {
return err
}
device := "ethernet" + adapter.ID
for key := range vmx {
if strings.HasPrefix(key, device) {
delete(vmx, key)
}
}
vmx[device+".present"] = "FALSE"
err = writeVmx(vmxPath, vmx)
if err != nil {
return err
}
return nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"RemoveNetworkAdapter",
"(",
"adapter",
"*",
"NetworkAdapter",
")",
"error",
"{",
"isVMRunning",
",",
"err",
":=",
"v",
".",
"IsRunning",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"isVMRunning",
"{",
"return",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.RemoveNetworkAdapter\"",
",",
"Code",
":",
"100000",
",",
"Text",
":",
"\"The VM has to be powered off in order to change its vmx settings\"",
",",
"}",
"\n",
"}",
"\n",
"vmxPath",
",",
"err",
":=",
"v",
".",
"VmxPath",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"vmx",
",",
"err",
":=",
"readVmx",
"(",
"vmxPath",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"device",
":=",
"\"ethernet\"",
"+",
"adapter",
".",
"ID",
"\n",
"for",
"key",
":=",
"range",
"vmx",
"{",
"if",
"strings",
".",
"HasPrefix",
"(",
"key",
",",
"device",
")",
"{",
"delete",
"(",
"vmx",
",",
"key",
")",
"\n",
"}",
"\n",
"}",
"\n",
"vmx",
"[",
"device",
"+",
"\".present\"",
"]",
"=",
"\"FALSE\"",
"\n",
"err",
"=",
"writeVmx",
"(",
"vmxPath",
",",
"vmx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// RemoveNetworkAdapter deletes network adapter from VMX file that matches the ID in "adapter.Id".
|
[
"RemoveNetworkAdapter",
"deletes",
"network",
"adapter",
"from",
"VMX",
"file",
"that",
"matches",
"the",
"ID",
"in",
"adapter",
".",
"Id",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/network.go#L371-L411
|
test
|
hooklift/govix
|
network.go
|
NetworkAdapters
|
func (v *VM) NetworkAdapters() ([]*NetworkAdapter, error) {
vmxPath, err := v.VmxPath()
if err != nil {
return nil, err
}
vmx, err := readVmx(vmxPath)
if err != nil {
return nil, err
}
var adapters []*NetworkAdapter
// VMX ethernet adapters seem to not be zero based
for i := 1; i <= v.totalNetworkAdapters(vmx); i++ {
id := strconv.Itoa(i)
prefix := "ethernet" + id
if vmx[prefix+".present"] == "FALSE" {
continue
}
wakeOnPckRcv, _ := strconv.ParseBool(vmx[prefix+".wakeOnPcktRcv"])
lnkStateProp, _ := strconv.ParseBool(vmx[prefix+".linkStatePropagation.enable"])
present, _ := strconv.ParseBool(vmx[prefix+".present"])
startConnected, _ := strconv.ParseBool(vmx[prefix+".startConnected"])
address, _ := net.ParseMAC(vmx[prefix+".address"])
genAddress, _ := net.ParseMAC(vmx[prefix+".generatedAddress"])
vswitch, _ := GetVSwitch(vmx[prefix+".vnet"])
adapter := &NetworkAdapter{
ID: id,
present: present,
ConnType: NetworkType(vmx[prefix+".connectionType"]),
Vdevice: VNetDevice(vmx[prefix+".virtualDev"]),
WakeOnPcktRcv: wakeOnPckRcv,
LinkStatePropagation: lnkStateProp,
MacAddrType: MacAddressType(vmx[prefix+".addressType"]),
MacAddress: address,
VSwitch: vswitch,
StartConnected: startConnected,
GeneratedMacAddress: genAddress,
GeneratedMacAddressOffset: vmx[prefix+".generatedAddressOffset"],
PciSlotNumber: vmx[prefix+".pciSlotNumber"],
}
adapters = append(adapters, adapter)
}
return adapters, nil
}
|
go
|
func (v *VM) NetworkAdapters() ([]*NetworkAdapter, error) {
vmxPath, err := v.VmxPath()
if err != nil {
return nil, err
}
vmx, err := readVmx(vmxPath)
if err != nil {
return nil, err
}
var adapters []*NetworkAdapter
// VMX ethernet adapters seem to not be zero based
for i := 1; i <= v.totalNetworkAdapters(vmx); i++ {
id := strconv.Itoa(i)
prefix := "ethernet" + id
if vmx[prefix+".present"] == "FALSE" {
continue
}
wakeOnPckRcv, _ := strconv.ParseBool(vmx[prefix+".wakeOnPcktRcv"])
lnkStateProp, _ := strconv.ParseBool(vmx[prefix+".linkStatePropagation.enable"])
present, _ := strconv.ParseBool(vmx[prefix+".present"])
startConnected, _ := strconv.ParseBool(vmx[prefix+".startConnected"])
address, _ := net.ParseMAC(vmx[prefix+".address"])
genAddress, _ := net.ParseMAC(vmx[prefix+".generatedAddress"])
vswitch, _ := GetVSwitch(vmx[prefix+".vnet"])
adapter := &NetworkAdapter{
ID: id,
present: present,
ConnType: NetworkType(vmx[prefix+".connectionType"]),
Vdevice: VNetDevice(vmx[prefix+".virtualDev"]),
WakeOnPcktRcv: wakeOnPckRcv,
LinkStatePropagation: lnkStateProp,
MacAddrType: MacAddressType(vmx[prefix+".addressType"]),
MacAddress: address,
VSwitch: vswitch,
StartConnected: startConnected,
GeneratedMacAddress: genAddress,
GeneratedMacAddressOffset: vmx[prefix+".generatedAddressOffset"],
PciSlotNumber: vmx[prefix+".pciSlotNumber"],
}
adapters = append(adapters, adapter)
}
return adapters, nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"NetworkAdapters",
"(",
")",
"(",
"[",
"]",
"*",
"NetworkAdapter",
",",
"error",
")",
"{",
"vmxPath",
",",
"err",
":=",
"v",
".",
"VmxPath",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"vmx",
",",
"err",
":=",
"readVmx",
"(",
"vmxPath",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"var",
"adapters",
"[",
"]",
"*",
"NetworkAdapter",
"\n",
"for",
"i",
":=",
"1",
";",
"i",
"<=",
"v",
".",
"totalNetworkAdapters",
"(",
"vmx",
")",
";",
"i",
"++",
"{",
"id",
":=",
"strconv",
".",
"Itoa",
"(",
"i",
")",
"\n",
"prefix",
":=",
"\"ethernet\"",
"+",
"id",
"\n",
"if",
"vmx",
"[",
"prefix",
"+",
"\".present\"",
"]",
"==",
"\"FALSE\"",
"{",
"continue",
"\n",
"}",
"\n",
"wakeOnPckRcv",
",",
"_",
":=",
"strconv",
".",
"ParseBool",
"(",
"vmx",
"[",
"prefix",
"+",
"\".wakeOnPcktRcv\"",
"]",
")",
"\n",
"lnkStateProp",
",",
"_",
":=",
"strconv",
".",
"ParseBool",
"(",
"vmx",
"[",
"prefix",
"+",
"\".linkStatePropagation.enable\"",
"]",
")",
"\n",
"present",
",",
"_",
":=",
"strconv",
".",
"ParseBool",
"(",
"vmx",
"[",
"prefix",
"+",
"\".present\"",
"]",
")",
"\n",
"startConnected",
",",
"_",
":=",
"strconv",
".",
"ParseBool",
"(",
"vmx",
"[",
"prefix",
"+",
"\".startConnected\"",
"]",
")",
"\n",
"address",
",",
"_",
":=",
"net",
".",
"ParseMAC",
"(",
"vmx",
"[",
"prefix",
"+",
"\".address\"",
"]",
")",
"\n",
"genAddress",
",",
"_",
":=",
"net",
".",
"ParseMAC",
"(",
"vmx",
"[",
"prefix",
"+",
"\".generatedAddress\"",
"]",
")",
"\n",
"vswitch",
",",
"_",
":=",
"GetVSwitch",
"(",
"vmx",
"[",
"prefix",
"+",
"\".vnet\"",
"]",
")",
"\n",
"adapter",
":=",
"&",
"NetworkAdapter",
"{",
"ID",
":",
"id",
",",
"present",
":",
"present",
",",
"ConnType",
":",
"NetworkType",
"(",
"vmx",
"[",
"prefix",
"+",
"\".connectionType\"",
"]",
")",
",",
"Vdevice",
":",
"VNetDevice",
"(",
"vmx",
"[",
"prefix",
"+",
"\".virtualDev\"",
"]",
")",
",",
"WakeOnPcktRcv",
":",
"wakeOnPckRcv",
",",
"LinkStatePropagation",
":",
"lnkStateProp",
",",
"MacAddrType",
":",
"MacAddressType",
"(",
"vmx",
"[",
"prefix",
"+",
"\".addressType\"",
"]",
")",
",",
"MacAddress",
":",
"address",
",",
"VSwitch",
":",
"vswitch",
",",
"StartConnected",
":",
"startConnected",
",",
"GeneratedMacAddress",
":",
"genAddress",
",",
"GeneratedMacAddressOffset",
":",
"vmx",
"[",
"prefix",
"+",
"\".generatedAddressOffset\"",
"]",
",",
"PciSlotNumber",
":",
"vmx",
"[",
"prefix",
"+",
"\".pciSlotNumber\"",
"]",
",",
"}",
"\n",
"adapters",
"=",
"append",
"(",
"adapters",
",",
"adapter",
")",
"\n",
"}",
"\n",
"return",
"adapters",
",",
"nil",
"\n",
"}"
] |
// NetworkAdapters lists current network adapters attached to the virtual
// machine.
|
[
"NetworkAdapters",
"lists",
"current",
"network",
"adapters",
"attached",
"to",
"the",
"virtual",
"machine",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/network.go#L415-L464
|
test
|
kjk/lzmadec
|
lzmadec.go
|
newArchive
|
func newArchive(path string, password *string) (*Archive, error) {
err := detect7zCached()
if err != nil {
return nil, err
}
cmd := exec.Command("7z", "l", "-slt", "-sccUTF-8", path)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
entries, err := parse7zListOutput(out)
if err != nil {
return nil, err
}
return &Archive{
Path: path,
Entries: entries,
password: password,
}, nil
}
|
go
|
func newArchive(path string, password *string) (*Archive, error) {
err := detect7zCached()
if err != nil {
return nil, err
}
cmd := exec.Command("7z", "l", "-slt", "-sccUTF-8", path)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
entries, err := parse7zListOutput(out)
if err != nil {
return nil, err
}
return &Archive{
Path: path,
Entries: entries,
password: password,
}, nil
}
|
[
"func",
"newArchive",
"(",
"path",
"string",
",",
"password",
"*",
"string",
")",
"(",
"*",
"Archive",
",",
"error",
")",
"{",
"err",
":=",
"detect7zCached",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"cmd",
":=",
"exec",
".",
"Command",
"(",
"\"7z\"",
",",
"\"l\"",
",",
"\"-slt\"",
",",
"\"-sccUTF-8\"",
",",
"path",
")",
"\n",
"out",
",",
"err",
":=",
"cmd",
".",
"CombinedOutput",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"entries",
",",
"err",
":=",
"parse7zListOutput",
"(",
"out",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"&",
"Archive",
"{",
"Path",
":",
"path",
",",
"Entries",
":",
"entries",
",",
"password",
":",
"password",
",",
"}",
",",
"nil",
"\n",
"}"
] |
// NewArchive uses 7z to extract a list of files in .7z archive
|
[
"NewArchive",
"uses",
"7z",
"to",
"extract",
"a",
"list",
"of",
"files",
"in",
".",
"7z",
"archive"
] |
4c61f00ce86f6f11ed9630fa16b771889b08147b
|
https://github.com/kjk/lzmadec/blob/4c61f00ce86f6f11ed9630fa16b771889b08147b/lzmadec.go#L198-L217
|
test
|
kjk/lzmadec
|
lzmadec.go
|
GetFileReader
|
func (a *Archive) GetFileReader(name string) (io.ReadCloser, error) {
found := false
for _, e := range a.Entries {
if e.Path == name {
found = true
break
}
}
if !found {
return nil, errors.New("file not in the archive")
}
params := []string{"x", "-so"}
if a.password != nil {
params = append(params, fmt.Sprintf("-p%s", *a.password))
}
params = append(params, a.Path, name)
cmd := exec.Command("7z", params...)
stdout, err := cmd.StdoutPipe()
rc := &readCloser{
rc: stdout,
cmd: cmd,
}
err = cmd.Start()
if err != nil {
stdout.Close()
return nil, err
}
return rc, nil
}
|
go
|
func (a *Archive) GetFileReader(name string) (io.ReadCloser, error) {
found := false
for _, e := range a.Entries {
if e.Path == name {
found = true
break
}
}
if !found {
return nil, errors.New("file not in the archive")
}
params := []string{"x", "-so"}
if a.password != nil {
params = append(params, fmt.Sprintf("-p%s", *a.password))
}
params = append(params, a.Path, name)
cmd := exec.Command("7z", params...)
stdout, err := cmd.StdoutPipe()
rc := &readCloser{
rc: stdout,
cmd: cmd,
}
err = cmd.Start()
if err != nil {
stdout.Close()
return nil, err
}
return rc, nil
}
|
[
"func",
"(",
"a",
"*",
"Archive",
")",
"GetFileReader",
"(",
"name",
"string",
")",
"(",
"io",
".",
"ReadCloser",
",",
"error",
")",
"{",
"found",
":=",
"false",
"\n",
"for",
"_",
",",
"e",
":=",
"range",
"a",
".",
"Entries",
"{",
"if",
"e",
".",
"Path",
"==",
"name",
"{",
"found",
"=",
"true",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"!",
"found",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"file not in the archive\"",
")",
"\n",
"}",
"\n",
"params",
":=",
"[",
"]",
"string",
"{",
"\"x\"",
",",
"\"-so\"",
"}",
"\n",
"if",
"a",
".",
"password",
"!=",
"nil",
"{",
"params",
"=",
"append",
"(",
"params",
",",
"fmt",
".",
"Sprintf",
"(",
"\"-p%s\"",
",",
"*",
"a",
".",
"password",
")",
")",
"\n",
"}",
"\n",
"params",
"=",
"append",
"(",
"params",
",",
"a",
".",
"Path",
",",
"name",
")",
"\n",
"cmd",
":=",
"exec",
".",
"Command",
"(",
"\"7z\"",
",",
"params",
"...",
")",
"\n",
"stdout",
",",
"err",
":=",
"cmd",
".",
"StdoutPipe",
"(",
")",
"\n",
"rc",
":=",
"&",
"readCloser",
"{",
"rc",
":",
"stdout",
",",
"cmd",
":",
"cmd",
",",
"}",
"\n",
"err",
"=",
"cmd",
".",
"Start",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"stdout",
".",
"Close",
"(",
")",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"rc",
",",
"nil",
"\n",
"}"
] |
// GetFileReader returns a reader for reading a given file
|
[
"GetFileReader",
"returns",
"a",
"reader",
"for",
"reading",
"a",
"given",
"file"
] |
4c61f00ce86f6f11ed9630fa16b771889b08147b
|
https://github.com/kjk/lzmadec/blob/4c61f00ce86f6f11ed9630fa16b771889b08147b/lzmadec.go#L238-L268
|
test
|
kjk/lzmadec
|
lzmadec.go
|
ExtractToWriter
|
func (a *Archive) ExtractToWriter(dst io.Writer, name string) error {
r, err := a.GetFileReader(name)
if err != nil {
return err
}
_, err = io.Copy(dst, r)
err2 := r.Close()
if err != nil {
return err
}
return err2
}
|
go
|
func (a *Archive) ExtractToWriter(dst io.Writer, name string) error {
r, err := a.GetFileReader(name)
if err != nil {
return err
}
_, err = io.Copy(dst, r)
err2 := r.Close()
if err != nil {
return err
}
return err2
}
|
[
"func",
"(",
"a",
"*",
"Archive",
")",
"ExtractToWriter",
"(",
"dst",
"io",
".",
"Writer",
",",
"name",
"string",
")",
"error",
"{",
"r",
",",
"err",
":=",
"a",
".",
"GetFileReader",
"(",
"name",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"_",
",",
"err",
"=",
"io",
".",
"Copy",
"(",
"dst",
",",
"r",
")",
"\n",
"err2",
":=",
"r",
".",
"Close",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"err2",
"\n",
"}"
] |
// ExtractToWriter writes the content of a given file inside the archive to dst
|
[
"ExtractToWriter",
"writes",
"the",
"content",
"of",
"a",
"given",
"file",
"inside",
"the",
"archive",
"to",
"dst"
] |
4c61f00ce86f6f11ed9630fa16b771889b08147b
|
https://github.com/kjk/lzmadec/blob/4c61f00ce86f6f11ed9630fa16b771889b08147b/lzmadec.go#L271-L282
|
test
|
kjk/lzmadec
|
lzmadec.go
|
ExtractToFile
|
func (a *Archive) ExtractToFile(dstPath string, name string) error {
f, err := os.Create(dstPath)
if err != nil {
return err
}
defer f.Close()
return a.ExtractToWriter(f, name)
}
|
go
|
func (a *Archive) ExtractToFile(dstPath string, name string) error {
f, err := os.Create(dstPath)
if err != nil {
return err
}
defer f.Close()
return a.ExtractToWriter(f, name)
}
|
[
"func",
"(",
"a",
"*",
"Archive",
")",
"ExtractToFile",
"(",
"dstPath",
"string",
",",
"name",
"string",
")",
"error",
"{",
"f",
",",
"err",
":=",
"os",
".",
"Create",
"(",
"dstPath",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"f",
".",
"Close",
"(",
")",
"\n",
"return",
"a",
".",
"ExtractToWriter",
"(",
"f",
",",
"name",
")",
"\n",
"}"
] |
// ExtractToFile extracts a given file from the archive to a file on disk
|
[
"ExtractToFile",
"extracts",
"a",
"given",
"file",
"from",
"the",
"archive",
"to",
"a",
"file",
"on",
"disk"
] |
4c61f00ce86f6f11ed9630fa16b771889b08147b
|
https://github.com/kjk/lzmadec/blob/4c61f00ce86f6f11ed9630fa16b771889b08147b/lzmadec.go#L285-L292
|
test
|
hooklift/govix
|
guest.go
|
SharedFoldersParentDir
|
func (g *Guest) SharedFoldersParentDir() (string, error) {
var err C.VixError = C.VIX_OK
var path *C.char
err = C.get_property(g.handle,
C.VIX_PROPERTY_GUEST_SHAREDFOLDERS_SHARES_PATH,
unsafe.Pointer(&path))
defer C.Vix_FreeBuffer(unsafe.Pointer(path))
if C.VIX_OK != err {
return "", &Error{
Operation: "guest.SharedFoldersParentDir",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(path), nil
}
|
go
|
func (g *Guest) SharedFoldersParentDir() (string, error) {
var err C.VixError = C.VIX_OK
var path *C.char
err = C.get_property(g.handle,
C.VIX_PROPERTY_GUEST_SHAREDFOLDERS_SHARES_PATH,
unsafe.Pointer(&path))
defer C.Vix_FreeBuffer(unsafe.Pointer(path))
if C.VIX_OK != err {
return "", &Error{
Operation: "guest.SharedFoldersParentDir",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(path), nil
}
|
[
"func",
"(",
"g",
"*",
"Guest",
")",
"SharedFoldersParentDir",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"var",
"path",
"*",
"C",
".",
"char",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"g",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_GUEST_SHAREDFOLDERS_SHARES_PATH",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"path",
")",
")",
"\n",
"defer",
"C",
".",
"Vix_FreeBuffer",
"(",
"unsafe",
".",
"Pointer",
"(",
"path",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"\"\"",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"guest.SharedFoldersParentDir\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"C",
".",
"GoString",
"(",
"path",
")",
",",
"nil",
"\n",
"}"
] |
// SharedFoldersParentDir returns the parent dir for share folders in the Guest.
|
[
"SharedFoldersParentDir",
"returns",
"the",
"parent",
"dir",
"for",
"share",
"folders",
"in",
"the",
"Guest",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/guest.go#L23-L42
|
test
|
hooklift/govix
|
snapshot.go
|
Name
|
func (s *Snapshot) Name() (string, error) {
var err C.VixError = C.VIX_OK
var name *C.char
err = C.get_property(s.handle,
C.VIX_PROPERTY_SNAPSHOT_DISPLAYNAME,
unsafe.Pointer(&name))
defer C.Vix_FreeBuffer(unsafe.Pointer(name))
if C.VIX_OK != err {
return "", &Error{
Operation: "snapshot.Name",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(name), nil
}
|
go
|
func (s *Snapshot) Name() (string, error) {
var err C.VixError = C.VIX_OK
var name *C.char
err = C.get_property(s.handle,
C.VIX_PROPERTY_SNAPSHOT_DISPLAYNAME,
unsafe.Pointer(&name))
defer C.Vix_FreeBuffer(unsafe.Pointer(name))
if C.VIX_OK != err {
return "", &Error{
Operation: "snapshot.Name",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(name), nil
}
|
[
"func",
"(",
"s",
"*",
"Snapshot",
")",
"Name",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"var",
"name",
"*",
"C",
".",
"char",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"s",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_SNAPSHOT_DISPLAYNAME",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"name",
")",
")",
"\n",
"defer",
"C",
".",
"Vix_FreeBuffer",
"(",
"unsafe",
".",
"Pointer",
"(",
"name",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"\"\"",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"snapshot.Name\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"C",
".",
"GoString",
"(",
"name",
")",
",",
"nil",
"\n",
"}"
] |
// Name returns user defined name for the snapshot.
|
[
"Name",
"returns",
"user",
"defined",
"name",
"for",
"the",
"snapshot",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/snapshot.go#L25-L44
|
test
|
hooklift/govix
|
snapshot.go
|
Description
|
func (s *Snapshot) Description() (string, error) {
var err C.VixError = C.VIX_OK
var desc *C.char
err = C.get_property(s.handle,
C.VIX_PROPERTY_SNAPSHOT_DESCRIPTION,
unsafe.Pointer(&desc))
defer C.Vix_FreeBuffer(unsafe.Pointer(desc))
if C.VIX_OK != err {
return "", &Error{
Operation: "snapshot.Description",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(desc), nil
}
|
go
|
func (s *Snapshot) Description() (string, error) {
var err C.VixError = C.VIX_OK
var desc *C.char
err = C.get_property(s.handle,
C.VIX_PROPERTY_SNAPSHOT_DESCRIPTION,
unsafe.Pointer(&desc))
defer C.Vix_FreeBuffer(unsafe.Pointer(desc))
if C.VIX_OK != err {
return "", &Error{
Operation: "snapshot.Description",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(desc), nil
}
|
[
"func",
"(",
"s",
"*",
"Snapshot",
")",
"Description",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"var",
"desc",
"*",
"C",
".",
"char",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"s",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_SNAPSHOT_DESCRIPTION",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"desc",
")",
")",
"\n",
"defer",
"C",
".",
"Vix_FreeBuffer",
"(",
"unsafe",
".",
"Pointer",
"(",
"desc",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"\"\"",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"snapshot.Description\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"C",
".",
"GoString",
"(",
"desc",
")",
",",
"nil",
"\n",
"}"
] |
// Description returns user defined description for the snapshot.
|
[
"Description",
"returns",
"user",
"defined",
"description",
"for",
"the",
"snapshot",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/snapshot.go#L47-L66
|
test
|
hooklift/govix
|
snapshot.go
|
cleanupSnapshot
|
func cleanupSnapshot(s *Snapshot) {
if s.handle != C.VIX_INVALID_HANDLE {
C.Vix_ReleaseHandle(s.handle)
s.handle = C.VIX_INVALID_HANDLE
}
}
|
go
|
func cleanupSnapshot(s *Snapshot) {
if s.handle != C.VIX_INVALID_HANDLE {
C.Vix_ReleaseHandle(s.handle)
s.handle = C.VIX_INVALID_HANDLE
}
}
|
[
"func",
"cleanupSnapshot",
"(",
"s",
"*",
"Snapshot",
")",
"{",
"if",
"s",
".",
"handle",
"!=",
"C",
".",
"VIX_INVALID_HANDLE",
"{",
"C",
".",
"Vix_ReleaseHandle",
"(",
"s",
".",
"handle",
")",
"\n",
"s",
".",
"handle",
"=",
"C",
".",
"VIX_INVALID_HANDLE",
"\n",
"}",
"\n",
"}"
] |
// cleanupSnapshot cleans up snapshot internal C handle.
|
[
"cleanupSnapshot",
"cleans",
"up",
"snapshot",
"internal",
"C",
"handle",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/snapshot.go#L162-L167
|
test
|
hooklift/govix
|
cddvd.go
|
BusTypeFromID
|
func BusTypeFromID(ID string) vmx.BusType {
var bus vmx.BusType
switch {
case strings.HasPrefix(ID, string(vmx.IDE)):
bus = vmx.IDE
case strings.HasPrefix(ID, string(vmx.SCSI)):
bus = vmx.SCSI
case strings.HasPrefix(ID, string(vmx.SATA)):
bus = vmx.SATA
}
return bus
}
|
go
|
func BusTypeFromID(ID string) vmx.BusType {
var bus vmx.BusType
switch {
case strings.HasPrefix(ID, string(vmx.IDE)):
bus = vmx.IDE
case strings.HasPrefix(ID, string(vmx.SCSI)):
bus = vmx.SCSI
case strings.HasPrefix(ID, string(vmx.SATA)):
bus = vmx.SATA
}
return bus
}
|
[
"func",
"BusTypeFromID",
"(",
"ID",
"string",
")",
"vmx",
".",
"BusType",
"{",
"var",
"bus",
"vmx",
".",
"BusType",
"\n",
"switch",
"{",
"case",
"strings",
".",
"HasPrefix",
"(",
"ID",
",",
"string",
"(",
"vmx",
".",
"IDE",
")",
")",
":",
"bus",
"=",
"vmx",
".",
"IDE",
"\n",
"case",
"strings",
".",
"HasPrefix",
"(",
"ID",
",",
"string",
"(",
"vmx",
".",
"SCSI",
")",
")",
":",
"bus",
"=",
"vmx",
".",
"SCSI",
"\n",
"case",
"strings",
".",
"HasPrefix",
"(",
"ID",
",",
"string",
"(",
"vmx",
".",
"SATA",
")",
")",
":",
"bus",
"=",
"vmx",
".",
"SATA",
"\n",
"}",
"\n",
"return",
"bus",
"\n",
"}"
] |
// BusTypeFromID gets BusType from device ID.
|
[
"BusTypeFromID",
"gets",
"BusType",
"from",
"device",
"ID",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/cddvd.go#L178-L190
|
test
|
hooklift/govix
|
vmx.go
|
Read
|
func (vmxfile *VMXFile) Read() error {
data, err := ioutil.ReadFile(vmxfile.path)
if err != nil {
return err
}
model := new(vmx.VirtualMachine)
err = vmx.Unmarshal(data, model)
if err != nil {
return err
}
vmxfile.model = model
return nil
}
|
go
|
func (vmxfile *VMXFile) Read() error {
data, err := ioutil.ReadFile(vmxfile.path)
if err != nil {
return err
}
model := new(vmx.VirtualMachine)
err = vmx.Unmarshal(data, model)
if err != nil {
return err
}
vmxfile.model = model
return nil
}
|
[
"func",
"(",
"vmxfile",
"*",
"VMXFile",
")",
"Read",
"(",
")",
"error",
"{",
"data",
",",
"err",
":=",
"ioutil",
".",
"ReadFile",
"(",
"vmxfile",
".",
"path",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"model",
":=",
"new",
"(",
"vmx",
".",
"VirtualMachine",
")",
"\n",
"err",
"=",
"vmx",
".",
"Unmarshal",
"(",
"data",
",",
"model",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"vmxfile",
".",
"model",
"=",
"model",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Read reads VMX file from disk and unmarshals it
|
[
"Read",
"reads",
"VMX",
"file",
"from",
"disk",
"and",
"unmarshals",
"it"
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vmx.go#L27-L43
|
test
|
hooklift/govix
|
vmx.go
|
Write
|
func (vmxfile *VMXFile) Write() error {
file, err := os.Create(vmxfile.path)
if err != nil {
return err
}
defer file.Close()
data, err := vmx.Marshal(vmxfile.model)
if err != nil {
return err
}
_, err = file.Write(data)
if err != nil {
return err
}
return nil
}
|
go
|
func (vmxfile *VMXFile) Write() error {
file, err := os.Create(vmxfile.path)
if err != nil {
return err
}
defer file.Close()
data, err := vmx.Marshal(vmxfile.model)
if err != nil {
return err
}
_, err = file.Write(data)
if err != nil {
return err
}
return nil
}
|
[
"func",
"(",
"vmxfile",
"*",
"VMXFile",
")",
"Write",
"(",
")",
"error",
"{",
"file",
",",
"err",
":=",
"os",
".",
"Create",
"(",
"vmxfile",
".",
"path",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"file",
".",
"Close",
"(",
")",
"\n",
"data",
",",
"err",
":=",
"vmx",
".",
"Marshal",
"(",
"vmxfile",
".",
"model",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"_",
",",
"err",
"=",
"file",
".",
"Write",
"(",
"data",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Write marshals and writes VMX file to disk
|
[
"Write",
"marshals",
"and",
"writes",
"VMX",
"file",
"to",
"disk"
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vmx.go#L46-L64
|
test
|
hooklift/govix
|
vm.go
|
NewVirtualMachine
|
func NewVirtualMachine(handle C.VixHandle, vmxpath string) (*VM, error) {
vmxfile := &VMXFile{
path: vmxpath,
}
// Loads VMX file in memory
err := vmxfile.Read()
if err != nil {
return nil, err
}
vm := &VM{
handle: handle,
vmxfile: vmxfile,
}
runtime.SetFinalizer(vm, cleanupVM)
return vm, nil
}
|
go
|
func NewVirtualMachine(handle C.VixHandle, vmxpath string) (*VM, error) {
vmxfile := &VMXFile{
path: vmxpath,
}
// Loads VMX file in memory
err := vmxfile.Read()
if err != nil {
return nil, err
}
vm := &VM{
handle: handle,
vmxfile: vmxfile,
}
runtime.SetFinalizer(vm, cleanupVM)
return vm, nil
}
|
[
"func",
"NewVirtualMachine",
"(",
"handle",
"C",
".",
"VixHandle",
",",
"vmxpath",
"string",
")",
"(",
"*",
"VM",
",",
"error",
")",
"{",
"vmxfile",
":=",
"&",
"VMXFile",
"{",
"path",
":",
"vmxpath",
",",
"}",
"\n",
"err",
":=",
"vmxfile",
".",
"Read",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"vm",
":=",
"&",
"VM",
"{",
"handle",
":",
"handle",
",",
"vmxfile",
":",
"vmxfile",
",",
"}",
"\n",
"runtime",
".",
"SetFinalizer",
"(",
"vm",
",",
"cleanupVM",
")",
"\n",
"return",
"vm",
",",
"nil",
"\n",
"}"
] |
// NewVirtualMachine creates a new VM instance.
|
[
"NewVirtualMachine",
"creates",
"a",
"new",
"VM",
"instance",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L33-L51
|
test
|
hooklift/govix
|
vm.go
|
Vcpus
|
func (v *VM) Vcpus() (uint8, error) {
var err C.VixError = C.VIX_OK
vcpus := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_NUM_VCPUS,
unsafe.Pointer(&vcpus))
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.Vcpus",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return uint8(vcpus), nil
}
|
go
|
func (v *VM) Vcpus() (uint8, error) {
var err C.VixError = C.VIX_OK
vcpus := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_NUM_VCPUS,
unsafe.Pointer(&vcpus))
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.Vcpus",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return uint8(vcpus), nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"Vcpus",
"(",
")",
"(",
"uint8",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"vcpus",
":=",
"C",
".",
"VIX_PROPERTY_NONE",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_NUM_VCPUS",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"vcpus",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"0",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.Vcpus\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"uint8",
"(",
"vcpus",
")",
",",
"nil",
"\n",
"}"
] |
// Vcpus returns number of virtual CPUs configured for the virtual machine.
|
[
"Vcpus",
"returns",
"number",
"of",
"virtual",
"CPUs",
"configured",
"for",
"the",
"virtual",
"machine",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L54-L71
|
test
|
hooklift/govix
|
vm.go
|
VmxPath
|
func (v *VM) VmxPath() (string, error) {
var err C.VixError = C.VIX_OK
var path *C.char
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_VMX_PATHNAME,
unsafe.Pointer(&path))
defer C.Vix_FreeBuffer(unsafe.Pointer(path))
if C.VIX_OK != err {
return "", &Error{
Operation: "vm.VmxPath",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(path), nil
}
|
go
|
func (v *VM) VmxPath() (string, error) {
var err C.VixError = C.VIX_OK
var path *C.char
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_VMX_PATHNAME,
unsafe.Pointer(&path))
defer C.Vix_FreeBuffer(unsafe.Pointer(path))
if C.VIX_OK != err {
return "", &Error{
Operation: "vm.VmxPath",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(path), nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"VmxPath",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"var",
"path",
"*",
"C",
".",
"char",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_VMX_PATHNAME",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"path",
")",
")",
"\n",
"defer",
"C",
".",
"Vix_FreeBuffer",
"(",
"unsafe",
".",
"Pointer",
"(",
"path",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"\"\"",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.VmxPath\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"C",
".",
"GoString",
"(",
"path",
")",
",",
"nil",
"\n",
"}"
] |
// VmxPath returns path to the virtual machine configuration file.
|
[
"VmxPath",
"returns",
"path",
"to",
"the",
"virtual",
"machine",
"configuration",
"file",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L74-L93
|
test
|
hooklift/govix
|
vm.go
|
MemorySize
|
func (v *VM) MemorySize() (uint, error) {
var err C.VixError = C.VIX_OK
memsize := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_MEMORY_SIZE,
unsafe.Pointer(&memsize))
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.MemorySize",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return uint(memsize), nil
}
|
go
|
func (v *VM) MemorySize() (uint, error) {
var err C.VixError = C.VIX_OK
memsize := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_MEMORY_SIZE,
unsafe.Pointer(&memsize))
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.MemorySize",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return uint(memsize), nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"MemorySize",
"(",
")",
"(",
"uint",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"memsize",
":=",
"C",
".",
"VIX_PROPERTY_NONE",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_MEMORY_SIZE",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"memsize",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"0",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.MemorySize\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"memsize",
")",
",",
"nil",
"\n",
"}"
] |
// MemorySize returns memory size of the virtual machine.
|
[
"MemorySize",
"returns",
"memory",
"size",
"of",
"the",
"virtual",
"machine",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L118-L135
|
test
|
hooklift/govix
|
vm.go
|
ReadOnly
|
func (v *VM) ReadOnly() (bool, error) {
var err C.VixError = C.VIX_OK
readonly := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_READ_ONLY,
unsafe.Pointer(&readonly))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.ReadOnly",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if readonly == 0 {
return false, nil
}
return true, nil
}
|
go
|
func (v *VM) ReadOnly() (bool, error) {
var err C.VixError = C.VIX_OK
readonly := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_READ_ONLY,
unsafe.Pointer(&readonly))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.ReadOnly",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if readonly == 0 {
return false, nil
}
return true, nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"ReadOnly",
"(",
")",
"(",
"bool",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"readonly",
":=",
"C",
".",
"VIX_PROPERTY_NONE",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_READ_ONLY",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"readonly",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"false",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.ReadOnly\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"if",
"readonly",
"==",
"0",
"{",
"return",
"false",
",",
"nil",
"\n",
"}",
"\n",
"return",
"true",
",",
"nil",
"\n",
"}"
] |
// ReadOnly tells whether or not the VM is read-only.
|
[
"ReadOnly",
"tells",
"whether",
"or",
"not",
"the",
"VM",
"is",
"read",
"-",
"only",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L138-L159
|
test
|
hooklift/govix
|
vm.go
|
InVMTeam
|
func (v *VM) InVMTeam() (bool, error) {
var err C.VixError = C.VIX_OK
inTeam := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_IN_VMTEAM,
unsafe.Pointer(&inTeam))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.InVmTeam",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if inTeam == 0 {
return false, nil
}
return true, nil
}
|
go
|
func (v *VM) InVMTeam() (bool, error) {
var err C.VixError = C.VIX_OK
inTeam := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_IN_VMTEAM,
unsafe.Pointer(&inTeam))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.InVmTeam",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if inTeam == 0 {
return false, nil
}
return true, nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"InVMTeam",
"(",
")",
"(",
"bool",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"inTeam",
":=",
"C",
".",
"VIX_PROPERTY_NONE",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_IN_VMTEAM",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"inTeam",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"false",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.InVmTeam\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"if",
"inTeam",
"==",
"0",
"{",
"return",
"false",
",",
"nil",
"\n",
"}",
"\n",
"return",
"true",
",",
"nil",
"\n",
"}"
] |
// InVMTeam returns whether the virtual machine is a member of a team.
|
[
"InVMTeam",
"returns",
"whether",
"the",
"virtual",
"machine",
"is",
"a",
"member",
"of",
"a",
"team",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L162-L183
|
test
|
hooklift/govix
|
vm.go
|
PowerState
|
func (v *VM) PowerState() (VMPowerState, error) {
var err C.VixError = C.VIX_OK
var state C.VixPowerState = 0x0
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_POWER_STATE,
unsafe.Pointer(&state))
if C.VIX_OK != err {
return VMPowerState(0x0), &Error{
Operation: "vm.PowerState",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return VMPowerState(state), nil
}
|
go
|
func (v *VM) PowerState() (VMPowerState, error) {
var err C.VixError = C.VIX_OK
var state C.VixPowerState = 0x0
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_POWER_STATE,
unsafe.Pointer(&state))
if C.VIX_OK != err {
return VMPowerState(0x0), &Error{
Operation: "vm.PowerState",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return VMPowerState(state), nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"PowerState",
"(",
")",
"(",
"VMPowerState",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"var",
"state",
"C",
".",
"VixPowerState",
"=",
"0x0",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_POWER_STATE",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"state",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"VMPowerState",
"(",
"0x0",
")",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.PowerState\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"VMPowerState",
"(",
"state",
")",
",",
"nil",
"\n",
"}"
] |
// PowerState returns power state of the virtual machine.
|
[
"PowerState",
"returns",
"power",
"state",
"of",
"the",
"virtual",
"machine",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L186-L203
|
test
|
hooklift/govix
|
vm.go
|
ToolsState
|
func (v *VM) ToolsState() (GuestToolsState, error) {
var err C.VixError = C.VIX_OK
state := C.VIX_TOOLSSTATE_UNKNOWN
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_TOOLS_STATE,
unsafe.Pointer(&state))
if C.VIX_OK != err {
return TOOLSSTATE_UNKNOWN, &Error{
Operation: "vm.ToolsState",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return GuestToolsState(state), nil
}
|
go
|
func (v *VM) ToolsState() (GuestToolsState, error) {
var err C.VixError = C.VIX_OK
state := C.VIX_TOOLSSTATE_UNKNOWN
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_TOOLS_STATE,
unsafe.Pointer(&state))
if C.VIX_OK != err {
return TOOLSSTATE_UNKNOWN, &Error{
Operation: "vm.ToolsState",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return GuestToolsState(state), nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"ToolsState",
"(",
")",
"(",
"GuestToolsState",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"state",
":=",
"C",
".",
"VIX_TOOLSSTATE_UNKNOWN",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_TOOLS_STATE",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"state",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"TOOLSSTATE_UNKNOWN",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.ToolsState\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"GuestToolsState",
"(",
"state",
")",
",",
"nil",
"\n",
"}"
] |
// ToolsState returns state of the VMware Tools suite in the guest.
|
[
"ToolsState",
"returns",
"state",
"of",
"the",
"VMware",
"Tools",
"suite",
"in",
"the",
"guest",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L206-L223
|
test
|
hooklift/govix
|
vm.go
|
IsRunning
|
func (v *VM) IsRunning() (bool, error) {
var err C.VixError = C.VIX_OK
running := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_IS_RUNNING,
unsafe.Pointer(&running))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.IsRunning",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if running == 0 {
return false, nil
}
return true, nil
}
|
go
|
func (v *VM) IsRunning() (bool, error) {
var err C.VixError = C.VIX_OK
running := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_IS_RUNNING,
unsafe.Pointer(&running))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.IsRunning",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if running == 0 {
return false, nil
}
return true, nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"IsRunning",
"(",
")",
"(",
"bool",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"running",
":=",
"C",
".",
"VIX_PROPERTY_NONE",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_IS_RUNNING",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"running",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"false",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.IsRunning\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"if",
"running",
"==",
"0",
"{",
"return",
"false",
",",
"nil",
"\n",
"}",
"\n",
"return",
"true",
",",
"nil",
"\n",
"}"
] |
// IsRunning returns whether the virtual machine is running.
|
[
"IsRunning",
"returns",
"whether",
"the",
"virtual",
"machine",
"is",
"running",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L226-L247
|
test
|
hooklift/govix
|
vm.go
|
GuestOS
|
func (v *VM) GuestOS() (string, error) {
var err C.VixError = C.VIX_OK
var os *C.char
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_GUESTOS,
unsafe.Pointer(&os))
defer C.Vix_FreeBuffer(unsafe.Pointer(os))
if C.VIX_OK != err {
return "", &Error{
Operation: "vm.GuestOS",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(os), nil
}
|
go
|
func (v *VM) GuestOS() (string, error) {
var err C.VixError = C.VIX_OK
var os *C.char
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_GUESTOS,
unsafe.Pointer(&os))
defer C.Vix_FreeBuffer(unsafe.Pointer(os))
if C.VIX_OK != err {
return "", &Error{
Operation: "vm.GuestOS",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(os), nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"GuestOS",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"err",
"C",
".",
"VixError",
"=",
"C",
".",
"VIX_OK",
"\n",
"var",
"os",
"*",
"C",
".",
"char",
"\n",
"err",
"=",
"C",
".",
"get_property",
"(",
"v",
".",
"handle",
",",
"C",
".",
"VIX_PROPERTY_VM_GUESTOS",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"os",
")",
")",
"\n",
"defer",
"C",
".",
"Vix_FreeBuffer",
"(",
"unsafe",
".",
"Pointer",
"(",
"os",
")",
")",
"\n",
"if",
"C",
".",
"VIX_OK",
"!=",
"err",
"{",
"return",
"\"\"",
",",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.GuestOS\"",
",",
"Code",
":",
"int",
"(",
"err",
"&",
"0xFFFF",
")",
",",
"Text",
":",
"C",
".",
"GoString",
"(",
"C",
".",
"Vix_GetErrorText",
"(",
"err",
",",
"nil",
")",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"C",
".",
"GoString",
"(",
"os",
")",
",",
"nil",
"\n",
"}"
] |
// GuestOS returns the guest os.
|
[
"GuestOS",
"returns",
"the",
"guest",
"os",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L250-L269
|
test
|
hooklift/govix
|
vm.go
|
cleanupVM
|
func cleanupVM(v *VM) {
if v.handle != C.VIX_INVALID_HANDLE {
C.Vix_ReleaseHandle(v.handle)
v.handle = C.VIX_INVALID_HANDLE
}
}
|
go
|
func cleanupVM(v *VM) {
if v.handle != C.VIX_INVALID_HANDLE {
C.Vix_ReleaseHandle(v.handle)
v.handle = C.VIX_INVALID_HANDLE
}
}
|
[
"func",
"cleanupVM",
"(",
"v",
"*",
"VM",
")",
"{",
"if",
"v",
".",
"handle",
"!=",
"C",
".",
"VIX_INVALID_HANDLE",
"{",
"C",
".",
"Vix_ReleaseHandle",
"(",
"v",
".",
"handle",
")",
"\n",
"v",
".",
"handle",
"=",
"C",
".",
"VIX_INVALID_HANDLE",
"\n",
"}",
"\n",
"}"
] |
// cleanupVM cleans up VM VIX handle.
|
[
"cleanupVM",
"cleans",
"up",
"VM",
"VIX",
"handle",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L593-L598
|
test
|
hooklift/govix
|
vm.go
|
updateVMX
|
func (v *VM) updateVMX(updateFunc func(model *vmx.VirtualMachine) error) error {
isVMRunning, err := v.IsRunning()
if err != nil {
return err
}
if isVMRunning {
return &Error{
Operation: "vm.updateVMX",
Code: 100000,
Text: "The VM has to be powered off in order to change its vmx settings",
}
}
err = v.vmxfile.Read()
if err != nil {
return &Error{
Operation: "vm.updateVMX",
Code: 300001,
Text: fmt.Sprintf("Error reading vmx file: %s", err),
}
}
err = updateFunc(v.vmxfile.model)
if err != nil {
return &Error{
Operation: "vm.updateVMX",
Code: 300002,
Text: fmt.Sprintf("Error changing vmx value: %s", err),
}
}
err = v.vmxfile.Write()
if err != nil {
return &Error{
Operation: "vm.updateVMX",
Code: 300003,
Text: fmt.Sprintf("Error writing vmx file: %s", err),
}
}
return nil
}
|
go
|
func (v *VM) updateVMX(updateFunc func(model *vmx.VirtualMachine) error) error {
isVMRunning, err := v.IsRunning()
if err != nil {
return err
}
if isVMRunning {
return &Error{
Operation: "vm.updateVMX",
Code: 100000,
Text: "The VM has to be powered off in order to change its vmx settings",
}
}
err = v.vmxfile.Read()
if err != nil {
return &Error{
Operation: "vm.updateVMX",
Code: 300001,
Text: fmt.Sprintf("Error reading vmx file: %s", err),
}
}
err = updateFunc(v.vmxfile.model)
if err != nil {
return &Error{
Operation: "vm.updateVMX",
Code: 300002,
Text: fmt.Sprintf("Error changing vmx value: %s", err),
}
}
err = v.vmxfile.Write()
if err != nil {
return &Error{
Operation: "vm.updateVMX",
Code: 300003,
Text: fmt.Sprintf("Error writing vmx file: %s", err),
}
}
return nil
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"updateVMX",
"(",
"updateFunc",
"func",
"(",
"model",
"*",
"vmx",
".",
"VirtualMachine",
")",
"error",
")",
"error",
"{",
"isVMRunning",
",",
"err",
":=",
"v",
".",
"IsRunning",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"isVMRunning",
"{",
"return",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.updateVMX\"",
",",
"Code",
":",
"100000",
",",
"Text",
":",
"\"The VM has to be powered off in order to change its vmx settings\"",
",",
"}",
"\n",
"}",
"\n",
"err",
"=",
"v",
".",
"vmxfile",
".",
"Read",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.updateVMX\"",
",",
"Code",
":",
"300001",
",",
"Text",
":",
"fmt",
".",
"Sprintf",
"(",
"\"Error reading vmx file: %s\"",
",",
"err",
")",
",",
"}",
"\n",
"}",
"\n",
"err",
"=",
"updateFunc",
"(",
"v",
".",
"vmxfile",
".",
"model",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.updateVMX\"",
",",
"Code",
":",
"300002",
",",
"Text",
":",
"fmt",
".",
"Sprintf",
"(",
"\"Error changing vmx value: %s\"",
",",
"err",
")",
",",
"}",
"\n",
"}",
"\n",
"err",
"=",
"v",
".",
"vmxfile",
".",
"Write",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"&",
"Error",
"{",
"Operation",
":",
"\"vm.updateVMX\"",
",",
"Code",
":",
"300003",
",",
"Text",
":",
"fmt",
".",
"Sprintf",
"(",
"\"Error writing vmx file: %s\"",
",",
"err",
")",
",",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// updateVMX updates VMX file for the VM.
|
[
"updateVMX",
"updates",
"VMX",
"file",
"for",
"the",
"VM",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L1959-L2001
|
test
|
hooklift/govix
|
vm.go
|
SetMemorySize
|
func (v *VM) SetMemorySize(size uint) error {
if size == 0 {
size = 4
}
// Makes sure memory size is divisible by 4, otherwise VMware is going to
// silently fail, cancelling vix operations.
if size%4 != 0 {
size = uint(math.Floor(float64((size / 4) * 4)))
}
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.Memsize = size
return nil
})
}
|
go
|
func (v *VM) SetMemorySize(size uint) error {
if size == 0 {
size = 4
}
// Makes sure memory size is divisible by 4, otherwise VMware is going to
// silently fail, cancelling vix operations.
if size%4 != 0 {
size = uint(math.Floor(float64((size / 4) * 4)))
}
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.Memsize = size
return nil
})
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"SetMemorySize",
"(",
"size",
"uint",
")",
"error",
"{",
"if",
"size",
"==",
"0",
"{",
"size",
"=",
"4",
"\n",
"}",
"\n",
"if",
"size",
"%",
"4",
"!=",
"0",
"{",
"size",
"=",
"uint",
"(",
"math",
".",
"Floor",
"(",
"float64",
"(",
"(",
"size",
"/",
"4",
")",
"*",
"4",
")",
")",
")",
"\n",
"}",
"\n",
"return",
"v",
".",
"updateVMX",
"(",
"func",
"(",
"model",
"*",
"vmx",
".",
"VirtualMachine",
")",
"error",
"{",
"model",
".",
"Memsize",
"=",
"size",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// SetMemorySize sets memory size in megabytes. VM has to be powered off in order to change this parameter.
|
[
"SetMemorySize",
"sets",
"memory",
"size",
"in",
"megabytes",
".",
"VM",
"has",
"to",
"be",
"powered",
"off",
"in",
"order",
"to",
"change",
"this",
"parameter",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L2004-L2019
|
test
|
hooklift/govix
|
vm.go
|
SetNumberVcpus
|
func (v *VM) SetNumberVcpus(vcpus uint) error {
if vcpus < 1 {
vcpus = 1
}
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.NumvCPUs = vcpus
return nil
})
}
|
go
|
func (v *VM) SetNumberVcpus(vcpus uint) error {
if vcpus < 1 {
vcpus = 1
}
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.NumvCPUs = vcpus
return nil
})
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"SetNumberVcpus",
"(",
"vcpus",
"uint",
")",
"error",
"{",
"if",
"vcpus",
"<",
"1",
"{",
"vcpus",
"=",
"1",
"\n",
"}",
"\n",
"return",
"v",
".",
"updateVMX",
"(",
"func",
"(",
"model",
"*",
"vmx",
".",
"VirtualMachine",
")",
"error",
"{",
"model",
".",
"NumvCPUs",
"=",
"vcpus",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// SetNumberVcpus sets number of virtual cpus assigned to this machine. VM has to be powered off in order to change this parameter.
|
[
"SetNumberVcpus",
"sets",
"number",
"of",
"virtual",
"cpus",
"assigned",
"to",
"this",
"machine",
".",
"VM",
"has",
"to",
"be",
"powered",
"off",
"in",
"order",
"to",
"change",
"this",
"parameter",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L2022-L2031
|
test
|
hooklift/govix
|
vm.go
|
SetDisplayName
|
func (v *VM) SetDisplayName(name string) error {
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.DisplayName = name
return nil
})
}
|
go
|
func (v *VM) SetDisplayName(name string) error {
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.DisplayName = name
return nil
})
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"SetDisplayName",
"(",
"name",
"string",
")",
"error",
"{",
"return",
"v",
".",
"updateVMX",
"(",
"func",
"(",
"model",
"*",
"vmx",
".",
"VirtualMachine",
")",
"error",
"{",
"model",
".",
"DisplayName",
"=",
"name",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// SetDisplayName sets virtual machine name.
|
[
"SetDisplayName",
"sets",
"virtual",
"machine",
"name",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L2034-L2039
|
test
|
hooklift/govix
|
vm.go
|
SetAnnotation
|
func (v *VM) SetAnnotation(text string) error {
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.Annotation = text
return nil
})
}
|
go
|
func (v *VM) SetAnnotation(text string) error {
return v.updateVMX(func(model *vmx.VirtualMachine) error {
model.Annotation = text
return nil
})
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"SetAnnotation",
"(",
"text",
"string",
")",
"error",
"{",
"return",
"v",
".",
"updateVMX",
"(",
"func",
"(",
"model",
"*",
"vmx",
".",
"VirtualMachine",
")",
"error",
"{",
"model",
".",
"Annotation",
"=",
"text",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// SetAnnotation sets annotations for the virtual machine.
|
[
"SetAnnotation",
"sets",
"annotations",
"for",
"the",
"virtual",
"machine",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L2047-L2052
|
test
|
hooklift/govix
|
vm.go
|
SetVirtualHwVersion
|
func (v *VM) SetVirtualHwVersion(version string) error {
return v.updateVMX(func(model *vmx.VirtualMachine) error {
version, err := strconv.ParseInt(version, 10, 32)
if err != nil {
return err
}
model.Vhardware.Compat = "hosted"
model.Vhardware.Version = int(version)
return nil
})
}
|
go
|
func (v *VM) SetVirtualHwVersion(version string) error {
return v.updateVMX(func(model *vmx.VirtualMachine) error {
version, err := strconv.ParseInt(version, 10, 32)
if err != nil {
return err
}
model.Vhardware.Compat = "hosted"
model.Vhardware.Version = int(version)
return nil
})
}
|
[
"func",
"(",
"v",
"*",
"VM",
")",
"SetVirtualHwVersion",
"(",
"version",
"string",
")",
"error",
"{",
"return",
"v",
".",
"updateVMX",
"(",
"func",
"(",
"model",
"*",
"vmx",
".",
"VirtualMachine",
")",
"error",
"{",
"version",
",",
"err",
":=",
"strconv",
".",
"ParseInt",
"(",
"version",
",",
"10",
",",
"32",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"model",
".",
"Vhardware",
".",
"Compat",
"=",
"\"hosted\"",
"\n",
"model",
".",
"Vhardware",
".",
"Version",
"=",
"int",
"(",
"version",
")",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// SetVirtualHwVersion sets a virtual hardware version in the VMX file of the VM.
|
[
"SetVirtualHwVersion",
"sets",
"a",
"virtual",
"hardware",
"version",
"in",
"the",
"VMX",
"file",
"of",
"the",
"VM",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vm.go#L2060-L2070
|
test
|
hooklift/govix
|
vix.go
|
Error
|
func (e *Error) Error() string {
return fmt.Sprintf("VIX Error: %s, code: %d, operation: %s", e.Text, e.Code, e.Operation)
}
|
go
|
func (e *Error) Error() string {
return fmt.Sprintf("VIX Error: %s, code: %d, operation: %s", e.Text, e.Code, e.Operation)
}
|
[
"func",
"(",
"e",
"*",
"Error",
")",
"Error",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"VIX Error: %s, code: %d, operation: %s\"",
",",
"e",
".",
"Text",
",",
"e",
".",
"Code",
",",
"e",
".",
"Operation",
")",
"\n",
"}"
] |
// Error returns a description of the error along with its code and operation
// implementing Go's error interface.
|
[
"Error",
"returns",
"a",
"description",
"of",
"the",
"error",
"along",
"with",
"its",
"code",
"and",
"operation",
"implementing",
"Go",
"s",
"error",
"interface",
"."
] |
063702285520a992b920fc1575e305dc9ffd6ffe
|
https://github.com/hooklift/govix/blob/063702285520a992b920fc1575e305dc9ffd6ffe/vix.go#L396-L398
|
test
|
st3v/tracerr
|
tracerr.go
|
Errorf
|
func Errorf(message string, a ...interface{}) error {
return wrap(fmt.Errorf(message, a...))
}
|
go
|
func Errorf(message string, a ...interface{}) error {
return wrap(fmt.Errorf(message, a...))
}
|
[
"func",
"Errorf",
"(",
"message",
"string",
",",
"a",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"wrap",
"(",
"fmt",
".",
"Errorf",
"(",
"message",
",",
"a",
"...",
")",
")",
"\n",
"}"
] |
// Errorf returns a traceable error with the given formatted message.
|
[
"Errorf",
"returns",
"a",
"traceable",
"error",
"with",
"the",
"given",
"formatted",
"message",
"."
] |
07f754d5ee02576c14a8272df820e8947d87e723
|
https://github.com/st3v/tracerr/blob/07f754d5ee02576c14a8272df820e8947d87e723/tracerr.go#L39-L41
|
test
|
st3v/tracerr
|
tracerr.go
|
Error
|
func (t *traceableError) Error() string {
str := t.err.Error()
for _, frame := range t.stack {
str += fmt.Sprintf("\n at %s", frame.string())
}
return str
}
|
go
|
func (t *traceableError) Error() string {
str := t.err.Error()
for _, frame := range t.stack {
str += fmt.Sprintf("\n at %s", frame.string())
}
return str
}
|
[
"func",
"(",
"t",
"*",
"traceableError",
")",
"Error",
"(",
")",
"string",
"{",
"str",
":=",
"t",
".",
"err",
".",
"Error",
"(",
")",
"\n",
"for",
"_",
",",
"frame",
":=",
"range",
"t",
".",
"stack",
"{",
"str",
"+=",
"fmt",
".",
"Sprintf",
"(",
"\"\\n at %s\"",
",",
"\\n",
")",
"\n",
"}",
"\n",
"frame",
".",
"string",
"(",
")",
"\n",
"}"
] |
// Error returns the original error message plus the stack trace captured
// at the time the error was first wrapped.
|
[
"Error",
"returns",
"the",
"original",
"error",
"message",
"plus",
"the",
"stack",
"trace",
"captured",
"at",
"the",
"time",
"the",
"error",
"was",
"first",
"wrapped",
"."
] |
07f754d5ee02576c14a8272df820e8947d87e723
|
https://github.com/st3v/tracerr/blob/07f754d5ee02576c14a8272df820e8947d87e723/tracerr.go#L65-L71
|
test
|
st3v/tracerr
|
tracerr.go
|
string
|
func (s *stackFrame) string() string {
return fmt.Sprintf("%s (%s:%d)", s.function, s.file, s.line)
}
|
go
|
func (s *stackFrame) string() string {
return fmt.Sprintf("%s (%s:%d)", s.function, s.file, s.line)
}
|
[
"func",
"(",
"s",
"*",
"stackFrame",
")",
"string",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"%s (%s:%d)\"",
",",
"s",
".",
"function",
",",
"s",
".",
"file",
",",
"s",
".",
"line",
")",
"\n",
"}"
] |
// string converts a given stack frame to a formated string.
|
[
"string",
"converts",
"a",
"given",
"stack",
"frame",
"to",
"a",
"formated",
"string",
"."
] |
07f754d5ee02576c14a8272df820e8947d87e723
|
https://github.com/st3v/tracerr/blob/07f754d5ee02576c14a8272df820e8947d87e723/tracerr.go#L81-L83
|
test
|
st3v/tracerr
|
tracerr.go
|
newStackFrame
|
func newStackFrame(pc uintptr) *stackFrame {
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
packagePath, funcSignature := parseFuncName(fn.Name())
_, fileName := filepath.Split(file)
return &stackFrame{
file: filepath.Join(packagePath, fileName),
line: line,
function: funcSignature,
}
}
|
go
|
func newStackFrame(pc uintptr) *stackFrame {
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
packagePath, funcSignature := parseFuncName(fn.Name())
_, fileName := filepath.Split(file)
return &stackFrame{
file: filepath.Join(packagePath, fileName),
line: line,
function: funcSignature,
}
}
|
[
"func",
"newStackFrame",
"(",
"pc",
"uintptr",
")",
"*",
"stackFrame",
"{",
"fn",
":=",
"runtime",
".",
"FuncForPC",
"(",
"pc",
")",
"\n",
"file",
",",
"line",
":=",
"fn",
".",
"FileLine",
"(",
"pc",
")",
"\n",
"packagePath",
",",
"funcSignature",
":=",
"parseFuncName",
"(",
"fn",
".",
"Name",
"(",
")",
")",
"\n",
"_",
",",
"fileName",
":=",
"filepath",
".",
"Split",
"(",
"file",
")",
"\n",
"return",
"&",
"stackFrame",
"{",
"file",
":",
"filepath",
".",
"Join",
"(",
"packagePath",
",",
"fileName",
")",
",",
"line",
":",
"line",
",",
"function",
":",
"funcSignature",
",",
"}",
"\n",
"}"
] |
// newStackFrame returns a new stack frame initialized from the passed
// in program counter.
|
[
"newStackFrame",
"returns",
"a",
"new",
"stack",
"frame",
"initialized",
"from",
"the",
"passed",
"in",
"program",
"counter",
"."
] |
07f754d5ee02576c14a8272df820e8947d87e723
|
https://github.com/st3v/tracerr/blob/07f754d5ee02576c14a8272df820e8947d87e723/tracerr.go#L87-L98
|
test
|
st3v/tracerr
|
tracerr.go
|
captureStack
|
func captureStack(skip, maxDepth int) []*stackFrame {
pcs := make([]uintptr, maxDepth)
count := runtime.Callers(skip+1, pcs)
frames := make([]*stackFrame, count)
for i, pc := range pcs[0:count] {
frames[i] = newStackFrame(pc)
}
return frames
}
|
go
|
func captureStack(skip, maxDepth int) []*stackFrame {
pcs := make([]uintptr, maxDepth)
count := runtime.Callers(skip+1, pcs)
frames := make([]*stackFrame, count)
for i, pc := range pcs[0:count] {
frames[i] = newStackFrame(pc)
}
return frames
}
|
[
"func",
"captureStack",
"(",
"skip",
",",
"maxDepth",
"int",
")",
"[",
"]",
"*",
"stackFrame",
"{",
"pcs",
":=",
"make",
"(",
"[",
"]",
"uintptr",
",",
"maxDepth",
")",
"\n",
"count",
":=",
"runtime",
".",
"Callers",
"(",
"skip",
"+",
"1",
",",
"pcs",
")",
"\n",
"frames",
":=",
"make",
"(",
"[",
"]",
"*",
"stackFrame",
",",
"count",
")",
"\n",
"for",
"i",
",",
"pc",
":=",
"range",
"pcs",
"[",
"0",
":",
"count",
"]",
"{",
"frames",
"[",
"i",
"]",
"=",
"newStackFrame",
"(",
"pc",
")",
"\n",
"}",
"\n",
"return",
"frames",
"\n",
"}"
] |
// captureStack returns a slice of stack frames representing the stack
// of the calling go routine.
|
[
"captureStack",
"returns",
"a",
"slice",
"of",
"stack",
"frames",
"representing",
"the",
"stack",
"of",
"the",
"calling",
"go",
"routine",
"."
] |
07f754d5ee02576c14a8272df820e8947d87e723
|
https://github.com/st3v/tracerr/blob/07f754d5ee02576c14a8272df820e8947d87e723/tracerr.go#L102-L112
|
test
|
st3v/tracerr
|
tracerr.go
|
parseFuncName
|
func parseFuncName(fnName string) (packagePath, signature string) {
regEx := regexp.MustCompile("([^\\(]*)\\.(.*)")
parts := regEx.FindStringSubmatch(fnName)
packagePath = parts[1]
signature = parts[2]
return
}
|
go
|
func parseFuncName(fnName string) (packagePath, signature string) {
regEx := regexp.MustCompile("([^\\(]*)\\.(.*)")
parts := regEx.FindStringSubmatch(fnName)
packagePath = parts[1]
signature = parts[2]
return
}
|
[
"func",
"parseFuncName",
"(",
"fnName",
"string",
")",
"(",
"packagePath",
",",
"signature",
"string",
")",
"{",
"regEx",
":=",
"regexp",
".",
"MustCompile",
"(",
"\"([^\\\\(]*)\\\\.(.*)\"",
")",
"\n",
"\\\\",
"\n",
"\\\\",
"\n",
"parts",
":=",
"regEx",
".",
"FindStringSubmatch",
"(",
"fnName",
")",
"\n",
"packagePath",
"=",
"parts",
"[",
"1",
"]",
"\n",
"}"
] |
// parseFuncName returns the package path and function signature for a
// give Func name.
|
[
"parseFuncName",
"returns",
"the",
"package",
"path",
"and",
"function",
"signature",
"for",
"a",
"give",
"Func",
"name",
"."
] |
07f754d5ee02576c14a8272df820e8947d87e723
|
https://github.com/st3v/tracerr/blob/07f754d5ee02576c14a8272df820e8947d87e723/tracerr.go#L116-L122
|
test
|
HiLittleCat/core
|
log/log.go
|
Stack
|
func Stack(err interface{}) {
stack := make([]byte, 64<<10)
stack = stack[:runtime.Stack(stack, false)]
log.Printf("%v\n%s", err, stack)
}
|
go
|
func Stack(err interface{}) {
stack := make([]byte, 64<<10)
stack = stack[:runtime.Stack(stack, false)]
log.Printf("%v\n%s", err, stack)
}
|
[
"func",
"Stack",
"(",
"err",
"interface",
"{",
"}",
")",
"{",
"stack",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"64",
"<<",
"10",
")",
"\n",
"stack",
"=",
"stack",
"[",
":",
"runtime",
".",
"Stack",
"(",
"stack",
",",
"false",
")",
"]",
"\n",
"log",
".",
"Printf",
"(",
"\"%v\\n%s\"",
",",
"\\n",
",",
"err",
")",
"\n",
"}"
] |
// Stack logs the error err with the stack trace.
|
[
"Stack",
"logs",
"the",
"error",
"err",
"with",
"the",
"stack",
"trace",
"."
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/log/log.go#L11-L16
|
test
|
HiLittleCat/core
|
log/log.go
|
StackWithCaller
|
func StackWithCaller(err interface{}) {
stack := make([]byte, 64<<10)
stack = stack[:runtime.Stack(stack, false)]
if pack, ok := callerPackage(); ok {
log.Printf("%s: %v\n%s", pack, err, stack)
} else {
log.Printf("%v\n%s", err, stack)
}
}
|
go
|
func StackWithCaller(err interface{}) {
stack := make([]byte, 64<<10)
stack = stack[:runtime.Stack(stack, false)]
if pack, ok := callerPackage(); ok {
log.Printf("%s: %v\n%s", pack, err, stack)
} else {
log.Printf("%v\n%s", err, stack)
}
}
|
[
"func",
"StackWithCaller",
"(",
"err",
"interface",
"{",
"}",
")",
"{",
"stack",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"64",
"<<",
"10",
")",
"\n",
"stack",
"=",
"stack",
"[",
":",
"runtime",
".",
"Stack",
"(",
"stack",
",",
"false",
")",
"]",
"\n",
"if",
"pack",
",",
"ok",
":=",
"callerPackage",
"(",
")",
";",
"ok",
"{",
"log",
".",
"Printf",
"(",
"\"%s: %v\\n%s\"",
",",
"\\n",
",",
"pack",
",",
"err",
")",
"\n",
"}",
"else",
"stack",
"\n",
"}"
] |
// StackWithCaller logs the error err with the caller package name and the stack trace.
|
[
"StackWithCaller",
"logs",
"the",
"error",
"err",
"with",
"the",
"caller",
"package",
"name",
"and",
"the",
"stack",
"trace",
"."
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/log/log.go#L19-L28
|
test
|
HiLittleCat/core
|
httputil/response.go
|
Write
|
func (w responseWriterBinder) Write(p []byte) (int, error) {
for _, f := range w.before {
f(p)
}
return w.Writer.Write(p)
}
|
go
|
func (w responseWriterBinder) Write(p []byte) (int, error) {
for _, f := range w.before {
f(p)
}
return w.Writer.Write(p)
}
|
[
"func",
"(",
"w",
"responseWriterBinder",
")",
"Write",
"(",
"p",
"[",
"]",
"byte",
")",
"(",
"int",
",",
"error",
")",
"{",
"for",
"_",
",",
"f",
":=",
"range",
"w",
".",
"before",
"{",
"f",
"(",
"p",
")",
"\n",
"}",
"\n",
"return",
"w",
".",
"Writer",
".",
"Write",
"(",
"p",
")",
"\n",
"}"
] |
// Write calls the writer upstream after executing the functions in the before field.
|
[
"Write",
"calls",
"the",
"writer",
"upstream",
"after",
"executing",
"the",
"functions",
"in",
"the",
"before",
"field",
"."
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/httputil/response.go#L20-L25
|
test
|
HiLittleCat/core
|
httputil/response.go
|
ResponseStatus
|
func ResponseStatus(w http.ResponseWriter) int {
return int(httpResponseStruct(reflect.ValueOf(w)).FieldByName("status").Int())
}
|
go
|
func ResponseStatus(w http.ResponseWriter) int {
return int(httpResponseStruct(reflect.ValueOf(w)).FieldByName("status").Int())
}
|
[
"func",
"ResponseStatus",
"(",
"w",
"http",
".",
"ResponseWriter",
")",
"int",
"{",
"return",
"int",
"(",
"httpResponseStruct",
"(",
"reflect",
".",
"ValueOf",
"(",
"w",
")",
")",
".",
"FieldByName",
"(",
"\"status\"",
")",
".",
"Int",
"(",
")",
")",
"\n",
"}"
] |
// ResponseStatus returns the HTTP response status.
// Remember that the status is only set by the server after WriteHeader has been called.
|
[
"ResponseStatus",
"returns",
"the",
"HTTP",
"response",
"status",
".",
"Remember",
"that",
"the",
"status",
"is",
"only",
"set",
"by",
"the",
"server",
"after",
"WriteHeader",
"has",
"been",
"called",
"."
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/httputil/response.go#L35-L37
|
test
|
HiLittleCat/core
|
httputil/response.go
|
httpResponseStruct
|
func httpResponseStruct(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Type().String() == "http.response" {
return v
}
return httpResponseStruct(v.FieldByName("ResponseWriter").Elem())
}
|
go
|
func httpResponseStruct(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Type().String() == "http.response" {
return v
}
return httpResponseStruct(v.FieldByName("ResponseWriter").Elem())
}
|
[
"func",
"httpResponseStruct",
"(",
"v",
"reflect",
".",
"Value",
")",
"reflect",
".",
"Value",
"{",
"if",
"v",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"v",
"=",
"v",
".",
"Elem",
"(",
")",
"\n",
"}",
"\n",
"if",
"v",
".",
"Type",
"(",
")",
".",
"String",
"(",
")",
"==",
"\"http.response\"",
"{",
"return",
"v",
"\n",
"}",
"\n",
"return",
"httpResponseStruct",
"(",
"v",
".",
"FieldByName",
"(",
"\"ResponseWriter\"",
")",
".",
"Elem",
"(",
")",
")",
"\n",
"}"
] |
// httpResponseStruct returns the response structure after going trough all the intermediary response writers.
|
[
"httpResponseStruct",
"returns",
"the",
"response",
"structure",
"after",
"going",
"trough",
"all",
"the",
"intermediary",
"response",
"writers",
"."
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/httputil/response.go#L40-L50
|
test
|
HiLittleCat/core
|
httputil/response.go
|
SetDetectedContentType
|
func SetDetectedContentType(w http.ResponseWriter, p []byte) string {
ct := w.Header().Get("Content-Type")
if ct == "" {
ct = http.DetectContentType(p)
w.Header().Set("Content-Type", ct)
}
return ct
}
|
go
|
func SetDetectedContentType(w http.ResponseWriter, p []byte) string {
ct := w.Header().Get("Content-Type")
if ct == "" {
ct = http.DetectContentType(p)
w.Header().Set("Content-Type", ct)
}
return ct
}
|
[
"func",
"SetDetectedContentType",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"p",
"[",
"]",
"byte",
")",
"string",
"{",
"ct",
":=",
"w",
".",
"Header",
"(",
")",
".",
"Get",
"(",
"\"Content-Type\"",
")",
"\n",
"if",
"ct",
"==",
"\"\"",
"{",
"ct",
"=",
"http",
".",
"DetectContentType",
"(",
"p",
")",
"\n",
"w",
".",
"Header",
"(",
")",
".",
"Set",
"(",
"\"Content-Type\"",
",",
"ct",
")",
"\n",
"}",
"\n",
"return",
"ct",
"\n",
"}"
] |
// SetDetectedContentType detects, sets and returns the response Conten-Type header value.
|
[
"SetDetectedContentType",
"detects",
"sets",
"and",
"returns",
"the",
"response",
"Conten",
"-",
"Type",
"header",
"value",
"."
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/httputil/response.go#L53-L60
|
test
|
HiLittleCat/core
|
errors.go
|
New
|
func (e *ServerError) New(message string) *ServerError {
e.HTTPCode = http.StatusInternalServerError
e.Errno = 0
e.Message = message
return e
}
|
go
|
func (e *ServerError) New(message string) *ServerError {
e.HTTPCode = http.StatusInternalServerError
e.Errno = 0
e.Message = message
return e
}
|
[
"func",
"(",
"e",
"*",
"ServerError",
")",
"New",
"(",
"message",
"string",
")",
"*",
"ServerError",
"{",
"e",
".",
"HTTPCode",
"=",
"http",
".",
"StatusInternalServerError",
"\n",
"e",
".",
"Errno",
"=",
"0",
"\n",
"e",
".",
"Message",
"=",
"message",
"\n",
"return",
"e",
"\n",
"}"
] |
// New ServerError.New
|
[
"New",
"ServerError",
".",
"New"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/errors.go#L50-L55
|
test
|
HiLittleCat/core
|
errors.go
|
New
|
func (e *DBError) New(dbName string, message string) *DBError {
e.HTTPCode = http.StatusInternalServerError
e.Errno = 0
e.Message = message
e.DBName = dbName
return e
}
|
go
|
func (e *DBError) New(dbName string, message string) *DBError {
e.HTTPCode = http.StatusInternalServerError
e.Errno = 0
e.Message = message
e.DBName = dbName
return e
}
|
[
"func",
"(",
"e",
"*",
"DBError",
")",
"New",
"(",
"dbName",
"string",
",",
"message",
"string",
")",
"*",
"DBError",
"{",
"e",
".",
"HTTPCode",
"=",
"http",
".",
"StatusInternalServerError",
"\n",
"e",
".",
"Errno",
"=",
"0",
"\n",
"e",
".",
"Message",
"=",
"message",
"\n",
"e",
".",
"DBName",
"=",
"dbName",
"\n",
"return",
"e",
"\n",
"}"
] |
// New DBError.New
|
[
"New",
"DBError",
".",
"New"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/errors.go#L77-L83
|
test
|
HiLittleCat/core
|
errors.go
|
New
|
func (e *ValidationError) New(message string) *ValidationError {
e.HTTPCode = http.StatusBadRequest
e.Errno = 0
e.Message = message
return e
}
|
go
|
func (e *ValidationError) New(message string) *ValidationError {
e.HTTPCode = http.StatusBadRequest
e.Errno = 0
e.Message = message
return e
}
|
[
"func",
"(",
"e",
"*",
"ValidationError",
")",
"New",
"(",
"message",
"string",
")",
"*",
"ValidationError",
"{",
"e",
".",
"HTTPCode",
"=",
"http",
".",
"StatusBadRequest",
"\n",
"e",
".",
"Errno",
"=",
"0",
"\n",
"e",
".",
"Message",
"=",
"message",
"\n",
"return",
"e",
"\n",
"}"
] |
// New ValidationError.New
|
[
"New",
"ValidationError",
".",
"New"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/errors.go#L91-L96
|
test
|
HiLittleCat/core
|
errors.go
|
New
|
func (e *NotFoundError) New(message string) *NotFoundError {
e.HTTPCode = http.StatusNotFound
e.Errno = 0
e.Message = message
return e
}
|
go
|
func (e *NotFoundError) New(message string) *NotFoundError {
e.HTTPCode = http.StatusNotFound
e.Errno = 0
e.Message = message
return e
}
|
[
"func",
"(",
"e",
"*",
"NotFoundError",
")",
"New",
"(",
"message",
"string",
")",
"*",
"NotFoundError",
"{",
"e",
".",
"HTTPCode",
"=",
"http",
".",
"StatusNotFound",
"\n",
"e",
".",
"Errno",
"=",
"0",
"\n",
"e",
".",
"Message",
"=",
"message",
"\n",
"return",
"e",
"\n",
"}"
] |
// New NotFoundError.New
|
[
"New",
"NotFoundError",
".",
"New"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/errors.go#L104-L109
|
test
|
HiLittleCat/core
|
controller.go
|
StrLength
|
func (c *Controller) StrLength(fieldName string, p interface{}, n int) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "长度应该为" + strconv.Itoa(n)))
}
b := c.Validate.Length(v, n)
if b == false {
panic((&ValidationError{}).New(fieldName + "长度应该为" + strconv.Itoa(n)))
}
return v
}
|
go
|
func (c *Controller) StrLength(fieldName string, p interface{}, n int) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "长度应该为" + strconv.Itoa(n)))
}
b := c.Validate.Length(v, n)
if b == false {
panic((&ValidationError{}).New(fieldName + "长度应该为" + strconv.Itoa(n)))
}
return v
}
|
[
"func",
"(",
"c",
"*",
"Controller",
")",
"StrLength",
"(",
"fieldName",
"string",
",",
"p",
"interface",
"{",
"}",
",",
"n",
"int",
")",
"string",
"{",
"if",
"p",
"==",
"nil",
"{",
"p",
"=",
"\"\"",
"\n",
"}",
"\n",
"v",
",",
"ok",
":=",
"p",
".",
"(",
"string",
")",
"\n",
"if",
"ok",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"长度应该为\" + strconv",
"v",
"I",
")",
")",
"\n",
"}",
"\n",
"oa(n)))",
".",
"field_identifier",
"(",
"identifier",
")",
"\n",
"b",
":=",
"c",
".",
"Validate",
".",
"Length",
"(",
"v",
",",
"n",
")",
"\n",
"if",
"b",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"长度应该为\" + strconv",
"v",
"I",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// StrLength param is a string, length must be n
|
[
"StrLength",
"param",
"is",
"a",
"string",
"length",
"must",
"be",
"n"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/controller.go#L154-L167
|
test
|
HiLittleCat/core
|
controller.go
|
StrLenIn
|
func (c *Controller) StrLenIn(fieldName string, p interface{}, l ...int) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
length := utf8.RuneCountInString(v)
b := false
for i := 0; i < len(l); i++ {
if l[i] == length {
b = true
}
}
if b == false {
panic((&ValidationError{}).New(fieldName + "值的长度应该在" + strings.Replace(strings.Trim(fmt.Sprint(l), "[]"), " ", ",", -1) + "中"))
}
return v
}
|
go
|
func (c *Controller) StrLenIn(fieldName string, p interface{}, l ...int) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
length := utf8.RuneCountInString(v)
b := false
for i := 0; i < len(l); i++ {
if l[i] == length {
b = true
}
}
if b == false {
panic((&ValidationError{}).New(fieldName + "值的长度应该在" + strings.Replace(strings.Trim(fmt.Sprint(l), "[]"), " ", ",", -1) + "中"))
}
return v
}
|
[
"func",
"(",
"c",
"*",
"Controller",
")",
"StrLenIn",
"(",
"fieldName",
"string",
",",
"p",
"interface",
"{",
"}",
",",
"l",
"...",
"int",
")",
"string",
"{",
"if",
"p",
"==",
"nil",
"{",
"p",
"=",
"\"\"",
"\n",
"}",
"\n",
"v",
",",
"ok",
":=",
"p",
".",
"(",
"string",
")",
"\n",
"if",
"ok",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"格式错误\"))",
")",
")",
"\n",
"}",
"\n",
"length",
":=",
"utf8",
".",
"RuneCountInString",
"(",
"v",
")",
"\n",
"b",
":=",
"false",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"l",
")",
";",
"i",
"++",
"{",
"if",
"l",
"[",
"i",
"]",
"==",
"length",
"{",
"b",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"b",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"值的长度应该在\" + strings.Rep",
"p",
"a",
"+",
"interpreted_string_literal",
")",
")",
"\n",
"}",
"\n",
"e(strin",
"g",
"s.Trim(",
"f",
"mt.Spri",
"n",
"t(l)",
",",
" \"[",
"]",
"\"), \" ",
"\"",
",",
" ",
"\"",
"\", -",
"-",
"1",
")",
"+ \"",
"+",
",",
"中",
")",
"\n",
"}"
] |
// StrLenIn param is a string, length is in array
|
[
"StrLenIn",
"param",
"is",
"a",
"string",
"length",
"is",
"in",
"array"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/controller.go#L186-L205
|
test
|
HiLittleCat/core
|
controller.go
|
StrIn
|
func (c *Controller) StrIn(fieldName string, p interface{}, l ...string) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
b := false
for i := 0; i < len(l); i++ {
if l[i] == v {
b = true
}
}
if b == false {
panic((&ValidationError{}).New(fieldName + "值应该在" + strings.Replace(strings.Trim(fmt.Sprint(l), "[]"), " ", ",", -1) + "中"))
}
return v
}
|
go
|
func (c *Controller) StrIn(fieldName string, p interface{}, l ...string) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
b := false
for i := 0; i < len(l); i++ {
if l[i] == v {
b = true
}
}
if b == false {
panic((&ValidationError{}).New(fieldName + "值应该在" + strings.Replace(strings.Trim(fmt.Sprint(l), "[]"), " ", ",", -1) + "中"))
}
return v
}
|
[
"func",
"(",
"c",
"*",
"Controller",
")",
"StrIn",
"(",
"fieldName",
"string",
",",
"p",
"interface",
"{",
"}",
",",
"l",
"...",
"string",
")",
"string",
"{",
"if",
"p",
"==",
"nil",
"{",
"p",
"=",
"\"\"",
"\n",
"}",
"\n",
"v",
",",
"ok",
":=",
"p",
".",
"(",
"string",
")",
"\n",
"if",
"ok",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"格式错误\"))",
")",
")",
"\n",
"}",
"\n",
"b",
":=",
"false",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"l",
")",
";",
"i",
"++",
"{",
"if",
"l",
"[",
"i",
"]",
"==",
"v",
"{",
"b",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"b",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"值应该在\" + strin",
"n",
"s",
"+",
"interpreted_string_literal",
")",
")",
"\n",
"}",
"\n",
"Replace",
"(",
"strings",
".",
"Trim(fm",
"t",
".Spr",
"i",
"nt(",
"l",
"), \"[]",
"\"",
")",
",",
" ",
" \", ",
" ",
",",
" ",
"\"",
", -",
",",
"-",
")",
"\n",
"}"
] |
// StrIn param is a string, the string is in array
|
[
"StrIn",
"param",
"is",
"a",
"string",
"the",
"string",
"is",
"in",
"array"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/controller.go#L208-L226
|
test
|
HiLittleCat/core
|
controller.go
|
GetEmail
|
func (c *Controller) GetEmail(fieldName string, p interface{}) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
b := c.Validate.Email(v)
if b == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
return v
}
|
go
|
func (c *Controller) GetEmail(fieldName string, p interface{}) string {
if p == nil {
p = ""
}
v, ok := p.(string)
if ok == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
b := c.Validate.Email(v)
if b == false {
panic((&ValidationError{}).New(fieldName + "格式错误"))
}
return v
}
|
[
"func",
"(",
"c",
"*",
"Controller",
")",
"GetEmail",
"(",
"fieldName",
"string",
",",
"p",
"interface",
"{",
"}",
")",
"string",
"{",
"if",
"p",
"==",
"nil",
"{",
"p",
"=",
"\"\"",
"\n",
"}",
"\n",
"v",
",",
"ok",
":=",
"p",
".",
"(",
"string",
")",
"\n",
"if",
"ok",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"格式错误\"))",
")",
")",
"\n",
"}",
"\n",
"b",
":=",
"c",
".",
"Validate",
".",
"Email",
"(",
"v",
")",
"\n",
"if",
"b",
"==",
"false",
"{",
"panic",
"(",
"(",
"&",
"ValidationError",
"{",
"}",
")",
".",
"New",
"(",
"fieldName",
"+",
"\"格式错误\"))",
")",
")",
"\n",
"}",
"\n",
"return",
"v",
"\n",
"}"
] |
// GetEmail check is a email
|
[
"GetEmail",
"check",
"is",
"a",
"email"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/controller.go#L229-L242
|
test
|
Financial-Times/neo-model-utils-go
|
mapper/types.go
|
MostSpecificType
|
func MostSpecificType(types []string) (string, error) {
if len(types) == 0 {
return "", errors.New("no types supplied")
}
sorted, err := SortTypes(types)
if err != nil {
return "", err
}
return sorted[len(sorted)-1], nil
}
|
go
|
func MostSpecificType(types []string) (string, error) {
if len(types) == 0 {
return "", errors.New("no types supplied")
}
sorted, err := SortTypes(types)
if err != nil {
return "", err
}
return sorted[len(sorted)-1], nil
}
|
[
"func",
"MostSpecificType",
"(",
"types",
"[",
"]",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"if",
"len",
"(",
"types",
")",
"==",
"0",
"{",
"return",
"\"\"",
",",
"errors",
".",
"New",
"(",
"\"no types supplied\"",
")",
"\n",
"}",
"\n",
"sorted",
",",
"err",
":=",
"SortTypes",
"(",
"types",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"sorted",
"[",
"len",
"(",
"sorted",
")",
"-",
"1",
"]",
",",
"nil",
"\n",
"}"
] |
// MostSpecific returns the most specific from a list of types in an hierarchy
// behaviour is undefined if any of the types are siblings.
|
[
"MostSpecific",
"returns",
"the",
"most",
"specific",
"from",
"a",
"list",
"of",
"types",
"in",
"an",
"hierarchy",
"behaviour",
"is",
"undefined",
"if",
"any",
"of",
"the",
"types",
"are",
"siblings",
"."
] |
aea1e95c83055aaed6761c5e546400dd383fb220
|
https://github.com/Financial-Times/neo-model-utils-go/blob/aea1e95c83055aaed6761c5e546400dd383fb220/mapper/types.go#L51-L60
|
test
|
Financial-Times/neo-model-utils-go
|
mapper/types.go
|
FullTypeHierarchy
|
func FullTypeHierarchy(highestLevelType string) []string {
var typeHierarchy []string
t := strings.Split(highestLevelType, "/")
typeToCheck := t[len(t)-1]
for {
typeHierarchy = append(typeHierarchy, typeToCheck)
parentType := ParentType(typeToCheck)
if parentType != "" {
typeToCheck = parentType
} else {
return TypeURIs(typeHierarchy)
}
}
}
|
go
|
func FullTypeHierarchy(highestLevelType string) []string {
var typeHierarchy []string
t := strings.Split(highestLevelType, "/")
typeToCheck := t[len(t)-1]
for {
typeHierarchy = append(typeHierarchy, typeToCheck)
parentType := ParentType(typeToCheck)
if parentType != "" {
typeToCheck = parentType
} else {
return TypeURIs(typeHierarchy)
}
}
}
|
[
"func",
"FullTypeHierarchy",
"(",
"highestLevelType",
"string",
")",
"[",
"]",
"string",
"{",
"var",
"typeHierarchy",
"[",
"]",
"string",
"\n",
"t",
":=",
"strings",
".",
"Split",
"(",
"highestLevelType",
",",
"\"/\"",
")",
"\n",
"typeToCheck",
":=",
"t",
"[",
"len",
"(",
"t",
")",
"-",
"1",
"]",
"\n",
"for",
"{",
"typeHierarchy",
"=",
"append",
"(",
"typeHierarchy",
",",
"typeToCheck",
")",
"\n",
"parentType",
":=",
"ParentType",
"(",
"typeToCheck",
")",
"\n",
"if",
"parentType",
"!=",
"\"\"",
"{",
"typeToCheck",
"=",
"parentType",
"\n",
"}",
"else",
"{",
"return",
"TypeURIs",
"(",
"typeHierarchy",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
//Full type hierarchy is returned when provided either the concept type
// or full uri of the most specific concept type
|
[
"Full",
"type",
"hierarchy",
"is",
"returned",
"when",
"provided",
"either",
"the",
"concept",
"type",
"or",
"full",
"uri",
"of",
"the",
"most",
"specific",
"concept",
"type"
] |
aea1e95c83055aaed6761c5e546400dd383fb220
|
https://github.com/Financial-Times/neo-model-utils-go/blob/aea1e95c83055aaed6761c5e546400dd383fb220/mapper/types.go#L64-L78
|
test
|
Financial-Times/neo-model-utils-go
|
mapper/types.go
|
SortTypes
|
func SortTypes(types []string) ([]string, error) {
ts := &typeSorter{types: make([]string, len(types))}
copy(ts.types, types)
sort.Sort(ts)
if ts.invalid {
return types, ErrNotHierarchy
}
return ts.types, nil
}
|
go
|
func SortTypes(types []string) ([]string, error) {
ts := &typeSorter{types: make([]string, len(types))}
copy(ts.types, types)
sort.Sort(ts)
if ts.invalid {
return types, ErrNotHierarchy
}
return ts.types, nil
}
|
[
"func",
"SortTypes",
"(",
"types",
"[",
"]",
"string",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"ts",
":=",
"&",
"typeSorter",
"{",
"types",
":",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"types",
")",
")",
"}",
"\n",
"copy",
"(",
"ts",
".",
"types",
",",
"types",
")",
"\n",
"sort",
".",
"Sort",
"(",
"ts",
")",
"\n",
"if",
"ts",
".",
"invalid",
"{",
"return",
"types",
",",
"ErrNotHierarchy",
"\n",
"}",
"\n",
"return",
"ts",
".",
"types",
",",
"nil",
"\n",
"}"
] |
// SortTypes sorts the given types from least specific to most specific
|
[
"SortTypes",
"sorts",
"the",
"given",
"types",
"from",
"least",
"specific",
"to",
"most",
"specific"
] |
aea1e95c83055aaed6761c5e546400dd383fb220
|
https://github.com/Financial-Times/neo-model-utils-go/blob/aea1e95c83055aaed6761c5e546400dd383fb220/mapper/types.go#L83-L91
|
test
|
HiLittleCat/core
|
store.go
|
Delete
|
func (rs *redisStore) Delete(key string) error {
delete(rs.Values, key)
err := provider.refresh(rs)
return err
}
|
go
|
func (rs *redisStore) Delete(key string) error {
delete(rs.Values, key)
err := provider.refresh(rs)
return err
}
|
[
"func",
"(",
"rs",
"*",
"redisStore",
")",
"Delete",
"(",
"key",
"string",
")",
"error",
"{",
"delete",
"(",
"rs",
".",
"Values",
",",
"key",
")",
"\n",
"err",
":=",
"provider",
".",
"refresh",
"(",
"rs",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// Delete value in redis session
|
[
"Delete",
"value",
"in",
"redis",
"session"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/store.go#L47-L51
|
test
|
HiLittleCat/core
|
store.go
|
Set
|
func (rp *redisProvider) Set(key string, values map[string]string) (*redisStore, error) {
rs := &redisStore{SID: key, Values: values}
err := provider.refresh(rs)
return rs, err
}
|
go
|
func (rp *redisProvider) Set(key string, values map[string]string) (*redisStore, error) {
rs := &redisStore{SID: key, Values: values}
err := provider.refresh(rs)
return rs, err
}
|
[
"func",
"(",
"rp",
"*",
"redisProvider",
")",
"Set",
"(",
"key",
"string",
",",
"values",
"map",
"[",
"string",
"]",
"string",
")",
"(",
"*",
"redisStore",
",",
"error",
")",
"{",
"rs",
":=",
"&",
"redisStore",
"{",
"SID",
":",
"key",
",",
"Values",
":",
"values",
"}",
"\n",
"err",
":=",
"provider",
".",
"refresh",
"(",
"rs",
")",
"\n",
"return",
"rs",
",",
"err",
"\n",
"}"
] |
// Set value in redis session
|
[
"Set",
"value",
"in",
"redis",
"session"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/store.go#L63-L67
|
test
|
HiLittleCat/core
|
store.go
|
refresh
|
func (rp *redisProvider) refresh(rs *redisStore) error {
var err error
redisPool.Exec(func(c *redis.Client) {
err = c.HMSet(rs.SID, rs.Values).Err()
if err != nil {
return
}
err = c.Expire(rs.SID, sessExpire).Err()
})
return nil
}
|
go
|
func (rp *redisProvider) refresh(rs *redisStore) error {
var err error
redisPool.Exec(func(c *redis.Client) {
err = c.HMSet(rs.SID, rs.Values).Err()
if err != nil {
return
}
err = c.Expire(rs.SID, sessExpire).Err()
})
return nil
}
|
[
"func",
"(",
"rp",
"*",
"redisProvider",
")",
"refresh",
"(",
"rs",
"*",
"redisStore",
")",
"error",
"{",
"var",
"err",
"error",
"\n",
"redisPool",
".",
"Exec",
"(",
"func",
"(",
"c",
"*",
"redis",
".",
"Client",
")",
"{",
"err",
"=",
"c",
".",
"HMSet",
"(",
"rs",
".",
"SID",
",",
"rs",
".",
"Values",
")",
".",
"Err",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"err",
"=",
"c",
".",
"Expire",
"(",
"rs",
".",
"SID",
",",
"sessExpire",
")",
".",
"Err",
"(",
")",
"\n",
"}",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// refresh refresh store to redis
|
[
"refresh",
"refresh",
"store",
"to",
"redis"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/store.go#L70-L80
|
test
|
HiLittleCat/core
|
store.go
|
Get
|
func (rp *redisProvider) Get(sid string) (*redisStore, error) {
var rs = &redisStore{}
var val map[string]string
var err error
redisPool.Exec(func(c *redis.Client) {
val, err = c.HGetAll(sid).Result()
rs.Values = val
})
return rs, err
}
|
go
|
func (rp *redisProvider) Get(sid string) (*redisStore, error) {
var rs = &redisStore{}
var val map[string]string
var err error
redisPool.Exec(func(c *redis.Client) {
val, err = c.HGetAll(sid).Result()
rs.Values = val
})
return rs, err
}
|
[
"func",
"(",
"rp",
"*",
"redisProvider",
")",
"Get",
"(",
"sid",
"string",
")",
"(",
"*",
"redisStore",
",",
"error",
")",
"{",
"var",
"rs",
"=",
"&",
"redisStore",
"{",
"}",
"\n",
"var",
"val",
"map",
"[",
"string",
"]",
"string",
"\n",
"var",
"err",
"error",
"\n",
"redisPool",
".",
"Exec",
"(",
"func",
"(",
"c",
"*",
"redis",
".",
"Client",
")",
"{",
"val",
",",
"err",
"=",
"c",
".",
"HGetAll",
"(",
"sid",
")",
".",
"Result",
"(",
")",
"\n",
"rs",
".",
"Values",
"=",
"val",
"\n",
"}",
")",
"\n",
"return",
"rs",
",",
"err",
"\n",
"}"
] |
// Get read redis session by sid
|
[
"Get",
"read",
"redis",
"session",
"by",
"sid"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/store.go#L83-L92
|
test
|
HiLittleCat/core
|
store.go
|
Destroy
|
func (rp *redisProvider) Destroy(sid string) error {
var err error
redisPool.Exec(func(c *redis.Client) {
err = c.Del(sid).Err()
})
return err
}
|
go
|
func (rp *redisProvider) Destroy(sid string) error {
var err error
redisPool.Exec(func(c *redis.Client) {
err = c.Del(sid).Err()
})
return err
}
|
[
"func",
"(",
"rp",
"*",
"redisProvider",
")",
"Destroy",
"(",
"sid",
"string",
")",
"error",
"{",
"var",
"err",
"error",
"\n",
"redisPool",
".",
"Exec",
"(",
"func",
"(",
"c",
"*",
"redis",
".",
"Client",
")",
"{",
"err",
"=",
"c",
".",
"Del",
"(",
"sid",
")",
".",
"Err",
"(",
")",
"\n",
"}",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// Destroy delete redis session by id
|
[
"Destroy",
"delete",
"redis",
"session",
"by",
"id"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/store.go#L95-L101
|
test
|
HiLittleCat/core
|
store.go
|
UpExpire
|
func (rp *redisProvider) UpExpire(sid string) error {
var err error
redisPool.Exec(func(c *redis.Client) {
err = c.Expire(sid, sessExpire).Err()
})
return err
}
|
go
|
func (rp *redisProvider) UpExpire(sid string) error {
var err error
redisPool.Exec(func(c *redis.Client) {
err = c.Expire(sid, sessExpire).Err()
})
return err
}
|
[
"func",
"(",
"rp",
"*",
"redisProvider",
")",
"UpExpire",
"(",
"sid",
"string",
")",
"error",
"{",
"var",
"err",
"error",
"\n",
"redisPool",
".",
"Exec",
"(",
"func",
"(",
"c",
"*",
"redis",
".",
"Client",
")",
"{",
"err",
"=",
"c",
".",
"Expire",
"(",
"sid",
",",
"sessExpire",
")",
".",
"Err",
"(",
")",
"\n",
"}",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// UpExpire refresh session expire
|
[
"UpExpire",
"refresh",
"session",
"expire"
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/store.go#L104-L110
|
test
|
HiLittleCat/core
|
handler.go
|
Use
|
func (hs *HandlersStack) Use(h RouterHandler) {
hs.Handlers = append(hs.Handlers, h)
}
|
go
|
func (hs *HandlersStack) Use(h RouterHandler) {
hs.Handlers = append(hs.Handlers, h)
}
|
[
"func",
"(",
"hs",
"*",
"HandlersStack",
")",
"Use",
"(",
"h",
"RouterHandler",
")",
"{",
"hs",
".",
"Handlers",
"=",
"append",
"(",
"hs",
".",
"Handlers",
",",
"h",
")",
"\n",
"}"
] |
// Use adds a handler to the handlers stack.
|
[
"Use",
"adds",
"a",
"handler",
"to",
"the",
"handlers",
"stack",
"."
] |
ae2101184ecd36354d3fcff0ea69d67d3fdbe156
|
https://github.com/HiLittleCat/core/blob/ae2101184ecd36354d3fcff0ea69d67d3fdbe156/handler.go#L22-L24
|
test
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.