Skip to content

Commit

Permalink
Update baggage.go
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Dec 14, 2023
1 parent 14e7c0f commit c5728bb
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,15 +531,15 @@ func (b Baggage) String() string {
return strings.Join(members, listDelimiter)
}

// parsePropertyInternal attempts to decode a Property from the passed string. It follows the spec at
// https://www.w3.org/TR/baggage/#definition.
// parsePropertyInternal attempts to decode a Property from the passed string.
// It follows the spec at https://www.w3.org/TR/baggage/#definition.
func parsePropertyInternal(s string) (p Property, ok bool) {
// For the entire function we will use " key = value " as an example.
// Attempting to parse the key.
// First skip spaces at the beginning "< >key = value " (they could be empty).
index := skipSpace(s, 0)

// now parse the key: " <key> = value ".
// Parse the key: " <key> = value ".
keyStart := index
keyEnd := index
for _, c := range s[keyStart:] {
Expand All @@ -549,32 +549,35 @@ func parsePropertyInternal(s string) (p Property, ok bool) {
keyEnd++
}

// if we couldn't find any valid key character, it means the key is either empty or invalid.
// If we couldn't find any valid key character,
// it means the key is either empty or invalid.
if keyStart == keyEnd {
return
}

// now skip spaces after the key: " key< >= value ".
// Skip spaces after the key: " key< >= value ".
index = skipSpace(s, keyEnd)

// a key could have no value, like: " key "
if index == len(s) {
// A key can have no value, like: " key ".
ok = true
p.key = s[keyStart:keyEnd]
return
}

// if we have not reached the end and we can't find the '=' delimiter, it means the property is invalid.
if s[index] != keyValueDelimiter[0] {
// If we have not reached the end and we can't find the '=' delimiter,
// it means the property is invalid.
if s[index] != keyValueDelimiter[0] {
return
}

// Attempting to parse the value.
// now match: " key =< >value ".
// Match: " key =< >value ".
index = skipSpace(s, index+1)

// now match the value string: " key = <value> ". A valid property can be: " key =". So, we don't have
// to check if the value is empty.
// Match the value string: " key = <value> ".
// A valid property can be: " key =".
// Therefore, we don't have to check if the value is empty.
valueStart := index
valueEnd := index
for _, c := range s[valueStart:] {
Expand All @@ -584,10 +587,11 @@ func parsePropertyInternal(s string) (p Property, ok bool) {
valueEnd++
}

// skip all trailing whitespaces: " key = value< >".
// Skip all trailing whitespaces: " key = value< >".
index = skipSpace(s, valueEnd)

// If after looking for the value and skipping whitespaces we have not reached the end, it means the property is
// If after looking for the value and skipping whitespaces
// we have not reached the end, it means the property is
// invalid, something like: " key = value value1".
if index != len(s) {
return
Expand Down

0 comments on commit c5728bb

Please sign in to comment.