switching bencode.Marshal to match other encoders
This commit is contained in:
		
							parent
							
								
									c4a2cbbbbe
								
							
						
					
					
						commit
						ba37eae00a
					
				
					 2 changed files with 12 additions and 6 deletions
				
			
		|  | @ -11,11 +11,11 @@ import ( | ||||||
| type any interface{} | type any interface{} | ||||||
| 
 | 
 | ||||||
| func checkMarshal(expected string, data any) (err error) { | func checkMarshal(expected string, data any) (err error) { | ||||||
| 	var b bytes.Buffer | 	var b []byte | ||||||
| 	if err = Marshal(&b, data); err != nil { | 	if b, err = Marshal(data); err != nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	s := b.String() | 	s := string(b) | ||||||
| 	if expected != s { | 	if expected != s { | ||||||
| 		err = errors.New(fmt.Sprintf("Expected %s got %s", expected, s)) | 		err = errors.New(fmt.Sprintf("Expected %s got %s", expected, s)) | ||||||
| 		return | 		return | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ | ||||||
| package bencode | package bencode | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"bytes" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
|  | @ -283,6 +284,9 @@ func (b *structBuilder) Key(k string) builder { | ||||||
| // slice of the correct type. | // slice of the correct type. | ||||||
| // | // | ||||||
| func Unmarshal(r io.Reader, val interface{}) (err error) { | func Unmarshal(r io.Reader, val interface{}) (err error) { | ||||||
|  | 	// TODO match other encoders, like encoding/json ... | ||||||
|  | 	// func Unmarshal(data []byte, v interface{}) error | ||||||
|  | 
 | ||||||
| 	// If e represents a value, the answer won't get back to the | 	// If e represents a value, the answer won't get back to the | ||||||
| 	// caller.  Make sure it's a pointer. | 	// caller.  Make sure it's a pointer. | ||||||
| 	if reflect.TypeOf(val).Kind() != reflect.Ptr { | 	if reflect.TypeOf(val).Kind() != reflect.Ptr { | ||||||
|  | @ -516,7 +520,7 @@ func isValueNil(val reflect.Value) bool { | ||||||
| //   Field int | //   Field int | ||||||
| // | // | ||||||
| //   // Field appears in bencode as key "myName". | //   // Field appears in bencode as key "myName". | ||||||
| //   Field int "myName" | //   Field int `bencode:"myName"` | ||||||
| // | // | ||||||
| // Anonymous struct fields are ignored. | // Anonymous struct fields are ignored. | ||||||
| // | // | ||||||
|  | @ -533,8 +537,10 @@ func isValueNil(val reflect.Value) bool { | ||||||
| // handle them.  Passing cyclic structures to Marshal will result in | // handle them.  Passing cyclic structures to Marshal will result in | ||||||
| // an infinite recursion. | // an infinite recursion. | ||||||
| // | // | ||||||
| func Marshal(w io.Writer, val interface{}) error { | func Marshal(val interface{}) ([]byte, error) { | ||||||
| 	// TODO match other encoders, like encoding/json ... | 	// TODO match other encoders, like encoding/json ... | ||||||
| 	// func Marshal(v interface{}) ([]byte, error) | 	// func Marshal(v interface{}) ([]byte, error) | ||||||
| 	return writeValue(w, reflect.ValueOf(val)) | 	buf := bytes.Buffer{} | ||||||
|  | 	err := writeValue(&buf, reflect.ValueOf(val)) | ||||||
|  | 	return buf.Bytes(), err | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue