mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-25 01:41:00 +00:00
6c0d93c6cb
* [chore]: Bump github.com/prometheus/client_golang from 1.18.0 to 1.19.1 Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.18.0 to 1.19.1. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](https://github.com/prometheus/client_golang/compare/v1.18.0...v1.19.1) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * [chore]: Bump github.com/KimMachineGun/automemlimit from 0.6.0 to 0.6.1 Bumps [github.com/KimMachineGun/automemlimit](https://github.com/KimMachineGun/automemlimit) from 0.6.0 to 0.6.1. - [Release notes](https://github.com/KimMachineGun/automemlimit/releases) - [Commits](https://github.com/KimMachineGun/automemlimit/compare/v0.6.0...v0.6.1) --- updated-dependencies: - dependency-name: github.com/KimMachineGun/automemlimit dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * [chore]: Bump github.com/tdewolff/minify/v2 from 2.20.20 to 2.20.24 Bumps [github.com/tdewolff/minify/v2](https://github.com/tdewolff/minify) from 2.20.20 to 2.20.24. - [Release notes](https://github.com/tdewolff/minify/releases) - [Commits](https://github.com/tdewolff/minify/compare/v2.20.20...v2.20.24) --- updated-dependencies: - dependency-name: github.com/tdewolff/minify/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * [chore]: Bump github.com/go-swagger/go-swagger Bumps [github.com/go-swagger/go-swagger](https://github.com/go-swagger/go-swagger) from 0.30.6-0.20240418033037-c46c303aaa02 to 0.31.0. - [Release notes](https://github.com/go-swagger/go-swagger/releases) - [Changelog](https://github.com/go-swagger/go-swagger/blob/master/.goreleaser.yml) - [Commits](https://github.com/go-swagger/go-swagger/commits/v0.31.0) --- updated-dependencies: - dependency-name: github.com/go-swagger/go-swagger dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * [chore]: Bump github.com/gin-gonic/gin from 1.9.1 to 1.10.0 Bumps [github.com/gin-gonic/gin](https://github.com/gin-gonic/gin) from 1.9.1 to 1.10.0. - [Release notes](https://github.com/gin-gonic/gin/releases) - [Changelog](https://github.com/gin-gonic/gin/blob/master/CHANGELOG.md) - [Commits](https://github.com/gin-gonic/gin/compare/v1.9.1...v1.10.0) --- updated-dependencies: - dependency-name: github.com/gin-gonic/gin dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
// Copyright 2020 Gin Core Team. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build nomsgpack
|
|
|
|
package binding
|
|
|
|
import "net/http"
|
|
|
|
// Content-Type MIME of the most common data formats.
|
|
const (
|
|
MIMEJSON = "application/json"
|
|
MIMEHTML = "text/html"
|
|
MIMEXML = "application/xml"
|
|
MIMEXML2 = "text/xml"
|
|
MIMEPlain = "text/plain"
|
|
MIMEPOSTForm = "application/x-www-form-urlencoded"
|
|
MIMEMultipartPOSTForm = "multipart/form-data"
|
|
MIMEPROTOBUF = "application/x-protobuf"
|
|
MIMEYAML = "application/x-yaml"
|
|
MIMEYAML2 = "application/yaml"
|
|
MIMETOML = "application/toml"
|
|
)
|
|
|
|
// Binding describes the interface which needs to be implemented for binding the
|
|
// data present in the request such as JSON request body, query parameters or
|
|
// the form POST.
|
|
type Binding interface {
|
|
Name() string
|
|
Bind(*http.Request, any) error
|
|
}
|
|
|
|
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
|
|
// but it reads the body from supplied bytes instead of req.Body.
|
|
type BindingBody interface {
|
|
Binding
|
|
BindBody([]byte, any) error
|
|
}
|
|
|
|
// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
|
|
// but it reads the Params.
|
|
type BindingUri interface {
|
|
Name() string
|
|
BindUri(map[string][]string, any) error
|
|
}
|
|
|
|
// StructValidator is the minimal interface which needs to be implemented in
|
|
// order for it to be used as the validator engine for ensuring the correctness
|
|
// of the request. Gin provides a default implementation for this using
|
|
// https://github.com/go-playground/validator/tree/v10.6.1.
|
|
type StructValidator interface {
|
|
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
|
|
// If the received type is not a struct, any validation should be skipped and nil must be returned.
|
|
// If the received type is a struct or pointer to a struct, the validation should be performed.
|
|
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
|
|
// Otherwise nil must be returned.
|
|
ValidateStruct(any) error
|
|
|
|
// Engine returns the underlying validator engine which powers the
|
|
// StructValidator implementation.
|
|
Engine() any
|
|
}
|
|
|
|
// Validator is the default validator which implements the StructValidator
|
|
// interface. It uses https://github.com/go-playground/validator/tree/v10.6.1
|
|
// under the hood.
|
|
var Validator StructValidator = &defaultValidator{}
|
|
|
|
// These implement the Binding interface and can be used to bind the data
|
|
// present in the request to struct instances.
|
|
var (
|
|
JSON = jsonBinding{}
|
|
XML = xmlBinding{}
|
|
Form = formBinding{}
|
|
Query = queryBinding{}
|
|
FormPost = formPostBinding{}
|
|
FormMultipart = formMultipartBinding{}
|
|
ProtoBuf = protobufBinding{}
|
|
YAML = yamlBinding{}
|
|
Uri = uriBinding{}
|
|
Header = headerBinding{}
|
|
TOML = tomlBinding{}
|
|
)
|
|
|
|
// Default returns the appropriate Binding instance based on the HTTP method
|
|
// and the content type.
|
|
func Default(method, contentType string) Binding {
|
|
if method == "GET" {
|
|
return Form
|
|
}
|
|
|
|
switch contentType {
|
|
case MIMEJSON:
|
|
return JSON
|
|
case MIMEXML, MIMEXML2:
|
|
return XML
|
|
case MIMEPROTOBUF:
|
|
return ProtoBuf
|
|
case MIMEYAML, MIMEYAML2:
|
|
return YAML
|
|
case MIMEMultipartPOSTForm:
|
|
return FormMultipart
|
|
case MIMETOML:
|
|
return TOML
|
|
default: // case MIMEPOSTForm:
|
|
return Form
|
|
}
|
|
}
|
|
|
|
func validate(obj any) error {
|
|
if Validator == nil {
|
|
return nil
|
|
}
|
|
return Validator.ValidateStruct(obj)
|
|
}
|