writer.go (2968B)
1 package tplc 2 3 import ( 4 "bytes" 5 "log" 6 "strconv" 7 "strings" 8 ) 9 10 type Writer struct { 11 builder *Builder 12 buf []byte 13 } 14 15 func (w *Writer) Grow(n int) { 16 w.appendMethod(strconv.FormatInt(int64(n), 10), "w.Grow") 17 } 18 19 func (w *Writer) WriteRaw(s string) { 20 w.appendMethod(s, "w.WriteRaw") 21 } 22 23 func (w *Writer) WriteRawBytes(s []byte) { 24 w.appendMethodBytes(s, "w.WriteRaw") 25 } 26 27 func (w *Writer) WriteRawWithBacktick(s string) { 28 w.appendMethodWithBacktick(s, "w.WriteRaw") 29 } 30 31 func (w *Writer) WriteRawBytesWithBacktick(s []byte) { 32 w.appendMethodBytesWithBacktick(s, "w.WriteRaw") 33 } 34 35 func (w *Writer) WriteString(s string) { 36 w.appendMethod(s, "w.WriteString") 37 } 38 39 func (w *Writer) WriteStringBytes(s []byte) { 40 w.appendMethodBytes(s, "w.WriteString") 41 } 42 43 func (w *Writer) WriteStringWithBacktick(s string) { 44 w.appendMethodWithBacktick(s, "w.WriteString") 45 } 46 47 func (w *Writer) WriteStringBytesWithBacktick(s []byte) { 48 w.appendMethodBytesWithBacktick(s, "w.WriteString") 49 } 50 51 func (w *Writer) WriteByteString(s string) { 52 w.appendMethod(s, "w.WriteByteString") 53 } 54 55 func (w *Writer) WriteByteStringBytes(s []byte) { 56 w.appendMethodBytes(s, "w.WriteByteString") 57 } 58 59 func (w *Writer) WriteInteger(s string) { 60 w.appendMethod(s, "w.WriteInteger") 61 } 62 63 func (w *Writer) WriteIntegerBytes(s []byte) { 64 w.appendMethodBytes(s, "w.WriteInteger") 65 } 66 67 func (w *Writer) WriteFloat(s, precision string) { 68 if len(precision) == 0 { 69 precision = "-1" 70 } 71 w.appendMethod( 72 strings.Join([]string{s, precision}, ", "), 73 "w.WriteFloat", 74 ) 75 } 76 77 func (w *Writer) WriteFloatBytes(s []byte, precision []byte) { 78 if len(precision) == 0 { 79 precision = []byte("-1") 80 } 81 w.appendMethodBytes( 82 bytes.Join([][]byte{s, precision}, []byte{','}), 83 "w.WriteFloat", 84 ) 85 } 86 87 func (w *Writer) appendMethod(s string, method string) { 88 w.buf = append(w.buf, method...) 89 w.buf = append(w.buf, '(') 90 w.buf = append(w.buf, s...) 91 w.buf = append(w.buf, ")\n"...) 92 } 93 94 func (w *Writer) appendMethodBytes(s []byte, method string) { 95 w.buf = append(w.buf, method...) 96 w.buf = append(w.buf, '(') 97 w.buf = append(w.buf, s...) 98 w.buf = append(w.buf, ")\n"...) 99 } 100 101 func (w *Writer) appendMethodWithBacktick(s string, method string) { 102 w.buf = append(w.buf, method...) 103 w.buf = append(w.buf, '(') 104 var err error 105 w.buf, err = appendBacktickString(w.buf, s) 106 if err != nil { 107 log.Fatalln(err) 108 } 109 w.buf = append(w.buf, ")\n"...) 110 } 111 112 func (w *Writer) appendMethodBytesWithBacktick(s []byte, method string) { 113 w.buf = append(w.buf, method...) 114 w.buf = append(w.buf, '(') 115 var err error 116 w.buf, err = appendBacktickByteString(w.buf, s) 117 if err != nil { 118 log.Fatalln(err) 119 } 120 w.buf = append(w.buf, ")\n"...) 121 } 122 123 func (w *Writer) WriteGoCode(ss ...string) { 124 for _, s := range ss { 125 w.buf = append(w.buf, s...) 126 } 127 } 128 129 func (w *Writer) WriteGoCodeBytes(zz ...[]byte) { 130 for _, z := range zz { 131 w.buf = append(w.buf, z...) 132 } 133 } 134 135 func (w *Writer) Bytes() []byte { 136 return w.buf 137 } 138 139 func (w *Writer) String() string { 140 return string(w.buf) 141 }