Hide Builder, StringValue, and StringValueArray types.
No need to export these types.
This commit is contained in:
parent
9b44363a13
commit
5c36da2622
3 changed files with 21 additions and 21 deletions
|
@ -60,7 +60,7 @@ func (j *decoder) Array() { j.value = make([]interface{}, 0, 8) }
|
||||||
|
|
||||||
func (j *decoder) Map() { j.value = make(map[string]interface{}) }
|
func (j *decoder) Map() { j.value = make(map[string]interface{}) }
|
||||||
|
|
||||||
func (j *decoder) Elem(i int) Builder {
|
func (j *decoder) Elem(i int) builder {
|
||||||
v, ok := j.value.([]interface{})
|
v, ok := j.value.([]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
v = make([]interface{}, 0, 8)
|
v = make([]interface{}, 0, 8)
|
||||||
|
@ -80,7 +80,7 @@ func (j *decoder) Elem(i int) Builder {
|
||||||
return newDecoder(v, i)
|
return newDecoder(v, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *decoder) Key(s string) Builder {
|
func (j *decoder) Key(s string) builder {
|
||||||
m, ok := j.value.(map[string]interface{})
|
m, ok := j.value.(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
m = make(map[string]interface{})
|
m = make(map[string]interface{})
|
||||||
|
|
18
parse.go
18
parse.go
|
@ -18,8 +18,8 @@ import (
|
||||||
// Parser
|
// Parser
|
||||||
//
|
//
|
||||||
// Implements parsing but not the actions. Those are
|
// Implements parsing but not the actions. Those are
|
||||||
// carried out by the implementation of the Builder interface.
|
// carried out by the implementation of the builder interface.
|
||||||
// A Builder represents the object being created.
|
// A builder represents the object being created.
|
||||||
// Calling a method like Int64(i) sets that object to i.
|
// Calling a method like Int64(i) sets that object to i.
|
||||||
// Calling a method like Elem(i) or Key(s) creates a
|
// Calling a method like Elem(i) or Key(s) creates a
|
||||||
// new builder for a subpiece of the object (logically,
|
// new builder for a subpiece of the object (logically,
|
||||||
|
@ -32,10 +32,10 @@ import (
|
||||||
// nested data structure, using the "map keys"
|
// nested data structure, using the "map keys"
|
||||||
// as struct field names.
|
// as struct field names.
|
||||||
|
|
||||||
// A Builder is an interface implemented by clients and passed
|
// A builder is an interface implemented by clients and passed
|
||||||
// to the bencode parser. It gives clients full control over the
|
// to the bencode parser. It gives clients full control over the
|
||||||
// eventual representation returned by the parser.
|
// eventual representation returned by the parser.
|
||||||
type Builder interface {
|
type builder interface {
|
||||||
// Set value
|
// Set value
|
||||||
Int64(i int64)
|
Int64(i int64)
|
||||||
Uint64(i uint64)
|
Uint64(i uint64)
|
||||||
|
@ -44,10 +44,10 @@ type Builder interface {
|
||||||
Map()
|
Map()
|
||||||
|
|
||||||
// Create sub-Builders
|
// Create sub-Builders
|
||||||
Elem(i int) Builder
|
Elem(i int) builder
|
||||||
Key(s string) Builder
|
Key(s string) builder
|
||||||
|
|
||||||
// Flush changes to parent Builder if necessary.
|
// Flush changes to parent builder if necessary.
|
||||||
Flush()
|
Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ func decodeString(r *bufio.Reader) (data string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFromReader(r *bufio.Reader, build Builder) (err error) {
|
func parseFromReader(r *bufio.Reader, build builder) (err error) {
|
||||||
c, err := r.ReadByte()
|
c, err := r.ReadByte()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
goto exit
|
goto exit
|
||||||
|
@ -195,6 +195,6 @@ exit:
|
||||||
|
|
||||||
// Parse parses the bencode stream and makes calls to
|
// Parse parses the bencode stream and makes calls to
|
||||||
// the builder to construct a parsed representation.
|
// the builder to construct a parsed representation.
|
||||||
func parse(r io.Reader, builder Builder) (err error) {
|
func parse(r io.Reader, builder builder) (err error) {
|
||||||
return parseFromReader(bufio.NewReader(r), builder)
|
return parseFromReader(bufio.NewReader(r), builder)
|
||||||
}
|
}
|
||||||
|
|
20
struct.go
20
struct.go
|
@ -145,7 +145,7 @@ func (b *structBuilder) Array() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *structBuilder) Elem(i int) Builder {
|
func (b *structBuilder) Elem(i int) builder {
|
||||||
if b == nil || i < 0 {
|
if b == nil || i < 0 {
|
||||||
return nobuilder
|
return nobuilder
|
||||||
}
|
}
|
||||||
|
@ -194,7 +194,7 @@ func (b *structBuilder) Map() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *structBuilder) Key(k string) Builder {
|
func (b *structBuilder) Key(k string) builder {
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return nobuilder
|
return nobuilder
|
||||||
}
|
}
|
||||||
|
@ -332,22 +332,22 @@ func writeArrayOrSlice(w io.Writer, val reflect.Value) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type StringValue struct {
|
type stringValue struct {
|
||||||
key string
|
key string
|
||||||
value reflect.Value
|
value reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
type StringValueArray []StringValue
|
type stringValueArray []stringValue
|
||||||
|
|
||||||
// Satisfy sort.Interface
|
// Satisfy sort.Interface
|
||||||
|
|
||||||
func (a StringValueArray) Len() int { return len(a) }
|
func (a stringValueArray) Len() int { return len(a) }
|
||||||
|
|
||||||
func (a StringValueArray) Less(i, j int) bool { return a[i].key < a[j].key }
|
func (a stringValueArray) Less(i, j int) bool { return a[i].key < a[j].key }
|
||||||
|
|
||||||
func (a StringValueArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
func (a stringValueArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
|
||||||
func writeSVList(w io.Writer, svList StringValueArray) (err error) {
|
func writeSVList(w io.Writer, svList stringValueArray) (err error) {
|
||||||
sort.Sort(svList)
|
sort.Sort(svList)
|
||||||
|
|
||||||
for _, sv := range svList {
|
for _, sv := range svList {
|
||||||
|
@ -381,7 +381,7 @@ func writeMap(w io.Writer, val reflect.Value) (err error) {
|
||||||
|
|
||||||
// Sort keys
|
// Sort keys
|
||||||
|
|
||||||
svList := make(StringValueArray, len(keys))
|
svList := make(stringValueArray, len(keys))
|
||||||
for i, key := range keys {
|
for i, key := range keys {
|
||||||
svList[i].key = key.String()
|
svList[i].key = key.String()
|
||||||
svList[i].value = val.MapIndex(key)
|
svList[i].value = val.MapIndex(key)
|
||||||
|
@ -408,7 +408,7 @@ func writeStruct(w io.Writer, val reflect.Value) (err error) {
|
||||||
typ := val.Type()
|
typ := val.Type()
|
||||||
|
|
||||||
numFields := val.NumField()
|
numFields := val.NumField()
|
||||||
svList := make(StringValueArray, numFields)
|
svList := make(stringValueArray, numFields)
|
||||||
|
|
||||||
for i := 0; i < numFields; i++ {
|
for i := 0; i < numFields; i++ {
|
||||||
field := typ.Field(i)
|
field := typ.Field(i)
|
||||||
|
|
Loading…
Reference in a new issue