Copyright 2018, OpenCensus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Package tracestate implements support for the Tracestate header of the W3C TraceContext propagation format.
package tracestate

import (
	
	
)

const (
	keyMaxSize       = 256
	valueMaxSize     = 256
	maxKeyValuePairs = 32
)

const (
	keyWithoutVendorFormat = `[a-z][_0-9a-z\-\*\/]{0,255}`
	keyWithVendorFormat    = `[a-z][_0-9a-z\-\*\/]{0,240}@[a-z][_0-9a-z\-\*\/]{0,13}`
	keyFormat              = `(` + keyWithoutVendorFormat + `)|(` + keyWithVendorFormat + `)`
	valueFormat            = `[\x20-\x2b\x2d-\x3c\x3e-\x7e]{0,255}[\x21-\x2b\x2d-\x3c\x3e-\x7e]`
)

var keyValidationRegExp = regexp.MustCompile(`^(` + keyFormat + `)$`)
var valueValidationRegExp = regexp.MustCompile(`^(` + valueFormat + `)$`)
Tracestate represents tracing-system specific context in a list of key-value pairs. Tracestate allows different vendors propagate additional information and inter-operate with their legacy Id formats.
type Tracestate struct {
	entries []Entry
}
Entry represents one key-value pair in a list of key-value pair of Tracestate.
Key is an opaque string up to 256 characters printable. It MUST begin with a lowercase letter, and can only contain lowercase letters a-z, digits 0-9, underscores _, dashes -, asterisks *, and forward slashes /.
Value is an opaque string up to 256 characters printable ASCII RFC0020 characters (i.e., the range 0x20 to 0x7E) except comma , and =.
Entries returns a slice of Entry.
func ( *Tracestate) () []Entry {
	if  == nil {
		return nil
	}
	return .entries
}

func ( *Tracestate) ( string) *Entry {
	for ,  := range .entries {
		if .Key ==  {
			.entries = append(.entries[:], .entries[+1:]...)
			return &
		}
	}
	return nil
}

func ( *Tracestate) ( []Entry) error {
	for ,  := range  {
		.remove(.Key)
	}
	if len(.entries)+len() > maxKeyValuePairs {
		return fmt.Errorf("adding %d key-value pairs to current %d pairs exceeds the limit of %d",
			len(), len(.entries), maxKeyValuePairs)
	}
	.entries = append(, .entries...)
	return nil
}

func ( Entry) bool {
	return keyValidationRegExp.MatchString(.Key) &&
		valueValidationRegExp.MatchString(.Value)
}

func ( ...Entry) (string, bool) {
	 := make(map[string]int)
	for ,  := range  {
		if ,  := [.Key];  {
			return .Key, true
		}
		[.Key] = 1
	}
	return "", false
}

func ( ...Entry) (*Entry, bool) {
	for ,  := range  {
		if !isValid() {
			return &, false
		}
	}
	return nil, true
}
New creates a Tracestate object from a parent and/or entries (key-value pair). Entries from the parent are copied if present. The entries passed to this function are inserted in front of those copied from the parent. If an entry copied from the parent contains the same key as one of the entry in entries then the entry copied from the parent is removed. See add func. An error is returned with nil Tracestate if 1. one or more entry in entries is invalid. 2. two or more entries in the input entries have the same key. 3. the number of entries combined from the parent and the input entries exceeds maxKeyValuePairs. (duplicate entry is counted only once).
func ( *Tracestate,  ...Entry) (*Tracestate, error) {
	if  == nil && len() == 0 {
		return nil, nil
	}
	if ,  := areEntriesValid(...); ! {
		return nil, fmt.Errorf("key-value pair {%s, %s} is invalid", .Key, .Value)
	}

	if ,  := containsDuplicateKey(...);  {
		return nil, fmt.Errorf("contains duplicate keys (%s)", )
	}

	 := Tracestate{}

	if  != nil && len(.entries) > 0 {
		.entries = append([]Entry{}, .entries...)
	}

	 := .add()
	if  != nil {
		return nil, 
	}
	return &, nil