@@ -39,6 +39,12 @@ func (s *WebCodecService) AddCodec(codec codecs.Codec) {
39
39
s .codecs = append (s .codecs , codec )
40
40
}
41
41
42
+ func (s * WebCodecService ) assertCodecs () {
43
+ if len (s .codecs ) == 0 {
44
+ panic ("codecs: No codecs are installed - use AddCodec to add some or use NewWebCodecService for default codecs." )
45
+ }
46
+ }
47
+
42
48
// GetCodecForResponding gets the codec to use to respond based on the
43
49
// given accept string, the extension provided and whether it has a callback
44
50
// or not.
@@ -47,6 +53,9 @@ func (s *WebCodecService) AddCodec(codec codecs.Codec) {
47
53
// This may be changed if additional callback capable codecs are added.
48
54
func (s * WebCodecService ) GetCodecForResponding (accept , extension string , hasCallback bool ) (codecs.Codec , error ) {
49
55
56
+ // make sure we have at least one codec
57
+ s .assertCodecs ()
58
+
50
59
// is there a callback? If so, look for JSONP
51
60
if hasCallback {
52
61
for _ , codec := range s .codecs {
@@ -74,6 +83,9 @@ func (s *WebCodecService) GetCodecForResponding(accept, extension string, hasCal
74
83
// content type.
75
84
func (s * WebCodecService ) GetCodec (contentType string ) (codecs.Codec , error ) {
76
85
86
+ // make sure we have at least one codec
87
+ s .assertCodecs ()
88
+
77
89
for _ , codec := range s .codecs {
78
90
79
91
// default codec
@@ -97,6 +109,9 @@ func (s *WebCodecService) GetCodec(contentType string) (codecs.Codec, error) {
97
109
// marshalled instead.
98
110
func (s * WebCodecService ) MarshalWithCodec (codec codecs.Codec , object interface {}, options map [string ]interface {}) ([]byte , error ) {
99
111
112
+ // make sure we have at least one codec
113
+ s .assertCodecs ()
114
+
100
115
// get the public data
101
116
publicData , err := codecs .PublicData (object , options )
102
117
@@ -111,5 +126,9 @@ func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{
111
126
112
127
// UnmarshalWithCodec unmarshals the specified data into the object with the specified codec.
113
128
func (s * WebCodecService ) UnmarshalWithCodec (codec codecs.Codec , data []byte , object interface {}) error {
129
+
130
+ // make sure we have at least one codec
131
+ s .assertCodecs ()
132
+
114
133
return codec .Unmarshal (data , object )
115
134
}
0 commit comments