Skip to content

Instantly share code, notes, and snippets.

@rnkoaa
Created September 28, 2020 18:29
Show Gist options
  • Save rnkoaa/80c6410ba2aacf2f89c2ab3ee7c8a220 to your computer and use it in GitHub Desktop.
Save rnkoaa/80c6410ba2aacf2f89c2ab3ee7c8a220 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strings"
)
var (
app = `db.username=hello
db.password=world
kafka.properties=&user.name=kafka&brokers=127.0.0.1`
)
func splitProperties(str string) map[string]string {
sep := "="
res := make(map[string]string, 0)
s := strings.Split(app, "\n")
for _, p := range s {
splits := strings.Split(p, sep)
if len(splits) >= 0 {
key := splits[0]
value := strings.Join(splits[1:], sep)
res[key] = value
}
}
return res
}
func mergeProperties(first, second str) string {
if second == "" {
return first
}
if first == "" {
return second
}
firstProps := splitProperties(first)
secondProps := splitProperties(second)
for k, v := range secondProps {
firstProps[k] = v
}
return joinProperties(firstProps)
}
func joinProperties(m map[string]string) string {
res := make([]string, 0)
for k, v := range m {
res = append(res, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(res, "\n")
}
func main() {
// fmt.Println(app)
props := splitProperties(app)
for k, v := range props {
fmt.Printf("Key: %s, Value: %s\n", k, v)
}
fmt.Println(joinProperties(props))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment