github.phpd.cn/cilium/cilium@v1.6.12/test/helpers/filterBuffer.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package helpers
    16  
    17  import (
    18  	"bytes"
    19  	"strings"
    20  )
    21  
    22  // FilterBuffer structs that extends buffer methods
    23  type FilterBuffer struct {
    24  	*bytes.Buffer
    25  }
    26  
    27  // ByLines returns buf string plit by the newline characters
    28  func (buf *FilterBuffer) ByLines() []string {
    29  	return strings.Split(buf.String(), "\n")
    30  }
    31  
    32  // KVOutput returns a map of the buff string split based on
    33  // the separator '='.
    34  // For example, the following strings would be split as follows:
    35  // 		a=1
    36  // 		b=2
    37  // 		c=3
    38  func (buf *FilterBuffer) KVOutput() map[string]string {
    39  	result := make(map[string]string)
    40  	for _, line := range buf.ByLines() {
    41  		vals := strings.Split(line, "=")
    42  		if len(vals) == 2 {
    43  			result[vals[0]] = vals[1]
    44  		}
    45  	}
    46  	return result
    47  }