1
0
mirror of https://github.com/regclient/regclient.git synced 2025-04-18 22:44:00 +03:00
Brandon Mitchell eea06e2a5c
Refactoring the type package
I feel like I need to explain, this is all to move the descriptor package.
The platform package could not use the predefined errors in types because of a circular dependency from descriptor.
The most appropriate way to reorg this is to move descriptor out of the type package since it was more complex than a self contained type.
When doing that, type aliases were needed to avoid breaking changes to existing users.
Those aliases themselves caused circular dependency loops because of the media types and errors, so those were also pulled out to separate packages.
All of the old values were aliased and deprecated, and to fix the linter, those deprecations were fixed by updating the imports... everywhere.

Signed-off-by: Brandon Mitchell <git@bmitch.net>
2024-03-04 15:43:18 -05:00

92 lines
1.6 KiB
Go

// Package strparse is used to parse strings
package strparse
import (
"fmt"
"github.com/regclient/regclient/types/errs"
)
// SplitCSKV splits a comma separated key=value list into a map
func SplitCSKV(s string) (map[string]string, error) {
state := "key"
key := ""
val := ""
result := map[string]string{}
procKV := func() {
if key != "" {
result[key] = val
}
state = "key"
key = ""
val = ""
}
for _, c := range s {
switch state {
case "key":
switch c {
case '"':
state = "keyQuote"
case '\\':
state = "keyEscape"
case '=':
state = "val"
case ',':
procKV()
default:
key = key + string(c)
}
case "keyQuote":
switch c {
case '"':
state = "key"
case '\\':
state = "keyEscapeQuote"
default:
key = key + string(c)
}
case "keyEscape":
key = key + string(c)
state = "key"
case "keyEscapeQuote":
key = key + string(c)
state = "keyQuote"
case "val":
switch c {
case '"':
state = "valQuote"
case ',':
procKV()
case '\\':
state = "valEscape"
default:
val = val + string(c)
}
case "valQuote":
switch c {
case '"':
state = "val"
case '\\':
state = "valEscapeQuote"
default:
val = val + string(c)
}
case "valEscape":
val = val + string(c)
state = "val"
case "valEscapeQuote":
val = val + string(c)
state = "valQuote"
default:
return nil, fmt.Errorf("unhandled state: %s", state)
}
}
switch state {
case "val", "key":
procKV()
default:
return nil, fmt.Errorf("string parsing failed, end state: %s%.0w", state, errs.ErrParsingFailed)
}
return result, nil
}