prepareNext adds possible comma and indentation for the next value based on last type and indent option. It also updates lastKind to next.
(next kind)
| 226 | // prepareNext adds possible comma and indentation for the next value based |
| 227 | // on last type and indent option. It also updates lastKind to next. |
| 228 | func (e *Encoder) prepareNext(next kind) { |
| 229 | defer func() { |
| 230 | // Set lastKind to next. |
| 231 | e.lastKind = next |
| 232 | }() |
| 233 | |
| 234 | if len(e.indent) == 0 { |
| 235 | // Need to add comma on the following condition. |
| 236 | if e.lastKind&(scalar|objectClose|arrayClose) != 0 && |
| 237 | next&(name|scalar|objectOpen|arrayOpen) != 0 { |
| 238 | e.out = append(e.out, ',') |
| 239 | // For single-line output, add a random extra space after each |
| 240 | // comma to make output unstable. |
| 241 | if detrand.Bool() { |
| 242 | e.out = append(e.out, ' ') |
| 243 | } |
| 244 | } |
| 245 | return |
| 246 | } |
| 247 | |
| 248 | switch { |
| 249 | case e.lastKind&(objectOpen|arrayOpen) != 0: |
| 250 | // If next type is NOT closing, add indent and newline. |
| 251 | if next&(objectClose|arrayClose) == 0 { |
| 252 | e.indents = append(e.indents, e.indent...) |
| 253 | e.out = append(e.out, '\n') |
| 254 | e.out = append(e.out, e.indents...) |
| 255 | } |
| 256 | |
| 257 | case e.lastKind&(scalar|objectClose|arrayClose) != 0: |
| 258 | switch { |
| 259 | // If next type is either a value or name, add comma and newline. |
| 260 | case next&(name|scalar|objectOpen|arrayOpen) != 0: |
| 261 | e.out = append(e.out, ',', '\n') |
| 262 | |
| 263 | // If next type is a closing object or array, adjust indentation. |
| 264 | case next&(objectClose|arrayClose) != 0: |
| 265 | e.indents = e.indents[:len(e.indents)-len(e.indent)] |
| 266 | e.out = append(e.out, '\n') |
| 267 | } |
| 268 | e.out = append(e.out, e.indents...) |
| 269 | |
| 270 | case e.lastKind&name != 0: |
| 271 | e.out = append(e.out, ' ') |
| 272 | // For multi-line output, add a random extra space after key: to make |
| 273 | // output unstable. |
| 274 | if detrand.Bool() { |
| 275 | e.out = append(e.out, ' ') |
| 276 | } |
| 277 | } |
| 278 | } |
no test coverage detected