MCPcopy
hub / github.com/segmentio/kafka-go / UnmarshalText

Method UnmarshalText

compress/compress.go:45–78  ·  view source on GitHub ↗
(b []byte)

Source from the content-addressed store, hash-verified

43}
44
45func (c *Compression) UnmarshalText(b []byte) error {
46 switch string(b) {
47 case "none", "uncompressed":
48 *c = None
49 return nil
50 }
51
52 for _, codec := range Codecs[None+1:] {
53 if codec.Name() == string(b) {
54 *c = Compression(codec.Code())
55 return nil
56 }
57 }
58
59 i, err := strconv.ParseInt(string(b), 10, 64)
60 if err == nil && i >= 0 && i < int64(len(Codecs)) {
61 *c = Compression(i)
62 return nil
63 }
64
65 s := &strings.Builder{}
66 s.WriteString("none, uncompressed")
67
68 for i, codec := range Codecs[None+1:] {
69 if i < (len(Codecs) - 1) {
70 s.WriteString(", ")
71 } else {
72 s.WriteString(", or ")
73 }
74 s.WriteString(codec.Name())
75 }
76
77 return fmt.Errorf("compression format must be one of %s, not %q", s, b)
78}
79
80var (
81 _ encoding.TextMarshaler = Compression(0)

Callers 1

testEncodeDecodeFunction · 0.45

Calls 4

CompressionTypeAlias · 0.70
NameMethod · 0.65
CodeMethod · 0.65
WriteStringMethod · 0.45

Tested by 1

testEncodeDecodeFunction · 0.36