github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/field_map.go (about)

     1  package kube
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
     8  	"github.com/jenkins-x/jx/v2/pkg/util"
     9  )
    10  
    11  // FieldMap is a map of field:value. It implements fields.Fields.
    12  type fieldMap map[string]interface{}
    13  
    14  func newFieldMap(pipelineActivity v1.PipelineActivity) (fieldMap, error) {
    15  	return util.ToObjectMap(pipelineActivity)
    16  }
    17  
    18  // Has returns whether the provided field exists in the map.
    19  func (m fieldMap) Has(field string) bool {
    20  	_, exists := m.get(field)
    21  	return exists
    22  }
    23  
    24  // Get returns the value in the map for the provided field.
    25  func (m fieldMap) Get(field string) string {
    26  	val, _ := m.get(field)
    27  	return val
    28  }
    29  
    30  // Get returns the value in the map for the provided field.
    31  func (m fieldMap) get(field string) (string, bool) {
    32  	pathElements := strings.Split(field, ".")
    33  	valueMap := m
    34  	value := ""
    35  	for i, element := range pathElements {
    36  		tmp, exists := valueMap[element]
    37  		if !exists {
    38  			return "", false
    39  		}
    40  
    41  		if i == len(pathElements)-1 {
    42  			value = fmt.Sprintf("%v", tmp)
    43  		} else {
    44  			switch v := tmp.(type) {
    45  			case map[string]interface{}:
    46  				valueMap = v
    47  			default:
    48  				return "", false
    49  			}
    50  		}
    51  	}
    52  
    53  	return value, true
    54  }