本文整理汇总了Golang中github.com/stretchr/codecs.Codec类的典型用法代码示例。如果您正苦于以下问题:Golang Codec类的具体用法?Golang Codec怎么用?Golang Codec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Codec类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: UnmarshalWithCodec
// UnmarshalWithCodec unmarshals the specified data into the object with the specified codec.
func (s *WebCodecService) UnmarshalWithCodec(codec codecs.Codec, data []byte, object interface{}) error {
// make sure we have at least one codec
s.assertCodecs()
return codec.Unmarshal(data, object)
}
开发者ID:Radiobox,项目名称:codecs,代码行数:8,代码来源:web_codec_service.go
示例2: TestGetCodec
func TestGetCodec(t *testing.T) {
service := NewWebCodecService()
var codec codecs.Codec
codec, _ = service.GetCodec(constants.ContentTypeJSON)
if assert.NotNil(t, codec, "Json should exist") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
}
// case insensitivity
codec, _ = service.GetCodec(strings.ToUpper(constants.ContentTypeJSON))
if assert.NotNil(t, codec, "Content case should not matter") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
}
// with noise
codec, _ = service.GetCodec(fmt.Sprintf("%s; charset=UTF-8", constants.ContentTypeJSON))
if assert.NotNil(t, codec, "charset in Content-Type should not matter") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
}
// default
codec, _ = service.GetCodec("")
if assert.NotNil(t, codec, "Empty contentType string should assume JSON") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "Should assume JSON.")
}
}
开发者ID:syohex,项目名称:codecs,代码行数:32,代码来源:web_codec_service_test.go
示例3: TestGetCodecForResponding_DefaultCodec
func TestGetCodecForResponding_DefaultCodec(t *testing.T) {
service := NewWebCodecService()
var codec codecs.Codec
codec, _ = service.GetCodecForResponding("", "", false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension should default to JSON") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "Should default to JSON")
}
}
开发者ID:syohex,项目名称:codecs,代码行数:12,代码来源:web_codec_service_test.go
示例4: MarshalWithCodec
// MarshalWithCodec marshals the specified object with the specified codec and options.
// If the object implements the Facade interface, the PublicData object should be
// marshalled instead.
func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options map[string]interface{}) ([]byte, error) {
// make sure we have at least one codec
s.assertCodecs()
// get the public data
publicData, err := codecs.PublicData(object, options)
// if there was an error - return it
if err != nil {
return nil, err
}
// let the codec do its work
return codec.Marshal(publicData, options)
}
开发者ID:Radiobox,项目名称:codecs,代码行数:19,代码来源:web_codec_service.go
示例5: TestGetCodecForResponding_Remove
func TestGetCodecForResponding_Remove(t *testing.T) {
service := NewWebCodecService()
var codec codecs.Codec
// File extension takes precedence over accept header
codec, _ = service.GetCodecForResponding(constants.ContentTypeJSON, constants.FileExtensionXML, false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeXML, codec.ContentType(), "Extension takes precedence over accept")
}
service.RemoveCodec(constants.ContentTypeXML)
codec, _ = service.GetCodecForResponding(constants.ContentTypeJSON, constants.FileExtensionXML, false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "XML codec should have been removed")
}
}
开发者ID:syohex,项目名称:codecs,代码行数:22,代码来源:web_codec_service_test.go
示例6: TestGetCodecForResponding
func TestGetCodecForResponding(t *testing.T) {
service := NewWebCodecService()
var codec codecs.Codec
// JSON - accept header
codec, _ = service.GetCodecForResponding("something/something,application/json,text/xml", "", false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson 1")
}
// JSON - accept header (case)
codec, _ = service.GetCodecForResponding("something/something,application/JSON,text/xml", "", false)
if assert.NotNil(t, codec, "Case should not matter") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "Case should not matter")
}
// JSON - file extension
codec, _ = service.GetCodecForResponding("", constants.FileExtensionJSON, false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
}
// JSONP - has callback
codec, _ = service.GetCodecForResponding("", "", true)
if assert.NotNil(t, codec, "Should return the first codec that can handle a callback") {
assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript")
}
// JSONP - file extension
codec, _ = service.GetCodecForResponding("", constants.FileExtensionJSONP, false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript")
}
// JSONP - file extension (case)
codec, _ = service.GetCodecForResponding("", strings.ToUpper(constants.FileExtensionJSONP), false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript 4")
}
// JSONP - Accept header
codec, _ = service.GetCodecForResponding("something/something,text/javascript,text/xml", "", false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript 5")
}
// hasCallback takes precedence over everything else
codec, _ = service.GetCodecForResponding(constants.ContentTypeJSON, constants.FileExtensionXML, true)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "HasCallback takes precedence over all")
}
// File extension takes precedence over accept header
codec, _ = service.GetCodecForResponding(constants.ContentTypeJSON, constants.FileExtensionXML, false)
if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
assert.Equal(t, constants.ContentTypeXML, codec.ContentType(), "Extension takes precedence over accept")
}
}
开发者ID:syohex,项目名称:codecs,代码行数:78,代码来源:web_codec_service_test.go
示例7: SetCodec
// SetCodec sets the codec to be used to communicate with Stretchr.
func (s *Session) SetCodec(codec codecs.Codec) {
Tracer.TraceDebug("Setting codec: %s", codec.ContentType())
s.codec = codec
}
开发者ID:stretchr,项目名称:sdk-go,代码行数:5,代码来源:session.go
注:本文中的github.com/stretchr/codecs.Codec类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论