func parseKeyValuePairs()

in pkg/engine/transform/apimodel_merger.go [130:190]


func parseKeyValuePairs(literal string) map[string]string {
	log.Debugln(fmt.Sprintf("parsing --set flag key/value pairs from %s", literal))
	inQuoteLiteral := false
	inDblQuoteLiteral := false
	inKey := true
	kvpMap := map[string]string{}

	currentKey := ""
	currentValue := ""

	for _, literalChar := range literal {
		switch literalChar {
		case '\'': // if we hit a ' char
			if !inQuoteLiteral && !inDblQuoteLiteral { // and we are not already in a literal
				inQuoteLiteral = true // start a new ' delimited literal value
				inKey = false
			} else if inQuoteLiteral { // we already are in a ' delimited literal value
				inQuoteLiteral = false // stop it
				inKey = true
			}
		case '"': // if we hit a " char
			if !inDblQuoteLiteral && !inQuoteLiteral { // and we are not already in a literal
				inDblQuoteLiteral = true // start a new " delimited literal value
				inKey = false
			} else if inDblQuoteLiteral { // we already are in a " delimited literal value
				inDblQuoteLiteral = false // stop it
				inKey = true
			}
		case ',': // if we hit a , char
			if inQuoteLiteral || inDblQuoteLiteral { // we are in a literal
				currentValue += string(literalChar)
			} else {
				log.Debugln(fmt.Sprintf("new key/value parsed: %s = %s", currentKey, currentValue))
				kvpMap[currentKey] = currentValue
				currentKey = ""
				currentValue = ""
				inKey = true
			}
		case '=': // if we hit a = char
			if inQuoteLiteral || inDblQuoteLiteral || !inKey { // we are in a literal / value
				currentValue += string(literalChar)
			} else {
				inKey = false
			}
		default: // we hit any other char
			if inKey {
				currentKey += string(literalChar)
			} else {
				currentValue += string(literalChar)
			}
		}
	}

	// push latest literal
	if currentKey != "" {
		log.Debugln(fmt.Sprintf("new key/value parsed: %s = %s", currentKey, currentValue))
		kvpMap[currentKey] = currentValue
	}

	return kvpMap
}