test support go1.18 (#2990)

This commit is contained in:
thinkerou
2022-03-21 09:43:17 +08:00
committed by GitHub
parent 9701b651b7
commit 2bde107686
47 changed files with 214 additions and 174 deletions

10
render/any.go Normal file
View File

@ -0,0 +1,10 @@
// Copyright 2021 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 !go1.18
// +build !go1.18
package render
type any = interface{}

View File

@ -20,7 +20,7 @@ type Delims struct {
// HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.
type HTMLRender interface {
// Instance returns an HTML instance.
Instance(string, interface{}) Render
Instance(string, any) Render
}
// HTMLProduction contains template reference and its delims.
@ -41,13 +41,13 @@ type HTMLDebug struct {
type HTML struct {
Template *template.Template
Name string
Data interface{}
Data any
}
var htmlContentType = []string{"text/html; charset=utf-8"}
// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
func (r HTMLProduction) Instance(name string, data interface{}) Render {
func (r HTMLProduction) Instance(name string, data any) Render {
return HTML{
Template: r.Template,
Name: name,
@ -56,7 +56,7 @@ func (r HTMLProduction) Instance(name string, data interface{}) Render {
}
// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.
func (r HTMLDebug) Instance(name string, data interface{}) Render {
func (r HTMLDebug) Instance(name string, data any) Render {
return HTML{
Template: r.loadTemplate(),
Name: name,

View File

@ -16,34 +16,34 @@ import (
// JSON contains the given interface object.
type JSON struct {
Data interface{}
Data any
}
// IndentedJSON contains the given interface object.
type IndentedJSON struct {
Data interface{}
Data any
}
// SecureJSON contains the given interface object and its prefix.
type SecureJSON struct {
Prefix string
Data interface{}
Data any
}
// JsonpJSON contains the given interface object its callback.
type JsonpJSON struct {
Callback string
Data interface{}
Data any
}
// AsciiJSON contains the given interface object.
type AsciiJSON struct {
Data interface{}
Data any
}
// PureJSON contains the given interface object.
type PureJSON struct {
Data interface{}
Data any
}
var (
@ -66,7 +66,7 @@ func (r JSON) WriteContentType(w http.ResponseWriter) {
}
// WriteJSON marshals the given interface object and writes it with custom ContentType.
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
func WriteJSON(w http.ResponseWriter, obj any) error {
writeContentType(w, jsonContentType)
jsonBytes, err := json.Marshal(obj)
if err != nil {

View File

@ -21,7 +21,7 @@ var (
// MsgPack contains the given interface object.
type MsgPack struct {
Data interface{}
Data any
}
var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
@ -37,7 +37,7 @@ func (r MsgPack) Render(w http.ResponseWriter) error {
}
// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {
func WriteMsgPack(w http.ResponseWriter, obj any) error {
writeContentType(w, msgpackContentType)
var mh codec.MsgpackHandle
return codec.NewEncoder(w, &mh).Encode(obj)

View File

@ -12,7 +12,7 @@ import (
// ProtoBuf contains the given interface object.
type ProtoBuf struct {
Data interface{}
Data any
}
var protobufContentType = []string{"application/x-protobuf"}

View File

@ -21,7 +21,7 @@ import (
func TestRenderMsgPack(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
data := map[string]any{
"foo": "bar",
}

View File

@ -24,7 +24,7 @@ import (
func TestRenderJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
data := map[string]any{
"foo": "bar",
"html": "<b>",
}
@ -49,7 +49,7 @@ func TestRenderJSONPanics(t *testing.T) {
func TestRenderIndentedJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
data := map[string]any{
"foo": "bar",
"bar": "foo",
}
@ -72,7 +72,7 @@ func TestRenderIndentedJSONPanics(t *testing.T) {
func TestRenderSecureJSON(t *testing.T) {
w1 := httptest.NewRecorder()
data := map[string]interface{}{
data := map[string]any{
"foo": "bar",
}
@ -86,7 +86,7 @@ func TestRenderSecureJSON(t *testing.T) {
assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
w2 := httptest.NewRecorder()
datas := []map[string]interface{}{{
datas := []map[string]any{{
"foo": "bar",
}, {
"bar": "foo",
@ -109,7 +109,7 @@ func TestRenderSecureJSONFail(t *testing.T) {
func TestRenderJsonpJSON(t *testing.T) {
w1 := httptest.NewRecorder()
data := map[string]interface{}{
data := map[string]any{
"foo": "bar",
}
@ -123,7 +123,7 @@ func TestRenderJsonpJSON(t *testing.T) {
assert.Equal(t, "application/javascript; charset=utf-8", w1.Header().Get("Content-Type"))
w2 := httptest.NewRecorder()
datas := []map[string]interface{}{{
datas := []map[string]any{{
"foo": "bar",
}, {
"bar": "foo",
@ -137,7 +137,7 @@ func TestRenderJsonpJSON(t *testing.T) {
func TestRenderJsonpJSONError2(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
data := map[string]any{
"foo": "bar",
}
(JsonpJSON{"", data}).WriteContentType(w)
@ -161,7 +161,7 @@ func TestRenderJsonpJSONFail(t *testing.T) {
func TestRenderAsciiJSON(t *testing.T) {
w1 := httptest.NewRecorder()
data1 := map[string]interface{}{
data1 := map[string]any{
"lang": "GO语言",
"tag": "<br>",
}
@ -190,7 +190,7 @@ func TestRenderAsciiJSONFail(t *testing.T) {
func TestRenderPureJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
data := map[string]any{
"foo": "bar",
"html": "<b>",
}
@ -200,7 +200,7 @@ func TestRenderPureJSON(t *testing.T) {
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
}
type xmlmap map[string]interface{}
type xmlmap map[string]any
// Allows type H to be used with xml.Marshal
func (h xmlmap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
@ -244,7 +244,7 @@ b:
type fail struct{}
// Hook MarshalYAML
func (ft *fail) MarshalYAML() (interface{}, error) {
func (ft *fail) MarshalYAML() (any, error) {
return nil, errors.New("fail")
}
@ -358,13 +358,13 @@ func TestRenderString(t *testing.T) {
(String{
Format: "hello %s %d",
Data: []interface{}{},
Data: []any{},
}).WriteContentType(w)
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
err := (String{
Format: "hola %s %d",
Data: []interface{}{"manu", 2},
Data: []any{"manu", 2},
}).Render(w)
assert.NoError(t, err)
@ -377,7 +377,7 @@ func TestRenderStringLenZero(t *testing.T) {
err := (String{
Format: "hola %s %d",
Data: []interface{}{},
Data: []any{},
}).Render(w)
assert.NoError(t, err)
@ -390,7 +390,7 @@ func TestRenderHTMLTemplate(t *testing.T) {
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
htmlRender := HTMLProduction{Template: templ}
instance := htmlRender.Instance("t", map[string]interface{}{
instance := htmlRender.Instance("t", map[string]any{
"name": "alexandernyquist",
})
@ -406,7 +406,7 @@ func TestRenderHTMLTemplateEmptyName(t *testing.T) {
templ := template.Must(template.New("").Parse(`Hello {{.name}}`))
htmlRender := HTMLProduction{Template: templ}
instance := htmlRender.Instance("", map[string]interface{}{
instance := htmlRender.Instance("", map[string]any{
"name": "alexandernyquist",
})
@ -425,7 +425,7 @@ func TestRenderHTMLDebugFiles(t *testing.T) {
Delims: Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
instance := htmlRender.Instance("hello.tmpl", map[string]any{
"name": "thinkerou",
})
@ -444,7 +444,7 @@ func TestRenderHTMLDebugGlob(t *testing.T) {
Delims: Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
instance := htmlRender.Instance("hello.tmpl", map[string]any{
"name": "thinkerou",
})

View File

@ -14,7 +14,7 @@ import (
// String contains the given interface object slice and its format.
type String struct {
Format string
Data []interface{}
Data []any
}
var plainContentType = []string{"text/plain; charset=utf-8"}
@ -30,7 +30,7 @@ func (r String) WriteContentType(w http.ResponseWriter) {
}
// WriteString writes data according to its format and write custom ContentType.
func WriteString(w http.ResponseWriter, format string, data []interface{}) (err error) {
func WriteString(w http.ResponseWriter, format string, data []any) (err error) {
writeContentType(w, plainContentType)
if len(data) > 0 {
_, err = fmt.Fprintf(w, format, data...)

View File

@ -11,7 +11,7 @@ import (
// XML contains the given interface object.
type XML struct {
Data interface{}
Data any
}
var xmlContentType = []string{"application/xml; charset=utf-8"}

View File

@ -12,7 +12,7 @@ import (
// YAML contains the given interface object.
type YAML struct {
Data interface{}
Data any
}
var yamlContentType = []string{"application/x-yaml; charset=utf-8"}