github.com/wfusion/gofusion@v1.1.14/http/parser/parser.go (about) 1 package parser 2 3 import ( 4 "fmt" 5 "io" 6 "reflect" 7 8 "github.com/gin-gonic/gin" 9 ) 10 11 type Parser interface { 12 PreParse(args map[string]string) error 13 Parse(src io.Reader, dst reflect.Value) error 14 } 15 16 func malformedRequest(msg string) error { 17 return fmt.Errorf("malformed request %s", msg) 18 } 19 20 func unsupportedContentType(typ string) error { 21 return fmt.Errorf("unsupported content-type %s", typ) 22 } 23 24 var ( 25 parserMap = map[string]reflect.Type{ 26 gin.MIMEJSON: reflect.TypeOf((*ApplicationJsonParser)(nil)), 27 gin.MIMEPOSTForm: reflect.TypeOf((*ApplicationFormUrlencodedParser)(nil)), 28 gin.MIMEMultipartPOSTForm: reflect.TypeOf((*MultipartFormDataParser)(nil)), 29 } 30 ) 31 32 func GetByContentType(typ string) (parser Parser, err error) { 33 parserType, ok := parserMap[typ] 34 if !ok { 35 return nil, unsupportedContentType(typ) 36 } 37 38 return reflect.New(parserType.Elem()).Interface().(Parser), nil 39 }