Fix json quotation bug.

This commit is contained in:
Kalzu Rekku
2025-10-05 21:39:09 +03:00
parent d69240913a
commit ad17dff2fa

54
main.go
View File

@@ -2,6 +2,7 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
@@ -235,15 +236,32 @@ func (c *KVSClient) handleGet(args []string) {
} }
func (c *KVSClient) handlePut(args []string) { func (c *KVSClient) handlePut(args []string) {
if len(args) < 2 { if len(args) < 1 {
fmt.Println(red("Usage: put <key> <json-data>")) fmt.Println(red("Usage: put <key> <json-data>"))
fmt.Println("Or: put <key> (then paste JSON on next line)")
return return
} }
var jsonStr string
if len(args) == 1 {
// Multiline mode - read from next input
fmt.Println(yellow("Enter JSON (Ctrl+D when done):"))
scanner := bufio.NewScanner(os.Stdin)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
jsonStr = strings.Join(lines, "\n")
} else {
// Single line mode
jsonStr = strings.Join(args[1:], " ")
}
// Parse JSON data // Parse JSON data
var data json.RawMessage var data json.RawMessage
if err := json.Unmarshal([]byte(args[1]), &data); err != nil { if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
fmt.Println(red("Invalid JSON:"), err) fmt.Println(red("Invalid JSON:"), err)
fmt.Println("Tip: Use proper JSON format with escaped quotes")
return return
} }
@@ -527,25 +545,35 @@ var completer = readline.NewPrefixCompleter(
func parseCommand(line string) []string { func parseCommand(line string) []string {
var parts []string var parts []string
var current strings.Builder var current strings.Builder
inQuotes := false var quoteChar rune = 0 // Use a rune to store the active quote character (' or ")
for i := 0; i < len(line); i++ { for _, ch := range line {
ch := line[i] switch {
switch ch { // If we see a quote character
case '"': case ch == '\'' || ch == '"':
inQuotes = !inQuotes if quoteChar == 0 {
case ' ', '\t': // Not currently inside a quoted string, so start one
if inQuotes { quoteChar = ch
current.WriteByte(ch) } else if quoteChar == ch {
} else if current.Len() > 0 { // Inside a quoted string and found the matching quote, so end it
quoteChar = 0
} else {
// Inside a quoted string but found the *other* type of quote, treat it as a literal character
current.WriteRune(ch)
}
// If we see a space or tab and are NOT inside a quoted string
case (ch == ' ' || ch == '\t') && quoteChar == 0:
if current.Len() > 0 {
parts = append(parts, current.String()) parts = append(parts, current.String())
current.Reset() current.Reset()
} }
// Any other character
default: default:
current.WriteByte(ch) current.WriteRune(ch)
} }
} }
// Add the last part if it exists
if current.Len() > 0 { if current.Len() > 0 {
parts = append(parts, current.String()) parts = append(parts, current.String())
} }