当前位置: 首页>>代码示例>>Golang>>正文


Golang mime.FormatMediaType函数代码示例

本文整理汇总了Golang中mime.FormatMediaType函数的典型用法代码示例。如果您正苦于以下问题:Golang FormatMediaType函数的具体用法?Golang FormatMediaType怎么用?Golang FormatMediaType使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了FormatMediaType函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: CollectionOf

// CollectionOf creates a collection media type from its element media type. A collection media
// type represents the content of responses that return a collection of resources such as "list"
// actions. This function can be called from any place where a media type can be used.
// The resulting media type identifier is built from the element media type by appending the media
// type parameter "type" with value "collection".
func CollectionOf(v interface{}, dsl ...func()) *design.MediaTypeDefinition {
	if generatedMediaTypes == nil {
		generatedMediaTypes = make(map[string]*design.MediaTypeDefinition)
	}
	var m *design.MediaTypeDefinition
	var ok bool
	m, ok = v.(*design.MediaTypeDefinition)
	if !ok {
		if id, ok := v.(string); ok {
			m = design.Design.MediaTypes[design.CanonicalIdentifier(id)]
		}
	}
	if m == nil {
		ReportError("invalid CollectionOf argument: not a media type and not a known media type identifier")
		return nil
	}
	id := m.Identifier
	mediatype, params, err := mime.ParseMediaType(id)
	if err != nil {
		ReportError("invalid media type identifier %#v: %s", id, err)
		return nil
	}
	hasType := false
	for param := range params {
		if param == "type" {
			hasType = true
			break
		}
	}
	if !hasType {
		params["type"] = "collection"
	}
	id = mime.FormatMediaType(mediatype, params)
	typeName := m.TypeName + "Collection"
	if mt, ok := generatedMediaTypes[typeName]; ok {
		// Already have a type for this collection, reuse it.
		return mt
	}
	mt := design.NewMediaTypeDefinition(typeName, id, func() {
		if mt, ok := mediaTypeDefinition(true); ok {
			mt.TypeName = typeName
			mt.AttributeDefinition = &design.AttributeDefinition{Type: ArrayOf(m)}
			if len(dsl) > 0 {
				executeDSL(dsl[0], mt)
			}
			if mt.Views == nil {
				// If the DSL didn't create any views (or there is no DSL at all)
				// then inherit the views from the collection element.
				mt.Views = make(map[string]*design.ViewDefinition)
				for n, v := range m.Views {
					mt.Views[n] = v
				}
			}
		}
	})
	// Do not execute the DSL right away, will be done last to make sure the element DSL has run
	// first.
	generatedMediaTypes[mt.TypeName] = mt
	return mt
}
开发者ID:cw2018,项目名称:goa,代码行数:65,代码来源:media_type.go

示例2: AddMultipart

// AddMultipart creates a nested part with mediaType and a randomly generated
// boundary. The returned nested part can then be used to add a text or
// an attachment.
//
// Example:
// 	alt, _ := part.AddMultipart("multipart/mixed")
// 	alt.AddText("text/plain", text)
// 	alt.AddAttachment("gopher.png", "", image)
// 	alt.Close()
func (p *Multipart) AddMultipart(mediaType string) (nested *Multipart, err error) {
	if !strings.HasPrefix(mediaType, "multipart") {
		return nil, errors.New("mail: mediaType must start with the word \"multipart\" as in multipart/mixed or multipart/alter")
	}

	if p.isClosed {
		return nil, ErrPartClosed
	}

	boundary := randomString(boundaryLength)

	// Mutlipart management
	var mimeType string
	if strings.HasPrefix(mediaType, "multipart") {
		mimeType = mime.FormatMediaType(
			mediaType,
			map[string]string{"boundary": boundary},
		)
	} else {
		mimeType = mediaType
	}

	// Header
	p.header = make(textproto.MIMEHeader)
	p.header["Content-Type"] = []string{mimeType}

	w, err := p.writer.CreatePart(p.header)
	if err != nil {
		return nil, err
	}

	nested = createPart(w, p.header, mediaType, boundary)
	return nested, nil
}
开发者ID:JaTochNietDan,项目名称:mail,代码行数:43,代码来源:message.go

示例3: contentType

func (post *Post) contentType() string {
	params := map[string]string{"type": post.Type}
	if post.Notification {
		params["rel"] = "https://tent.io/rels/notification"
	}
	return mime.FormatMediaType(MediaTypePost, params)
}
开发者ID:hendrikcech,项目名称:tent-client-go,代码行数:7,代码来源:post.go

示例4: fmtContentType

func fmtContentType(s string) string {
	m, p, err := mime.ParseMediaType(s)
	if err != nil {
		return s
	}
	return mime.FormatMediaType(m, p)
}
开发者ID:fanyang01,项目名称:crawler,代码行数:7,代码来源:fetch.go

示例5: Fuzz

func Fuzz(data []byte) int {
	sdata := string(data)
	mt, params, err := mime.ParseMediaType(sdata)
	if err != nil {
		return 0
	}
	sdata1 := mime.FormatMediaType(mt, params)
	mt1, params1, err := mime.ParseMediaType(sdata1)
	if err != nil {
		if err.Error() == "mime: no media type" {
			// https://github.com/golang/go/issues/11289
			return 0
		}
		if err.Error() == "mime: invalid media parameter" {
			// https://github.com/golang/go/issues/11290
			return 0
		}
		fmt.Printf("%q(%q, %+v) -> %q\n", sdata, mt, params, sdata1)
		panic(err)
	}
	if !fuzz.DeepEqual(mt, mt1) {
		fmt.Printf("%q -> %q\n", mt, mt1)
		panic("mediatype changed")
	}
	if !fuzz.DeepEqual(params, params1) {
		fmt.Printf("%+v -> %+v\n", params, params1)
		panic("params changed")
	}
	return 1
}
开发者ID:sjn1978,项目名称:go-fuzz,代码行数:30,代码来源:main.go

示例6: IsError

// IsError returns true if the media type is implemented via a goa struct.
func (m *MediaTypeDefinition) IsError() bool {
	base, params, err := mime.ParseMediaType(m.Identifier)
	if err != nil {
		panic("invalid media type identifier " + m.Identifier) // bug
	}
	delete(params, "view")
	return mime.FormatMediaType(base, params) == ErrorMedia.Identifier
}
开发者ID:konstantin-dzreev,项目名称:goa,代码行数:9,代码来源:types.go

示例7: projectIdentifier

// projectIdentifier computes the projected media type identifier by adding the "view" param.  We
// need the projected media type identifier to be different so that looking up projected media types
// from ProjectedMediaTypes works correctly. It's also good for clients.
func (m *MediaTypeDefinition) projectIdentifier(view string) string {
	base, params, err := mime.ParseMediaType(m.Identifier)
	if err != nil {
		base = m.Identifier
	}
	params["view"] = view
	return mime.FormatMediaType(base, params)
}
开发者ID:konstantin-dzreev,项目名称:goa,代码行数:11,代码来源:types.go

示例8: SetContentType

// SetContentType returns the MIME type of the message.
func (m *Message) SetContentType(mediaType string) {
	_, params, _ := mime.ParseMediaType(m.GetHeader("Content-Type"))
	if m.root != nil {
		params["boundary"] = m.root.Boundary()
	}

	m.SetHeader("Content-Type", mime.FormatMediaType(mediaType, params))
}
开发者ID:JaTochNietDan,项目名称:mail,代码行数:9,代码来源:message.go

示例9: projectCanonical

// projectIdentifier computes the projected canonical media type identifier by adding the "view"
// param if the view is not the default view.
func (m *MediaTypeDefinition) projectCanonical(view string) string {
	cano := CanonicalIdentifier(m.Identifier)
	base, params, _ := mime.ParseMediaType(cano)
	if params["view"] != "" {
		return cano // Already projected
	}
	params["view"] = view
	return mime.FormatMediaType(base, params)
}
开发者ID:konstantin-dzreev,项目名称:goa,代码行数:11,代码来源:types.go

示例10: MediaType

func MediaType(name string, params ...string) string {
	if len(params)%2 != 0 {
		panic(errors.New("params must be an even sized list"))
	}
	p := make(map[string]string, len(params)/2)
	for i := 0; i < len(params); i += 2 {
		p[params[i]] = params[i+1]
	}
	return mime.FormatMediaType(name, p)
}
开发者ID:stevan,项目名称:file-editor-server,代码行数:10,代码来源:util.go

示例11: NewMultipart

// NewMultipart modifies msg to become a multipart message and returns the root
// part inside which other parts, texts and attachments can be nested.
//
// Example:
// 	multipart := NewMultipart("multipart/alternative", msg)
// 	multipart.AddPart("text/plain", text)
// 	multipart.AddPart("text/html", html)
// 	multipart.Close()
func NewMultipart(mediaType string, msg *Message) (root *Multipart) {
	boundary := randomString(boundaryLength)
	msg.root = createPart(msg.Body, make(textproto.MIMEHeader), mediaType, boundary)
	_, params, _ := mime.ParseMediaType(msg.GetHeader("Content-Type"))
	if params == nil {
		params = make(map[string]string)
	}
	params["boundary"] = boundary
	msg.SetHeader("Content-Type", mime.FormatMediaType(mediaType, params))
	return msg.root
}
开发者ID:JaTochNietDan,项目名称:mail,代码行数:19,代码来源:message.go

示例12: CanonicalIdentifier

// CanonicalIdentifier returns the media type identifier sans suffix
// which is what the DSL uses to store and lookup media types.
func CanonicalIdentifier(identifier string) string {
	base, params, err := mime.ParseMediaType(identifier)
	if err != nil {
		return identifier
	}
	id := base
	if i := strings.Index(id, "+"); i != -1 {
		id = id[:i]
	}
	return mime.FormatMediaType(id, params)
}
开发者ID:goist,项目名称:goa,代码行数:13,代码来源:definitions.go

示例13: MediaType

// MediaType implements the media type definition DSL. A media type definition describes the
// representation of a resource used in a response body.
//
// Media types are defined with a unique identifier as defined by RFC6838. The identifier also
// defines the default value for the Content-Type header of responses. The ContentType DSL allows
// overridding the default as shown in the example below.
//
// The media type definition includes a listing of all the potential attributes that can appear in
// the body. Views specify which of the attributes are actually rendered so that the same media type
// definition may represent multiple rendering of a given resource representation.
//
// All media types must define a view named "default". This view is used to render the media type in
// response bodies when no other view is specified.
//
// A media type definition may also define links to other media types. This is done by first
// defining an attribute for the linked-to media type and then referring to that attribute in the
// Links DSL. Views may then elect to render one or the other or both. Links are rendered using the
// special "link" view. Media types that are linked to must define that view. Here is an example
// showing all the possible media type sub-definitions:
//
//    MediaType("application/vnd.goa.example.bottle", func() {
//        Description("A bottle of wine")
//        TypeName("BottleMedia")         // Override default generated name
//        ContentType("application/json") // Override default Content-Type header value
//        Attributes(func() {
//            Attribute("id", Integer, "ID of bottle")
//            Attribute("href", String, "API href of bottle")
//            Attribute("account", Account, "Owner account")
//            Attribute("origin", Origin, "Details on wine origin")
//            Links(func() {
//                Link("account")         // Defines link to Account media type
//                Link("origin", "tiny")  // Set view used to render link if not "link"
//            })
//            Required("id", "href")
//        })
//        View("default", func() {
//            Attribute("id")
//            Attribute("href")
//            Attribute("links")          // Renders links
//        })
//        View("extended", func() {
//            Attribute("id")
//            Attribute("href")
//            Attribute("account")        // Renders account inline
//            Attribute("origin")         // Renders origin inline
//            Attribute("links")          // Renders links
//        })
//     })
//
// This function returns the media type definition so it can be referred to throughout the apidsl.
func MediaType(identifier string, apidsl func()) *design.MediaTypeDefinition {
	if design.Design.MediaTypes == nil {
		design.Design.MediaTypes = make(map[string]*design.MediaTypeDefinition)
	}

	if !dslengine.IsTopLevelDefinition() {
		dslengine.IncompatibleDSL()
		return nil
	}

	// Validate Media Type
	identifier, params, err := mime.ParseMediaType(identifier)
	if err != nil {
		dslengine.ReportError("invalid media type identifier %#v: %s",
			identifier, err)
		// We don't return so that other errors may be
		// captured in this one run.
		identifier = "text/plain"
	}
	canonicalID := design.CanonicalIdentifier(identifier)
	// Validate that media type identifier doesn't clash
	if _, ok := design.Design.MediaTypes[canonicalID]; ok {
		dslengine.ReportError("media type %#v with canonical identifier %#v is defined twice", identifier, canonicalID)
		return nil
	}
	identifier = mime.FormatMediaType(identifier, params)
	lastPart := identifier
	lastPartIndex := strings.LastIndex(identifier, "/")
	if lastPartIndex > -1 {
		lastPart = identifier[lastPartIndex+1:]
	}
	plusIndex := strings.Index(lastPart, "+")
	if plusIndex > 0 {
		lastPart = lastPart[:plusIndex]
	}
	lastPart = strings.TrimPrefix(lastPart, "vnd.")
	elems := strings.Split(lastPart, ".")
	for i, e := range elems {
		elems[i] = strings.Title(e)
	}
	typeName := strings.Join(elems, "")
	if typeName == "" {
		mediaTypeCount++
		typeName = fmt.Sprintf("MediaType%d", mediaTypeCount)
	}
	// Now save the type in the API media types map
	mt := design.NewMediaTypeDefinition(typeName, identifier, apidsl)
	design.Design.MediaTypes[canonicalID] = mt
	return mt
}
开发者ID:konstantin-dzreev,项目名称:goa,代码行数:100,代码来源:media_type.go

示例14: SetBodyWithCharset

// SetBodyWithCharset translates and sets the body according to given charset.
//
// Header field Content-Transfer-Encoding is set to DefaultTransferEncoding.
// Header field Content-Type is set according to charset.
// All lines are modified to ensure CRLF.
//
// Use SetBody to use default character encoding.
func (m *Message) SetBodyWithCharset(charset, body string) error {
	m.Header.Set(HEADER_CONTENT_TRANSFER_ENCODING, DefaultTransferEncoding)
	m.Header.Set(HEADER_CONTENT_TYPE, mime.FormatMediaType(
		"text/plain",
		map[string]string{"charset": DefaultCharset},
	))

	bytes, err := StringToBody(body, DefaultCharset)
	if err != nil {
		return err
	}

	m.body = bytes
	m.Header.Set(HEADER_BODY, fmt.Sprintf("%d", len(bytes)))
	return nil
}
开发者ID:LA3QMA,项目名称:wl2k-go,代码行数:23,代码来源:message.go

示例15: ServeHTTP

func (ar assertResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	t := asserts.MediaType
	if ar.bundle {
		t = mime.FormatMediaType(t, map[string]string{"bundle": "y"})
	}
	w.Header().Set("Content-Type", t)
	w.Header().Set("X-Ubuntu-Assertions-Count", strconv.Itoa(len(ar.assertions)))
	enc := asserts.NewEncoder(w)
	for _, a := range ar.assertions {
		err := enc.Encode(a)
		if err != nil {
			logger.Noticef("unable to write encoded assertion into response: %v", err)
			break

		}
	}
}
开发者ID:alecu,项目名称:snappy,代码行数:17,代码来源:response.go


注:本文中的mime.FormatMediaType函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。