mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-08-26 22:47:51 +02:00
* Inital routes to git refs api * Git refs API implementation * Update swagger * Fix copyright * Make swagger happy add basic test * Fix test * Fix test again :)
23 lines
623 B
Go
23 lines
623 B
Go
package types
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"reflect"
|
|
)
|
|
|
|
// ScanFully uses fmt.Sscanf with verb to fully scan val into ptr.
|
|
func ScanFully(ptr interface{}, val string, verb byte) error {
|
|
t := reflect.ValueOf(ptr).Elem().Type()
|
|
// attempt to read extra bytes to make sure the value is consumed
|
|
var b []byte
|
|
n, err := fmt.Sscanf(val, "%"+string(verb)+"%s", ptr, &b)
|
|
switch {
|
|
case n < 1 || n == 1 && err != io.EOF:
|
|
return fmt.Errorf("failed to parse %q as %v: %v", val, t, err)
|
|
case n > 1:
|
|
return fmt.Errorf("failed to parse %q as %v: extra characters %q", val, t, string(b))
|
|
}
|
|
// n == 1 && err == io.EOF
|
|
return nil
|
|
}
|