content

package
v0.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2021 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package content 与生成内容相关的功能

Index

Constants

View Source
const DefaultCharset = "utf-8"

DefaultCharset 默认的字符集

View Source
const DefaultIndexPage = "index.html"

DefaultIndexPage ServeFileFS index 参数的默认值

View Source
const DefaultMimetype = "application/octet-stream"

DefaultMimetype 默认的媒体类型

在不能获取输入和输出的媒体类型时, 会采用此值作为其默认值。

若编码函数中指定该类型的函数,则会使用该编码优先匹配 */* 等格式的请求。

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildResultFunc added in v0.40.0

type BuildResultFunc func(status, code int, message string) Result

BuildResultFunc 用于生成 Result 接口对象的函数

type Content added in v0.40.0

type Content struct {
	// contains filtered or unexported fields
}

Content 管理反馈给用户的数据

func New added in v0.40.0

func New(builder BuildResultFunc) *Content

New 返回 *Content 实例

func (*Content) AddMimetype added in v0.40.0

func (c *Content) AddMimetype(m MarshalFunc, u UnmarshalFunc, name ...string) error

AddMimetype 添加序列化函数

m 和 u 可以为 nil,表示仅作为一个占位符使用,具体处理要在 ServeHTTP 中另作处理;

name 表示名称,一般为 mimetype 名称,比如 application/xml 等,用户也可以添加其它值,比如:

c.AddMimetype(json.Marshal, json.Unmarshal, "application/json", ".json")

后期用户可以根据文件后缀名从 c.Mimetype 直接查找相应的序列化函数。

func (*Content) AddResult added in v0.40.0

func (c *Content) AddResult(status, code int, key message.Reference, v ...interface{})

AddResult 添加一条错误信息

status 指定了该错误代码反馈给客户端的 HTTP 状态码;

func (*Content) CatalogBuilder added in v0.40.0

func (c *Content) CatalogBuilder() *catalog.Builder

CatalogBuilder 返回本地化操作的相关接口

func (*Content) DeleteMimetype added in v0.40.0

func (c *Content) DeleteMimetype(name string)

DeleteMimetype 删除指定名称的数据

func (*Content) LocaleBuilder added in v0.40.0

func (c *Content) LocaleBuilder(tag language.Tag) *LocaleBuilder

LocaleBuilder 声明 LocaleBuilder

func (*Content) Mimetype added in v0.40.0

func (c *Content) Mimetype(name string) (MarshalFunc, UnmarshalFunc, bool)

Mimetype 查找指定名称的序列化方法

func (*Content) MimetypeFunc added in v0.40.0

func (c *Content) MimetypeFunc(match func(string) bool) (MarshalFunc, UnmarshalFunc, bool)

MimetypeFunc 以指定函数查找是否有符合要求的序列化函数

match 为匹配函数,原型为 func(name string)bool,name 为序列化函数的名称, 用户可以判断此值是否符合要求,返回 true,表示匹配,会中断后续的匹配。

func (*Content) NewContext added in v0.40.0

func (c *Content) NewContext(l *log.Logger, w http.ResponseWriter, r *http.Request) (*Context, int)

NewContext 从用户请求中构建一个 Context 实例

如果不合规则,会以指定的状码返回并向 l 输出信息。

func (*Content) NewLocalePrinter added in v0.40.0

func (c *Content) NewLocalePrinter(tag language.Tag) *message.Printer

NewLocalePrinter 返回指定语言的 message.Printer

func (*Content) Result added in v0.40.0

func (c *Content) Result(p *message.Printer, code int, fields Fields) Result

Result 返回 Result 实例

如果找不到 code 对应的错误信息,则会直接 panic。 fields 表示明细字段,可以为空,之后通过 Result.Add 添加。

func (*Content) Results added in v0.40.0

func (c *Content) Results(p *message.Printer) map[int]string

Results 错误信息列表

p 用于返回特定语言的内容。

func (*Content) SetMimetype added in v0.40.0

func (c *Content) SetMimetype(name string, m MarshalFunc, u UnmarshalFunc) error

SetMimetype 修改编解码函数

type Context added in v0.40.0

type Context struct {
	Response http.ResponseWriter
	Request  *http.Request

	// 指定输出时所使用的媒体类型,以及名称
	OutputMimetype     MarshalFunc
	OutputMimetypeName string

	// 输出到客户端的字符集
	//
	// 若值为 encoding.Nop 或是空,表示为 utf-8
	OutputCharset     encoding.Encoding
	OutputCharsetName string

	// 客户端内容所使用的媒体类型
	InputMimetype UnmarshalFunc

	// 客户端内容所使用的字符集
	//
	// 若值为 encoding.Nop 或是空,表示为 utf-8
	InputCharset encoding.Encoding

	// 输出语言的相关设置项
	OutputTag     language.Tag
	LocalePrinter *message.Printer
	// contains filtered or unexported fields
}

Context 单次请求生成的上下文数据

NOTE: 用户不应该直接引用该对象,而是 server.Context。

func (*Context) Body added in v0.40.0

func (ctx *Context) Body() (body []byte, err error)

Body 获取用户提交的内容

相对于 ctx.Request.Body,此函数可多次读取。不存在 body 时,返回 nil

func (*Context) Fprint added in v0.40.0

func (ctx *Context) Fprint(w io.Writer, v ...interface{}) (int, error)

Fprint 相当于 ctx.LocalePrinter.Fprint

func (*Context) Fprintf added in v0.40.0

func (ctx *Context) Fprintf(w io.Writer, key message.Reference, v ...interface{}) (int, error)

Fprintf 相当于 ctx.LocalePrinter.Fprintf

func (*Context) Fprintln added in v0.40.0

func (ctx *Context) Fprintln(w io.Writer, v ...interface{}) (int, error)

Fprintln 相当于 ctx.LocalePrinter.Fprintln

func (*Context) Marshal added in v0.40.0

func (ctx *Context) Marshal(status int, v interface{}, headers map[string]string) error

Marshal 将 v 解码并发送给客户端

NOTE: 若 v 是一个 nil 值,则不会向客户端输出任何内容; 若是需要正常输出一个 nil 类型到客户端(比如JSON 中的 null), 可以传递一个 *struct{} 值,或是自定义实现相应的解码函数。

NOTE: 如果需要指定一个特定的 Content-Type 和 Content-Language, 可以在 headers 中指定,否则使用当前的编码和语言名称。

func (*Context) Print added in v0.40.0

func (ctx *Context) Print(v ...interface{}) (int, error)

Print 相当于 ctx.LocalePrinter.Print

func (*Context) Printf added in v0.40.0

func (ctx *Context) Printf(key message.Reference, v ...interface{}) (int, error)

Printf 相当于 ctx.LocalePrinter.Printf

func (*Context) Println added in v0.40.0

func (ctx *Context) Println(v ...interface{}) (int, error)

Println 相当于 ctx.LocalePrinter.Println

func (*Context) ServeContent added in v0.40.0

func (ctx *Context) ServeContent(buf io.ReadSeeker, name string, mod time.Time, headers map[string]string)

ServeContent 将一块内存中的内容转换为文件提供下载

功能与 http.ServeContent 相同,提供了可自定义报头的功能。

func (*Context) ServeFS added in v0.40.0

func (ctx *Context) ServeFS(f fs.FS, p, index string, headers map[string]string) error

ServeFS 提供基于 fs.FS 的文件下载服

p 表示文件地址,用户应该保证 p 的正确性; 如果 p 是目录,则会自动读 p 目录下的 index 文件, 如果 index 为空,则采用 DefaultIndexPage 作为其默认值。

func (*Context) Sprint added in v0.40.0

func (ctx *Context) Sprint(v ...interface{}) string

Sprint 相当于 ctx.LocalePrinter.Sprint

func (*Context) Sprintf added in v0.40.0

func (ctx *Context) Sprintf(key message.Reference, v ...interface{}) string

Sprintf 相当于 ctx.LocalePrinter.Sprintf

func (*Context) Sprintln added in v0.40.0

func (ctx *Context) Sprintln(v ...interface{}) string

Sprintln 相当于 ctx.LocalePrinter.Sprintln

func (*Context) Unmarshal added in v0.40.0

func (ctx *Context) Unmarshal(v interface{}) error

Unmarshal 将提交的内容转换成 v 对象

func (*Context) Upload added in v0.40.0

func (ctx *Context) Upload(field string, u *upload.Upload) ([]string, error)

Upload 执行上传文件的相关操作

返回的是文件列表

type Fields added in v0.40.0

type Fields = validation.Messages

Fields 表示字段的错误信息列表

原始类型为 map[string][]string

type LocaleBuilder added in v0.40.0

type LocaleBuilder struct {
	// contains filtered or unexported fields
}

LocaleBuilder 带 tag 值的 catalog.Builder

func (*LocaleBuilder) Set added in v0.40.0

func (b *LocaleBuilder) Set(key string, msg ...catalog.Message) error

func (*LocaleBuilder) SetMacro added in v0.40.0

func (b *LocaleBuilder) SetMacro(key string, msg ...catalog.Message) error

func (*LocaleBuilder) SetString added in v0.40.0

func (b *LocaleBuilder) SetString(key, msg string) error

type MarshalFunc

type MarshalFunc func(v interface{}) ([]byte, error)

MarshalFunc 序列化函数原型

type Result added in v0.40.0

type Result interface {
	// 添加详细的错误信息
	//
	// 相同的 key 应该能关联多个 val 值。
	Add(key string, val ...string)

	// 设置详细的错误信息
	//
	// 如果已经相同的 key,会被覆盖。
	Set(key string, val ...string)

	// 是否存在详细的错误信息
	//
	// 如果有通过 Add 添加内容,那么应该返回 true
	HasFields() bool

	// HTTP 状态码
	//
	// 最终会经此值作为 HTTP 状态会返回给用户
	Status() int
}

Result 自定义错误代码的实现接口

一般是对客户端提交数据 400 的具体反馈信息。 用户可以根据自己的需求,展示自定义的错误码以及相关的错误信息格式。 该对象最终也是调用 MarshalFunc 进行解码输出。 只要该对象同时实现了 Result 接口即可。

比如类似以下的错误内容:

{
    'message': 'error message',
    'code': 4000001,
    'detail':[
        {'field': 'username': 'message': '已经存在相同用户名'},
        {'field': 'username': 'message': '已经存在相同用户名'},
    ]
}

func DefaultBuilder added in v0.40.0

func DefaultBuilder(status, code int, message string) Result

DefaultBuilder 默认的 BuildResultFunc 实现

定义了以下格式的返回信息:

JSON:

{
    'message': 'error message',
    'code': 4000001,
    'fields':[
        {'name': 'username': 'message': ['名称过短', '不能包含特殊符号']},
        {'name': 'password': 'message': ['不能为空']},
    ]
}

XML:

<result code="400">
    <message>error message</message>
    <field name="username">
        <message>名称过短</message>
        <message>不能包含特殊符号</message>
    </field>
    <field name="password"><message>不能为空</message></field>
</result>

YAML:

message: 'error message'
code: 40000001
fields:
  - name: username
    message:
      - 名称过短
      - 不能包含特殊符号
  - name: password
    message:
      - 不能为空

FormData:

message=errormessage&code=4000001&fields.username=名称过短&fields.username=不能包含特殊符号&fields.password=不能为空

type UnmarshalFunc

type UnmarshalFunc func([]byte, interface{}) error

UnmarshalFunc 反序列化函数原型

Directories

Path Synopsis
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
Package gob 提供 GOB 格式的编解码
Package gob 提供 GOB 格式的编解码
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewContent() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) { ctx.Render(200, html.Tpl("index", map[string]interface{}{...}), nil) }
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewContent() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) { ctx.Render(200, html.Tpl("index", map[string]interface{}{...}), nil) }
Package text 针对文本内容的编解码实现
Package text 针对文本内容的编解码实现
testobject
Package testobject 用于测试 mimetype 的对象
Package testobject 用于测试 mimetype 的对象

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL