repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
list | docstring
stringlengths 6
2.61k
| docstring_tokens
list | sha
stringlengths 40
40
| url
stringlengths 85
252
| partition
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
asdine/storm
|
finder.go
|
One
|
func (n *node) One(fieldName string, value interface{}, to interface{}) error {
sink, err := newFirstSink(n, to)
if err != nil {
return err
}
bucketName := sink.bucketName()
if bucketName == "" {
return ErrNoName
}
if fieldName == "" {
return ErrNotFound
}
ref := reflect.Indirect(sink.ref)
cfg, err := extractSingleField(&ref, fieldName)
if err != nil {
return err
}
field, ok := cfg.Fields[fieldName]
if !ok || (!field.IsID && field.Index == "") {
query := newQuery(n, q.StrictEq(fieldName, value))
query.Limit(1)
if n.tx != nil {
err = query.query(n.tx, sink)
} else {
err = n.s.Bolt.View(func(tx *bolt.Tx) error {
return query.query(tx, sink)
})
}
if err != nil {
return err
}
return sink.flush()
}
val, err := toBytes(value, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
return n.one(tx, bucketName, fieldName, cfg, to, val, field.IsID)
})
}
|
go
|
func (n *node) One(fieldName string, value interface{}, to interface{}) error {
sink, err := newFirstSink(n, to)
if err != nil {
return err
}
bucketName := sink.bucketName()
if bucketName == "" {
return ErrNoName
}
if fieldName == "" {
return ErrNotFound
}
ref := reflect.Indirect(sink.ref)
cfg, err := extractSingleField(&ref, fieldName)
if err != nil {
return err
}
field, ok := cfg.Fields[fieldName]
if !ok || (!field.IsID && field.Index == "") {
query := newQuery(n, q.StrictEq(fieldName, value))
query.Limit(1)
if n.tx != nil {
err = query.query(n.tx, sink)
} else {
err = n.s.Bolt.View(func(tx *bolt.Tx) error {
return query.query(tx, sink)
})
}
if err != nil {
return err
}
return sink.flush()
}
val, err := toBytes(value, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
return n.one(tx, bucketName, fieldName, cfg, to, val, field.IsID)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"One",
"(",
"fieldName",
"string",
",",
"value",
"interface",
"{",
"}",
",",
"to",
"interface",
"{",
"}",
")",
"error",
"{",
"sink",
",",
"err",
":=",
"newFirstSink",
"(",
"n",
",",
"to",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"bucketName",
":=",
"sink",
".",
"bucketName",
"(",
")",
"\n",
"if",
"bucketName",
"==",
"\"",
"\"",
"{",
"return",
"ErrNoName",
"\n",
"}",
"\n\n",
"if",
"fieldName",
"==",
"\"",
"\"",
"{",
"return",
"ErrNotFound",
"\n",
"}",
"\n\n",
"ref",
":=",
"reflect",
".",
"Indirect",
"(",
"sink",
".",
"ref",
")",
"\n",
"cfg",
",",
"err",
":=",
"extractSingleField",
"(",
"&",
"ref",
",",
"fieldName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"field",
",",
"ok",
":=",
"cfg",
".",
"Fields",
"[",
"fieldName",
"]",
"\n",
"if",
"!",
"ok",
"||",
"(",
"!",
"field",
".",
"IsID",
"&&",
"field",
".",
"Index",
"==",
"\"",
"\"",
")",
"{",
"query",
":=",
"newQuery",
"(",
"n",
",",
"q",
".",
"StrictEq",
"(",
"fieldName",
",",
"value",
")",
")",
"\n",
"query",
".",
"Limit",
"(",
"1",
")",
"\n\n",
"if",
"n",
".",
"tx",
"!=",
"nil",
"{",
"err",
"=",
"query",
".",
"query",
"(",
"n",
".",
"tx",
",",
"sink",
")",
"\n",
"}",
"else",
"{",
"err",
"=",
"n",
".",
"s",
".",
"Bolt",
".",
"View",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"query",
".",
"query",
"(",
"tx",
",",
"sink",
")",
"\n",
"}",
")",
"\n",
"}",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"sink",
".",
"flush",
"(",
")",
"\n",
"}",
"\n\n",
"val",
",",
"err",
":=",
"toBytes",
"(",
"value",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"one",
"(",
"tx",
",",
"bucketName",
",",
"fieldName",
",",
"cfg",
",",
"to",
",",
"val",
",",
"field",
".",
"IsID",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// One returns one record by the specified index
|
[
"One",
"returns",
"one",
"record",
"by",
"the",
"specified",
"index"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/finder.go#L41-L90
|
train
|
asdine/storm
|
finder.go
|
AllByIndex
|
func (n *node) AllByIndex(fieldName string, to interface{}, options ...func(*index.Options)) error {
if fieldName == "" {
return n.All(to, options...)
}
ref := reflect.ValueOf(to)
if ref.Kind() != reflect.Ptr || ref.Elem().Kind() != reflect.Slice {
return ErrSlicePtrNeeded
}
typ := reflect.Indirect(ref).Type().Elem()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
newElem := reflect.New(typ)
cfg, err := extract(&newElem)
if err != nil {
return err
}
if cfg.ID.Name == fieldName {
return n.All(to, options...)
}
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
return n.readTx(func(tx *bolt.Tx) error {
return n.allByIndex(tx, fieldName, cfg, &ref, opts)
})
}
|
go
|
func (n *node) AllByIndex(fieldName string, to interface{}, options ...func(*index.Options)) error {
if fieldName == "" {
return n.All(to, options...)
}
ref := reflect.ValueOf(to)
if ref.Kind() != reflect.Ptr || ref.Elem().Kind() != reflect.Slice {
return ErrSlicePtrNeeded
}
typ := reflect.Indirect(ref).Type().Elem()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
newElem := reflect.New(typ)
cfg, err := extract(&newElem)
if err != nil {
return err
}
if cfg.ID.Name == fieldName {
return n.All(to, options...)
}
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
return n.readTx(func(tx *bolt.Tx) error {
return n.allByIndex(tx, fieldName, cfg, &ref, opts)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"AllByIndex",
"(",
"fieldName",
"string",
",",
"to",
"interface",
"{",
"}",
",",
"options",
"...",
"func",
"(",
"*",
"index",
".",
"Options",
")",
")",
"error",
"{",
"if",
"fieldName",
"==",
"\"",
"\"",
"{",
"return",
"n",
".",
"All",
"(",
"to",
",",
"options",
"...",
")",
"\n",
"}",
"\n\n",
"ref",
":=",
"reflect",
".",
"ValueOf",
"(",
"to",
")",
"\n\n",
"if",
"ref",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"||",
"ref",
".",
"Elem",
"(",
")",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Slice",
"{",
"return",
"ErrSlicePtrNeeded",
"\n",
"}",
"\n\n",
"typ",
":=",
"reflect",
".",
"Indirect",
"(",
"ref",
")",
".",
"Type",
"(",
")",
".",
"Elem",
"(",
")",
"\n\n",
"if",
"typ",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"typ",
"=",
"typ",
".",
"Elem",
"(",
")",
"\n",
"}",
"\n\n",
"newElem",
":=",
"reflect",
".",
"New",
"(",
"typ",
")",
"\n\n",
"cfg",
",",
"err",
":=",
"extract",
"(",
"&",
"newElem",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"cfg",
".",
"ID",
".",
"Name",
"==",
"fieldName",
"{",
"return",
"n",
".",
"All",
"(",
"to",
",",
"options",
"...",
")",
"\n",
"}",
"\n\n",
"opts",
":=",
"index",
".",
"NewOptions",
"(",
")",
"\n",
"for",
"_",
",",
"fn",
":=",
"range",
"options",
"{",
"fn",
"(",
"opts",
")",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"allByIndex",
"(",
"tx",
",",
"fieldName",
",",
"cfg",
",",
"&",
"ref",
",",
"opts",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// AllByIndex gets all the records of a bucket that are indexed in the specified index
|
[
"AllByIndex",
"gets",
"all",
"the",
"records",
"of",
"a",
"bucket",
"that",
"are",
"indexed",
"in",
"the",
"specified",
"index"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/finder.go#L213-L249
|
train
|
asdine/storm
|
finder.go
|
All
|
func (n *node) All(to interface{}, options ...func(*index.Options)) error {
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
query := newQuery(n, nil).Limit(opts.Limit).Skip(opts.Skip)
if opts.Reverse {
query.Reverse()
}
err := query.Find(to)
if err != nil && err != ErrNotFound {
return err
}
if err == ErrNotFound {
ref := reflect.ValueOf(to)
results := reflect.MakeSlice(reflect.Indirect(ref).Type(), 0, 0)
reflect.Indirect(ref).Set(results)
}
return nil
}
|
go
|
func (n *node) All(to interface{}, options ...func(*index.Options)) error {
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
query := newQuery(n, nil).Limit(opts.Limit).Skip(opts.Skip)
if opts.Reverse {
query.Reverse()
}
err := query.Find(to)
if err != nil && err != ErrNotFound {
return err
}
if err == ErrNotFound {
ref := reflect.ValueOf(to)
results := reflect.MakeSlice(reflect.Indirect(ref).Type(), 0, 0)
reflect.Indirect(ref).Set(results)
}
return nil
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"All",
"(",
"to",
"interface",
"{",
"}",
",",
"options",
"...",
"func",
"(",
"*",
"index",
".",
"Options",
")",
")",
"error",
"{",
"opts",
":=",
"index",
".",
"NewOptions",
"(",
")",
"\n",
"for",
"_",
",",
"fn",
":=",
"range",
"options",
"{",
"fn",
"(",
"opts",
")",
"\n",
"}",
"\n\n",
"query",
":=",
"newQuery",
"(",
"n",
",",
"nil",
")",
".",
"Limit",
"(",
"opts",
".",
"Limit",
")",
".",
"Skip",
"(",
"opts",
".",
"Skip",
")",
"\n",
"if",
"opts",
".",
"Reverse",
"{",
"query",
".",
"Reverse",
"(",
")",
"\n",
"}",
"\n\n",
"err",
":=",
"query",
".",
"Find",
"(",
"to",
")",
"\n",
"if",
"err",
"!=",
"nil",
"&&",
"err",
"!=",
"ErrNotFound",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"err",
"==",
"ErrNotFound",
"{",
"ref",
":=",
"reflect",
".",
"ValueOf",
"(",
"to",
")",
"\n",
"results",
":=",
"reflect",
".",
"MakeSlice",
"(",
"reflect",
".",
"Indirect",
"(",
"ref",
")",
".",
"Type",
"(",
")",
",",
"0",
",",
"0",
")",
"\n",
"reflect",
".",
"Indirect",
"(",
"ref",
")",
".",
"Set",
"(",
"results",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// All gets all the records of a bucket.
// If there are no records it returns no error and the 'to' parameter is set to an empty slice.
|
[
"All",
"gets",
"all",
"the",
"records",
"of",
"a",
"bucket",
".",
"If",
"there",
"are",
"no",
"records",
"it",
"returns",
"no",
"error",
"and",
"the",
"to",
"parameter",
"is",
"set",
"to",
"an",
"empty",
"slice",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/finder.go#L295-L317
|
train
|
asdine/storm
|
finder.go
|
Range
|
func (n *node) Range(fieldName string, min, max, to interface{}, options ...func(*index.Options)) error {
sink, err := newListSink(n, to)
if err != nil {
return err
}
bucketName := sink.bucketName()
if bucketName == "" {
return ErrNoName
}
ref := reflect.Indirect(reflect.New(sink.elemType))
cfg, err := extractSingleField(&ref, fieldName)
if err != nil {
return err
}
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
field, ok := cfg.Fields[fieldName]
if !ok || (!field.IsID && field.Index == "") {
query := newQuery(n, q.And(q.Gte(fieldName, min), q.Lte(fieldName, max)))
query.Skip(opts.Skip).Limit(opts.Limit)
if opts.Reverse {
query.Reverse()
}
err = n.readTx(func(tx *bolt.Tx) error {
return query.query(tx, sink)
})
if err != nil {
return err
}
return sink.flush()
}
mn, err := toBytes(min, n.codec)
if err != nil {
return err
}
mx, err := toBytes(max, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
return n.rnge(tx, bucketName, fieldName, cfg, sink, mn, mx, opts)
})
}
|
go
|
func (n *node) Range(fieldName string, min, max, to interface{}, options ...func(*index.Options)) error {
sink, err := newListSink(n, to)
if err != nil {
return err
}
bucketName := sink.bucketName()
if bucketName == "" {
return ErrNoName
}
ref := reflect.Indirect(reflect.New(sink.elemType))
cfg, err := extractSingleField(&ref, fieldName)
if err != nil {
return err
}
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
field, ok := cfg.Fields[fieldName]
if !ok || (!field.IsID && field.Index == "") {
query := newQuery(n, q.And(q.Gte(fieldName, min), q.Lte(fieldName, max)))
query.Skip(opts.Skip).Limit(opts.Limit)
if opts.Reverse {
query.Reverse()
}
err = n.readTx(func(tx *bolt.Tx) error {
return query.query(tx, sink)
})
if err != nil {
return err
}
return sink.flush()
}
mn, err := toBytes(min, n.codec)
if err != nil {
return err
}
mx, err := toBytes(max, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
return n.rnge(tx, bucketName, fieldName, cfg, sink, mn, mx, opts)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Range",
"(",
"fieldName",
"string",
",",
"min",
",",
"max",
",",
"to",
"interface",
"{",
"}",
",",
"options",
"...",
"func",
"(",
"*",
"index",
".",
"Options",
")",
")",
"error",
"{",
"sink",
",",
"err",
":=",
"newListSink",
"(",
"n",
",",
"to",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"bucketName",
":=",
"sink",
".",
"bucketName",
"(",
")",
"\n",
"if",
"bucketName",
"==",
"\"",
"\"",
"{",
"return",
"ErrNoName",
"\n",
"}",
"\n\n",
"ref",
":=",
"reflect",
".",
"Indirect",
"(",
"reflect",
".",
"New",
"(",
"sink",
".",
"elemType",
")",
")",
"\n",
"cfg",
",",
"err",
":=",
"extractSingleField",
"(",
"&",
"ref",
",",
"fieldName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"opts",
":=",
"index",
".",
"NewOptions",
"(",
")",
"\n",
"for",
"_",
",",
"fn",
":=",
"range",
"options",
"{",
"fn",
"(",
"opts",
")",
"\n",
"}",
"\n\n",
"field",
",",
"ok",
":=",
"cfg",
".",
"Fields",
"[",
"fieldName",
"]",
"\n",
"if",
"!",
"ok",
"||",
"(",
"!",
"field",
".",
"IsID",
"&&",
"field",
".",
"Index",
"==",
"\"",
"\"",
")",
"{",
"query",
":=",
"newQuery",
"(",
"n",
",",
"q",
".",
"And",
"(",
"q",
".",
"Gte",
"(",
"fieldName",
",",
"min",
")",
",",
"q",
".",
"Lte",
"(",
"fieldName",
",",
"max",
")",
")",
")",
"\n",
"query",
".",
"Skip",
"(",
"opts",
".",
"Skip",
")",
".",
"Limit",
"(",
"opts",
".",
"Limit",
")",
"\n\n",
"if",
"opts",
".",
"Reverse",
"{",
"query",
".",
"Reverse",
"(",
")",
"\n",
"}",
"\n\n",
"err",
"=",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"query",
".",
"query",
"(",
"tx",
",",
"sink",
")",
"\n",
"}",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"sink",
".",
"flush",
"(",
")",
"\n",
"}",
"\n\n",
"mn",
",",
"err",
":=",
"toBytes",
"(",
"min",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"mx",
",",
"err",
":=",
"toBytes",
"(",
"max",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"rnge",
"(",
"tx",
",",
"bucketName",
",",
"fieldName",
",",
"cfg",
",",
"sink",
",",
"mn",
",",
"mx",
",",
"opts",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Range returns one or more records by the specified index within the specified range
|
[
"Range",
"returns",
"one",
"or",
"more",
"records",
"by",
"the",
"specified",
"index",
"within",
"the",
"specified",
"range"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/finder.go#L320-L375
|
train
|
asdine/storm
|
finder.go
|
Prefix
|
func (n *node) Prefix(fieldName string, prefix string, to interface{}, options ...func(*index.Options)) error {
sink, err := newListSink(n, to)
if err != nil {
return err
}
bucketName := sink.bucketName()
if bucketName == "" {
return ErrNoName
}
ref := reflect.Indirect(reflect.New(sink.elemType))
cfg, err := extractSingleField(&ref, fieldName)
if err != nil {
return err
}
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
field, ok := cfg.Fields[fieldName]
if !ok || (!field.IsID && field.Index == "") {
query := newQuery(n, q.Re(fieldName, fmt.Sprintf("^%s", prefix)))
query.Skip(opts.Skip).Limit(opts.Limit)
if opts.Reverse {
query.Reverse()
}
err = n.readTx(func(tx *bolt.Tx) error {
return query.query(tx, sink)
})
if err != nil {
return err
}
return sink.flush()
}
prfx, err := toBytes(prefix, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
return n.prefix(tx, bucketName, fieldName, cfg, sink, prfx, opts)
})
}
|
go
|
func (n *node) Prefix(fieldName string, prefix string, to interface{}, options ...func(*index.Options)) error {
sink, err := newListSink(n, to)
if err != nil {
return err
}
bucketName := sink.bucketName()
if bucketName == "" {
return ErrNoName
}
ref := reflect.Indirect(reflect.New(sink.elemType))
cfg, err := extractSingleField(&ref, fieldName)
if err != nil {
return err
}
opts := index.NewOptions()
for _, fn := range options {
fn(opts)
}
field, ok := cfg.Fields[fieldName]
if !ok || (!field.IsID && field.Index == "") {
query := newQuery(n, q.Re(fieldName, fmt.Sprintf("^%s", prefix)))
query.Skip(opts.Skip).Limit(opts.Limit)
if opts.Reverse {
query.Reverse()
}
err = n.readTx(func(tx *bolt.Tx) error {
return query.query(tx, sink)
})
if err != nil {
return err
}
return sink.flush()
}
prfx, err := toBytes(prefix, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
return n.prefix(tx, bucketName, fieldName, cfg, sink, prfx, opts)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Prefix",
"(",
"fieldName",
"string",
",",
"prefix",
"string",
",",
"to",
"interface",
"{",
"}",
",",
"options",
"...",
"func",
"(",
"*",
"index",
".",
"Options",
")",
")",
"error",
"{",
"sink",
",",
"err",
":=",
"newListSink",
"(",
"n",
",",
"to",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"bucketName",
":=",
"sink",
".",
"bucketName",
"(",
")",
"\n",
"if",
"bucketName",
"==",
"\"",
"\"",
"{",
"return",
"ErrNoName",
"\n",
"}",
"\n\n",
"ref",
":=",
"reflect",
".",
"Indirect",
"(",
"reflect",
".",
"New",
"(",
"sink",
".",
"elemType",
")",
")",
"\n",
"cfg",
",",
"err",
":=",
"extractSingleField",
"(",
"&",
"ref",
",",
"fieldName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"opts",
":=",
"index",
".",
"NewOptions",
"(",
")",
"\n",
"for",
"_",
",",
"fn",
":=",
"range",
"options",
"{",
"fn",
"(",
"opts",
")",
"\n",
"}",
"\n\n",
"field",
",",
"ok",
":=",
"cfg",
".",
"Fields",
"[",
"fieldName",
"]",
"\n",
"if",
"!",
"ok",
"||",
"(",
"!",
"field",
".",
"IsID",
"&&",
"field",
".",
"Index",
"==",
"\"",
"\"",
")",
"{",
"query",
":=",
"newQuery",
"(",
"n",
",",
"q",
".",
"Re",
"(",
"fieldName",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"prefix",
")",
")",
")",
"\n",
"query",
".",
"Skip",
"(",
"opts",
".",
"Skip",
")",
".",
"Limit",
"(",
"opts",
".",
"Limit",
")",
"\n\n",
"if",
"opts",
".",
"Reverse",
"{",
"query",
".",
"Reverse",
"(",
")",
"\n",
"}",
"\n\n",
"err",
"=",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"query",
".",
"query",
"(",
"tx",
",",
"sink",
")",
"\n",
"}",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"sink",
".",
"flush",
"(",
")",
"\n",
"}",
"\n\n",
"prfx",
",",
"err",
":=",
"toBytes",
"(",
"prefix",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"prefix",
"(",
"tx",
",",
"bucketName",
",",
"fieldName",
",",
"cfg",
",",
"sink",
",",
"prfx",
",",
"opts",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Prefix returns one or more records whose given field starts with the specified prefix.
|
[
"Prefix",
"returns",
"one",
"or",
"more",
"records",
"whose",
"given",
"field",
"starts",
"with",
"the",
"specified",
"prefix",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/finder.go#L411-L461
|
train
|
asdine/storm
|
finder.go
|
Count
|
func (n *node) Count(data interface{}) (int, error) {
return n.Select().Count(data)
}
|
go
|
func (n *node) Count(data interface{}) (int, error) {
return n.Select().Count(data)
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Count",
"(",
"data",
"interface",
"{",
"}",
")",
"(",
"int",
",",
"error",
")",
"{",
"return",
"n",
".",
"Select",
"(",
")",
".",
"Count",
"(",
"data",
")",
"\n",
"}"
] |
// Count counts all the records of a bucket
|
[
"Count",
"counts",
"all",
"the",
"records",
"of",
"a",
"bucket"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/finder.go#L497-L499
|
train
|
asdine/storm
|
query.go
|
Select
|
func (n *node) Select(matchers ...q.Matcher) Query {
tree := q.And(matchers...)
return newQuery(n, tree)
}
|
go
|
func (n *node) Select(matchers ...q.Matcher) Query {
tree := q.And(matchers...)
return newQuery(n, tree)
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Select",
"(",
"matchers",
"...",
"q",
".",
"Matcher",
")",
"Query",
"{",
"tree",
":=",
"q",
".",
"And",
"(",
"matchers",
"...",
")",
"\n",
"return",
"newQuery",
"(",
"n",
",",
"tree",
")",
"\n",
"}"
] |
// Select a list of records that match a list of matchers. Doesn't use indexes.
|
[
"Select",
"a",
"list",
"of",
"records",
"that",
"match",
"a",
"list",
"of",
"matchers",
".",
"Doesn",
"t",
"use",
"indexes",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/query.go#L10-L13
|
train
|
asdine/storm
|
index/list.go
|
NewListIndex
|
func NewListIndex(parent *bolt.Bucket, indexName []byte) (*ListIndex, error) {
var err error
b := parent.Bucket(indexName)
if b == nil {
if !parent.Writable() {
return nil, ErrNotFound
}
b, err = parent.CreateBucket(indexName)
if err != nil {
return nil, err
}
}
ids, err := NewUniqueIndex(b, []byte("storm__ids"))
if err != nil {
return nil, err
}
return &ListIndex{
IndexBucket: b,
Parent: parent,
IDs: ids,
}, nil
}
|
go
|
func NewListIndex(parent *bolt.Bucket, indexName []byte) (*ListIndex, error) {
var err error
b := parent.Bucket(indexName)
if b == nil {
if !parent.Writable() {
return nil, ErrNotFound
}
b, err = parent.CreateBucket(indexName)
if err != nil {
return nil, err
}
}
ids, err := NewUniqueIndex(b, []byte("storm__ids"))
if err != nil {
return nil, err
}
return &ListIndex{
IndexBucket: b,
Parent: parent,
IDs: ids,
}, nil
}
|
[
"func",
"NewListIndex",
"(",
"parent",
"*",
"bolt",
".",
"Bucket",
",",
"indexName",
"[",
"]",
"byte",
")",
"(",
"*",
"ListIndex",
",",
"error",
")",
"{",
"var",
"err",
"error",
"\n",
"b",
":=",
"parent",
".",
"Bucket",
"(",
"indexName",
")",
"\n",
"if",
"b",
"==",
"nil",
"{",
"if",
"!",
"parent",
".",
"Writable",
"(",
")",
"{",
"return",
"nil",
",",
"ErrNotFound",
"\n",
"}",
"\n",
"b",
",",
"err",
"=",
"parent",
".",
"CreateBucket",
"(",
"indexName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"ids",
",",
"err",
":=",
"NewUniqueIndex",
"(",
"b",
",",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"ListIndex",
"{",
"IndexBucket",
":",
"b",
",",
"Parent",
":",
"parent",
",",
"IDs",
":",
"ids",
",",
"}",
",",
"nil",
"\n",
"}"
] |
// NewListIndex loads a ListIndex
|
[
"NewListIndex",
"loads",
"a",
"ListIndex"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/list.go#L11-L34
|
train
|
asdine/storm
|
index/list.go
|
Add
|
func (idx *ListIndex) Add(newValue []byte, targetID []byte) error {
if newValue == nil || len(newValue) == 0 {
return ErrNilParam
}
if targetID == nil || len(targetID) == 0 {
return ErrNilParam
}
key := idx.IDs.Get(targetID)
if key != nil {
err := idx.IndexBucket.Delete(key)
if err != nil {
return err
}
err = idx.IDs.Remove(targetID)
if err != nil {
return err
}
key = key[:0]
}
key = append(key, newValue...)
key = append(key, '_')
key = append(key, '_')
key = append(key, targetID...)
err := idx.IDs.Add(targetID, key)
if err != nil {
return err
}
return idx.IndexBucket.Put(key, targetID)
}
|
go
|
func (idx *ListIndex) Add(newValue []byte, targetID []byte) error {
if newValue == nil || len(newValue) == 0 {
return ErrNilParam
}
if targetID == nil || len(targetID) == 0 {
return ErrNilParam
}
key := idx.IDs.Get(targetID)
if key != nil {
err := idx.IndexBucket.Delete(key)
if err != nil {
return err
}
err = idx.IDs.Remove(targetID)
if err != nil {
return err
}
key = key[:0]
}
key = append(key, newValue...)
key = append(key, '_')
key = append(key, '_')
key = append(key, targetID...)
err := idx.IDs.Add(targetID, key)
if err != nil {
return err
}
return idx.IndexBucket.Put(key, targetID)
}
|
[
"func",
"(",
"idx",
"*",
"ListIndex",
")",
"Add",
"(",
"newValue",
"[",
"]",
"byte",
",",
"targetID",
"[",
"]",
"byte",
")",
"error",
"{",
"if",
"newValue",
"==",
"nil",
"||",
"len",
"(",
"newValue",
")",
"==",
"0",
"{",
"return",
"ErrNilParam",
"\n",
"}",
"\n",
"if",
"targetID",
"==",
"nil",
"||",
"len",
"(",
"targetID",
")",
"==",
"0",
"{",
"return",
"ErrNilParam",
"\n",
"}",
"\n\n",
"key",
":=",
"idx",
".",
"IDs",
".",
"Get",
"(",
"targetID",
")",
"\n",
"if",
"key",
"!=",
"nil",
"{",
"err",
":=",
"idx",
".",
"IndexBucket",
".",
"Delete",
"(",
"key",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"idx",
".",
"IDs",
".",
"Remove",
"(",
"targetID",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"key",
"=",
"key",
"[",
":",
"0",
"]",
"\n",
"}",
"\n\n",
"key",
"=",
"append",
"(",
"key",
",",
"newValue",
"...",
")",
"\n",
"key",
"=",
"append",
"(",
"key",
",",
"'_'",
")",
"\n",
"key",
"=",
"append",
"(",
"key",
",",
"'_'",
")",
"\n",
"key",
"=",
"append",
"(",
"key",
",",
"targetID",
"...",
")",
"\n\n",
"err",
":=",
"idx",
".",
"IDs",
".",
"Add",
"(",
"targetID",
",",
"key",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"idx",
".",
"IndexBucket",
".",
"Put",
"(",
"key",
",",
"targetID",
")",
"\n",
"}"
] |
// Add a value to the list index
|
[
"Add",
"a",
"value",
"to",
"the",
"list",
"index"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/list.go#L44-L78
|
train
|
asdine/storm
|
index/list.go
|
RemoveID
|
func (idx *ListIndex) RemoveID(targetID []byte) error {
value := idx.IDs.Get(targetID)
if value == nil {
return nil
}
err := idx.IndexBucket.Delete(value)
if err != nil {
return err
}
return idx.IDs.Remove(targetID)
}
|
go
|
func (idx *ListIndex) RemoveID(targetID []byte) error {
value := idx.IDs.Get(targetID)
if value == nil {
return nil
}
err := idx.IndexBucket.Delete(value)
if err != nil {
return err
}
return idx.IDs.Remove(targetID)
}
|
[
"func",
"(",
"idx",
"*",
"ListIndex",
")",
"RemoveID",
"(",
"targetID",
"[",
"]",
"byte",
")",
"error",
"{",
"value",
":=",
"idx",
".",
"IDs",
".",
"Get",
"(",
"targetID",
")",
"\n",
"if",
"value",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"err",
":=",
"idx",
".",
"IndexBucket",
".",
"Delete",
"(",
"value",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"idx",
".",
"IDs",
".",
"Remove",
"(",
"targetID",
")",
"\n",
"}"
] |
// RemoveID removes an ID from the list index
|
[
"RemoveID",
"removes",
"an",
"ID",
"from",
"the",
"list",
"index"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/list.go#L103-L115
|
train
|
asdine/storm
|
index/list.go
|
Get
|
func (idx *ListIndex) Get(value []byte) []byte {
c := idx.IndexBucket.Cursor()
prefix := generatePrefix(value)
for k, id := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, id = c.Next() {
return id
}
return nil
}
|
go
|
func (idx *ListIndex) Get(value []byte) []byte {
c := idx.IndexBucket.Cursor()
prefix := generatePrefix(value)
for k, id := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, id = c.Next() {
return id
}
return nil
}
|
[
"func",
"(",
"idx",
"*",
"ListIndex",
")",
"Get",
"(",
"value",
"[",
"]",
"byte",
")",
"[",
"]",
"byte",
"{",
"c",
":=",
"idx",
".",
"IndexBucket",
".",
"Cursor",
"(",
")",
"\n",
"prefix",
":=",
"generatePrefix",
"(",
"value",
")",
"\n\n",
"for",
"k",
",",
"id",
":=",
"c",
".",
"Seek",
"(",
"prefix",
")",
";",
"bytes",
".",
"HasPrefix",
"(",
"k",
",",
"prefix",
")",
";",
"k",
",",
"id",
"=",
"c",
".",
"Next",
"(",
")",
"{",
"return",
"id",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Get the first ID corresponding to the given value
|
[
"Get",
"the",
"first",
"ID",
"corresponding",
"to",
"the",
"given",
"value"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/list.go#L118-L127
|
train
|
asdine/storm
|
index/list.go
|
All
|
func (idx *ListIndex) All(value []byte, opts *Options) ([][]byte, error) {
var list [][]byte
c := idx.IndexBucket.Cursor()
cur := internal.Cursor{C: c, Reverse: opts != nil && opts.Reverse}
prefix := generatePrefix(value)
k, id := c.Seek(prefix)
if cur.Reverse {
var count int
kc := k
idc := id
for ; kc != nil && bytes.HasPrefix(kc, prefix); kc, idc = c.Next() {
count++
k, id = kc, idc
}
if kc != nil {
k, id = c.Prev()
}
list = make([][]byte, 0, count)
}
for ; bytes.HasPrefix(k, prefix); k, id = cur.Next() {
if opts != nil && opts.Skip > 0 {
opts.Skip--
continue
}
if opts != nil && opts.Limit == 0 {
break
}
if opts != nil && opts.Limit > 0 {
opts.Limit--
}
list = append(list, id)
}
return list, nil
}
|
go
|
func (idx *ListIndex) All(value []byte, opts *Options) ([][]byte, error) {
var list [][]byte
c := idx.IndexBucket.Cursor()
cur := internal.Cursor{C: c, Reverse: opts != nil && opts.Reverse}
prefix := generatePrefix(value)
k, id := c.Seek(prefix)
if cur.Reverse {
var count int
kc := k
idc := id
for ; kc != nil && bytes.HasPrefix(kc, prefix); kc, idc = c.Next() {
count++
k, id = kc, idc
}
if kc != nil {
k, id = c.Prev()
}
list = make([][]byte, 0, count)
}
for ; bytes.HasPrefix(k, prefix); k, id = cur.Next() {
if opts != nil && opts.Skip > 0 {
opts.Skip--
continue
}
if opts != nil && opts.Limit == 0 {
break
}
if opts != nil && opts.Limit > 0 {
opts.Limit--
}
list = append(list, id)
}
return list, nil
}
|
[
"func",
"(",
"idx",
"*",
"ListIndex",
")",
"All",
"(",
"value",
"[",
"]",
"byte",
",",
"opts",
"*",
"Options",
")",
"(",
"[",
"]",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"var",
"list",
"[",
"]",
"[",
"]",
"byte",
"\n",
"c",
":=",
"idx",
".",
"IndexBucket",
".",
"Cursor",
"(",
")",
"\n",
"cur",
":=",
"internal",
".",
"Cursor",
"{",
"C",
":",
"c",
",",
"Reverse",
":",
"opts",
"!=",
"nil",
"&&",
"opts",
".",
"Reverse",
"}",
"\n\n",
"prefix",
":=",
"generatePrefix",
"(",
"value",
")",
"\n\n",
"k",
",",
"id",
":=",
"c",
".",
"Seek",
"(",
"prefix",
")",
"\n",
"if",
"cur",
".",
"Reverse",
"{",
"var",
"count",
"int",
"\n",
"kc",
":=",
"k",
"\n",
"idc",
":=",
"id",
"\n",
"for",
";",
"kc",
"!=",
"nil",
"&&",
"bytes",
".",
"HasPrefix",
"(",
"kc",
",",
"prefix",
")",
";",
"kc",
",",
"idc",
"=",
"c",
".",
"Next",
"(",
")",
"{",
"count",
"++",
"\n",
"k",
",",
"id",
"=",
"kc",
",",
"idc",
"\n",
"}",
"\n",
"if",
"kc",
"!=",
"nil",
"{",
"k",
",",
"id",
"=",
"c",
".",
"Prev",
"(",
")",
"\n",
"}",
"\n",
"list",
"=",
"make",
"(",
"[",
"]",
"[",
"]",
"byte",
",",
"0",
",",
"count",
")",
"\n",
"}",
"\n\n",
"for",
";",
"bytes",
".",
"HasPrefix",
"(",
"k",
",",
"prefix",
")",
";",
"k",
",",
"id",
"=",
"cur",
".",
"Next",
"(",
")",
"{",
"if",
"opts",
"!=",
"nil",
"&&",
"opts",
".",
"Skip",
">",
"0",
"{",
"opts",
".",
"Skip",
"--",
"\n",
"continue",
"\n",
"}",
"\n\n",
"if",
"opts",
"!=",
"nil",
"&&",
"opts",
".",
"Limit",
"==",
"0",
"{",
"break",
"\n",
"}",
"\n\n",
"if",
"opts",
"!=",
"nil",
"&&",
"opts",
".",
"Limit",
">",
"0",
"{",
"opts",
".",
"Limit",
"--",
"\n",
"}",
"\n\n",
"list",
"=",
"append",
"(",
"list",
",",
"id",
")",
"\n",
"}",
"\n\n",
"return",
"list",
",",
"nil",
"\n",
"}"
] |
// All the IDs corresponding to the given value
|
[
"All",
"the",
"IDs",
"corresponding",
"to",
"the",
"given",
"value"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/list.go#L130-L170
|
train
|
asdine/storm
|
node.go
|
From
|
func (n node) From(addend ...string) Node {
n.rootBucket = append(n.rootBucket, addend...)
return &n
}
|
go
|
func (n node) From(addend ...string) Node {
n.rootBucket = append(n.rootBucket, addend...)
return &n
}
|
[
"func",
"(",
"n",
"node",
")",
"From",
"(",
"addend",
"...",
"string",
")",
"Node",
"{",
"n",
".",
"rootBucket",
"=",
"append",
"(",
"n",
".",
"rootBucket",
",",
"addend",
"...",
")",
"\n",
"return",
"&",
"n",
"\n",
"}"
] |
// From returns a new Storm Node with a new bucket root below the current.
// All DB operations on the new node will be executed relative to this bucket.
|
[
"From",
"returns",
"a",
"new",
"Storm",
"Node",
"with",
"a",
"new",
"bucket",
"root",
"below",
"the",
"current",
".",
"All",
"DB",
"operations",
"on",
"the",
"new",
"node",
"will",
"be",
"executed",
"relative",
"to",
"this",
"bucket",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/node.go#L65-L68
|
train
|
asdine/storm
|
node.go
|
WithTransaction
|
func (n node) WithTransaction(tx *bolt.Tx) Node {
n.tx = tx
return &n
}
|
go
|
func (n node) WithTransaction(tx *bolt.Tx) Node {
n.tx = tx
return &n
}
|
[
"func",
"(",
"n",
"node",
")",
"WithTransaction",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"Node",
"{",
"n",
".",
"tx",
"=",
"tx",
"\n",
"return",
"&",
"n",
"\n",
"}"
] |
// WithTransaction returns a new Storm Node that will use the given transaction.
|
[
"WithTransaction",
"returns",
"a",
"new",
"Storm",
"Node",
"that",
"will",
"use",
"the",
"given",
"transaction",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/node.go#L71-L74
|
train
|
asdine/storm
|
node.go
|
WithCodec
|
func (n node) WithCodec(codec codec.MarshalUnmarshaler) Node {
n.codec = codec
return &n
}
|
go
|
func (n node) WithCodec(codec codec.MarshalUnmarshaler) Node {
n.codec = codec
return &n
}
|
[
"func",
"(",
"n",
"node",
")",
"WithCodec",
"(",
"codec",
"codec",
".",
"MarshalUnmarshaler",
")",
"Node",
"{",
"n",
".",
"codec",
"=",
"codec",
"\n",
"return",
"&",
"n",
"\n",
"}"
] |
// WithCodec returns a new Storm Node that will use the given Codec.
|
[
"WithCodec",
"returns",
"a",
"new",
"Storm",
"Node",
"that",
"will",
"use",
"the",
"given",
"Codec",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/node.go#L77-L80
|
train
|
asdine/storm
|
node.go
|
WithBatch
|
func (n node) WithBatch(enabled bool) Node {
n.batchMode = enabled
return &n
}
|
go
|
func (n node) WithBatch(enabled bool) Node {
n.batchMode = enabled
return &n
}
|
[
"func",
"(",
"n",
"node",
")",
"WithBatch",
"(",
"enabled",
"bool",
")",
"Node",
"{",
"n",
".",
"batchMode",
"=",
"enabled",
"\n",
"return",
"&",
"n",
"\n",
"}"
] |
// WithBatch returns a new Storm Node with the batch mode enabled.
|
[
"WithBatch",
"returns",
"a",
"new",
"Storm",
"Node",
"with",
"the",
"batch",
"mode",
"enabled",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/node.go#L83-L86
|
train
|
asdine/storm
|
node.go
|
readWriteTx
|
func (n *node) readWriteTx(fn func(tx *bolt.Tx) error) error {
if n.tx != nil {
return fn(n.tx)
}
if n.batchMode {
return n.s.Bolt.Batch(func(tx *bolt.Tx) error {
return fn(tx)
})
}
return n.s.Bolt.Update(func(tx *bolt.Tx) error {
return fn(tx)
})
}
|
go
|
func (n *node) readWriteTx(fn func(tx *bolt.Tx) error) error {
if n.tx != nil {
return fn(n.tx)
}
if n.batchMode {
return n.s.Bolt.Batch(func(tx *bolt.Tx) error {
return fn(tx)
})
}
return n.s.Bolt.Update(func(tx *bolt.Tx) error {
return fn(tx)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"readWriteTx",
"(",
"fn",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
")",
"error",
"{",
"if",
"n",
".",
"tx",
"!=",
"nil",
"{",
"return",
"fn",
"(",
"n",
".",
"tx",
")",
"\n",
"}",
"\n\n",
"if",
"n",
".",
"batchMode",
"{",
"return",
"n",
".",
"s",
".",
"Bolt",
".",
"Batch",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"fn",
"(",
"tx",
")",
"\n",
"}",
")",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"s",
".",
"Bolt",
".",
"Update",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"fn",
"(",
"tx",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Detects if already in transaction or runs a read write transaction.
// Uses batch mode if enabled.
|
[
"Detects",
"if",
"already",
"in",
"transaction",
"or",
"runs",
"a",
"read",
"write",
"transaction",
".",
"Uses",
"batch",
"mode",
"if",
"enabled",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/node.go#L101-L115
|
train
|
asdine/storm
|
node.go
|
readTx
|
func (n *node) readTx(fn func(tx *bolt.Tx) error) error {
if n.tx != nil {
return fn(n.tx)
}
return n.s.Bolt.View(func(tx *bolt.Tx) error {
return fn(tx)
})
}
|
go
|
func (n *node) readTx(fn func(tx *bolt.Tx) error) error {
if n.tx != nil {
return fn(n.tx)
}
return n.s.Bolt.View(func(tx *bolt.Tx) error {
return fn(tx)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"readTx",
"(",
"fn",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
")",
"error",
"{",
"if",
"n",
".",
"tx",
"!=",
"nil",
"{",
"return",
"fn",
"(",
"n",
".",
"tx",
")",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"s",
".",
"Bolt",
".",
"View",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"fn",
"(",
"tx",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Detects if already in transaction or runs a read transaction.
|
[
"Detects",
"if",
"already",
"in",
"transaction",
"or",
"runs",
"a",
"read",
"transaction",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/node.go#L118-L126
|
train
|
asdine/storm
|
index/unique.go
|
NewUniqueIndex
|
func NewUniqueIndex(parent *bolt.Bucket, indexName []byte) (*UniqueIndex, error) {
var err error
b := parent.Bucket(indexName)
if b == nil {
if !parent.Writable() {
return nil, ErrNotFound
}
b, err = parent.CreateBucket(indexName)
if err != nil {
return nil, err
}
}
return &UniqueIndex{
IndexBucket: b,
Parent: parent,
}, nil
}
|
go
|
func NewUniqueIndex(parent *bolt.Bucket, indexName []byte) (*UniqueIndex, error) {
var err error
b := parent.Bucket(indexName)
if b == nil {
if !parent.Writable() {
return nil, ErrNotFound
}
b, err = parent.CreateBucket(indexName)
if err != nil {
return nil, err
}
}
return &UniqueIndex{
IndexBucket: b,
Parent: parent,
}, nil
}
|
[
"func",
"NewUniqueIndex",
"(",
"parent",
"*",
"bolt",
".",
"Bucket",
",",
"indexName",
"[",
"]",
"byte",
")",
"(",
"*",
"UniqueIndex",
",",
"error",
")",
"{",
"var",
"err",
"error",
"\n",
"b",
":=",
"parent",
".",
"Bucket",
"(",
"indexName",
")",
"\n",
"if",
"b",
"==",
"nil",
"{",
"if",
"!",
"parent",
".",
"Writable",
"(",
")",
"{",
"return",
"nil",
",",
"ErrNotFound",
"\n",
"}",
"\n",
"b",
",",
"err",
"=",
"parent",
".",
"CreateBucket",
"(",
"indexName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"&",
"UniqueIndex",
"{",
"IndexBucket",
":",
"b",
",",
"Parent",
":",
"parent",
",",
"}",
",",
"nil",
"\n",
"}"
] |
// NewUniqueIndex loads a UniqueIndex
|
[
"NewUniqueIndex",
"loads",
"a",
"UniqueIndex"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/unique.go#L11-L28
|
train
|
asdine/storm
|
index/unique.go
|
Add
|
func (idx *UniqueIndex) Add(value []byte, targetID []byte) error {
if value == nil || len(value) == 0 {
return ErrNilParam
}
if targetID == nil || len(targetID) == 0 {
return ErrNilParam
}
exists := idx.IndexBucket.Get(value)
if exists != nil {
if bytes.Equal(exists, targetID) {
return nil
}
return ErrAlreadyExists
}
return idx.IndexBucket.Put(value, targetID)
}
|
go
|
func (idx *UniqueIndex) Add(value []byte, targetID []byte) error {
if value == nil || len(value) == 0 {
return ErrNilParam
}
if targetID == nil || len(targetID) == 0 {
return ErrNilParam
}
exists := idx.IndexBucket.Get(value)
if exists != nil {
if bytes.Equal(exists, targetID) {
return nil
}
return ErrAlreadyExists
}
return idx.IndexBucket.Put(value, targetID)
}
|
[
"func",
"(",
"idx",
"*",
"UniqueIndex",
")",
"Add",
"(",
"value",
"[",
"]",
"byte",
",",
"targetID",
"[",
"]",
"byte",
")",
"error",
"{",
"if",
"value",
"==",
"nil",
"||",
"len",
"(",
"value",
")",
"==",
"0",
"{",
"return",
"ErrNilParam",
"\n",
"}",
"\n",
"if",
"targetID",
"==",
"nil",
"||",
"len",
"(",
"targetID",
")",
"==",
"0",
"{",
"return",
"ErrNilParam",
"\n",
"}",
"\n\n",
"exists",
":=",
"idx",
".",
"IndexBucket",
".",
"Get",
"(",
"value",
")",
"\n",
"if",
"exists",
"!=",
"nil",
"{",
"if",
"bytes",
".",
"Equal",
"(",
"exists",
",",
"targetID",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"ErrAlreadyExists",
"\n",
"}",
"\n\n",
"return",
"idx",
".",
"IndexBucket",
".",
"Put",
"(",
"value",
",",
"targetID",
")",
"\n",
"}"
] |
// Add a value to the unique index
|
[
"Add",
"a",
"value",
"to",
"the",
"unique",
"index"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/unique.go#L37-L54
|
train
|
asdine/storm
|
index/unique.go
|
RemoveID
|
func (idx *UniqueIndex) RemoveID(id []byte) error {
c := idx.IndexBucket.Cursor()
for val, ident := c.First(); val != nil; val, ident = c.Next() {
if bytes.Equal(ident, id) {
return idx.Remove(val)
}
}
return nil
}
|
go
|
func (idx *UniqueIndex) RemoveID(id []byte) error {
c := idx.IndexBucket.Cursor()
for val, ident := c.First(); val != nil; val, ident = c.Next() {
if bytes.Equal(ident, id) {
return idx.Remove(val)
}
}
return nil
}
|
[
"func",
"(",
"idx",
"*",
"UniqueIndex",
")",
"RemoveID",
"(",
"id",
"[",
"]",
"byte",
")",
"error",
"{",
"c",
":=",
"idx",
".",
"IndexBucket",
".",
"Cursor",
"(",
")",
"\n\n",
"for",
"val",
",",
"ident",
":=",
"c",
".",
"First",
"(",
")",
";",
"val",
"!=",
"nil",
";",
"val",
",",
"ident",
"=",
"c",
".",
"Next",
"(",
")",
"{",
"if",
"bytes",
".",
"Equal",
"(",
"ident",
",",
"id",
")",
"{",
"return",
"idx",
".",
"Remove",
"(",
"val",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// RemoveID removes an ID from the unique index
|
[
"RemoveID",
"removes",
"an",
"ID",
"from",
"the",
"unique",
"index"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/unique.go#L62-L71
|
train
|
asdine/storm
|
index/unique.go
|
Get
|
func (idx *UniqueIndex) Get(value []byte) []byte {
return idx.IndexBucket.Get(value)
}
|
go
|
func (idx *UniqueIndex) Get(value []byte) []byte {
return idx.IndexBucket.Get(value)
}
|
[
"func",
"(",
"idx",
"*",
"UniqueIndex",
")",
"Get",
"(",
"value",
"[",
"]",
"byte",
")",
"[",
"]",
"byte",
"{",
"return",
"idx",
".",
"IndexBucket",
".",
"Get",
"(",
"value",
")",
"\n",
"}"
] |
// Get the id corresponding to the given value
|
[
"Get",
"the",
"id",
"corresponding",
"to",
"the",
"given",
"value"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/unique.go#L74-L76
|
train
|
asdine/storm
|
index/unique.go
|
All
|
func (idx *UniqueIndex) All(value []byte, opts *Options) ([][]byte, error) {
id := idx.IndexBucket.Get(value)
if id != nil {
return [][]byte{id}, nil
}
return nil, nil
}
|
go
|
func (idx *UniqueIndex) All(value []byte, opts *Options) ([][]byte, error) {
id := idx.IndexBucket.Get(value)
if id != nil {
return [][]byte{id}, nil
}
return nil, nil
}
|
[
"func",
"(",
"idx",
"*",
"UniqueIndex",
")",
"All",
"(",
"value",
"[",
"]",
"byte",
",",
"opts",
"*",
"Options",
")",
"(",
"[",
"]",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"id",
":=",
"idx",
".",
"IndexBucket",
".",
"Get",
"(",
"value",
")",
"\n",
"if",
"id",
"!=",
"nil",
"{",
"return",
"[",
"]",
"[",
"]",
"byte",
"{",
"id",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"nil",
",",
"nil",
"\n",
"}"
] |
// All returns all the ids corresponding to the given value
|
[
"All",
"returns",
"all",
"the",
"ids",
"corresponding",
"to",
"the",
"given",
"value"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/unique.go#L79-L86
|
train
|
asdine/storm
|
index/unique.go
|
first
|
func (idx *UniqueIndex) first() []byte {
c := idx.IndexBucket.Cursor()
for val, ident := c.First(); val != nil; val, ident = c.Next() {
return ident
}
return nil
}
|
go
|
func (idx *UniqueIndex) first() []byte {
c := idx.IndexBucket.Cursor()
for val, ident := c.First(); val != nil; val, ident = c.Next() {
return ident
}
return nil
}
|
[
"func",
"(",
"idx",
"*",
"UniqueIndex",
")",
"first",
"(",
")",
"[",
"]",
"byte",
"{",
"c",
":=",
"idx",
".",
"IndexBucket",
".",
"Cursor",
"(",
")",
"\n\n",
"for",
"val",
",",
"ident",
":=",
"c",
".",
"First",
"(",
")",
";",
"val",
"!=",
"nil",
";",
"val",
",",
"ident",
"=",
"c",
".",
"Next",
"(",
")",
"{",
"return",
"ident",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// first returns the first ID of this index
|
[
"first",
"returns",
"the",
"first",
"ID",
"of",
"this",
"index"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/index/unique.go#L176-L183
|
train
|
asdine/storm
|
codec/protobuf/protobuf.go
|
Marshal
|
func (c protobufCodec) Marshal(v interface{}) ([]byte, error) {
message, ok := v.(proto.Message)
if !ok {
// toBytes() may need to encode non-protobuf type, if that occurs use json
return json.Codec.Marshal(v)
}
return proto.Marshal(message)
}
|
go
|
func (c protobufCodec) Marshal(v interface{}) ([]byte, error) {
message, ok := v.(proto.Message)
if !ok {
// toBytes() may need to encode non-protobuf type, if that occurs use json
return json.Codec.Marshal(v)
}
return proto.Marshal(message)
}
|
[
"func",
"(",
"c",
"protobufCodec",
")",
"Marshal",
"(",
"v",
"interface",
"{",
"}",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"message",
",",
"ok",
":=",
"v",
".",
"(",
"proto",
".",
"Message",
")",
"\n",
"if",
"!",
"ok",
"{",
"// toBytes() may need to encode non-protobuf type, if that occurs use json",
"return",
"json",
".",
"Codec",
".",
"Marshal",
"(",
"v",
")",
"\n",
"}",
"\n",
"return",
"proto",
".",
"Marshal",
"(",
"message",
")",
"\n",
"}"
] |
// Encode value with protocol buffer.
// If type isn't a Protocol buffer Message, json encoder will be used instead.
|
[
"Encode",
"value",
"with",
"protocol",
"buffer",
".",
"If",
"type",
"isn",
"t",
"a",
"Protocol",
"buffer",
"Message",
"json",
"encoder",
"will",
"be",
"used",
"instead",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/codec/protobuf/protobuf.go#L23-L30
|
train
|
asdine/storm
|
storm.go
|
Open
|
func Open(path string, stormOptions ...func(*Options) error) (*DB, error) {
var err error
var opts Options
for _, option := range stormOptions {
if err = option(&opts); err != nil {
return nil, err
}
}
s := DB{
Bolt: opts.bolt,
}
n := node{
s: &s,
codec: opts.codec,
batchMode: opts.batchMode,
rootBucket: opts.rootBucket,
}
if n.codec == nil {
n.codec = defaultCodec
}
if opts.boltMode == 0 {
opts.boltMode = 0600
}
if opts.boltOptions == nil {
opts.boltOptions = &bolt.Options{Timeout: 1 * time.Second}
}
s.Node = &n
// skip if UseDB option is used
if s.Bolt == nil {
s.Bolt, err = bolt.Open(path, opts.boltMode, opts.boltOptions)
if err != nil {
return nil, err
}
}
err = s.checkVersion()
if err != nil {
return nil, err
}
return &s, nil
}
|
go
|
func Open(path string, stormOptions ...func(*Options) error) (*DB, error) {
var err error
var opts Options
for _, option := range stormOptions {
if err = option(&opts); err != nil {
return nil, err
}
}
s := DB{
Bolt: opts.bolt,
}
n := node{
s: &s,
codec: opts.codec,
batchMode: opts.batchMode,
rootBucket: opts.rootBucket,
}
if n.codec == nil {
n.codec = defaultCodec
}
if opts.boltMode == 0 {
opts.boltMode = 0600
}
if opts.boltOptions == nil {
opts.boltOptions = &bolt.Options{Timeout: 1 * time.Second}
}
s.Node = &n
// skip if UseDB option is used
if s.Bolt == nil {
s.Bolt, err = bolt.Open(path, opts.boltMode, opts.boltOptions)
if err != nil {
return nil, err
}
}
err = s.checkVersion()
if err != nil {
return nil, err
}
return &s, nil
}
|
[
"func",
"Open",
"(",
"path",
"string",
",",
"stormOptions",
"...",
"func",
"(",
"*",
"Options",
")",
"error",
")",
"(",
"*",
"DB",
",",
"error",
")",
"{",
"var",
"err",
"error",
"\n\n",
"var",
"opts",
"Options",
"\n",
"for",
"_",
",",
"option",
":=",
"range",
"stormOptions",
"{",
"if",
"err",
"=",
"option",
"(",
"&",
"opts",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"s",
":=",
"DB",
"{",
"Bolt",
":",
"opts",
".",
"bolt",
",",
"}",
"\n\n",
"n",
":=",
"node",
"{",
"s",
":",
"&",
"s",
",",
"codec",
":",
"opts",
".",
"codec",
",",
"batchMode",
":",
"opts",
".",
"batchMode",
",",
"rootBucket",
":",
"opts",
".",
"rootBucket",
",",
"}",
"\n\n",
"if",
"n",
".",
"codec",
"==",
"nil",
"{",
"n",
".",
"codec",
"=",
"defaultCodec",
"\n",
"}",
"\n\n",
"if",
"opts",
".",
"boltMode",
"==",
"0",
"{",
"opts",
".",
"boltMode",
"=",
"0600",
"\n",
"}",
"\n\n",
"if",
"opts",
".",
"boltOptions",
"==",
"nil",
"{",
"opts",
".",
"boltOptions",
"=",
"&",
"bolt",
".",
"Options",
"{",
"Timeout",
":",
"1",
"*",
"time",
".",
"Second",
"}",
"\n",
"}",
"\n\n",
"s",
".",
"Node",
"=",
"&",
"n",
"\n\n",
"// skip if UseDB option is used",
"if",
"s",
".",
"Bolt",
"==",
"nil",
"{",
"s",
".",
"Bolt",
",",
"err",
"=",
"bolt",
".",
"Open",
"(",
"path",
",",
"opts",
".",
"boltMode",
",",
"opts",
".",
"boltOptions",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"err",
"=",
"s",
".",
"checkVersion",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"s",
",",
"nil",
"\n",
"}"
] |
// Open opens a database at the given path with optional Storm options.
|
[
"Open",
"opens",
"a",
"database",
"at",
"the",
"given",
"path",
"with",
"optional",
"Storm",
"options",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/storm.go#L22-L71
|
train
|
asdine/storm
|
storm.go
|
toBytes
|
func toBytes(key interface{}, codec codec.MarshalUnmarshaler) ([]byte, error) {
if key == nil {
return nil, nil
}
switch t := key.(type) {
case []byte:
return t, nil
case string:
return []byte(t), nil
case int:
return numbertob(int64(t))
case uint:
return numbertob(uint64(t))
case int8, int16, int32, int64, uint8, uint16, uint32, uint64:
return numbertob(t)
default:
return codec.Marshal(key)
}
}
|
go
|
func toBytes(key interface{}, codec codec.MarshalUnmarshaler) ([]byte, error) {
if key == nil {
return nil, nil
}
switch t := key.(type) {
case []byte:
return t, nil
case string:
return []byte(t), nil
case int:
return numbertob(int64(t))
case uint:
return numbertob(uint64(t))
case int8, int16, int32, int64, uint8, uint16, uint32, uint64:
return numbertob(t)
default:
return codec.Marshal(key)
}
}
|
[
"func",
"toBytes",
"(",
"key",
"interface",
"{",
"}",
",",
"codec",
"codec",
".",
"MarshalUnmarshaler",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"if",
"key",
"==",
"nil",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n",
"switch",
"t",
":=",
"key",
".",
"(",
"type",
")",
"{",
"case",
"[",
"]",
"byte",
":",
"return",
"t",
",",
"nil",
"\n",
"case",
"string",
":",
"return",
"[",
"]",
"byte",
"(",
"t",
")",
",",
"nil",
"\n",
"case",
"int",
":",
"return",
"numbertob",
"(",
"int64",
"(",
"t",
")",
")",
"\n",
"case",
"uint",
":",
"return",
"numbertob",
"(",
"uint64",
"(",
"t",
")",
")",
"\n",
"case",
"int8",
",",
"int16",
",",
"int32",
",",
"int64",
",",
"uint8",
",",
"uint16",
",",
"uint32",
",",
"uint64",
":",
"return",
"numbertob",
"(",
"t",
")",
"\n",
"default",
":",
"return",
"codec",
".",
"Marshal",
"(",
"key",
")",
"\n",
"}",
"\n",
"}"
] |
// toBytes turns an interface into a slice of bytes
|
[
"toBytes",
"turns",
"an",
"interface",
"into",
"a",
"slice",
"of",
"bytes"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/storm.go#L105-L123
|
train
|
asdine/storm
|
options.go
|
BoltOptions
|
func BoltOptions(mode os.FileMode, options *bolt.Options) func(*Options) error {
return func(opts *Options) error {
opts.boltMode = mode
opts.boltOptions = options
return nil
}
}
|
go
|
func BoltOptions(mode os.FileMode, options *bolt.Options) func(*Options) error {
return func(opts *Options) error {
opts.boltMode = mode
opts.boltOptions = options
return nil
}
}
|
[
"func",
"BoltOptions",
"(",
"mode",
"os",
".",
"FileMode",
",",
"options",
"*",
"bolt",
".",
"Options",
")",
"func",
"(",
"*",
"Options",
")",
"error",
"{",
"return",
"func",
"(",
"opts",
"*",
"Options",
")",
"error",
"{",
"opts",
".",
"boltMode",
"=",
"mode",
"\n",
"opts",
".",
"boltOptions",
"=",
"options",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// BoltOptions used to pass options to BoltDB.
|
[
"BoltOptions",
"used",
"to",
"pass",
"options",
"to",
"BoltDB",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/options.go#L12-L18
|
train
|
asdine/storm
|
options.go
|
Codec
|
func Codec(c codec.MarshalUnmarshaler) func(*Options) error {
return func(opts *Options) error {
opts.codec = c
return nil
}
}
|
go
|
func Codec(c codec.MarshalUnmarshaler) func(*Options) error {
return func(opts *Options) error {
opts.codec = c
return nil
}
}
|
[
"func",
"Codec",
"(",
"c",
"codec",
".",
"MarshalUnmarshaler",
")",
"func",
"(",
"*",
"Options",
")",
"error",
"{",
"return",
"func",
"(",
"opts",
"*",
"Options",
")",
"error",
"{",
"opts",
".",
"codec",
"=",
"c",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// Codec used to set a custom encoder and decoder. The default is JSON.
|
[
"Codec",
"used",
"to",
"set",
"a",
"custom",
"encoder",
"and",
"decoder",
".",
"The",
"default",
"is",
"JSON",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/options.go#L21-L26
|
train
|
asdine/storm
|
options.go
|
Batch
|
func Batch() func(*Options) error {
return func(opts *Options) error {
opts.batchMode = true
return nil
}
}
|
go
|
func Batch() func(*Options) error {
return func(opts *Options) error {
opts.batchMode = true
return nil
}
}
|
[
"func",
"Batch",
"(",
")",
"func",
"(",
"*",
"Options",
")",
"error",
"{",
"return",
"func",
"(",
"opts",
"*",
"Options",
")",
"error",
"{",
"opts",
".",
"batchMode",
"=",
"true",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// Batch enables the use of batch instead of update for read-write transactions.
|
[
"Batch",
"enables",
"the",
"use",
"of",
"batch",
"instead",
"of",
"update",
"for",
"read",
"-",
"write",
"transactions",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/options.go#L29-L34
|
train
|
asdine/storm
|
options.go
|
Root
|
func Root(root ...string) func(*Options) error {
return func(opts *Options) error {
opts.rootBucket = root
return nil
}
}
|
go
|
func Root(root ...string) func(*Options) error {
return func(opts *Options) error {
opts.rootBucket = root
return nil
}
}
|
[
"func",
"Root",
"(",
"root",
"...",
"string",
")",
"func",
"(",
"*",
"Options",
")",
"error",
"{",
"return",
"func",
"(",
"opts",
"*",
"Options",
")",
"error",
"{",
"opts",
".",
"rootBucket",
"=",
"root",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// Root used to set the root bucket. See also the From method.
|
[
"Root",
"used",
"to",
"set",
"the",
"root",
"bucket",
".",
"See",
"also",
"the",
"From",
"method",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/options.go#L37-L42
|
train
|
asdine/storm
|
options.go
|
Limit
|
func Limit(limit int) func(*index.Options) {
return func(opts *index.Options) {
opts.Limit = limit
}
}
|
go
|
func Limit(limit int) func(*index.Options) {
return func(opts *index.Options) {
opts.Limit = limit
}
}
|
[
"func",
"Limit",
"(",
"limit",
"int",
")",
"func",
"(",
"*",
"index",
".",
"Options",
")",
"{",
"return",
"func",
"(",
"opts",
"*",
"index",
".",
"Options",
")",
"{",
"opts",
".",
"Limit",
"=",
"limit",
"\n",
"}",
"\n",
"}"
] |
// Limit sets the maximum number of records to return
|
[
"Limit",
"sets",
"the",
"maximum",
"number",
"of",
"records",
"to",
"return"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/options.go#L55-L59
|
train
|
asdine/storm
|
options.go
|
Skip
|
func Skip(offset int) func(*index.Options) {
return func(opts *index.Options) {
opts.Skip = offset
}
}
|
go
|
func Skip(offset int) func(*index.Options) {
return func(opts *index.Options) {
opts.Skip = offset
}
}
|
[
"func",
"Skip",
"(",
"offset",
"int",
")",
"func",
"(",
"*",
"index",
".",
"Options",
")",
"{",
"return",
"func",
"(",
"opts",
"*",
"index",
".",
"Options",
")",
"{",
"opts",
".",
"Skip",
"=",
"offset",
"\n",
"}",
"\n",
"}"
] |
// Skip sets the number of records to skip
|
[
"Skip",
"sets",
"the",
"number",
"of",
"records",
"to",
"skip"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/options.go#L62-L66
|
train
|
asdine/storm
|
store.go
|
Init
|
func (n *node) Init(data interface{}) error {
v := reflect.ValueOf(data)
cfg, err := extract(&v)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.init(tx, cfg)
})
}
|
go
|
func (n *node) Init(data interface{}) error {
v := reflect.ValueOf(data)
cfg, err := extract(&v)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.init(tx, cfg)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Init",
"(",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"v",
":=",
"reflect",
".",
"ValueOf",
"(",
"data",
")",
"\n",
"cfg",
",",
"err",
":=",
"extract",
"(",
"&",
"v",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readWriteTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"init",
"(",
"tx",
",",
"cfg",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Init creates the indexes and buckets for a given structure
|
[
"Init",
"creates",
"the",
"indexes",
"and",
"buckets",
"for",
"a",
"given",
"structure"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/store.go#L38-L48
|
train
|
asdine/storm
|
store.go
|
Save
|
func (n *node) Save(data interface{}) error {
ref := reflect.ValueOf(data)
if !ref.IsValid() || ref.Kind() != reflect.Ptr || ref.Elem().Kind() != reflect.Struct {
return ErrStructPtrNeeded
}
cfg, err := extract(&ref)
if err != nil {
return err
}
if cfg.ID.IsZero {
if !cfg.ID.IsInteger || !cfg.ID.Increment {
return ErrZeroID
}
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.save(tx, cfg, data, false)
})
}
|
go
|
func (n *node) Save(data interface{}) error {
ref := reflect.ValueOf(data)
if !ref.IsValid() || ref.Kind() != reflect.Ptr || ref.Elem().Kind() != reflect.Struct {
return ErrStructPtrNeeded
}
cfg, err := extract(&ref)
if err != nil {
return err
}
if cfg.ID.IsZero {
if !cfg.ID.IsInteger || !cfg.ID.Increment {
return ErrZeroID
}
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.save(tx, cfg, data, false)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Save",
"(",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"ref",
":=",
"reflect",
".",
"ValueOf",
"(",
"data",
")",
"\n\n",
"if",
"!",
"ref",
".",
"IsValid",
"(",
")",
"||",
"ref",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"||",
"ref",
".",
"Elem",
"(",
")",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Struct",
"{",
"return",
"ErrStructPtrNeeded",
"\n",
"}",
"\n\n",
"cfg",
",",
"err",
":=",
"extract",
"(",
"&",
"ref",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"cfg",
".",
"ID",
".",
"IsZero",
"{",
"if",
"!",
"cfg",
".",
"ID",
".",
"IsInteger",
"||",
"!",
"cfg",
".",
"ID",
".",
"Increment",
"{",
"return",
"ErrZeroID",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readWriteTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"save",
"(",
"tx",
",",
"cfg",
",",
"data",
",",
"false",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Save a structure
|
[
"Save",
"a",
"structure"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/store.go#L138-L159
|
train
|
asdine/storm
|
store.go
|
Update
|
func (n *node) Update(data interface{}) error {
return n.update(data, func(ref *reflect.Value, current *reflect.Value, cfg *structConfig) error {
numfield := ref.NumField()
for i := 0; i < numfield; i++ {
f := ref.Field(i)
if ref.Type().Field(i).PkgPath != "" {
continue
}
zero := reflect.Zero(f.Type()).Interface()
actual := f.Interface()
if !reflect.DeepEqual(actual, zero) {
cf := current.Field(i)
cf.Set(f)
idxInfo, ok := cfg.Fields[ref.Type().Field(i).Name]
if ok {
idxInfo.Value = &cf
}
}
}
return nil
})
}
|
go
|
func (n *node) Update(data interface{}) error {
return n.update(data, func(ref *reflect.Value, current *reflect.Value, cfg *structConfig) error {
numfield := ref.NumField()
for i := 0; i < numfield; i++ {
f := ref.Field(i)
if ref.Type().Field(i).PkgPath != "" {
continue
}
zero := reflect.Zero(f.Type()).Interface()
actual := f.Interface()
if !reflect.DeepEqual(actual, zero) {
cf := current.Field(i)
cf.Set(f)
idxInfo, ok := cfg.Fields[ref.Type().Field(i).Name]
if ok {
idxInfo.Value = &cf
}
}
}
return nil
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Update",
"(",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"n",
".",
"update",
"(",
"data",
",",
"func",
"(",
"ref",
"*",
"reflect",
".",
"Value",
",",
"current",
"*",
"reflect",
".",
"Value",
",",
"cfg",
"*",
"structConfig",
")",
"error",
"{",
"numfield",
":=",
"ref",
".",
"NumField",
"(",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"numfield",
";",
"i",
"++",
"{",
"f",
":=",
"ref",
".",
"Field",
"(",
"i",
")",
"\n",
"if",
"ref",
".",
"Type",
"(",
")",
".",
"Field",
"(",
"i",
")",
".",
"PkgPath",
"!=",
"\"",
"\"",
"{",
"continue",
"\n",
"}",
"\n",
"zero",
":=",
"reflect",
".",
"Zero",
"(",
"f",
".",
"Type",
"(",
")",
")",
".",
"Interface",
"(",
")",
"\n",
"actual",
":=",
"f",
".",
"Interface",
"(",
")",
"\n",
"if",
"!",
"reflect",
".",
"DeepEqual",
"(",
"actual",
",",
"zero",
")",
"{",
"cf",
":=",
"current",
".",
"Field",
"(",
"i",
")",
"\n",
"cf",
".",
"Set",
"(",
"f",
")",
"\n",
"idxInfo",
",",
"ok",
":=",
"cfg",
".",
"Fields",
"[",
"ref",
".",
"Type",
"(",
")",
".",
"Field",
"(",
"i",
")",
".",
"Name",
"]",
"\n",
"if",
"ok",
"{",
"idxInfo",
".",
"Value",
"=",
"&",
"cf",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// Update a structure
|
[
"Update",
"a",
"structure"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/store.go#L258-L279
|
train
|
asdine/storm
|
store.go
|
UpdateField
|
func (n *node) UpdateField(data interface{}, fieldName string, value interface{}) error {
return n.update(data, func(ref *reflect.Value, current *reflect.Value, cfg *structConfig) error {
f := current.FieldByName(fieldName)
if !f.IsValid() {
return ErrNotFound
}
tf, _ := current.Type().FieldByName(fieldName)
if tf.PkgPath != "" {
return ErrNotFound
}
v := reflect.ValueOf(value)
if v.Kind() != f.Kind() {
return ErrIncompatibleValue
}
f.Set(v)
idxInfo, ok := cfg.Fields[fieldName]
if ok {
idxInfo.Value = &f
idxInfo.IsZero = isZero(idxInfo.Value)
idxInfo.ForceUpdate = true
}
return nil
})
}
|
go
|
func (n *node) UpdateField(data interface{}, fieldName string, value interface{}) error {
return n.update(data, func(ref *reflect.Value, current *reflect.Value, cfg *structConfig) error {
f := current.FieldByName(fieldName)
if !f.IsValid() {
return ErrNotFound
}
tf, _ := current.Type().FieldByName(fieldName)
if tf.PkgPath != "" {
return ErrNotFound
}
v := reflect.ValueOf(value)
if v.Kind() != f.Kind() {
return ErrIncompatibleValue
}
f.Set(v)
idxInfo, ok := cfg.Fields[fieldName]
if ok {
idxInfo.Value = &f
idxInfo.IsZero = isZero(idxInfo.Value)
idxInfo.ForceUpdate = true
}
return nil
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"UpdateField",
"(",
"data",
"interface",
"{",
"}",
",",
"fieldName",
"string",
",",
"value",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"n",
".",
"update",
"(",
"data",
",",
"func",
"(",
"ref",
"*",
"reflect",
".",
"Value",
",",
"current",
"*",
"reflect",
".",
"Value",
",",
"cfg",
"*",
"structConfig",
")",
"error",
"{",
"f",
":=",
"current",
".",
"FieldByName",
"(",
"fieldName",
")",
"\n",
"if",
"!",
"f",
".",
"IsValid",
"(",
")",
"{",
"return",
"ErrNotFound",
"\n",
"}",
"\n",
"tf",
",",
"_",
":=",
"current",
".",
"Type",
"(",
")",
".",
"FieldByName",
"(",
"fieldName",
")",
"\n",
"if",
"tf",
".",
"PkgPath",
"!=",
"\"",
"\"",
"{",
"return",
"ErrNotFound",
"\n",
"}",
"\n",
"v",
":=",
"reflect",
".",
"ValueOf",
"(",
"value",
")",
"\n",
"if",
"v",
".",
"Kind",
"(",
")",
"!=",
"f",
".",
"Kind",
"(",
")",
"{",
"return",
"ErrIncompatibleValue",
"\n",
"}",
"\n",
"f",
".",
"Set",
"(",
"v",
")",
"\n",
"idxInfo",
",",
"ok",
":=",
"cfg",
".",
"Fields",
"[",
"fieldName",
"]",
"\n",
"if",
"ok",
"{",
"idxInfo",
".",
"Value",
"=",
"&",
"f",
"\n",
"idxInfo",
".",
"IsZero",
"=",
"isZero",
"(",
"idxInfo",
".",
"Value",
")",
"\n",
"idxInfo",
".",
"ForceUpdate",
"=",
"true",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// UpdateField updates a single field
|
[
"UpdateField",
"updates",
"a",
"single",
"field"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/store.go#L282-L305
|
train
|
asdine/storm
|
store.go
|
Drop
|
func (n *node) Drop(data interface{}) error {
var bucketName string
v := reflect.ValueOf(data)
if v.Kind() != reflect.String {
info, err := extract(&v)
if err != nil {
return err
}
bucketName = info.Name
} else {
bucketName = v.Interface().(string)
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.drop(tx, bucketName)
})
}
|
go
|
func (n *node) Drop(data interface{}) error {
var bucketName string
v := reflect.ValueOf(data)
if v.Kind() != reflect.String {
info, err := extract(&v)
if err != nil {
return err
}
bucketName = info.Name
} else {
bucketName = v.Interface().(string)
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.drop(tx, bucketName)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Drop",
"(",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"var",
"bucketName",
"string",
"\n\n",
"v",
":=",
"reflect",
".",
"ValueOf",
"(",
"data",
")",
"\n",
"if",
"v",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"String",
"{",
"info",
",",
"err",
":=",
"extract",
"(",
"&",
"v",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"bucketName",
"=",
"info",
".",
"Name",
"\n",
"}",
"else",
"{",
"bucketName",
"=",
"v",
".",
"Interface",
"(",
")",
".",
"(",
"string",
")",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readWriteTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"drop",
"(",
"tx",
",",
"bucketName",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Drop a bucket
|
[
"Drop",
"a",
"bucket"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/store.go#L342-L360
|
train
|
asdine/storm
|
store.go
|
DeleteStruct
|
func (n *node) DeleteStruct(data interface{}) error {
ref := reflect.ValueOf(data)
if !ref.IsValid() || ref.Kind() != reflect.Ptr || ref.Elem().Kind() != reflect.Struct {
return ErrStructPtrNeeded
}
cfg, err := extract(&ref)
if err != nil {
return err
}
id, err := toBytes(cfg.ID.Value.Interface(), n.codec)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.deleteStruct(tx, cfg, id)
})
}
|
go
|
func (n *node) DeleteStruct(data interface{}) error {
ref := reflect.ValueOf(data)
if !ref.IsValid() || ref.Kind() != reflect.Ptr || ref.Elem().Kind() != reflect.Struct {
return ErrStructPtrNeeded
}
cfg, err := extract(&ref)
if err != nil {
return err
}
id, err := toBytes(cfg.ID.Value.Interface(), n.codec)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.deleteStruct(tx, cfg, id)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"DeleteStruct",
"(",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"ref",
":=",
"reflect",
".",
"ValueOf",
"(",
"data",
")",
"\n\n",
"if",
"!",
"ref",
".",
"IsValid",
"(",
")",
"||",
"ref",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"||",
"ref",
".",
"Elem",
"(",
")",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Struct",
"{",
"return",
"ErrStructPtrNeeded",
"\n",
"}",
"\n\n",
"cfg",
",",
"err",
":=",
"extract",
"(",
"&",
"ref",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"id",
",",
"err",
":=",
"toBytes",
"(",
"cfg",
".",
"ID",
".",
"Value",
".",
"Interface",
"(",
")",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readWriteTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"deleteStruct",
"(",
"tx",
",",
"cfg",
",",
"id",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// DeleteStruct deletes a structure from the associated bucket
|
[
"DeleteStruct",
"deletes",
"a",
"structure",
"from",
"the",
"associated",
"bucket"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/store.go#L372-L392
|
train
|
asdine/storm
|
kv.go
|
SetBytes
|
func (n *node) SetBytes(bucketName string, key interface{}, value []byte) error {
if key == nil {
return ErrNilParam
}
id, err := toBytes(key, n.codec)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.setBytes(tx, bucketName, id, value)
})
}
|
go
|
func (n *node) SetBytes(bucketName string, key interface{}, value []byte) error {
if key == nil {
return ErrNilParam
}
id, err := toBytes(key, n.codec)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.setBytes(tx, bucketName, id, value)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"SetBytes",
"(",
"bucketName",
"string",
",",
"key",
"interface",
"{",
"}",
",",
"value",
"[",
"]",
"byte",
")",
"error",
"{",
"if",
"key",
"==",
"nil",
"{",
"return",
"ErrNilParam",
"\n",
"}",
"\n\n",
"id",
",",
"err",
":=",
"toBytes",
"(",
"key",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readWriteTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"setBytes",
"(",
"tx",
",",
"bucketName",
",",
"id",
",",
"value",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// SetBytes sets a raw value into a bucket.
|
[
"SetBytes",
"sets",
"a",
"raw",
"value",
"into",
"a",
"bucket",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/kv.go#L61-L74
|
train
|
asdine/storm
|
kv.go
|
Get
|
func (n *node) Get(bucketName string, key interface{}, to interface{}) error {
ref := reflect.ValueOf(to)
if !ref.IsValid() || ref.Kind() != reflect.Ptr {
return ErrPtrNeeded
}
id, err := toBytes(key, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
raw, err := n.getBytes(tx, bucketName, id)
if err != nil {
return err
}
return n.codec.Unmarshal(raw, to)
})
}
|
go
|
func (n *node) Get(bucketName string, key interface{}, to interface{}) error {
ref := reflect.ValueOf(to)
if !ref.IsValid() || ref.Kind() != reflect.Ptr {
return ErrPtrNeeded
}
id, err := toBytes(key, n.codec)
if err != nil {
return err
}
return n.readTx(func(tx *bolt.Tx) error {
raw, err := n.getBytes(tx, bucketName, id)
if err != nil {
return err
}
return n.codec.Unmarshal(raw, to)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Get",
"(",
"bucketName",
"string",
",",
"key",
"interface",
"{",
"}",
",",
"to",
"interface",
"{",
"}",
")",
"error",
"{",
"ref",
":=",
"reflect",
".",
"ValueOf",
"(",
"to",
")",
"\n\n",
"if",
"!",
"ref",
".",
"IsValid",
"(",
")",
"||",
"ref",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"{",
"return",
"ErrPtrNeeded",
"\n",
"}",
"\n\n",
"id",
",",
"err",
":=",
"toBytes",
"(",
"key",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"raw",
",",
"err",
":=",
"n",
".",
"getBytes",
"(",
"tx",
",",
"bucketName",
",",
"id",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"codec",
".",
"Unmarshal",
"(",
"raw",
",",
"to",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Get a value from a bucket
|
[
"Get",
"a",
"value",
"from",
"a",
"bucket"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/kv.go#L92-L112
|
train
|
asdine/storm
|
kv.go
|
Delete
|
func (n *node) Delete(bucketName string, key interface{}) error {
id, err := toBytes(key, n.codec)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.delete(tx, bucketName, id)
})
}
|
go
|
func (n *node) Delete(bucketName string, key interface{}) error {
id, err := toBytes(key, n.codec)
if err != nil {
return err
}
return n.readWriteTx(func(tx *bolt.Tx) error {
return n.delete(tx, bucketName, id)
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"Delete",
"(",
"bucketName",
"string",
",",
"key",
"interface",
"{",
"}",
")",
"error",
"{",
"id",
",",
"err",
":=",
"toBytes",
"(",
"key",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"readWriteTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"return",
"n",
".",
"delete",
"(",
"tx",
",",
"bucketName",
",",
"id",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// Delete deletes a key from a bucket
|
[
"Delete",
"deletes",
"a",
"key",
"from",
"a",
"bucket"
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/kv.go#L129-L138
|
train
|
asdine/storm
|
kv.go
|
KeyExists
|
func (n *node) KeyExists(bucketName string, key interface{}) (bool, error) {
id, err := toBytes(key, n.codec)
if err != nil {
return false, err
}
var exists bool
return exists, n.readTx(func(tx *bolt.Tx) error {
bucket := n.GetBucket(tx, bucketName)
if bucket == nil {
return ErrNotFound
}
v := bucket.Get(id)
if v != nil {
exists = true
}
return nil
})
}
|
go
|
func (n *node) KeyExists(bucketName string, key interface{}) (bool, error) {
id, err := toBytes(key, n.codec)
if err != nil {
return false, err
}
var exists bool
return exists, n.readTx(func(tx *bolt.Tx) error {
bucket := n.GetBucket(tx, bucketName)
if bucket == nil {
return ErrNotFound
}
v := bucket.Get(id)
if v != nil {
exists = true
}
return nil
})
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"KeyExists",
"(",
"bucketName",
"string",
",",
"key",
"interface",
"{",
"}",
")",
"(",
"bool",
",",
"error",
")",
"{",
"id",
",",
"err",
":=",
"toBytes",
"(",
"key",
",",
"n",
".",
"codec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"false",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"exists",
"bool",
"\n",
"return",
"exists",
",",
"n",
".",
"readTx",
"(",
"func",
"(",
"tx",
"*",
"bolt",
".",
"Tx",
")",
"error",
"{",
"bucket",
":=",
"n",
".",
"GetBucket",
"(",
"tx",
",",
"bucketName",
")",
"\n",
"if",
"bucket",
"==",
"nil",
"{",
"return",
"ErrNotFound",
"\n",
"}",
"\n\n",
"v",
":=",
"bucket",
".",
"Get",
"(",
"id",
")",
"\n",
"if",
"v",
"!=",
"nil",
"{",
"exists",
"=",
"true",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// KeyExists reports the presence of a key in a bucket.
|
[
"KeyExists",
"reports",
"the",
"presence",
"of",
"a",
"key",
"in",
"a",
"bucket",
"."
] |
e0f77eada154c7c2670527a8566d3c045880224f
|
https://github.com/asdine/storm/blob/e0f77eada154c7c2670527a8566d3c045880224f/kv.go#L150-L170
|
train
|
gorilla/sessions
|
store.go
|
save
|
func (s *FilesystemStore) save(session *Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
s.Codecs...)
if err != nil {
return err
}
filename := filepath.Join(s.path, "session_"+session.ID)
fileMutex.Lock()
defer fileMutex.Unlock()
return ioutil.WriteFile(filename, []byte(encoded), 0600)
}
|
go
|
func (s *FilesystemStore) save(session *Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
s.Codecs...)
if err != nil {
return err
}
filename := filepath.Join(s.path, "session_"+session.ID)
fileMutex.Lock()
defer fileMutex.Unlock()
return ioutil.WriteFile(filename, []byte(encoded), 0600)
}
|
[
"func",
"(",
"s",
"*",
"FilesystemStore",
")",
"save",
"(",
"session",
"*",
"Session",
")",
"error",
"{",
"encoded",
",",
"err",
":=",
"securecookie",
".",
"EncodeMulti",
"(",
"session",
".",
"Name",
"(",
")",
",",
"session",
".",
"Values",
",",
"s",
".",
"Codecs",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"filename",
":=",
"filepath",
".",
"Join",
"(",
"s",
".",
"path",
",",
"\"",
"\"",
"+",
"session",
".",
"ID",
")",
"\n",
"fileMutex",
".",
"Lock",
"(",
")",
"\n",
"defer",
"fileMutex",
".",
"Unlock",
"(",
")",
"\n",
"return",
"ioutil",
".",
"WriteFile",
"(",
"filename",
",",
"[",
"]",
"byte",
"(",
"encoded",
")",
",",
"0600",
")",
"\n",
"}"
] |
// save writes encoded session.Values to a file.
|
[
"save",
"writes",
"encoded",
"session",
".",
"Values",
"to",
"a",
"file",
"."
] |
12bd4761fc66ac946e16fcc2a32b1e0b066f6177
|
https://github.com/gorilla/sessions/blob/12bd4761fc66ac946e16fcc2a32b1e0b066f6177/store.go#L255-L265
|
train
|
gorilla/sessions
|
store.go
|
erase
|
func (s *FilesystemStore) erase(session *Session) error {
filename := filepath.Join(s.path, "session_"+session.ID)
fileMutex.RLock()
defer fileMutex.RUnlock()
err := os.Remove(filename)
return err
}
|
go
|
func (s *FilesystemStore) erase(session *Session) error {
filename := filepath.Join(s.path, "session_"+session.ID)
fileMutex.RLock()
defer fileMutex.RUnlock()
err := os.Remove(filename)
return err
}
|
[
"func",
"(",
"s",
"*",
"FilesystemStore",
")",
"erase",
"(",
"session",
"*",
"Session",
")",
"error",
"{",
"filename",
":=",
"filepath",
".",
"Join",
"(",
"s",
".",
"path",
",",
"\"",
"\"",
"+",
"session",
".",
"ID",
")",
"\n\n",
"fileMutex",
".",
"RLock",
"(",
")",
"\n",
"defer",
"fileMutex",
".",
"RUnlock",
"(",
")",
"\n\n",
"err",
":=",
"os",
".",
"Remove",
"(",
"filename",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// delete session file
|
[
"delete",
"session",
"file"
] |
12bd4761fc66ac946e16fcc2a32b1e0b066f6177
|
https://github.com/gorilla/sessions/blob/12bd4761fc66ac946e16fcc2a32b1e0b066f6177/store.go#L284-L292
|
train
|
gorilla/sessions
|
sessions.go
|
GetRegistry
|
func GetRegistry(r *http.Request) *Registry {
var ctx = r.Context()
registry := ctx.Value(registryKey)
if registry != nil {
return registry.(*Registry)
}
newRegistry := &Registry{
request: r,
sessions: make(map[string]sessionInfo),
}
*r = *r.WithContext(context.WithValue(ctx, registryKey, newRegistry))
return newRegistry
}
|
go
|
func GetRegistry(r *http.Request) *Registry {
var ctx = r.Context()
registry := ctx.Value(registryKey)
if registry != nil {
return registry.(*Registry)
}
newRegistry := &Registry{
request: r,
sessions: make(map[string]sessionInfo),
}
*r = *r.WithContext(context.WithValue(ctx, registryKey, newRegistry))
return newRegistry
}
|
[
"func",
"GetRegistry",
"(",
"r",
"*",
"http",
".",
"Request",
")",
"*",
"Registry",
"{",
"var",
"ctx",
"=",
"r",
".",
"Context",
"(",
")",
"\n",
"registry",
":=",
"ctx",
".",
"Value",
"(",
"registryKey",
")",
"\n",
"if",
"registry",
"!=",
"nil",
"{",
"return",
"registry",
".",
"(",
"*",
"Registry",
")",
"\n",
"}",
"\n",
"newRegistry",
":=",
"&",
"Registry",
"{",
"request",
":",
"r",
",",
"sessions",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"sessionInfo",
")",
",",
"}",
"\n",
"*",
"r",
"=",
"*",
"r",
".",
"WithContext",
"(",
"context",
".",
"WithValue",
"(",
"ctx",
",",
"registryKey",
",",
"newRegistry",
")",
")",
"\n",
"return",
"newRegistry",
"\n",
"}"
] |
// GetRegistry returns a registry instance for the current request.
|
[
"GetRegistry",
"returns",
"a",
"registry",
"instance",
"for",
"the",
"current",
"request",
"."
] |
12bd4761fc66ac946e16fcc2a32b1e0b066f6177
|
https://github.com/gorilla/sessions/blob/12bd4761fc66ac946e16fcc2a32b1e0b066f6177/sessions.go#L109-L121
|
train
|
gorilla/sessions
|
sessions.go
|
Get
|
func (s *Registry) Get(store Store, name string) (session *Session, err error) {
if !isCookieNameValid(name) {
return nil, fmt.Errorf("sessions: invalid character in cookie name: %s", name)
}
if info, ok := s.sessions[name]; ok {
session, err = info.s, info.e
} else {
session, err = store.New(s.request, name)
session.name = name
s.sessions[name] = sessionInfo{s: session, e: err}
}
session.store = store
return
}
|
go
|
func (s *Registry) Get(store Store, name string) (session *Session, err error) {
if !isCookieNameValid(name) {
return nil, fmt.Errorf("sessions: invalid character in cookie name: %s", name)
}
if info, ok := s.sessions[name]; ok {
session, err = info.s, info.e
} else {
session, err = store.New(s.request, name)
session.name = name
s.sessions[name] = sessionInfo{s: session, e: err}
}
session.store = store
return
}
|
[
"func",
"(",
"s",
"*",
"Registry",
")",
"Get",
"(",
"store",
"Store",
",",
"name",
"string",
")",
"(",
"session",
"*",
"Session",
",",
"err",
"error",
")",
"{",
"if",
"!",
"isCookieNameValid",
"(",
"name",
")",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"name",
")",
"\n",
"}",
"\n",
"if",
"info",
",",
"ok",
":=",
"s",
".",
"sessions",
"[",
"name",
"]",
";",
"ok",
"{",
"session",
",",
"err",
"=",
"info",
".",
"s",
",",
"info",
".",
"e",
"\n",
"}",
"else",
"{",
"session",
",",
"err",
"=",
"store",
".",
"New",
"(",
"s",
".",
"request",
",",
"name",
")",
"\n",
"session",
".",
"name",
"=",
"name",
"\n",
"s",
".",
"sessions",
"[",
"name",
"]",
"=",
"sessionInfo",
"{",
"s",
":",
"session",
",",
"e",
":",
"err",
"}",
"\n",
"}",
"\n",
"session",
".",
"store",
"=",
"store",
"\n",
"return",
"\n",
"}"
] |
// Get registers and returns a session for the given name and session store.
//
// It returns a new session if there are no sessions registered for the name.
|
[
"Get",
"registers",
"and",
"returns",
"a",
"session",
"for",
"the",
"given",
"name",
"and",
"session",
"store",
".",
"It",
"returns",
"a",
"new",
"session",
"if",
"there",
"are",
"no",
"sessions",
"registered",
"for",
"the",
"name",
"."
] |
12bd4761fc66ac946e16fcc2a32b1e0b066f6177
|
https://github.com/gorilla/sessions/blob/12bd4761fc66ac946e16fcc2a32b1e0b066f6177/sessions.go#L132-L145
|
train
|
gorilla/sessions
|
sessions.go
|
Save
|
func (s *Registry) Save(w http.ResponseWriter) error {
var errMulti MultiError
for name, info := range s.sessions {
session := info.s
if session.store == nil {
errMulti = append(errMulti, fmt.Errorf(
"sessions: missing store for session %q", name))
} else if err := session.store.Save(s.request, w, session); err != nil {
errMulti = append(errMulti, fmt.Errorf(
"sessions: error saving session %q -- %v", name, err))
}
}
if errMulti != nil {
return errMulti
}
return nil
}
|
go
|
func (s *Registry) Save(w http.ResponseWriter) error {
var errMulti MultiError
for name, info := range s.sessions {
session := info.s
if session.store == nil {
errMulti = append(errMulti, fmt.Errorf(
"sessions: missing store for session %q", name))
} else if err := session.store.Save(s.request, w, session); err != nil {
errMulti = append(errMulti, fmt.Errorf(
"sessions: error saving session %q -- %v", name, err))
}
}
if errMulti != nil {
return errMulti
}
return nil
}
|
[
"func",
"(",
"s",
"*",
"Registry",
")",
"Save",
"(",
"w",
"http",
".",
"ResponseWriter",
")",
"error",
"{",
"var",
"errMulti",
"MultiError",
"\n",
"for",
"name",
",",
"info",
":=",
"range",
"s",
".",
"sessions",
"{",
"session",
":=",
"info",
".",
"s",
"\n",
"if",
"session",
".",
"store",
"==",
"nil",
"{",
"errMulti",
"=",
"append",
"(",
"errMulti",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"name",
")",
")",
"\n",
"}",
"else",
"if",
"err",
":=",
"session",
".",
"store",
".",
"Save",
"(",
"s",
".",
"request",
",",
"w",
",",
"session",
")",
";",
"err",
"!=",
"nil",
"{",
"errMulti",
"=",
"append",
"(",
"errMulti",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"name",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"errMulti",
"!=",
"nil",
"{",
"return",
"errMulti",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Save saves all sessions registered for the current request.
|
[
"Save",
"saves",
"all",
"sessions",
"registered",
"for",
"the",
"current",
"request",
"."
] |
12bd4761fc66ac946e16fcc2a32b1e0b066f6177
|
https://github.com/gorilla/sessions/blob/12bd4761fc66ac946e16fcc2a32b1e0b066f6177/sessions.go#L148-L164
|
train
|
gorilla/sessions
|
sessions.go
|
Save
|
func Save(r *http.Request, w http.ResponseWriter) error {
return GetRegistry(r).Save(w)
}
|
go
|
func Save(r *http.Request, w http.ResponseWriter) error {
return GetRegistry(r).Save(w)
}
|
[
"func",
"Save",
"(",
"r",
"*",
"http",
".",
"Request",
",",
"w",
"http",
".",
"ResponseWriter",
")",
"error",
"{",
"return",
"GetRegistry",
"(",
"r",
")",
".",
"Save",
"(",
"w",
")",
"\n",
"}"
] |
// Save saves all sessions used during the current request.
|
[
"Save",
"saves",
"all",
"sessions",
"used",
"during",
"the",
"current",
"request",
"."
] |
12bd4761fc66ac946e16fcc2a32b1e0b066f6177
|
https://github.com/gorilla/sessions/blob/12bd4761fc66ac946e16fcc2a32b1e0b066f6177/sessions.go#L173-L175
|
train
|
gorilla/sessions
|
cookie_go111.go
|
newCookieFromOptions
|
func newCookieFromOptions(name, value string, options *Options) *http.Cookie {
return &http.Cookie{
Name: name,
Value: value,
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HttpOnly,
SameSite: options.SameSite,
}
}
|
go
|
func newCookieFromOptions(name, value string, options *Options) *http.Cookie {
return &http.Cookie{
Name: name,
Value: value,
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HttpOnly,
SameSite: options.SameSite,
}
}
|
[
"func",
"newCookieFromOptions",
"(",
"name",
",",
"value",
"string",
",",
"options",
"*",
"Options",
")",
"*",
"http",
".",
"Cookie",
"{",
"return",
"&",
"http",
".",
"Cookie",
"{",
"Name",
":",
"name",
",",
"Value",
":",
"value",
",",
"Path",
":",
"options",
".",
"Path",
",",
"Domain",
":",
"options",
".",
"Domain",
",",
"MaxAge",
":",
"options",
".",
"MaxAge",
",",
"Secure",
":",
"options",
".",
"Secure",
",",
"HttpOnly",
":",
"options",
".",
"HttpOnly",
",",
"SameSite",
":",
"options",
".",
"SameSite",
",",
"}",
"\n\n",
"}"
] |
// newCookieFromOptions returns an http.Cookie with the options set.
|
[
"newCookieFromOptions",
"returns",
"an",
"http",
".",
"Cookie",
"with",
"the",
"options",
"set",
"."
] |
12bd4761fc66ac946e16fcc2a32b1e0b066f6177
|
https://github.com/gorilla/sessions/blob/12bd4761fc66ac946e16fcc2a32b1e0b066f6177/cookie_go111.go#L8-L20
|
train
|
nfnt/resize
|
ycc.go
|
SubImage
|
func (p *ycc) SubImage(r image.Rectangle) image.Image {
r = r.Intersect(p.Rect)
if r.Empty() {
return &ycc{SubsampleRatio: p.SubsampleRatio}
}
i := p.PixOffset(r.Min.X, r.Min.Y)
return &ycc{
Pix: p.Pix[i:],
Stride: p.Stride,
Rect: r,
SubsampleRatio: p.SubsampleRatio,
}
}
|
go
|
func (p *ycc) SubImage(r image.Rectangle) image.Image {
r = r.Intersect(p.Rect)
if r.Empty() {
return &ycc{SubsampleRatio: p.SubsampleRatio}
}
i := p.PixOffset(r.Min.X, r.Min.Y)
return &ycc{
Pix: p.Pix[i:],
Stride: p.Stride,
Rect: r,
SubsampleRatio: p.SubsampleRatio,
}
}
|
[
"func",
"(",
"p",
"*",
"ycc",
")",
"SubImage",
"(",
"r",
"image",
".",
"Rectangle",
")",
"image",
".",
"Image",
"{",
"r",
"=",
"r",
".",
"Intersect",
"(",
"p",
".",
"Rect",
")",
"\n",
"if",
"r",
".",
"Empty",
"(",
")",
"{",
"return",
"&",
"ycc",
"{",
"SubsampleRatio",
":",
"p",
".",
"SubsampleRatio",
"}",
"\n",
"}",
"\n",
"i",
":=",
"p",
".",
"PixOffset",
"(",
"r",
".",
"Min",
".",
"X",
",",
"r",
".",
"Min",
".",
"Y",
")",
"\n",
"return",
"&",
"ycc",
"{",
"Pix",
":",
"p",
".",
"Pix",
"[",
"i",
":",
"]",
",",
"Stride",
":",
"p",
".",
"Stride",
",",
"Rect",
":",
"r",
",",
"SubsampleRatio",
":",
"p",
".",
"SubsampleRatio",
",",
"}",
"\n",
"}"
] |
// SubImage returns an image representing the portion of the image p visible
// through r. The returned value shares pixels with the original image.
|
[
"SubImage",
"returns",
"an",
"image",
"representing",
"the",
"portion",
"of",
"the",
"image",
"p",
"visible",
"through",
"r",
".",
"The",
"returned",
"value",
"shares",
"pixels",
"with",
"the",
"original",
"image",
"."
] |
83c6a9932646f83e3267f353373d47347b6036b2
|
https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/ycc.go#L70-L82
|
train
|
nfnt/resize
|
ycc.go
|
newYCC
|
func newYCC(r image.Rectangle, s image.YCbCrSubsampleRatio) *ycc {
w, h := r.Dx(), r.Dy()
buf := make([]uint8, 3*w*h)
return &ycc{Pix: buf, Stride: 3 * w, Rect: r, SubsampleRatio: s}
}
|
go
|
func newYCC(r image.Rectangle, s image.YCbCrSubsampleRatio) *ycc {
w, h := r.Dx(), r.Dy()
buf := make([]uint8, 3*w*h)
return &ycc{Pix: buf, Stride: 3 * w, Rect: r, SubsampleRatio: s}
}
|
[
"func",
"newYCC",
"(",
"r",
"image",
".",
"Rectangle",
",",
"s",
"image",
".",
"YCbCrSubsampleRatio",
")",
"*",
"ycc",
"{",
"w",
",",
"h",
":=",
"r",
".",
"Dx",
"(",
")",
",",
"r",
".",
"Dy",
"(",
")",
"\n",
"buf",
":=",
"make",
"(",
"[",
"]",
"uint8",
",",
"3",
"*",
"w",
"*",
"h",
")",
"\n",
"return",
"&",
"ycc",
"{",
"Pix",
":",
"buf",
",",
"Stride",
":",
"3",
"*",
"w",
",",
"Rect",
":",
"r",
",",
"SubsampleRatio",
":",
"s",
"}",
"\n",
"}"
] |
// newYCC returns a new ycc with the given bounds and subsample ratio.
|
[
"newYCC",
"returns",
"a",
"new",
"ycc",
"with",
"the",
"given",
"bounds",
"and",
"subsample",
"ratio",
"."
] |
83c6a9932646f83e3267f353373d47347b6036b2
|
https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/ycc.go#L85-L89
|
train
|
nfnt/resize
|
ycc.go
|
YCbCr
|
func (p *ycc) YCbCr() *image.YCbCr {
ycbcr := image.NewYCbCr(p.Rect, p.SubsampleRatio)
switch ycbcr.SubsampleRatio {
case ycbcrSubsampleRatio422:
return p.ycbcr422(ycbcr)
case ycbcrSubsampleRatio420:
return p.ycbcr420(ycbcr)
case ycbcrSubsampleRatio440:
return p.ycbcr440(ycbcr)
case ycbcrSubsampleRatio444:
return p.ycbcr444(ycbcr)
case ycbcrSubsampleRatio411:
return p.ycbcr411(ycbcr)
case ycbcrSubsampleRatio410:
return p.ycbcr410(ycbcr)
}
return ycbcr
}
|
go
|
func (p *ycc) YCbCr() *image.YCbCr {
ycbcr := image.NewYCbCr(p.Rect, p.SubsampleRatio)
switch ycbcr.SubsampleRatio {
case ycbcrSubsampleRatio422:
return p.ycbcr422(ycbcr)
case ycbcrSubsampleRatio420:
return p.ycbcr420(ycbcr)
case ycbcrSubsampleRatio440:
return p.ycbcr440(ycbcr)
case ycbcrSubsampleRatio444:
return p.ycbcr444(ycbcr)
case ycbcrSubsampleRatio411:
return p.ycbcr411(ycbcr)
case ycbcrSubsampleRatio410:
return p.ycbcr410(ycbcr)
}
return ycbcr
}
|
[
"func",
"(",
"p",
"*",
"ycc",
")",
"YCbCr",
"(",
")",
"*",
"image",
".",
"YCbCr",
"{",
"ycbcr",
":=",
"image",
".",
"NewYCbCr",
"(",
"p",
".",
"Rect",
",",
"p",
".",
"SubsampleRatio",
")",
"\n",
"switch",
"ycbcr",
".",
"SubsampleRatio",
"{",
"case",
"ycbcrSubsampleRatio422",
":",
"return",
"p",
".",
"ycbcr422",
"(",
"ycbcr",
")",
"\n",
"case",
"ycbcrSubsampleRatio420",
":",
"return",
"p",
".",
"ycbcr420",
"(",
"ycbcr",
")",
"\n",
"case",
"ycbcrSubsampleRatio440",
":",
"return",
"p",
".",
"ycbcr440",
"(",
"ycbcr",
")",
"\n",
"case",
"ycbcrSubsampleRatio444",
":",
"return",
"p",
".",
"ycbcr444",
"(",
"ycbcr",
")",
"\n",
"case",
"ycbcrSubsampleRatio411",
":",
"return",
"p",
".",
"ycbcr411",
"(",
"ycbcr",
")",
"\n",
"case",
"ycbcrSubsampleRatio410",
":",
"return",
"p",
".",
"ycbcr410",
"(",
"ycbcr",
")",
"\n",
"}",
"\n",
"return",
"ycbcr",
"\n",
"}"
] |
// YCbCr converts ycc to a YCbCr image with the same subsample ratio
// as the YCbCr image that ycc was generated from.
|
[
"YCbCr",
"converts",
"ycc",
"to",
"a",
"YCbCr",
"image",
"with",
"the",
"same",
"subsample",
"ratio",
"as",
"the",
"YCbCr",
"image",
"that",
"ycc",
"was",
"generated",
"from",
"."
] |
83c6a9932646f83e3267f353373d47347b6036b2
|
https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/ycc.go#L104-L121
|
train
|
nfnt/resize
|
ycc.go
|
imageYCbCrToYCC
|
func imageYCbCrToYCC(in *image.YCbCr) *ycc {
w, h := in.Rect.Dx(), in.Rect.Dy()
p := ycc{
Pix: make([]uint8, 3*w*h),
Stride: 3 * w,
Rect: image.Rect(0, 0, w, h),
SubsampleRatio: in.SubsampleRatio,
}
switch in.SubsampleRatio {
case ycbcrSubsampleRatio422:
return convertToYCC422(in, &p)
case ycbcrSubsampleRatio420:
return convertToYCC420(in, &p)
case ycbcrSubsampleRatio440:
return convertToYCC440(in, &p)
case ycbcrSubsampleRatio444:
return convertToYCC444(in, &p)
case ycbcrSubsampleRatio411:
return convertToYCC411(in, &p)
case ycbcrSubsampleRatio410:
return convertToYCC410(in, &p)
}
return &p
}
|
go
|
func imageYCbCrToYCC(in *image.YCbCr) *ycc {
w, h := in.Rect.Dx(), in.Rect.Dy()
p := ycc{
Pix: make([]uint8, 3*w*h),
Stride: 3 * w,
Rect: image.Rect(0, 0, w, h),
SubsampleRatio: in.SubsampleRatio,
}
switch in.SubsampleRatio {
case ycbcrSubsampleRatio422:
return convertToYCC422(in, &p)
case ycbcrSubsampleRatio420:
return convertToYCC420(in, &p)
case ycbcrSubsampleRatio440:
return convertToYCC440(in, &p)
case ycbcrSubsampleRatio444:
return convertToYCC444(in, &p)
case ycbcrSubsampleRatio411:
return convertToYCC411(in, &p)
case ycbcrSubsampleRatio410:
return convertToYCC410(in, &p)
}
return &p
}
|
[
"func",
"imageYCbCrToYCC",
"(",
"in",
"*",
"image",
".",
"YCbCr",
")",
"*",
"ycc",
"{",
"w",
",",
"h",
":=",
"in",
".",
"Rect",
".",
"Dx",
"(",
")",
",",
"in",
".",
"Rect",
".",
"Dy",
"(",
")",
"\n",
"p",
":=",
"ycc",
"{",
"Pix",
":",
"make",
"(",
"[",
"]",
"uint8",
",",
"3",
"*",
"w",
"*",
"h",
")",
",",
"Stride",
":",
"3",
"*",
"w",
",",
"Rect",
":",
"image",
".",
"Rect",
"(",
"0",
",",
"0",
",",
"w",
",",
"h",
")",
",",
"SubsampleRatio",
":",
"in",
".",
"SubsampleRatio",
",",
"}",
"\n",
"switch",
"in",
".",
"SubsampleRatio",
"{",
"case",
"ycbcrSubsampleRatio422",
":",
"return",
"convertToYCC422",
"(",
"in",
",",
"&",
"p",
")",
"\n",
"case",
"ycbcrSubsampleRatio420",
":",
"return",
"convertToYCC420",
"(",
"in",
",",
"&",
"p",
")",
"\n",
"case",
"ycbcrSubsampleRatio440",
":",
"return",
"convertToYCC440",
"(",
"in",
",",
"&",
"p",
")",
"\n",
"case",
"ycbcrSubsampleRatio444",
":",
"return",
"convertToYCC444",
"(",
"in",
",",
"&",
"p",
")",
"\n",
"case",
"ycbcrSubsampleRatio411",
":",
"return",
"convertToYCC411",
"(",
"in",
",",
"&",
"p",
")",
"\n",
"case",
"ycbcrSubsampleRatio410",
":",
"return",
"convertToYCC410",
"(",
"in",
",",
"&",
"p",
")",
"\n",
"}",
"\n",
"return",
"&",
"p",
"\n",
"}"
] |
// imageYCbCrToYCC converts a YCbCr image to a ycc image for resizing.
|
[
"imageYCbCrToYCC",
"converts",
"a",
"YCbCr",
"image",
"to",
"a",
"ycc",
"image",
"for",
"resizing",
"."
] |
83c6a9932646f83e3267f353373d47347b6036b2
|
https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/ycc.go#L124-L147
|
train
|
nfnt/resize
|
thumbnail.go
|
Thumbnail
|
func Thumbnail(maxWidth, maxHeight uint, img image.Image, interp InterpolationFunction) image.Image {
origBounds := img.Bounds()
origWidth := uint(origBounds.Dx())
origHeight := uint(origBounds.Dy())
newWidth, newHeight := origWidth, origHeight
// Return original image if it have same or smaller size as constraints
if maxWidth >= origWidth && maxHeight >= origHeight {
return img
}
// Preserve aspect ratio
if origWidth > maxWidth {
newHeight = uint(origHeight * maxWidth / origWidth)
if newHeight < 1 {
newHeight = 1
}
newWidth = maxWidth
}
if newHeight > maxHeight {
newWidth = uint(newWidth * maxHeight / newHeight)
if newWidth < 1 {
newWidth = 1
}
newHeight = maxHeight
}
return Resize(newWidth, newHeight, img, interp)
}
|
go
|
func Thumbnail(maxWidth, maxHeight uint, img image.Image, interp InterpolationFunction) image.Image {
origBounds := img.Bounds()
origWidth := uint(origBounds.Dx())
origHeight := uint(origBounds.Dy())
newWidth, newHeight := origWidth, origHeight
// Return original image if it have same or smaller size as constraints
if maxWidth >= origWidth && maxHeight >= origHeight {
return img
}
// Preserve aspect ratio
if origWidth > maxWidth {
newHeight = uint(origHeight * maxWidth / origWidth)
if newHeight < 1 {
newHeight = 1
}
newWidth = maxWidth
}
if newHeight > maxHeight {
newWidth = uint(newWidth * maxHeight / newHeight)
if newWidth < 1 {
newWidth = 1
}
newHeight = maxHeight
}
return Resize(newWidth, newHeight, img, interp)
}
|
[
"func",
"Thumbnail",
"(",
"maxWidth",
",",
"maxHeight",
"uint",
",",
"img",
"image",
".",
"Image",
",",
"interp",
"InterpolationFunction",
")",
"image",
".",
"Image",
"{",
"origBounds",
":=",
"img",
".",
"Bounds",
"(",
")",
"\n",
"origWidth",
":=",
"uint",
"(",
"origBounds",
".",
"Dx",
"(",
")",
")",
"\n",
"origHeight",
":=",
"uint",
"(",
"origBounds",
".",
"Dy",
"(",
")",
")",
"\n",
"newWidth",
",",
"newHeight",
":=",
"origWidth",
",",
"origHeight",
"\n\n",
"// Return original image if it have same or smaller size as constraints",
"if",
"maxWidth",
">=",
"origWidth",
"&&",
"maxHeight",
">=",
"origHeight",
"{",
"return",
"img",
"\n",
"}",
"\n\n",
"// Preserve aspect ratio",
"if",
"origWidth",
">",
"maxWidth",
"{",
"newHeight",
"=",
"uint",
"(",
"origHeight",
"*",
"maxWidth",
"/",
"origWidth",
")",
"\n",
"if",
"newHeight",
"<",
"1",
"{",
"newHeight",
"=",
"1",
"\n",
"}",
"\n",
"newWidth",
"=",
"maxWidth",
"\n",
"}",
"\n\n",
"if",
"newHeight",
">",
"maxHeight",
"{",
"newWidth",
"=",
"uint",
"(",
"newWidth",
"*",
"maxHeight",
"/",
"newHeight",
")",
"\n",
"if",
"newWidth",
"<",
"1",
"{",
"newWidth",
"=",
"1",
"\n",
"}",
"\n",
"newHeight",
"=",
"maxHeight",
"\n",
"}",
"\n",
"return",
"Resize",
"(",
"newWidth",
",",
"newHeight",
",",
"img",
",",
"interp",
")",
"\n",
"}"
] |
// Thumbnail will downscale provided image to max width and height preserving
// original aspect ratio and using the interpolation function interp.
// It will return original image, without processing it, if original sizes
// are already smaller than provided constraints.
|
[
"Thumbnail",
"will",
"downscale",
"provided",
"image",
"to",
"max",
"width",
"and",
"height",
"preserving",
"original",
"aspect",
"ratio",
"and",
"using",
"the",
"interpolation",
"function",
"interp",
".",
"It",
"will",
"return",
"original",
"image",
"without",
"processing",
"it",
"if",
"original",
"sizes",
"are",
"already",
"smaller",
"than",
"provided",
"constraints",
"."
] |
83c6a9932646f83e3267f353373d47347b6036b2
|
https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/thumbnail.go#L27-L55
|
train
|
nfnt/resize
|
resize.go
|
kernel
|
func (i InterpolationFunction) kernel() (int, func(float64) float64) {
switch i {
case Bilinear:
return 2, linear
case Bicubic:
return 4, cubic
case MitchellNetravali:
return 4, mitchellnetravali
case Lanczos2:
return 4, lanczos2
case Lanczos3:
return 6, lanczos3
default:
// Default to NearestNeighbor.
return 2, nearest
}
}
|
go
|
func (i InterpolationFunction) kernel() (int, func(float64) float64) {
switch i {
case Bilinear:
return 2, linear
case Bicubic:
return 4, cubic
case MitchellNetravali:
return 4, mitchellnetravali
case Lanczos2:
return 4, lanczos2
case Lanczos3:
return 6, lanczos3
default:
// Default to NearestNeighbor.
return 2, nearest
}
}
|
[
"func",
"(",
"i",
"InterpolationFunction",
")",
"kernel",
"(",
")",
"(",
"int",
",",
"func",
"(",
"float64",
")",
"float64",
")",
"{",
"switch",
"i",
"{",
"case",
"Bilinear",
":",
"return",
"2",
",",
"linear",
"\n",
"case",
"Bicubic",
":",
"return",
"4",
",",
"cubic",
"\n",
"case",
"MitchellNetravali",
":",
"return",
"4",
",",
"mitchellnetravali",
"\n",
"case",
"Lanczos2",
":",
"return",
"4",
",",
"lanczos2",
"\n",
"case",
"Lanczos3",
":",
"return",
"6",
",",
"lanczos3",
"\n",
"default",
":",
"// Default to NearestNeighbor.",
"return",
"2",
",",
"nearest",
"\n",
"}",
"\n",
"}"
] |
// kernal, returns an InterpolationFunctions taps and kernel.
|
[
"kernal",
"returns",
"an",
"InterpolationFunctions",
"taps",
"and",
"kernel",
"."
] |
83c6a9932646f83e3267f353373d47347b6036b2
|
https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/resize.go#L55-L71
|
train
|
nfnt/resize
|
resize.go
|
calcFactors
|
func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
if width == 0 {
if height == 0 {
scaleX = 1.0
scaleY = 1.0
} else {
scaleY = oldHeight / float64(height)
scaleX = scaleY
}
} else {
scaleX = oldWidth / float64(width)
if height == 0 {
scaleY = scaleX
} else {
scaleY = oldHeight / float64(height)
}
}
return
}
|
go
|
func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
if width == 0 {
if height == 0 {
scaleX = 1.0
scaleY = 1.0
} else {
scaleY = oldHeight / float64(height)
scaleX = scaleY
}
} else {
scaleX = oldWidth / float64(width)
if height == 0 {
scaleY = scaleX
} else {
scaleY = oldHeight / float64(height)
}
}
return
}
|
[
"func",
"calcFactors",
"(",
"width",
",",
"height",
"uint",
",",
"oldWidth",
",",
"oldHeight",
"float64",
")",
"(",
"scaleX",
",",
"scaleY",
"float64",
")",
"{",
"if",
"width",
"==",
"0",
"{",
"if",
"height",
"==",
"0",
"{",
"scaleX",
"=",
"1.0",
"\n",
"scaleY",
"=",
"1.0",
"\n",
"}",
"else",
"{",
"scaleY",
"=",
"oldHeight",
"/",
"float64",
"(",
"height",
")",
"\n",
"scaleX",
"=",
"scaleY",
"\n",
"}",
"\n",
"}",
"else",
"{",
"scaleX",
"=",
"oldWidth",
"/",
"float64",
"(",
"width",
")",
"\n",
"if",
"height",
"==",
"0",
"{",
"scaleY",
"=",
"scaleX",
"\n",
"}",
"else",
"{",
"scaleY",
"=",
"oldHeight",
"/",
"float64",
"(",
"height",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Calculates scaling factors using old and new image dimensions.
|
[
"Calculates",
"scaling",
"factors",
"using",
"old",
"and",
"new",
"image",
"dimensions",
"."
] |
83c6a9932646f83e3267f353373d47347b6036b2
|
https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/resize.go#L593-L611
|
train
|
gliderlabs/ssh
|
server.go
|
Shutdown
|
func (srv *Server) Shutdown(ctx context.Context) error {
srv.mu.Lock()
lnerr := srv.closeListenersLocked()
srv.closeDoneChanLocked()
srv.mu.Unlock()
finished := make(chan struct{}, 1)
go func() {
srv.listenerWg.Wait()
srv.connWg.Wait()
finished <- struct{}{}
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-finished:
return lnerr
}
}
|
go
|
func (srv *Server) Shutdown(ctx context.Context) error {
srv.mu.Lock()
lnerr := srv.closeListenersLocked()
srv.closeDoneChanLocked()
srv.mu.Unlock()
finished := make(chan struct{}, 1)
go func() {
srv.listenerWg.Wait()
srv.connWg.Wait()
finished <- struct{}{}
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-finished:
return lnerr
}
}
|
[
"func",
"(",
"srv",
"*",
"Server",
")",
"Shutdown",
"(",
"ctx",
"context",
".",
"Context",
")",
"error",
"{",
"srv",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"lnerr",
":=",
"srv",
".",
"closeListenersLocked",
"(",
")",
"\n",
"srv",
".",
"closeDoneChanLocked",
"(",
")",
"\n",
"srv",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"finished",
":=",
"make",
"(",
"chan",
"struct",
"{",
"}",
",",
"1",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"srv",
".",
"listenerWg",
".",
"Wait",
"(",
")",
"\n",
"srv",
".",
"connWg",
".",
"Wait",
"(",
")",
"\n",
"finished",
"<-",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"(",
")",
"\n\n",
"select",
"{",
"case",
"<-",
"ctx",
".",
"Done",
"(",
")",
":",
"return",
"ctx",
".",
"Err",
"(",
")",
"\n",
"case",
"<-",
"finished",
":",
"return",
"lnerr",
"\n",
"}",
"\n",
"}"
] |
// Shutdown gracefully shuts down the server without interrupting any
// active connections. Shutdown works by first closing all open
// listeners, and then waiting indefinitely for connections to close.
// If the provided context expires before the shutdown is complete,
// then the context's error is returned.
|
[
"Shutdown",
"gracefully",
"shuts",
"down",
"the",
"server",
"without",
"interrupting",
"any",
"active",
"connections",
".",
"Shutdown",
"works",
"by",
"first",
"closing",
"all",
"open",
"listeners",
"and",
"then",
"waiting",
"indefinitely",
"for",
"connections",
"to",
"close",
".",
"If",
"the",
"provided",
"context",
"expires",
"before",
"the",
"shutdown",
"is",
"complete",
"then",
"the",
"context",
"s",
"error",
"is",
"returned",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/server.go#L154-L173
|
train
|
gliderlabs/ssh
|
server.go
|
Serve
|
func (srv *Server) Serve(l net.Listener) error {
srv.ensureHandlers()
defer l.Close()
if err := srv.ensureHostSigner(); err != nil {
return err
}
if srv.Handler == nil {
srv.Handler = DefaultHandler
}
if srv.channelHandlers == nil {
srv.channelHandlers = map[string]channelHandler{
"session": sessionHandler,
"direct-tcpip": directTcpipHandler,
}
}
var tempDelay time.Duration
srv.trackListener(l, true)
defer srv.trackListener(l, false)
for {
conn, e := l.Accept()
if e != nil {
select {
case <-srv.getDoneChan():
return ErrServerClosed
default:
}
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
time.Sleep(tempDelay)
continue
}
return e
}
go srv.handleConn(conn)
}
}
|
go
|
func (srv *Server) Serve(l net.Listener) error {
srv.ensureHandlers()
defer l.Close()
if err := srv.ensureHostSigner(); err != nil {
return err
}
if srv.Handler == nil {
srv.Handler = DefaultHandler
}
if srv.channelHandlers == nil {
srv.channelHandlers = map[string]channelHandler{
"session": sessionHandler,
"direct-tcpip": directTcpipHandler,
}
}
var tempDelay time.Duration
srv.trackListener(l, true)
defer srv.trackListener(l, false)
for {
conn, e := l.Accept()
if e != nil {
select {
case <-srv.getDoneChan():
return ErrServerClosed
default:
}
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
time.Sleep(tempDelay)
continue
}
return e
}
go srv.handleConn(conn)
}
}
|
[
"func",
"(",
"srv",
"*",
"Server",
")",
"Serve",
"(",
"l",
"net",
".",
"Listener",
")",
"error",
"{",
"srv",
".",
"ensureHandlers",
"(",
")",
"\n",
"defer",
"l",
".",
"Close",
"(",
")",
"\n",
"if",
"err",
":=",
"srv",
".",
"ensureHostSigner",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"srv",
".",
"Handler",
"==",
"nil",
"{",
"srv",
".",
"Handler",
"=",
"DefaultHandler",
"\n",
"}",
"\n",
"if",
"srv",
".",
"channelHandlers",
"==",
"nil",
"{",
"srv",
".",
"channelHandlers",
"=",
"map",
"[",
"string",
"]",
"channelHandler",
"{",
"\"",
"\"",
":",
"sessionHandler",
",",
"\"",
"\"",
":",
"directTcpipHandler",
",",
"}",
"\n",
"}",
"\n",
"var",
"tempDelay",
"time",
".",
"Duration",
"\n\n",
"srv",
".",
"trackListener",
"(",
"l",
",",
"true",
")",
"\n",
"defer",
"srv",
".",
"trackListener",
"(",
"l",
",",
"false",
")",
"\n",
"for",
"{",
"conn",
",",
"e",
":=",
"l",
".",
"Accept",
"(",
")",
"\n",
"if",
"e",
"!=",
"nil",
"{",
"select",
"{",
"case",
"<-",
"srv",
".",
"getDoneChan",
"(",
")",
":",
"return",
"ErrServerClosed",
"\n",
"default",
":",
"}",
"\n",
"if",
"ne",
",",
"ok",
":=",
"e",
".",
"(",
"net",
".",
"Error",
")",
";",
"ok",
"&&",
"ne",
".",
"Temporary",
"(",
")",
"{",
"if",
"tempDelay",
"==",
"0",
"{",
"tempDelay",
"=",
"5",
"*",
"time",
".",
"Millisecond",
"\n",
"}",
"else",
"{",
"tempDelay",
"*=",
"2",
"\n",
"}",
"\n",
"if",
"max",
":=",
"1",
"*",
"time",
".",
"Second",
";",
"tempDelay",
">",
"max",
"{",
"tempDelay",
"=",
"max",
"\n",
"}",
"\n",
"time",
".",
"Sleep",
"(",
"tempDelay",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"return",
"e",
"\n",
"}",
"\n",
"go",
"srv",
".",
"handleConn",
"(",
"conn",
")",
"\n",
"}",
"\n",
"}"
] |
// Serve accepts incoming connections on the Listener l, creating a new
// connection goroutine for each. The connection goroutines read requests and then
// calls srv.Handler to handle sessions.
//
// Serve always returns a non-nil error.
|
[
"Serve",
"accepts",
"incoming",
"connections",
"on",
"the",
"Listener",
"l",
"creating",
"a",
"new",
"connection",
"goroutine",
"for",
"each",
".",
"The",
"connection",
"goroutines",
"read",
"requests",
"and",
"then",
"calls",
"srv",
".",
"Handler",
"to",
"handle",
"sessions",
".",
"Serve",
"always",
"returns",
"a",
"non",
"-",
"nil",
"error",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/server.go#L180-L223
|
train
|
gliderlabs/ssh
|
ssh.go
|
Serve
|
func Serve(l net.Listener, handler Handler, options ...Option) error {
srv := &Server{Handler: handler}
for _, option := range options {
if err := srv.SetOption(option); err != nil {
return err
}
}
return srv.Serve(l)
}
|
go
|
func Serve(l net.Listener, handler Handler, options ...Option) error {
srv := &Server{Handler: handler}
for _, option := range options {
if err := srv.SetOption(option); err != nil {
return err
}
}
return srv.Serve(l)
}
|
[
"func",
"Serve",
"(",
"l",
"net",
".",
"Listener",
",",
"handler",
"Handler",
",",
"options",
"...",
"Option",
")",
"error",
"{",
"srv",
":=",
"&",
"Server",
"{",
"Handler",
":",
"handler",
"}",
"\n",
"for",
"_",
",",
"option",
":=",
"range",
"options",
"{",
"if",
"err",
":=",
"srv",
".",
"SetOption",
"(",
"option",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"srv",
".",
"Serve",
"(",
"l",
")",
"\n",
"}"
] |
// Serve accepts incoming SSH connections on the listener l, creating a new
// connection goroutine for each. The connection goroutines read requests and
// then calls handler to handle sessions. Handler is typically nil, in which
// case the DefaultHandler is used.
|
[
"Serve",
"accepts",
"incoming",
"SSH",
"connections",
"on",
"the",
"listener",
"l",
"creating",
"a",
"new",
"connection",
"goroutine",
"for",
"each",
".",
"The",
"connection",
"goroutines",
"read",
"requests",
"and",
"then",
"calls",
"handler",
"to",
"handle",
"sessions",
".",
"Handler",
"is",
"typically",
"nil",
"in",
"which",
"case",
"the",
"DefaultHandler",
"is",
"used",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/ssh.go#L84-L92
|
train
|
gliderlabs/ssh
|
ssh.go
|
ListenAndServe
|
func ListenAndServe(addr string, handler Handler, options ...Option) error {
srv := &Server{Addr: addr, Handler: handler}
for _, option := range options {
if err := srv.SetOption(option); err != nil {
return err
}
}
return srv.ListenAndServe()
}
|
go
|
func ListenAndServe(addr string, handler Handler, options ...Option) error {
srv := &Server{Addr: addr, Handler: handler}
for _, option := range options {
if err := srv.SetOption(option); err != nil {
return err
}
}
return srv.ListenAndServe()
}
|
[
"func",
"ListenAndServe",
"(",
"addr",
"string",
",",
"handler",
"Handler",
",",
"options",
"...",
"Option",
")",
"error",
"{",
"srv",
":=",
"&",
"Server",
"{",
"Addr",
":",
"addr",
",",
"Handler",
":",
"handler",
"}",
"\n",
"for",
"_",
",",
"option",
":=",
"range",
"options",
"{",
"if",
"err",
":=",
"srv",
".",
"SetOption",
"(",
"option",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"srv",
".",
"ListenAndServe",
"(",
")",
"\n",
"}"
] |
// ListenAndServe listens on the TCP network address addr and then calls Serve
// with handler to handle sessions on incoming connections. Handler is typically
// nil, in which case the DefaultHandler is used.
|
[
"ListenAndServe",
"listens",
"on",
"the",
"TCP",
"network",
"address",
"addr",
"and",
"then",
"calls",
"Serve",
"with",
"handler",
"to",
"handle",
"sessions",
"on",
"incoming",
"connections",
".",
"Handler",
"is",
"typically",
"nil",
"in",
"which",
"case",
"the",
"DefaultHandler",
"is",
"used",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/ssh.go#L97-L105
|
train
|
gliderlabs/ssh
|
ssh.go
|
KeysEqual
|
func KeysEqual(ak, bk PublicKey) bool {
//avoid panic if one of the keys is nil, return false instead
if ak == nil || bk == nil {
return false
}
a := ak.Marshal()
b := bk.Marshal()
return (len(a) == len(b) && subtle.ConstantTimeCompare(a, b) == 1)
}
|
go
|
func KeysEqual(ak, bk PublicKey) bool {
//avoid panic if one of the keys is nil, return false instead
if ak == nil || bk == nil {
return false
}
a := ak.Marshal()
b := bk.Marshal()
return (len(a) == len(b) && subtle.ConstantTimeCompare(a, b) == 1)
}
|
[
"func",
"KeysEqual",
"(",
"ak",
",",
"bk",
"PublicKey",
")",
"bool",
"{",
"//avoid panic if one of the keys is nil, return false instead",
"if",
"ak",
"==",
"nil",
"||",
"bk",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"a",
":=",
"ak",
".",
"Marshal",
"(",
")",
"\n",
"b",
":=",
"bk",
".",
"Marshal",
"(",
")",
"\n",
"return",
"(",
"len",
"(",
"a",
")",
"==",
"len",
"(",
"b",
")",
"&&",
"subtle",
".",
"ConstantTimeCompare",
"(",
"a",
",",
"b",
")",
"==",
"1",
")",
"\n",
"}"
] |
// KeysEqual is constant time compare of the keys to avoid timing attacks.
|
[
"KeysEqual",
"is",
"constant",
"time",
"compare",
"of",
"the",
"keys",
"to",
"avoid",
"timing",
"attacks",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/ssh.go#L113-L123
|
train
|
gliderlabs/ssh
|
options.go
|
PasswordAuth
|
func PasswordAuth(fn PasswordHandler) Option {
return func(srv *Server) error {
srv.PasswordHandler = fn
return nil
}
}
|
go
|
func PasswordAuth(fn PasswordHandler) Option {
return func(srv *Server) error {
srv.PasswordHandler = fn
return nil
}
}
|
[
"func",
"PasswordAuth",
"(",
"fn",
"PasswordHandler",
")",
"Option",
"{",
"return",
"func",
"(",
"srv",
"*",
"Server",
")",
"error",
"{",
"srv",
".",
"PasswordHandler",
"=",
"fn",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// PasswordAuth returns a functional option that sets PasswordHandler on the server.
|
[
"PasswordAuth",
"returns",
"a",
"functional",
"option",
"that",
"sets",
"PasswordHandler",
"on",
"the",
"server",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/options.go#L10-L15
|
train
|
gliderlabs/ssh
|
options.go
|
PublicKeyAuth
|
func PublicKeyAuth(fn PublicKeyHandler) Option {
return func(srv *Server) error {
srv.PublicKeyHandler = fn
return nil
}
}
|
go
|
func PublicKeyAuth(fn PublicKeyHandler) Option {
return func(srv *Server) error {
srv.PublicKeyHandler = fn
return nil
}
}
|
[
"func",
"PublicKeyAuth",
"(",
"fn",
"PublicKeyHandler",
")",
"Option",
"{",
"return",
"func",
"(",
"srv",
"*",
"Server",
")",
"error",
"{",
"srv",
".",
"PublicKeyHandler",
"=",
"fn",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// PublicKeyAuth returns a functional option that sets PublicKeyHandler on the server.
|
[
"PublicKeyAuth",
"returns",
"a",
"functional",
"option",
"that",
"sets",
"PublicKeyHandler",
"on",
"the",
"server",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/options.go#L18-L23
|
train
|
gliderlabs/ssh
|
options.go
|
HostKeyFile
|
func HostKeyFile(filepath string) Option {
return func(srv *Server) error {
pemBytes, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
signer, err := gossh.ParsePrivateKey(pemBytes)
if err != nil {
return err
}
srv.AddHostKey(signer)
return nil
}
}
|
go
|
func HostKeyFile(filepath string) Option {
return func(srv *Server) error {
pemBytes, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
signer, err := gossh.ParsePrivateKey(pemBytes)
if err != nil {
return err
}
srv.AddHostKey(signer)
return nil
}
}
|
[
"func",
"HostKeyFile",
"(",
"filepath",
"string",
")",
"Option",
"{",
"return",
"func",
"(",
"srv",
"*",
"Server",
")",
"error",
"{",
"pemBytes",
",",
"err",
":=",
"ioutil",
".",
"ReadFile",
"(",
"filepath",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"signer",
",",
"err",
":=",
"gossh",
".",
"ParsePrivateKey",
"(",
"pemBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"srv",
".",
"AddHostKey",
"(",
"signer",
")",
"\n\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// HostKeyFile returns a functional option that adds HostSigners to the server
// from a PEM file at filepath.
|
[
"HostKeyFile",
"returns",
"a",
"functional",
"option",
"that",
"adds",
"HostSigners",
"to",
"the",
"server",
"from",
"a",
"PEM",
"file",
"at",
"filepath",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/options.go#L27-L43
|
train
|
gliderlabs/ssh
|
options.go
|
HostKeyPEM
|
func HostKeyPEM(bytes []byte) Option {
return func(srv *Server) error {
signer, err := gossh.ParsePrivateKey(bytes)
if err != nil {
return err
}
srv.AddHostKey(signer)
return nil
}
}
|
go
|
func HostKeyPEM(bytes []byte) Option {
return func(srv *Server) error {
signer, err := gossh.ParsePrivateKey(bytes)
if err != nil {
return err
}
srv.AddHostKey(signer)
return nil
}
}
|
[
"func",
"HostKeyPEM",
"(",
"bytes",
"[",
"]",
"byte",
")",
"Option",
"{",
"return",
"func",
"(",
"srv",
"*",
"Server",
")",
"error",
"{",
"signer",
",",
"err",
":=",
"gossh",
".",
"ParsePrivateKey",
"(",
"bytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"srv",
".",
"AddHostKey",
"(",
"signer",
")",
"\n\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// HostKeyPEM returns a functional option that adds HostSigners to the server
// from a PEM file as bytes.
|
[
"HostKeyPEM",
"returns",
"a",
"functional",
"option",
"that",
"adds",
"HostSigners",
"to",
"the",
"server",
"from",
"a",
"PEM",
"file",
"as",
"bytes",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/options.go#L47-L58
|
train
|
gliderlabs/ssh
|
options.go
|
NoPty
|
func NoPty() Option {
return func(srv *Server) error {
srv.PtyCallback = func(ctx Context, pty Pty) bool {
return false
}
return nil
}
}
|
go
|
func NoPty() Option {
return func(srv *Server) error {
srv.PtyCallback = func(ctx Context, pty Pty) bool {
return false
}
return nil
}
}
|
[
"func",
"NoPty",
"(",
")",
"Option",
"{",
"return",
"func",
"(",
"srv",
"*",
"Server",
")",
"error",
"{",
"srv",
".",
"PtyCallback",
"=",
"func",
"(",
"ctx",
"Context",
",",
"pty",
"Pty",
")",
"bool",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// NoPty returns a functional option that sets PtyCallback to return false,
// denying PTY requests.
|
[
"NoPty",
"returns",
"a",
"functional",
"option",
"that",
"sets",
"PtyCallback",
"to",
"return",
"false",
"denying",
"PTY",
"requests",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/options.go#L62-L69
|
train
|
gliderlabs/ssh
|
options.go
|
WrapConn
|
func WrapConn(fn ConnCallback) Option {
return func(srv *Server) error {
srv.ConnCallback = fn
return nil
}
}
|
go
|
func WrapConn(fn ConnCallback) Option {
return func(srv *Server) error {
srv.ConnCallback = fn
return nil
}
}
|
[
"func",
"WrapConn",
"(",
"fn",
"ConnCallback",
")",
"Option",
"{",
"return",
"func",
"(",
"srv",
"*",
"Server",
")",
"error",
"{",
"srv",
".",
"ConnCallback",
"=",
"fn",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// WrapConn returns a functional option that sets ConnCallback on the server.
|
[
"WrapConn",
"returns",
"a",
"functional",
"option",
"that",
"sets",
"ConnCallback",
"on",
"the",
"server",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/options.go#L72-L77
|
train
|
gliderlabs/ssh
|
context.go
|
applyConnMetadata
|
func applyConnMetadata(ctx Context, conn gossh.ConnMetadata) {
if ctx.Value(ContextKeySessionID) != nil {
return
}
ctx.SetValue(ContextKeySessionID, hex.EncodeToString(conn.SessionID()))
ctx.SetValue(ContextKeyClientVersion, string(conn.ClientVersion()))
ctx.SetValue(ContextKeyServerVersion, string(conn.ServerVersion()))
ctx.SetValue(ContextKeyUser, conn.User())
ctx.SetValue(ContextKeyLocalAddr, conn.LocalAddr())
ctx.SetValue(ContextKeyRemoteAddr, conn.RemoteAddr())
}
|
go
|
func applyConnMetadata(ctx Context, conn gossh.ConnMetadata) {
if ctx.Value(ContextKeySessionID) != nil {
return
}
ctx.SetValue(ContextKeySessionID, hex.EncodeToString(conn.SessionID()))
ctx.SetValue(ContextKeyClientVersion, string(conn.ClientVersion()))
ctx.SetValue(ContextKeyServerVersion, string(conn.ServerVersion()))
ctx.SetValue(ContextKeyUser, conn.User())
ctx.SetValue(ContextKeyLocalAddr, conn.LocalAddr())
ctx.SetValue(ContextKeyRemoteAddr, conn.RemoteAddr())
}
|
[
"func",
"applyConnMetadata",
"(",
"ctx",
"Context",
",",
"conn",
"gossh",
".",
"ConnMetadata",
")",
"{",
"if",
"ctx",
".",
"Value",
"(",
"ContextKeySessionID",
")",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"ctx",
".",
"SetValue",
"(",
"ContextKeySessionID",
",",
"hex",
".",
"EncodeToString",
"(",
"conn",
".",
"SessionID",
"(",
")",
")",
")",
"\n",
"ctx",
".",
"SetValue",
"(",
"ContextKeyClientVersion",
",",
"string",
"(",
"conn",
".",
"ClientVersion",
"(",
")",
")",
")",
"\n",
"ctx",
".",
"SetValue",
"(",
"ContextKeyServerVersion",
",",
"string",
"(",
"conn",
".",
"ServerVersion",
"(",
")",
")",
")",
"\n",
"ctx",
".",
"SetValue",
"(",
"ContextKeyUser",
",",
"conn",
".",
"User",
"(",
")",
")",
"\n",
"ctx",
".",
"SetValue",
"(",
"ContextKeyLocalAddr",
",",
"conn",
".",
"LocalAddr",
"(",
")",
")",
"\n",
"ctx",
".",
"SetValue",
"(",
"ContextKeyRemoteAddr",
",",
"conn",
".",
"RemoteAddr",
"(",
")",
")",
"\n",
"}"
] |
// this is separate from newContext because we will get ConnMetadata
// at different points so it needs to be applied separately
|
[
"this",
"is",
"separate",
"from",
"newContext",
"because",
"we",
"will",
"get",
"ConnMetadata",
"at",
"different",
"points",
"so",
"it",
"needs",
"to",
"be",
"applied",
"separately"
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/context.go#L110-L120
|
train
|
gliderlabs/ssh
|
agent.go
|
NewAgentListener
|
func NewAgentListener() (net.Listener, error) {
dir, err := ioutil.TempDir("", agentTempDir)
if err != nil {
return nil, err
}
l, err := net.Listen("unix", path.Join(dir, agentListenFile))
if err != nil {
return nil, err
}
return l, nil
}
|
go
|
func NewAgentListener() (net.Listener, error) {
dir, err := ioutil.TempDir("", agentTempDir)
if err != nil {
return nil, err
}
l, err := net.Listen("unix", path.Join(dir, agentListenFile))
if err != nil {
return nil, err
}
return l, nil
}
|
[
"func",
"NewAgentListener",
"(",
")",
"(",
"net",
".",
"Listener",
",",
"error",
")",
"{",
"dir",
",",
"err",
":=",
"ioutil",
".",
"TempDir",
"(",
"\"",
"\"",
",",
"agentTempDir",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"l",
",",
"err",
":=",
"net",
".",
"Listen",
"(",
"\"",
"\"",
",",
"path",
".",
"Join",
"(",
"dir",
",",
"agentListenFile",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"l",
",",
"nil",
"\n",
"}"
] |
// NewAgentListener sets up a temporary Unix socket that can be communicated
// to the session environment and used for forwarding connections.
|
[
"NewAgentListener",
"sets",
"up",
"a",
"temporary",
"Unix",
"socket",
"that",
"can",
"be",
"communicated",
"to",
"the",
"session",
"environment",
"and",
"used",
"for",
"forwarding",
"connections",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/agent.go#L38-L48
|
train
|
gliderlabs/ssh
|
agent.go
|
ForwardAgentConnections
|
func ForwardAgentConnections(l net.Listener, s Session) {
sshConn := s.Context().Value(ContextKeyConn).(gossh.Conn)
for {
conn, err := l.Accept()
if err != nil {
return
}
go func(conn net.Conn) {
defer conn.Close()
channel, reqs, err := sshConn.OpenChannel(agentChannelType, nil)
if err != nil {
return
}
defer channel.Close()
go gossh.DiscardRequests(reqs)
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(conn, channel)
conn.(*net.UnixConn).CloseWrite()
wg.Done()
}()
go func() {
io.Copy(channel, conn)
channel.CloseWrite()
wg.Done()
}()
wg.Wait()
}(conn)
}
}
|
go
|
func ForwardAgentConnections(l net.Listener, s Session) {
sshConn := s.Context().Value(ContextKeyConn).(gossh.Conn)
for {
conn, err := l.Accept()
if err != nil {
return
}
go func(conn net.Conn) {
defer conn.Close()
channel, reqs, err := sshConn.OpenChannel(agentChannelType, nil)
if err != nil {
return
}
defer channel.Close()
go gossh.DiscardRequests(reqs)
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(conn, channel)
conn.(*net.UnixConn).CloseWrite()
wg.Done()
}()
go func() {
io.Copy(channel, conn)
channel.CloseWrite()
wg.Done()
}()
wg.Wait()
}(conn)
}
}
|
[
"func",
"ForwardAgentConnections",
"(",
"l",
"net",
".",
"Listener",
",",
"s",
"Session",
")",
"{",
"sshConn",
":=",
"s",
".",
"Context",
"(",
")",
".",
"Value",
"(",
"ContextKeyConn",
")",
".",
"(",
"gossh",
".",
"Conn",
")",
"\n",
"for",
"{",
"conn",
",",
"err",
":=",
"l",
".",
"Accept",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"go",
"func",
"(",
"conn",
"net",
".",
"Conn",
")",
"{",
"defer",
"conn",
".",
"Close",
"(",
")",
"\n",
"channel",
",",
"reqs",
",",
"err",
":=",
"sshConn",
".",
"OpenChannel",
"(",
"agentChannelType",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"defer",
"channel",
".",
"Close",
"(",
")",
"\n",
"go",
"gossh",
".",
"DiscardRequests",
"(",
"reqs",
")",
"\n",
"var",
"wg",
"sync",
".",
"WaitGroup",
"\n",
"wg",
".",
"Add",
"(",
"2",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"io",
".",
"Copy",
"(",
"conn",
",",
"channel",
")",
"\n",
"conn",
".",
"(",
"*",
"net",
".",
"UnixConn",
")",
".",
"CloseWrite",
"(",
")",
"\n",
"wg",
".",
"Done",
"(",
")",
"\n",
"}",
"(",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"io",
".",
"Copy",
"(",
"channel",
",",
"conn",
")",
"\n",
"channel",
".",
"CloseWrite",
"(",
")",
"\n",
"wg",
".",
"Done",
"(",
")",
"\n",
"}",
"(",
")",
"\n",
"wg",
".",
"Wait",
"(",
")",
"\n",
"}",
"(",
"conn",
")",
"\n",
"}",
"\n",
"}"
] |
// ForwardAgentConnections takes connections from a listener to proxy into the
// session on the OpenSSH channel for agent connections. It blocks and services
// connections until the listener stop accepting.
|
[
"ForwardAgentConnections",
"takes",
"connections",
"from",
"a",
"listener",
"to",
"proxy",
"into",
"the",
"session",
"on",
"the",
"OpenSSH",
"channel",
"for",
"agent",
"connections",
".",
"It",
"blocks",
"and",
"services",
"connections",
"until",
"the",
"listener",
"stop",
"accepting",
"."
] |
a9daacccc9f1368e52f390591d908470cee477d2
|
https://github.com/gliderlabs/ssh/blob/a9daacccc9f1368e52f390591d908470cee477d2/agent.go#L53-L83
|
train
|
aarzilli/nucular
|
masterwindow.go
|
PopupOpen
|
func (mw *masterWindowCommon) PopupOpen(title string, flags WindowFlags, rect rect.Rect, scale bool, updateFn UpdateFn) {
go func() {
mw.ctx.mw.Lock()
defer mw.ctx.mw.Unlock()
mw.ctx.popupOpen(title, flags, rect, scale, updateFn)
mw.ctx.mw.Changed()
}()
}
|
go
|
func (mw *masterWindowCommon) PopupOpen(title string, flags WindowFlags, rect rect.Rect, scale bool, updateFn UpdateFn) {
go func() {
mw.ctx.mw.Lock()
defer mw.ctx.mw.Unlock()
mw.ctx.popupOpen(title, flags, rect, scale, updateFn)
mw.ctx.mw.Changed()
}()
}
|
[
"func",
"(",
"mw",
"*",
"masterWindowCommon",
")",
"PopupOpen",
"(",
"title",
"string",
",",
"flags",
"WindowFlags",
",",
"rect",
"rect",
".",
"Rect",
",",
"scale",
"bool",
",",
"updateFn",
"UpdateFn",
")",
"{",
"go",
"func",
"(",
")",
"{",
"mw",
".",
"ctx",
".",
"mw",
".",
"Lock",
"(",
")",
"\n",
"defer",
"mw",
".",
"ctx",
".",
"mw",
".",
"Unlock",
"(",
")",
"\n",
"mw",
".",
"ctx",
".",
"popupOpen",
"(",
"title",
",",
"flags",
",",
"rect",
",",
"scale",
",",
"updateFn",
")",
"\n",
"mw",
".",
"ctx",
".",
"mw",
".",
"Changed",
"(",
")",
"\n",
"}",
"(",
")",
"\n",
"}"
] |
// Opens a popup window inside win. Will return true until the
// popup window is closed.
// The contents of the popup window will be updated by updateFn
|
[
"Opens",
"a",
"popup",
"window",
"inside",
"win",
".",
"Will",
"return",
"true",
"until",
"the",
"popup",
"window",
"is",
"closed",
".",
"The",
"contents",
"of",
"the",
"popup",
"window",
"will",
"be",
"updated",
"by",
"updateFn"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/masterwindow.go#L134-L141
|
train
|
aarzilli/nucular
|
masterwindow.go
|
drawChanged
|
func (w *masterWindowCommon) drawChanged() bool {
contextAllCommands(w.ctx)
w.ctx.Reset()
cmds := w.ctx.cmds
if len(cmds) != len(w.prevCmds) {
return true
}
for i := range cmds {
if cmds[i].Kind != w.prevCmds[i].Kind {
return true
}
cmd := &cmds[i]
pcmd := &w.prevCmds[i]
switch cmds[i].Kind {
case command.ScissorCmd:
if *pcmd != *cmd {
return true
}
case command.LineCmd:
if *pcmd != *cmd {
return true
}
case command.RectFilledCmd:
if i == 0 {
cmd.RectFilled.Color.A = 0xff
}
if *pcmd != *cmd {
return true
}
case command.TriangleFilledCmd:
if *pcmd != *cmd {
return true
}
case command.CircleFilledCmd:
if *pcmd != *cmd {
return true
}
case command.ImageCmd:
if *pcmd != *cmd {
return true
}
case command.TextCmd:
if *pcmd != *cmd {
return true
}
default:
panic(UnknownCommandErr)
}
}
return false
}
|
go
|
func (w *masterWindowCommon) drawChanged() bool {
contextAllCommands(w.ctx)
w.ctx.Reset()
cmds := w.ctx.cmds
if len(cmds) != len(w.prevCmds) {
return true
}
for i := range cmds {
if cmds[i].Kind != w.prevCmds[i].Kind {
return true
}
cmd := &cmds[i]
pcmd := &w.prevCmds[i]
switch cmds[i].Kind {
case command.ScissorCmd:
if *pcmd != *cmd {
return true
}
case command.LineCmd:
if *pcmd != *cmd {
return true
}
case command.RectFilledCmd:
if i == 0 {
cmd.RectFilled.Color.A = 0xff
}
if *pcmd != *cmd {
return true
}
case command.TriangleFilledCmd:
if *pcmd != *cmd {
return true
}
case command.CircleFilledCmd:
if *pcmd != *cmd {
return true
}
case command.ImageCmd:
if *pcmd != *cmd {
return true
}
case command.TextCmd:
if *pcmd != *cmd {
return true
}
default:
panic(UnknownCommandErr)
}
}
return false
}
|
[
"func",
"(",
"w",
"*",
"masterWindowCommon",
")",
"drawChanged",
"(",
")",
"bool",
"{",
"contextAllCommands",
"(",
"w",
".",
"ctx",
")",
"\n",
"w",
".",
"ctx",
".",
"Reset",
"(",
")",
"\n\n",
"cmds",
":=",
"w",
".",
"ctx",
".",
"cmds",
"\n\n",
"if",
"len",
"(",
"cmds",
")",
"!=",
"len",
"(",
"w",
".",
"prevCmds",
")",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"for",
"i",
":=",
"range",
"cmds",
"{",
"if",
"cmds",
"[",
"i",
"]",
".",
"Kind",
"!=",
"w",
".",
"prevCmds",
"[",
"i",
"]",
".",
"Kind",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"cmd",
":=",
"&",
"cmds",
"[",
"i",
"]",
"\n",
"pcmd",
":=",
"&",
"w",
".",
"prevCmds",
"[",
"i",
"]",
"\n\n",
"switch",
"cmds",
"[",
"i",
"]",
".",
"Kind",
"{",
"case",
"command",
".",
"ScissorCmd",
":",
"if",
"*",
"pcmd",
"!=",
"*",
"cmd",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"case",
"command",
".",
"LineCmd",
":",
"if",
"*",
"pcmd",
"!=",
"*",
"cmd",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"case",
"command",
".",
"RectFilledCmd",
":",
"if",
"i",
"==",
"0",
"{",
"cmd",
".",
"RectFilled",
".",
"Color",
".",
"A",
"=",
"0xff",
"\n",
"}",
"\n",
"if",
"*",
"pcmd",
"!=",
"*",
"cmd",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"case",
"command",
".",
"TriangleFilledCmd",
":",
"if",
"*",
"pcmd",
"!=",
"*",
"cmd",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"case",
"command",
".",
"CircleFilledCmd",
":",
"if",
"*",
"pcmd",
"!=",
"*",
"cmd",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"case",
"command",
".",
"ImageCmd",
":",
"if",
"*",
"pcmd",
"!=",
"*",
"cmd",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"case",
"command",
".",
"TextCmd",
":",
"if",
"*",
"pcmd",
"!=",
"*",
"cmd",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"default",
":",
"panic",
"(",
"UnknownCommandErr",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] |
// compares cmds to the last draw frame, returns true if there is a change
|
[
"compares",
"cmds",
"to",
"the",
"last",
"draw",
"frame",
"returns",
"true",
"if",
"there",
"is",
"a",
"change"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/masterwindow.go#L191-L255
|
train
|
aarzilli/nucular
|
shiny.go
|
NewMasterWindowSize
|
func NewMasterWindowSize(flags WindowFlags, title string, sz image.Point, updatefn UpdateFn) MasterWindow {
ctx := &context{}
wnd := &masterWindow{}
wnd.masterWindowCommonInit(ctx, flags, updatefn, wnd)
wnd.Title = title
wnd.initialSize = sz
clipboardMu.Lock()
if !clipboardStarted {
clipboardStarted = true
clipboard.Start()
}
clipboardMu.Unlock()
return wnd
}
|
go
|
func NewMasterWindowSize(flags WindowFlags, title string, sz image.Point, updatefn UpdateFn) MasterWindow {
ctx := &context{}
wnd := &masterWindow{}
wnd.masterWindowCommonInit(ctx, flags, updatefn, wnd)
wnd.Title = title
wnd.initialSize = sz
clipboardMu.Lock()
if !clipboardStarted {
clipboardStarted = true
clipboard.Start()
}
clipboardMu.Unlock()
return wnd
}
|
[
"func",
"NewMasterWindowSize",
"(",
"flags",
"WindowFlags",
",",
"title",
"string",
",",
"sz",
"image",
".",
"Point",
",",
"updatefn",
"UpdateFn",
")",
"MasterWindow",
"{",
"ctx",
":=",
"&",
"context",
"{",
"}",
"\n",
"wnd",
":=",
"&",
"masterWindow",
"{",
"}",
"\n\n",
"wnd",
".",
"masterWindowCommonInit",
"(",
"ctx",
",",
"flags",
",",
"updatefn",
",",
"wnd",
")",
"\n\n",
"wnd",
".",
"Title",
"=",
"title",
"\n",
"wnd",
".",
"initialSize",
"=",
"sz",
"\n\n",
"clipboardMu",
".",
"Lock",
"(",
")",
"\n",
"if",
"!",
"clipboardStarted",
"{",
"clipboardStarted",
"=",
"true",
"\n",
"clipboard",
".",
"Start",
"(",
")",
"\n",
"}",
"\n",
"clipboardMu",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"wnd",
"\n",
"}"
] |
// Creates new master window
|
[
"Creates",
"new",
"master",
"window"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/shiny.go#L52-L69
|
train
|
aarzilli/nucular
|
shiny.go
|
Close
|
func (mw *masterWindow) Close() {
mw.wnd.Send(lifecycle.Event{From: lifecycle.StageAlive, To: lifecycle.StageDead})
}
|
go
|
func (mw *masterWindow) Close() {
mw.wnd.Send(lifecycle.Event{From: lifecycle.StageAlive, To: lifecycle.StageDead})
}
|
[
"func",
"(",
"mw",
"*",
"masterWindow",
")",
"Close",
"(",
")",
"{",
"mw",
".",
"wnd",
".",
"Send",
"(",
"lifecycle",
".",
"Event",
"{",
"From",
":",
"lifecycle",
".",
"StageAlive",
",",
"To",
":",
"lifecycle",
".",
"StageDead",
"}",
")",
"\n",
"}"
] |
// Programmatically closes window.
|
[
"Programmatically",
"closes",
"window",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/shiny.go#L293-L295
|
train
|
aarzilli/nucular
|
shiny.go
|
Closed
|
func (mw *masterWindow) Closed() bool {
mw.uilock.Lock()
defer mw.uilock.Unlock()
return mw.closing
}
|
go
|
func (mw *masterWindow) Closed() bool {
mw.uilock.Lock()
defer mw.uilock.Unlock()
return mw.closing
}
|
[
"func",
"(",
"mw",
"*",
"masterWindow",
")",
"Closed",
"(",
")",
"bool",
"{",
"mw",
".",
"uilock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"mw",
".",
"uilock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"mw",
".",
"closing",
"\n",
"}"
] |
// Returns true if the window is closed.
|
[
"Returns",
"true",
"if",
"the",
"window",
"is",
"closed",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/shiny.go#L298-L302
|
train
|
aarzilli/nucular
|
text.go
|
Delete
|
func (edit *TextEditor) Delete(where int, len int) {
/* delete characters while updating undo */
edit.makeundoDelete(where, len)
edit.Buffer = strDeleteText(edit.Buffer, where, len)
edit.HasPreferredX = false
}
|
go
|
func (edit *TextEditor) Delete(where int, len int) {
/* delete characters while updating undo */
edit.makeundoDelete(where, len)
edit.Buffer = strDeleteText(edit.Buffer, where, len)
edit.HasPreferredX = false
}
|
[
"func",
"(",
"edit",
"*",
"TextEditor",
")",
"Delete",
"(",
"where",
"int",
",",
"len",
"int",
")",
"{",
"/* delete characters while updating undo */",
"edit",
".",
"makeundoDelete",
"(",
"where",
",",
"len",
")",
"\n\n",
"edit",
".",
"Buffer",
"=",
"strDeleteText",
"(",
"edit",
".",
"Buffer",
",",
"where",
",",
"len",
")",
"\n",
"edit",
".",
"HasPreferredX",
"=",
"false",
"\n",
"}"
] |
// Deletes a chunk of text in the editor.
|
[
"Deletes",
"a",
"chunk",
"of",
"text",
"in",
"the",
"editor",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L460-L466
|
train
|
aarzilli/nucular
|
text.go
|
DeleteSelection
|
func (edit *TextEditor) DeleteSelection() {
/* delete the section */
edit.clamp()
if edit.hasSelection() {
if edit.SelectStart < edit.SelectEnd {
edit.Delete(edit.SelectStart, edit.SelectEnd-edit.SelectStart)
edit.Cursor = edit.SelectStart
edit.SelectEnd = edit.Cursor
} else {
edit.Delete(edit.SelectEnd, edit.SelectStart-edit.SelectEnd)
edit.Cursor = edit.SelectEnd
edit.SelectStart = edit.Cursor
}
edit.HasPreferredX = false
}
}
|
go
|
func (edit *TextEditor) DeleteSelection() {
/* delete the section */
edit.clamp()
if edit.hasSelection() {
if edit.SelectStart < edit.SelectEnd {
edit.Delete(edit.SelectStart, edit.SelectEnd-edit.SelectStart)
edit.Cursor = edit.SelectStart
edit.SelectEnd = edit.Cursor
} else {
edit.Delete(edit.SelectEnd, edit.SelectStart-edit.SelectEnd)
edit.Cursor = edit.SelectEnd
edit.SelectStart = edit.Cursor
}
edit.HasPreferredX = false
}
}
|
[
"func",
"(",
"edit",
"*",
"TextEditor",
")",
"DeleteSelection",
"(",
")",
"{",
"/* delete the section */",
"edit",
".",
"clamp",
"(",
")",
"\n\n",
"if",
"edit",
".",
"hasSelection",
"(",
")",
"{",
"if",
"edit",
".",
"SelectStart",
"<",
"edit",
".",
"SelectEnd",
"{",
"edit",
".",
"Delete",
"(",
"edit",
".",
"SelectStart",
",",
"edit",
".",
"SelectEnd",
"-",
"edit",
".",
"SelectStart",
")",
"\n",
"edit",
".",
"Cursor",
"=",
"edit",
".",
"SelectStart",
"\n",
"edit",
".",
"SelectEnd",
"=",
"edit",
".",
"Cursor",
"\n",
"}",
"else",
"{",
"edit",
".",
"Delete",
"(",
"edit",
".",
"SelectEnd",
",",
"edit",
".",
"SelectStart",
"-",
"edit",
".",
"SelectEnd",
")",
"\n",
"edit",
".",
"Cursor",
"=",
"edit",
".",
"SelectEnd",
"\n",
"edit",
".",
"SelectStart",
"=",
"edit",
".",
"Cursor",
"\n",
"}",
"\n\n",
"edit",
".",
"HasPreferredX",
"=",
"false",
"\n",
"}",
"\n",
"}"
] |
// Deletes selection.
|
[
"Deletes",
"selection",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L469-L486
|
train
|
aarzilli/nucular
|
text.go
|
tonl
|
func (state *TextEditor) tonl(start int, dir int) int {
sz := len(state.Buffer)
i := start
if i < 0 {
return 0
}
if i >= sz {
i = sz - 1
}
for ; (i >= 0) && (i < sz); i += dir {
c := state.Buffer[i]
if c == '\n' {
if dir >= 0 {
return i
} else {
return i + 1
}
}
}
if dir < 0 {
return 0
} else {
return sz
}
}
|
go
|
func (state *TextEditor) tonl(start int, dir int) int {
sz := len(state.Buffer)
i := start
if i < 0 {
return 0
}
if i >= sz {
i = sz - 1
}
for ; (i >= 0) && (i < sz); i += dir {
c := state.Buffer[i]
if c == '\n' {
if dir >= 0 {
return i
} else {
return i + 1
}
}
}
if dir < 0 {
return 0
} else {
return sz
}
}
|
[
"func",
"(",
"state",
"*",
"TextEditor",
")",
"tonl",
"(",
"start",
"int",
",",
"dir",
"int",
")",
"int",
"{",
"sz",
":=",
"len",
"(",
"state",
".",
"Buffer",
")",
"\n\n",
"i",
":=",
"start",
"\n",
"if",
"i",
"<",
"0",
"{",
"return",
"0",
"\n",
"}",
"\n",
"if",
"i",
">=",
"sz",
"{",
"i",
"=",
"sz",
"-",
"1",
"\n",
"}",
"\n",
"for",
";",
"(",
"i",
">=",
"0",
")",
"&&",
"(",
"i",
"<",
"sz",
")",
";",
"i",
"+=",
"dir",
"{",
"c",
":=",
"state",
".",
"Buffer",
"[",
"i",
"]",
"\n\n",
"if",
"c",
"==",
"'\\n'",
"{",
"if",
"dir",
">=",
"0",
"{",
"return",
"i",
"\n",
"}",
"else",
"{",
"return",
"i",
"+",
"1",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"dir",
"<",
"0",
"{",
"return",
"0",
"\n",
"}",
"else",
"{",
"return",
"sz",
"\n",
"}",
"\n",
"}"
] |
// Moves to the beginning or end of a line
|
[
"Moves",
"to",
"the",
"beginning",
"or",
"end",
"of",
"a",
"line"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L519-L545
|
train
|
aarzilli/nucular
|
text.go
|
towd
|
func (state *TextEditor) towd(start int, dir int, dontForceAdvance bool) int {
first := (dir < 0)
notfirst := !first
var i int
for i = start; (i >= 0) && (i < len(state.Buffer)); i += dir {
c := state.Buffer[i]
if !(unicode.IsLetter(c) || unicode.IsDigit(c) || (c == '_')) {
if !first && !dontForceAdvance {
i++
}
break
}
first = notfirst
}
if i < 0 {
i = 0
}
return i
}
|
go
|
func (state *TextEditor) towd(start int, dir int, dontForceAdvance bool) int {
first := (dir < 0)
notfirst := !first
var i int
for i = start; (i >= 0) && (i < len(state.Buffer)); i += dir {
c := state.Buffer[i]
if !(unicode.IsLetter(c) || unicode.IsDigit(c) || (c == '_')) {
if !first && !dontForceAdvance {
i++
}
break
}
first = notfirst
}
if i < 0 {
i = 0
}
return i
}
|
[
"func",
"(",
"state",
"*",
"TextEditor",
")",
"towd",
"(",
"start",
"int",
",",
"dir",
"int",
",",
"dontForceAdvance",
"bool",
")",
"int",
"{",
"first",
":=",
"(",
"dir",
"<",
"0",
")",
"\n",
"notfirst",
":=",
"!",
"first",
"\n",
"var",
"i",
"int",
"\n",
"for",
"i",
"=",
"start",
";",
"(",
"i",
">=",
"0",
")",
"&&",
"(",
"i",
"<",
"len",
"(",
"state",
".",
"Buffer",
")",
")",
";",
"i",
"+=",
"dir",
"{",
"c",
":=",
"state",
".",
"Buffer",
"[",
"i",
"]",
"\n",
"if",
"!",
"(",
"unicode",
".",
"IsLetter",
"(",
"c",
")",
"||",
"unicode",
".",
"IsDigit",
"(",
"c",
")",
"||",
"(",
"c",
"==",
"'_'",
")",
")",
"{",
"if",
"!",
"first",
"&&",
"!",
"dontForceAdvance",
"{",
"i",
"++",
"\n",
"}",
"\n",
"break",
"\n",
"}",
"\n",
"first",
"=",
"notfirst",
"\n",
"}",
"\n",
"if",
"i",
"<",
"0",
"{",
"i",
"=",
"0",
"\n",
"}",
"\n",
"return",
"i",
"\n",
"}"
] |
// Moves to the beginning or end of an alphanumerically delimited word
|
[
"Moves",
"to",
"the",
"beginning",
"or",
"end",
"of",
"an",
"alphanumerically",
"delimited",
"word"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L548-L566
|
train
|
aarzilli/nucular
|
text.go
|
tospc
|
func (state *TextEditor) tospc(start int, dir int) int {
return state.tof(start, dir, unicode.IsSpace)
}
|
go
|
func (state *TextEditor) tospc(start int, dir int) int {
return state.tof(start, dir, unicode.IsSpace)
}
|
[
"func",
"(",
"state",
"*",
"TextEditor",
")",
"tospc",
"(",
"start",
"int",
",",
"dir",
"int",
")",
"int",
"{",
"return",
"state",
".",
"tof",
"(",
"start",
",",
"dir",
",",
"unicode",
".",
"IsSpace",
")",
"\n",
"}"
] |
// Moves to the beginning or end of a space delimited word
|
[
"Moves",
"to",
"the",
"beginning",
"or",
"end",
"of",
"a",
"space",
"delimited",
"word"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L569-L571
|
train
|
aarzilli/nucular
|
text.go
|
tof
|
func (state *TextEditor) tof(start int, dir int, f func(rune) bool) int {
first := (dir < 0)
notfirst := !first
var i int
for i = start; (i >= 0) && (i < len(state.Buffer)); i += dir {
c := state.Buffer[i]
if f(c) {
if !first {
i++
}
break
}
first = notfirst
}
if i < 0 {
i = 0
}
return i
}
|
go
|
func (state *TextEditor) tof(start int, dir int, f func(rune) bool) int {
first := (dir < 0)
notfirst := !first
var i int
for i = start; (i >= 0) && (i < len(state.Buffer)); i += dir {
c := state.Buffer[i]
if f(c) {
if !first {
i++
}
break
}
first = notfirst
}
if i < 0 {
i = 0
}
return i
}
|
[
"func",
"(",
"state",
"*",
"TextEditor",
")",
"tof",
"(",
"start",
"int",
",",
"dir",
"int",
",",
"f",
"func",
"(",
"rune",
")",
"bool",
")",
"int",
"{",
"first",
":=",
"(",
"dir",
"<",
"0",
")",
"\n",
"notfirst",
":=",
"!",
"first",
"\n",
"var",
"i",
"int",
"\n",
"for",
"i",
"=",
"start",
";",
"(",
"i",
">=",
"0",
")",
"&&",
"(",
"i",
"<",
"len",
"(",
"state",
".",
"Buffer",
")",
")",
";",
"i",
"+=",
"dir",
"{",
"c",
":=",
"state",
".",
"Buffer",
"[",
"i",
"]",
"\n",
"if",
"f",
"(",
"c",
")",
"{",
"if",
"!",
"first",
"{",
"i",
"++",
"\n",
"}",
"\n",
"break",
"\n",
"}",
"\n",
"first",
"=",
"notfirst",
"\n",
"}",
"\n",
"if",
"i",
"<",
"0",
"{",
"i",
"=",
"0",
"\n",
"}",
"\n",
"return",
"i",
"\n",
"}"
] |
// Moves to the first position where f returns true
|
[
"Moves",
"to",
"the",
"first",
"position",
"where",
"f",
"returns",
"true"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L574-L592
|
train
|
aarzilli/nucular
|
text.go
|
tofp
|
func (state *TextEditor) tofp(start int, dir int) int {
first := (dir < 0)
notfirst := !first
var i int
for i = start; (i >= 0) && (i < len(state.Buffer)); i += dir {
c := state.Buffer[i]
if !(unicode.IsLetter(c) || unicode.IsDigit(c) || (c == '_') || (c == '-') || (c == '+') || (c == '/') || (c == '=') || (c == '~') || (c == '!') || (c == ':') || (c == ',') || (c == '.')) {
if !first {
i++
}
break
}
first = notfirst
}
if i < 0 {
i = 0
}
return i
}
|
go
|
func (state *TextEditor) tofp(start int, dir int) int {
first := (dir < 0)
notfirst := !first
var i int
for i = start; (i >= 0) && (i < len(state.Buffer)); i += dir {
c := state.Buffer[i]
if !(unicode.IsLetter(c) || unicode.IsDigit(c) || (c == '_') || (c == '-') || (c == '+') || (c == '/') || (c == '=') || (c == '~') || (c == '!') || (c == ':') || (c == ',') || (c == '.')) {
if !first {
i++
}
break
}
first = notfirst
}
if i < 0 {
i = 0
}
return i
}
|
[
"func",
"(",
"state",
"*",
"TextEditor",
")",
"tofp",
"(",
"start",
"int",
",",
"dir",
"int",
")",
"int",
"{",
"first",
":=",
"(",
"dir",
"<",
"0",
")",
"\n",
"notfirst",
":=",
"!",
"first",
"\n",
"var",
"i",
"int",
"\n",
"for",
"i",
"=",
"start",
";",
"(",
"i",
">=",
"0",
")",
"&&",
"(",
"i",
"<",
"len",
"(",
"state",
".",
"Buffer",
")",
")",
";",
"i",
"+=",
"dir",
"{",
"c",
":=",
"state",
".",
"Buffer",
"[",
"i",
"]",
"\n",
"if",
"!",
"(",
"unicode",
".",
"IsLetter",
"(",
"c",
")",
"||",
"unicode",
".",
"IsDigit",
"(",
"c",
")",
"||",
"(",
"c",
"==",
"'_'",
")",
"||",
"(",
"c",
"==",
"'-'",
")",
"||",
"(",
"c",
"==",
"'+'",
")",
"||",
"(",
"c",
"==",
"'/'",
")",
"||",
"(",
"c",
"==",
"'='",
")",
"||",
"(",
"c",
"==",
"'~'",
")",
"||",
"(",
"c",
"==",
"'!'",
")",
"||",
"(",
"c",
"==",
"':'",
")",
"||",
"(",
"c",
"==",
"','",
")",
"||",
"(",
"c",
"==",
"'.'",
")",
")",
"{",
"if",
"!",
"first",
"{",
"i",
"++",
"\n",
"}",
"\n",
"break",
"\n",
"}",
"\n",
"first",
"=",
"notfirst",
"\n",
"}",
"\n",
"if",
"i",
"<",
"0",
"{",
"i",
"=",
"0",
"\n",
"}",
"\n",
"return",
"i",
"\n\n",
"}"
] |
// Moves to the beginning or end of something that looks like a file path
|
[
"Moves",
"to",
"the",
"beginning",
"or",
"end",
"of",
"something",
"that",
"looks",
"like",
"a",
"file",
"path"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L595-L614
|
train
|
aarzilli/nucular
|
text.go
|
Paste
|
func (edit *TextEditor) Paste(ctext string) {
if edit.Flags&EditReadOnly != 0 {
return
}
/* if there's a selection, the paste should delete it */
edit.clamp()
edit.DeleteSelection()
text := []rune(ctext)
edit.Buffer = strInsertText(edit.Buffer, edit.Cursor, text)
edit.makeundoInsert(edit.Cursor, len(text))
edit.Cursor += len(text)
edit.HasPreferredX = false
}
|
go
|
func (edit *TextEditor) Paste(ctext string) {
if edit.Flags&EditReadOnly != 0 {
return
}
/* if there's a selection, the paste should delete it */
edit.clamp()
edit.DeleteSelection()
text := []rune(ctext)
edit.Buffer = strInsertText(edit.Buffer, edit.Cursor, text)
edit.makeundoInsert(edit.Cursor, len(text))
edit.Cursor += len(text)
edit.HasPreferredX = false
}
|
[
"func",
"(",
"edit",
"*",
"TextEditor",
")",
"Paste",
"(",
"ctext",
"string",
")",
"{",
"if",
"edit",
".",
"Flags",
"&",
"EditReadOnly",
"!=",
"0",
"{",
"return",
"\n",
"}",
"\n\n",
"/* if there's a selection, the paste should delete it */",
"edit",
".",
"clamp",
"(",
")",
"\n\n",
"edit",
".",
"DeleteSelection",
"(",
")",
"\n\n",
"text",
":=",
"[",
"]",
"rune",
"(",
"ctext",
")",
"\n\n",
"edit",
".",
"Buffer",
"=",
"strInsertText",
"(",
"edit",
".",
"Buffer",
",",
"edit",
".",
"Cursor",
",",
"text",
")",
"\n\n",
"edit",
".",
"makeundoInsert",
"(",
"edit",
".",
"Cursor",
",",
"len",
"(",
"text",
")",
")",
"\n",
"edit",
".",
"Cursor",
"+=",
"len",
"(",
"text",
")",
"\n",
"edit",
".",
"HasPreferredX",
"=",
"false",
"\n",
"}"
] |
// Paste from clipboard
|
[
"Paste",
"from",
"clipboard"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L641-L658
|
train
|
aarzilli/nucular
|
text.go
|
Edit
|
func (edit *TextEditor) Edit(win *Window) EditEvents {
edit.init(win)
if edit.Maxlen > 0 {
if len(edit.Buffer) > edit.Maxlen {
edit.Buffer = edit.Buffer[:edit.Maxlen]
}
}
if edit.Flags&EditNoCursor != 0 {
edit.Cursor = len(edit.Buffer)
}
if edit.Flags&EditSelectable == 0 {
edit.SelectStart = edit.Cursor
edit.SelectEnd = edit.Cursor
}
var bounds rect.Rect
style := &edit.win.ctx.Style
widget_state, bounds, _ := edit.win.widget()
if !widget_state {
return 0
}
in := edit.win.inputMaybe(widget_state)
var cut, copy, paste bool
if w := win.ContextualOpen(0, image.Point{}, bounds, nil); w != nil {
w.Row(20).Dynamic(1)
visible := false
if edit.Flags&EditClipboard != 0 {
visible = true
if w.MenuItem(label.TA("Cut", "LC")) {
cut = true
}
if w.MenuItem(label.TA("Copy", "LC")) {
copy = true
}
if w.MenuItem(label.TA("Paste", "LC")) {
paste = true
}
}
if edit.Flags&EditMultiline != 0 {
visible = true
if w.MenuItem(label.TA("Find...", "LC")) {
edit.popupFind()
}
}
if !visible {
w.Close()
}
}
ev := edit.doEdit(bounds, &style.Edit, in, cut, copy, paste)
return ev
}
|
go
|
func (edit *TextEditor) Edit(win *Window) EditEvents {
edit.init(win)
if edit.Maxlen > 0 {
if len(edit.Buffer) > edit.Maxlen {
edit.Buffer = edit.Buffer[:edit.Maxlen]
}
}
if edit.Flags&EditNoCursor != 0 {
edit.Cursor = len(edit.Buffer)
}
if edit.Flags&EditSelectable == 0 {
edit.SelectStart = edit.Cursor
edit.SelectEnd = edit.Cursor
}
var bounds rect.Rect
style := &edit.win.ctx.Style
widget_state, bounds, _ := edit.win.widget()
if !widget_state {
return 0
}
in := edit.win.inputMaybe(widget_state)
var cut, copy, paste bool
if w := win.ContextualOpen(0, image.Point{}, bounds, nil); w != nil {
w.Row(20).Dynamic(1)
visible := false
if edit.Flags&EditClipboard != 0 {
visible = true
if w.MenuItem(label.TA("Cut", "LC")) {
cut = true
}
if w.MenuItem(label.TA("Copy", "LC")) {
copy = true
}
if w.MenuItem(label.TA("Paste", "LC")) {
paste = true
}
}
if edit.Flags&EditMultiline != 0 {
visible = true
if w.MenuItem(label.TA("Find...", "LC")) {
edit.popupFind()
}
}
if !visible {
w.Close()
}
}
ev := edit.doEdit(bounds, &style.Edit, in, cut, copy, paste)
return ev
}
|
[
"func",
"(",
"edit",
"*",
"TextEditor",
")",
"Edit",
"(",
"win",
"*",
"Window",
")",
"EditEvents",
"{",
"edit",
".",
"init",
"(",
"win",
")",
"\n",
"if",
"edit",
".",
"Maxlen",
">",
"0",
"{",
"if",
"len",
"(",
"edit",
".",
"Buffer",
")",
">",
"edit",
".",
"Maxlen",
"{",
"edit",
".",
"Buffer",
"=",
"edit",
".",
"Buffer",
"[",
":",
"edit",
".",
"Maxlen",
"]",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"edit",
".",
"Flags",
"&",
"EditNoCursor",
"!=",
"0",
"{",
"edit",
".",
"Cursor",
"=",
"len",
"(",
"edit",
".",
"Buffer",
")",
"\n",
"}",
"\n",
"if",
"edit",
".",
"Flags",
"&",
"EditSelectable",
"==",
"0",
"{",
"edit",
".",
"SelectStart",
"=",
"edit",
".",
"Cursor",
"\n",
"edit",
".",
"SelectEnd",
"=",
"edit",
".",
"Cursor",
"\n",
"}",
"\n\n",
"var",
"bounds",
"rect",
".",
"Rect",
"\n\n",
"style",
":=",
"&",
"edit",
".",
"win",
".",
"ctx",
".",
"Style",
"\n",
"widget_state",
",",
"bounds",
",",
"_",
":=",
"edit",
".",
"win",
".",
"widget",
"(",
")",
"\n",
"if",
"!",
"widget_state",
"{",
"return",
"0",
"\n",
"}",
"\n",
"in",
":=",
"edit",
".",
"win",
".",
"inputMaybe",
"(",
"widget_state",
")",
"\n\n",
"var",
"cut",
",",
"copy",
",",
"paste",
"bool",
"\n\n",
"if",
"w",
":=",
"win",
".",
"ContextualOpen",
"(",
"0",
",",
"image",
".",
"Point",
"{",
"}",
",",
"bounds",
",",
"nil",
")",
";",
"w",
"!=",
"nil",
"{",
"w",
".",
"Row",
"(",
"20",
")",
".",
"Dynamic",
"(",
"1",
")",
"\n",
"visible",
":=",
"false",
"\n",
"if",
"edit",
".",
"Flags",
"&",
"EditClipboard",
"!=",
"0",
"{",
"visible",
"=",
"true",
"\n",
"if",
"w",
".",
"MenuItem",
"(",
"label",
".",
"TA",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
")",
"{",
"cut",
"=",
"true",
"\n",
"}",
"\n",
"if",
"w",
".",
"MenuItem",
"(",
"label",
".",
"TA",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
")",
"{",
"copy",
"=",
"true",
"\n",
"}",
"\n",
"if",
"w",
".",
"MenuItem",
"(",
"label",
".",
"TA",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
")",
"{",
"paste",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"edit",
".",
"Flags",
"&",
"EditMultiline",
"!=",
"0",
"{",
"visible",
"=",
"true",
"\n",
"if",
"w",
".",
"MenuItem",
"(",
"label",
".",
"TA",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
")",
"{",
"edit",
".",
"popupFind",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"!",
"visible",
"{",
"w",
".",
"Close",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"ev",
":=",
"edit",
".",
"doEdit",
"(",
"bounds",
",",
"&",
"style",
".",
"Edit",
",",
"in",
",",
"cut",
",",
"copy",
",",
"paste",
")",
"\n",
"return",
"ev",
"\n",
"}"
] |
// Adds text editor edit to win.
// Initial contents of the text editor will be set to text. If
// alwaysSet is specified the contents of the editor will be reset
// to text.
|
[
"Adds",
"text",
"editor",
"edit",
"to",
"win",
".",
"Initial",
"contents",
"of",
"the",
"text",
"editor",
"will",
"be",
"set",
"to",
"text",
".",
"If",
"alwaysSet",
"is",
"specified",
"the",
"contents",
"of",
"the",
"editor",
"will",
"be",
"reset",
"to",
"text",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/text.go#L1780-L1835
|
train
|
aarzilli/nucular
|
context.go
|
SetColor
|
func (r *myRGBAPainter) SetColor(c color.Color) {
r.cr, r.cg, r.cb, r.ca = c.RGBA()
}
|
go
|
func (r *myRGBAPainter) SetColor(c color.Color) {
r.cr, r.cg, r.cb, r.ca = c.RGBA()
}
|
[
"func",
"(",
"r",
"*",
"myRGBAPainter",
")",
"SetColor",
"(",
"c",
"color",
".",
"Color",
")",
"{",
"r",
".",
"cr",
",",
"r",
".",
"cg",
",",
"r",
".",
"cb",
",",
"r",
".",
"ca",
"=",
"c",
".",
"RGBA",
"(",
")",
"\n",
"}"
] |
// SetColor sets the color to paint the spans.
|
[
"SetColor",
"sets",
"the",
"color",
"to",
"paint",
"the",
"spans",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/context.go#L724-L726
|
train
|
aarzilli/nucular
|
nucular.go
|
MenubarBegin
|
func (win *Window) MenubarBegin() {
layout := win.layout
layout.Menu.X = layout.AtX
layout.Menu.Y = layout.Bounds.Y + layout.HeaderH
layout.Menu.W = layout.Width
layout.Menu.Offset = *layout.Offset
layout.Offset.Y = 0
}
|
go
|
func (win *Window) MenubarBegin() {
layout := win.layout
layout.Menu.X = layout.AtX
layout.Menu.Y = layout.Bounds.Y + layout.HeaderH
layout.Menu.W = layout.Width
layout.Menu.Offset = *layout.Offset
layout.Offset.Y = 0
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"MenubarBegin",
"(",
")",
"{",
"layout",
":=",
"win",
".",
"layout",
"\n\n",
"layout",
".",
"Menu",
".",
"X",
"=",
"layout",
".",
"AtX",
"\n",
"layout",
".",
"Menu",
".",
"Y",
"=",
"layout",
".",
"Bounds",
".",
"Y",
"+",
"layout",
".",
"HeaderH",
"\n",
"layout",
".",
"Menu",
".",
"W",
"=",
"layout",
".",
"Width",
"\n",
"layout",
".",
"Menu",
".",
"Offset",
"=",
"*",
"layout",
".",
"Offset",
"\n",
"layout",
".",
"Offset",
".",
"Y",
"=",
"0",
"\n",
"}"
] |
// MenubarBegin adds a menubar to the current window.
// A menubar is an area displayed at the top of the window that is unaffected by scrolling.
// Remember to call MenubarEnd when you are done adding elements to the menubar.
|
[
"MenubarBegin",
"adds",
"a",
"menubar",
"to",
"the",
"current",
"window",
".",
"A",
"menubar",
"is",
"an",
"area",
"displayed",
"at",
"the",
"top",
"of",
"the",
"window",
"that",
"is",
"unaffected",
"by",
"scrolling",
".",
"Remember",
"to",
"call",
"MenubarEnd",
"when",
"you",
"are",
"done",
"adding",
"elements",
"to",
"the",
"menubar",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L704-L712
|
train
|
aarzilli/nucular
|
nucular.go
|
MenubarEnd
|
func (win *Window) MenubarEnd() {
layout := win.layout
layout.Menu.H = layout.AtY - layout.Menu.Y
layout.Clip.Y = layout.Bounds.Y + layout.HeaderH + layout.Menu.H + layout.Row.Height
layout.Height -= layout.Menu.H
*layout.Offset = layout.Menu.Offset
layout.Clip.H -= layout.Menu.H + layout.Row.Height
layout.AtY = layout.Menu.Y + layout.Menu.H
win.cmds.PushScissor(layout.Clip)
}
|
go
|
func (win *Window) MenubarEnd() {
layout := win.layout
layout.Menu.H = layout.AtY - layout.Menu.Y
layout.Clip.Y = layout.Bounds.Y + layout.HeaderH + layout.Menu.H + layout.Row.Height
layout.Height -= layout.Menu.H
*layout.Offset = layout.Menu.Offset
layout.Clip.H -= layout.Menu.H + layout.Row.Height
layout.AtY = layout.Menu.Y + layout.Menu.H
win.cmds.PushScissor(layout.Clip)
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"MenubarEnd",
"(",
")",
"{",
"layout",
":=",
"win",
".",
"layout",
"\n\n",
"layout",
".",
"Menu",
".",
"H",
"=",
"layout",
".",
"AtY",
"-",
"layout",
".",
"Menu",
".",
"Y",
"\n",
"layout",
".",
"Clip",
".",
"Y",
"=",
"layout",
".",
"Bounds",
".",
"Y",
"+",
"layout",
".",
"HeaderH",
"+",
"layout",
".",
"Menu",
".",
"H",
"+",
"layout",
".",
"Row",
".",
"Height",
"\n",
"layout",
".",
"Height",
"-=",
"layout",
".",
"Menu",
".",
"H",
"\n",
"*",
"layout",
".",
"Offset",
"=",
"layout",
".",
"Menu",
".",
"Offset",
"\n",
"layout",
".",
"Clip",
".",
"H",
"-=",
"layout",
".",
"Menu",
".",
"H",
"+",
"layout",
".",
"Row",
".",
"Height",
"\n",
"layout",
".",
"AtY",
"=",
"layout",
".",
"Menu",
".",
"Y",
"+",
"layout",
".",
"Menu",
".",
"H",
"\n",
"win",
".",
"cmds",
".",
"PushScissor",
"(",
"layout",
".",
"Clip",
")",
"\n",
"}"
] |
// MenubarEnd signals that all widgets have been added to the menubar.
|
[
"MenubarEnd",
"signals",
"that",
"all",
"widgets",
"have",
"been",
"added",
"to",
"the",
"menubar",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L745-L755
|
train
|
aarzilli/nucular
|
nucular.go
|
LayoutReserveRow
|
func (win *Window) LayoutReserveRow(height int, num int) {
win.LayoutReserveRowScaled(win.ctx.scale(height), num)
}
|
go
|
func (win *Window) LayoutReserveRow(height int, num int) {
win.LayoutReserveRowScaled(win.ctx.scale(height), num)
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"LayoutReserveRow",
"(",
"height",
"int",
",",
"num",
"int",
")",
"{",
"win",
".",
"LayoutReserveRowScaled",
"(",
"win",
".",
"ctx",
".",
"scale",
"(",
"height",
")",
",",
"num",
")",
"\n",
"}"
] |
// Reserves space for num rows of the specified height at the bottom
// of the panel.
// If a row of height == 0 is inserted it will take reserved space
// into account.
|
[
"Reserves",
"space",
"for",
"num",
"rows",
"of",
"the",
"specified",
"height",
"at",
"the",
"bottom",
"of",
"the",
"panel",
".",
"If",
"a",
"row",
"of",
"height",
"==",
"0",
"is",
"inserted",
"it",
"will",
"take",
"reserved",
"space",
"into",
"account",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L995-L997
|
train
|
aarzilli/nucular
|
nucular.go
|
LayoutReserveRowScaled
|
func (win *Window) LayoutReserveRowScaled(height int, num int) {
win.layout.ReservedHeight += height*num + win.style().Spacing.Y*num
}
|
go
|
func (win *Window) LayoutReserveRowScaled(height int, num int) {
win.layout.ReservedHeight += height*num + win.style().Spacing.Y*num
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"LayoutReserveRowScaled",
"(",
"height",
"int",
",",
"num",
"int",
")",
"{",
"win",
".",
"layout",
".",
"ReservedHeight",
"+=",
"height",
"*",
"num",
"+",
"win",
".",
"style",
"(",
")",
".",
"Spacing",
".",
"Y",
"*",
"num",
"\n",
"}"
] |
// Like LayoutReserveRow but with a scaled height.
|
[
"Like",
"LayoutReserveRow",
"but",
"with",
"a",
"scaled",
"height",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1000-L1002
|
train
|
aarzilli/nucular
|
nucular.go
|
RowScaled
|
func (win *Window) RowScaled(height int) *rowConstructor {
win.rowCtor.height = height
return &win.rowCtor
}
|
go
|
func (win *Window) RowScaled(height int) *rowConstructor {
win.rowCtor.height = height
return &win.rowCtor
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"RowScaled",
"(",
"height",
"int",
")",
"*",
"rowConstructor",
"{",
"win",
".",
"rowCtor",
".",
"height",
"=",
"height",
"\n",
"return",
"&",
"win",
".",
"rowCtor",
"\n",
"}"
] |
// Same as Row but with scaled units.
|
[
"Same",
"as",
"Row",
"but",
"with",
"scaled",
"units",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1014-L1017
|
train
|
aarzilli/nucular
|
nucular.go
|
Dynamic
|
func (ctr *rowConstructor) Dynamic(cols int) {
rowLayoutCtr(ctr.win, ctr.height, cols, 0)
}
|
go
|
func (ctr *rowConstructor) Dynamic(cols int) {
rowLayoutCtr(ctr.win, ctr.height, cols, 0)
}
|
[
"func",
"(",
"ctr",
"*",
"rowConstructor",
")",
"Dynamic",
"(",
"cols",
"int",
")",
"{",
"rowLayoutCtr",
"(",
"ctr",
".",
"win",
",",
"ctr",
".",
"height",
",",
"cols",
",",
"0",
")",
"\n",
"}"
] |
// Starts new row that has cols columns of equal width that automatically
// resize to fill the available space.
|
[
"Starts",
"new",
"row",
"that",
"has",
"cols",
"columns",
"of",
"equal",
"width",
"that",
"automatically",
"resize",
"to",
"fill",
"the",
"available",
"space",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1026-L1028
|
train
|
aarzilli/nucular
|
nucular.go
|
Ratio
|
func (ctr *rowConstructor) Ratio(ratio ...float64) {
layout := ctr.win.layout
panelLayout(ctr.win.ctx, ctr.win, ctr.height, len(ratio), 0)
/* calculate width of undefined widget ratios */
r := 0.0
n_undef := 0
layout.Row.Ratio = ratio
for i := range ratio {
if ratio[i] < 0.0 {
n_undef++
} else {
r += ratio[i]
}
}
r = saturateFloat(1.0 - r)
layout.Row.Type = layoutDynamic
layout.Row.ItemWidth = 0
layout.Row.ItemRatio = 0.0
if r > 0 && n_undef > 0 {
layout.Row.ItemRatio = (r / float64(n_undef))
}
layout.Row.ItemOffset = 0
layout.Row.Filled = 0
}
|
go
|
func (ctr *rowConstructor) Ratio(ratio ...float64) {
layout := ctr.win.layout
panelLayout(ctr.win.ctx, ctr.win, ctr.height, len(ratio), 0)
/* calculate width of undefined widget ratios */
r := 0.0
n_undef := 0
layout.Row.Ratio = ratio
for i := range ratio {
if ratio[i] < 0.0 {
n_undef++
} else {
r += ratio[i]
}
}
r = saturateFloat(1.0 - r)
layout.Row.Type = layoutDynamic
layout.Row.ItemWidth = 0
layout.Row.ItemRatio = 0.0
if r > 0 && n_undef > 0 {
layout.Row.ItemRatio = (r / float64(n_undef))
}
layout.Row.ItemOffset = 0
layout.Row.Filled = 0
}
|
[
"func",
"(",
"ctr",
"*",
"rowConstructor",
")",
"Ratio",
"(",
"ratio",
"...",
"float64",
")",
"{",
"layout",
":=",
"ctr",
".",
"win",
".",
"layout",
"\n",
"panelLayout",
"(",
"ctr",
".",
"win",
".",
"ctx",
",",
"ctr",
".",
"win",
",",
"ctr",
".",
"height",
",",
"len",
"(",
"ratio",
")",
",",
"0",
")",
"\n\n",
"/* calculate width of undefined widget ratios */",
"r",
":=",
"0.0",
"\n",
"n_undef",
":=",
"0",
"\n",
"layout",
".",
"Row",
".",
"Ratio",
"=",
"ratio",
"\n",
"for",
"i",
":=",
"range",
"ratio",
"{",
"if",
"ratio",
"[",
"i",
"]",
"<",
"0.0",
"{",
"n_undef",
"++",
"\n",
"}",
"else",
"{",
"r",
"+=",
"ratio",
"[",
"i",
"]",
"\n",
"}",
"\n",
"}",
"\n\n",
"r",
"=",
"saturateFloat",
"(",
"1.0",
"-",
"r",
")",
"\n",
"layout",
".",
"Row",
".",
"Type",
"=",
"layoutDynamic",
"\n",
"layout",
".",
"Row",
".",
"ItemWidth",
"=",
"0",
"\n",
"layout",
".",
"Row",
".",
"ItemRatio",
"=",
"0.0",
"\n",
"if",
"r",
">",
"0",
"&&",
"n_undef",
">",
"0",
"{",
"layout",
".",
"Row",
".",
"ItemRatio",
"=",
"(",
"r",
"/",
"float64",
"(",
"n_undef",
")",
")",
"\n",
"}",
"\n\n",
"layout",
".",
"Row",
".",
"ItemOffset",
"=",
"0",
"\n",
"layout",
".",
"Row",
".",
"Filled",
"=",
"0",
"\n\n",
"}"
] |
// Starts new row with a fixed number of columns of width proportional
// to the size of the window.
|
[
"Starts",
"new",
"row",
"with",
"a",
"fixed",
"number",
"of",
"columns",
"of",
"width",
"proportional",
"to",
"the",
"size",
"of",
"the",
"window",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1032-L1059
|
train
|
aarzilli/nucular
|
nucular.go
|
StaticScaled
|
func (ctr *rowConstructor) StaticScaled(width ...int) {
layout := ctr.win.layout
cnt := 0
if len(width) == 0 {
if len(layout.Row.WidthArr) == 0 {
cnt = layout.Cnt
} else {
cnt = ctr.win.lastLayoutCnt + 1
ctr.win.lastLayoutCnt = cnt
}
}
panelLayout(ctr.win.ctx, ctr.win, ctr.height, len(width), cnt)
ctr.win.staticZeros(width)
layout.Row.WidthArr = width
layout.Row.Type = layoutStatic
layout.Row.ItemWidth = 0
layout.Row.ItemRatio = 0.0
layout.Row.ItemOffset = 0
layout.Row.Filled = 0
}
|
go
|
func (ctr *rowConstructor) StaticScaled(width ...int) {
layout := ctr.win.layout
cnt := 0
if len(width) == 0 {
if len(layout.Row.WidthArr) == 0 {
cnt = layout.Cnt
} else {
cnt = ctr.win.lastLayoutCnt + 1
ctr.win.lastLayoutCnt = cnt
}
}
panelLayout(ctr.win.ctx, ctr.win, ctr.height, len(width), cnt)
ctr.win.staticZeros(width)
layout.Row.WidthArr = width
layout.Row.Type = layoutStatic
layout.Row.ItemWidth = 0
layout.Row.ItemRatio = 0.0
layout.Row.ItemOffset = 0
layout.Row.Filled = 0
}
|
[
"func",
"(",
"ctr",
"*",
"rowConstructor",
")",
"StaticScaled",
"(",
"width",
"...",
"int",
")",
"{",
"layout",
":=",
"ctr",
".",
"win",
".",
"layout",
"\n\n",
"cnt",
":=",
"0",
"\n\n",
"if",
"len",
"(",
"width",
")",
"==",
"0",
"{",
"if",
"len",
"(",
"layout",
".",
"Row",
".",
"WidthArr",
")",
"==",
"0",
"{",
"cnt",
"=",
"layout",
".",
"Cnt",
"\n",
"}",
"else",
"{",
"cnt",
"=",
"ctr",
".",
"win",
".",
"lastLayoutCnt",
"+",
"1",
"\n",
"ctr",
".",
"win",
".",
"lastLayoutCnt",
"=",
"cnt",
"\n",
"}",
"\n",
"}",
"\n\n",
"panelLayout",
"(",
"ctr",
".",
"win",
".",
"ctx",
",",
"ctr",
".",
"win",
",",
"ctr",
".",
"height",
",",
"len",
"(",
"width",
")",
",",
"cnt",
")",
"\n\n",
"ctr",
".",
"win",
".",
"staticZeros",
"(",
"width",
")",
"\n\n",
"layout",
".",
"Row",
".",
"WidthArr",
"=",
"width",
"\n",
"layout",
".",
"Row",
".",
"Type",
"=",
"layoutStatic",
"\n",
"layout",
".",
"Row",
".",
"ItemWidth",
"=",
"0",
"\n",
"layout",
".",
"Row",
".",
"ItemRatio",
"=",
"0.0",
"\n",
"layout",
".",
"Row",
".",
"ItemOffset",
"=",
"0",
"\n",
"layout",
".",
"Row",
".",
"Filled",
"=",
"0",
"\n",
"}"
] |
// Like Static but with scaled sizes.
|
[
"Like",
"Static",
"but",
"with",
"scaled",
"sizes",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1106-L1130
|
train
|
aarzilli/nucular
|
nucular.go
|
LayoutResetStatic
|
func (win *Window) LayoutResetStatic(width ...int) {
for i := range width {
width[i] = win.ctx.scale(width[i])
}
win.LayoutResetStaticScaled(width...)
}
|
go
|
func (win *Window) LayoutResetStatic(width ...int) {
for i := range width {
width[i] = win.ctx.scale(width[i])
}
win.LayoutResetStaticScaled(width...)
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"LayoutResetStatic",
"(",
"width",
"...",
"int",
")",
"{",
"for",
"i",
":=",
"range",
"width",
"{",
"width",
"[",
"i",
"]",
"=",
"win",
".",
"ctx",
".",
"scale",
"(",
"width",
"[",
"i",
"]",
")",
"\n",
"}",
"\n",
"win",
".",
"LayoutResetStaticScaled",
"(",
"width",
"...",
")",
"\n",
"}"
] |
// Reset static row
|
[
"Reset",
"static",
"row"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1133-L1138
|
train
|
aarzilli/nucular
|
nucular.go
|
SpaceBeginRatio
|
func (ctr *rowConstructor) SpaceBeginRatio(widget_count int) {
layout := ctr.win.layout
panelLayout(ctr.win.ctx, ctr.win, ctr.height, widget_count, 0)
layout.Row.Type = layoutDynamicFree
layout.Row.Ratio = nil
layout.Row.ItemWidth = 0
layout.Row.ItemRatio = 0.0
layout.Row.ItemOffset = 0
layout.Row.Filled = 0
}
|
go
|
func (ctr *rowConstructor) SpaceBeginRatio(widget_count int) {
layout := ctr.win.layout
panelLayout(ctr.win.ctx, ctr.win, ctr.height, widget_count, 0)
layout.Row.Type = layoutDynamicFree
layout.Row.Ratio = nil
layout.Row.ItemWidth = 0
layout.Row.ItemRatio = 0.0
layout.Row.ItemOffset = 0
layout.Row.Filled = 0
}
|
[
"func",
"(",
"ctr",
"*",
"rowConstructor",
")",
"SpaceBeginRatio",
"(",
"widget_count",
"int",
")",
"{",
"layout",
":=",
"ctr",
".",
"win",
".",
"layout",
"\n",
"panelLayout",
"(",
"ctr",
".",
"win",
".",
"ctx",
",",
"ctr",
".",
"win",
",",
"ctr",
".",
"height",
",",
"widget_count",
",",
"0",
")",
"\n",
"layout",
".",
"Row",
".",
"Type",
"=",
"layoutDynamicFree",
"\n\n",
"layout",
".",
"Row",
".",
"Ratio",
"=",
"nil",
"\n",
"layout",
".",
"Row",
".",
"ItemWidth",
"=",
"0",
"\n",
"layout",
".",
"Row",
".",
"ItemRatio",
"=",
"0.0",
"\n",
"layout",
".",
"Row",
".",
"ItemOffset",
"=",
"0",
"\n",
"layout",
".",
"Row",
".",
"Filled",
"=",
"0",
"\n",
"}"
] |
// Starts new row that will contain widget_count widgets.
// The size and position of widgets inside this row will be specified
// by callling LayoutSpacePushRatio.
|
[
"Starts",
"new",
"row",
"that",
"will",
"contain",
"widget_count",
"widgets",
".",
"The",
"size",
"and",
"position",
"of",
"widgets",
"inside",
"this",
"row",
"will",
"be",
"specified",
"by",
"callling",
"LayoutSpacePushRatio",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1184-L1194
|
train
|
aarzilli/nucular
|
nucular.go
|
LayoutSetWidth
|
func (win *Window) LayoutSetWidth(width int) {
layout := win.layout
if layout.Row.Type != layoutStatic || len(layout.Row.WidthArr) > 0 {
panic(WrongLayoutErr)
}
layout.Row.Index2++
layout.Row.CalcMaxWidth = false
layout.Row.ItemWidth = win.ctx.scale(width)
}
|
go
|
func (win *Window) LayoutSetWidth(width int) {
layout := win.layout
if layout.Row.Type != layoutStatic || len(layout.Row.WidthArr) > 0 {
panic(WrongLayoutErr)
}
layout.Row.Index2++
layout.Row.CalcMaxWidth = false
layout.Row.ItemWidth = win.ctx.scale(width)
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"LayoutSetWidth",
"(",
"width",
"int",
")",
"{",
"layout",
":=",
"win",
".",
"layout",
"\n",
"if",
"layout",
".",
"Row",
".",
"Type",
"!=",
"layoutStatic",
"||",
"len",
"(",
"layout",
".",
"Row",
".",
"WidthArr",
")",
">",
"0",
"{",
"panic",
"(",
"WrongLayoutErr",
")",
"\n",
"}",
"\n",
"layout",
".",
"Row",
".",
"Index2",
"++",
"\n",
"layout",
".",
"Row",
".",
"CalcMaxWidth",
"=",
"false",
"\n",
"layout",
".",
"Row",
".",
"ItemWidth",
"=",
"win",
".",
"ctx",
".",
"scale",
"(",
"width",
")",
"\n",
"}"
] |
// LayoutSetWidth adds a new column with the specified width to a static
// layout.
|
[
"LayoutSetWidth",
"adds",
"a",
"new",
"column",
"with",
"the",
"specified",
"width",
"to",
"a",
"static",
"layout",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1198-L1206
|
train
|
aarzilli/nucular
|
nucular.go
|
LayoutFitWidth
|
func (win *Window) LayoutFitWidth(id int, minwidth int) {
layout := win.layout
if layout.Row.Type != layoutStatic || len(layout.Row.WidthArr) > 0 {
panic(WrongLayoutErr)
}
if win.adjust == nil {
win.adjust = make(map[int]map[int]*adjustCol)
}
adjust, ok := win.adjust[layout.Cnt]
if !ok {
adjust = make(map[int]*adjustCol)
win.adjust[layout.Cnt] = adjust
}
col, ok := adjust[layout.Row.Index2]
if !ok || col.id != id || col.font != win.ctx.Style.Font {
if !ok {
col = &adjustCol{id: id, width: minwidth}
win.adjust[layout.Cnt][layout.Row.Index2] = col
}
col.id = id
col.font = win.ctx.Style.Font
col.width = minwidth
col.first = true
win.ctx.trashFrame = true
win.LayoutSetWidth(minwidth)
layout.Row.CalcMaxWidth = true
return
}
win.LayoutSetWidthScaled(col.width)
layout.Row.CalcMaxWidth = col.first
}
|
go
|
func (win *Window) LayoutFitWidth(id int, minwidth int) {
layout := win.layout
if layout.Row.Type != layoutStatic || len(layout.Row.WidthArr) > 0 {
panic(WrongLayoutErr)
}
if win.adjust == nil {
win.adjust = make(map[int]map[int]*adjustCol)
}
adjust, ok := win.adjust[layout.Cnt]
if !ok {
adjust = make(map[int]*adjustCol)
win.adjust[layout.Cnt] = adjust
}
col, ok := adjust[layout.Row.Index2]
if !ok || col.id != id || col.font != win.ctx.Style.Font {
if !ok {
col = &adjustCol{id: id, width: minwidth}
win.adjust[layout.Cnt][layout.Row.Index2] = col
}
col.id = id
col.font = win.ctx.Style.Font
col.width = minwidth
col.first = true
win.ctx.trashFrame = true
win.LayoutSetWidth(minwidth)
layout.Row.CalcMaxWidth = true
return
}
win.LayoutSetWidthScaled(col.width)
layout.Row.CalcMaxWidth = col.first
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"LayoutFitWidth",
"(",
"id",
"int",
",",
"minwidth",
"int",
")",
"{",
"layout",
":=",
"win",
".",
"layout",
"\n",
"if",
"layout",
".",
"Row",
".",
"Type",
"!=",
"layoutStatic",
"||",
"len",
"(",
"layout",
".",
"Row",
".",
"WidthArr",
")",
">",
"0",
"{",
"panic",
"(",
"WrongLayoutErr",
")",
"\n",
"}",
"\n",
"if",
"win",
".",
"adjust",
"==",
"nil",
"{",
"win",
".",
"adjust",
"=",
"make",
"(",
"map",
"[",
"int",
"]",
"map",
"[",
"int",
"]",
"*",
"adjustCol",
")",
"\n",
"}",
"\n",
"adjust",
",",
"ok",
":=",
"win",
".",
"adjust",
"[",
"layout",
".",
"Cnt",
"]",
"\n",
"if",
"!",
"ok",
"{",
"adjust",
"=",
"make",
"(",
"map",
"[",
"int",
"]",
"*",
"adjustCol",
")",
"\n",
"win",
".",
"adjust",
"[",
"layout",
".",
"Cnt",
"]",
"=",
"adjust",
"\n",
"}",
"\n",
"col",
",",
"ok",
":=",
"adjust",
"[",
"layout",
".",
"Row",
".",
"Index2",
"]",
"\n",
"if",
"!",
"ok",
"||",
"col",
".",
"id",
"!=",
"id",
"||",
"col",
".",
"font",
"!=",
"win",
".",
"ctx",
".",
"Style",
".",
"Font",
"{",
"if",
"!",
"ok",
"{",
"col",
"=",
"&",
"adjustCol",
"{",
"id",
":",
"id",
",",
"width",
":",
"minwidth",
"}",
"\n",
"win",
".",
"adjust",
"[",
"layout",
".",
"Cnt",
"]",
"[",
"layout",
".",
"Row",
".",
"Index2",
"]",
"=",
"col",
"\n",
"}",
"\n",
"col",
".",
"id",
"=",
"id",
"\n",
"col",
".",
"font",
"=",
"win",
".",
"ctx",
".",
"Style",
".",
"Font",
"\n",
"col",
".",
"width",
"=",
"minwidth",
"\n",
"col",
".",
"first",
"=",
"true",
"\n",
"win",
".",
"ctx",
".",
"trashFrame",
"=",
"true",
"\n",
"win",
".",
"LayoutSetWidth",
"(",
"minwidth",
")",
"\n",
"layout",
".",
"Row",
".",
"CalcMaxWidth",
"=",
"true",
"\n",
"return",
"\n",
"}",
"\n",
"win",
".",
"LayoutSetWidthScaled",
"(",
"col",
".",
"width",
")",
"\n",
"layout",
".",
"Row",
".",
"CalcMaxWidth",
"=",
"col",
".",
"first",
"\n",
"}"
] |
// LayoutFitWidth adds a new column to a static layout.
// The width of the column will be large enough to fit the largest widget
// exactly. The largest widget will only be calculated once per id, if the
// dataset changes the id should change.
|
[
"LayoutFitWidth",
"adds",
"a",
"new",
"column",
"to",
"a",
"static",
"layout",
".",
"The",
"width",
"of",
"the",
"column",
"will",
"be",
"large",
"enough",
"to",
"fit",
"the",
"largest",
"widget",
"exactly",
".",
"The",
"largest",
"widget",
"will",
"only",
"be",
"calculated",
"once",
"per",
"id",
"if",
"the",
"dataset",
"changes",
"the",
"id",
"should",
"change",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1224-L1254
|
train
|
aarzilli/nucular
|
nucular.go
|
LayoutSpacePushScaled
|
func (win *Window) LayoutSpacePushScaled(rect rect.Rect) {
if win.layout.Row.Type != layoutStaticFree {
panic(WrongLayoutErr)
}
win.layout.Row.Item = rect
}
|
go
|
func (win *Window) LayoutSpacePushScaled(rect rect.Rect) {
if win.layout.Row.Type != layoutStaticFree {
panic(WrongLayoutErr)
}
win.layout.Row.Item = rect
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"LayoutSpacePushScaled",
"(",
"rect",
"rect",
".",
"Rect",
")",
"{",
"if",
"win",
".",
"layout",
".",
"Row",
".",
"Type",
"!=",
"layoutStaticFree",
"{",
"panic",
"(",
"WrongLayoutErr",
")",
"\n",
"}",
"\n",
"win",
".",
"layout",
".",
"Row",
".",
"Item",
"=",
"rect",
"\n",
"}"
] |
// Like LayoutSpacePush but with scaled units
|
[
"Like",
"LayoutSpacePush",
"but",
"with",
"scaled",
"units"
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1271-L1276
|
train
|
aarzilli/nucular
|
nucular.go
|
WidgetBounds
|
func (win *Window) WidgetBounds() rect.Rect {
var bounds rect.Rect
win.layoutPeek(&bounds)
return bounds
}
|
go
|
func (win *Window) WidgetBounds() rect.Rect {
var bounds rect.Rect
win.layoutPeek(&bounds)
return bounds
}
|
[
"func",
"(",
"win",
"*",
"Window",
")",
"WidgetBounds",
"(",
")",
"rect",
".",
"Rect",
"{",
"var",
"bounds",
"rect",
".",
"Rect",
"\n",
"win",
".",
"layoutPeek",
"(",
"&",
"bounds",
")",
"\n",
"return",
"bounds",
"\n",
"}"
] |
// Returns the position and size of the next widget that will be
// added to the current row.
// Note that the return value is in scaled units.
|
[
"Returns",
"the",
"position",
"and",
"size",
"of",
"the",
"next",
"widget",
"that",
"will",
"be",
"added",
"to",
"the",
"current",
"row",
".",
"Note",
"that",
"the",
"return",
"value",
"is",
"in",
"scaled",
"units",
"."
] |
64ec1eba91814ebb417978927206070dd1fc44e1
|
https://github.com/aarzilli/nucular/blob/64ec1eba91814ebb417978927206070dd1fc44e1/nucular.go#L1307-L1311
|
train
|
Subsets and Splits
SQL Console for semeru/code-text-go
Retrieves a limited set of code samples with their languages, with a specific case adjustment for 'Go' language.