Created
February 9, 2025 10:10
-
-
Save gfanton/5721b750532eaae93c9f58a26c595630 to your computer and use it in GitHub Desktop.
lazy query load tag format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // type FormatKind string | |
| // const ( | |
| // FormatKindDefault FormatKind = FormatKindMachine | |
| // FormatKindMachine = "machine" // Default machine representation | |
| // FormatKindString = "string" // Single string representation | |
| // FormatKindJSON = "json" // XXX: EXPERIMENTAL, only supports primitive types for now | |
| // FormatKindJSONP = "jsonp" // XXX: EXPERIMENTAL, only supports primitive types for now | |
| // ) | |
| // func FormatFromValues(name string, args url.Values) FormatKind { | |
| // keys := make([]string, 0, len(args)) | |
| // for k := range args { | |
| // keys = append(keys, k) | |
| // } | |
| // sort.Strings(keys) | |
| // var tag strings.Builder | |
| // tag.WriteString(name) | |
| // for _, k := range keys { | |
| // tag.WriteRune(' ') | |
| // tag.WriteString(k) | |
| // tag.WriteRune(':') | |
| // v := strings.Join(args[k], ",") | |
| // tag.WriteString(strconv.Quote(v)) | |
| // } | |
| // return FormatKind(tag.String()) | |
| // } | |
| // // Get returns the value associated with key in the f string. | |
| // // If there is no such key in the f, Get returns the empty string. | |
| // // If the f does not have the conventional format, the value | |
| // // returned by Get is unspecified. To determine whether a f is | |
| // // explicitly set to the empty string, use [StructF.Lookup]. | |
| // func (f FormatKind) Get(key string) string { | |
| // v, _ := f.Lookup(key) | |
| // return v | |
| // } | |
| // // Lookup returns the value associated with key in the f string. | |
| // // If the key is present in the f the value (which may be empty) | |
| // // is returned. Otherwise the returned value will be the empty string. | |
| // // The ok return value reports whether the value was explicitly set in | |
| // // the f string. If the f does not have the conventional format, | |
| // // the value returned by Lookup is unspecified. | |
| // func (f FormatKind) Lookup(key string) (value string, ok bool) { | |
| // // When modifying this code, also update the validateStructF code | |
| // // in cmd/vet/structf.go. | |
| // for f != "" { | |
| // // Skip leading space. | |
| // i := 0 | |
| // for i < len(f) && f[i] == ' ' { | |
| // i++ | |
| // } | |
| // f = f[i:] | |
| // if f == "" { | |
| // break | |
| // } | |
| // // Scan to colon. A space, a quote or a control character is a syntax error. | |
| // // Strictly speaking, control chars include the range [0x7f, 0x9f], not just | |
| // // [0x00, 0x1f], but in practice, we ignore the multi-byte control characters | |
| // // as it is simpler to inspect the f's bytes than the f's runes. | |
| // i = 0 | |
| // for i < len(f) && f[i] > ' ' && f[i] != ':' && f[i] != '"' && f[i] != 0x7f { | |
| // i++ | |
| // } | |
| // if i == 0 || i+1 >= len(f) || f[i] != ':' || f[i+1] != '"' { | |
| // break | |
| // } | |
| // name := string(f[:i]) | |
| // f = f[i+1:] | |
| // // Scan quoted string to find value. | |
| // i = 1 | |
| // for i < len(f) && f[i] != '"' { | |
| // if f[i] == '\\' { | |
| // i++ | |
| // } | |
| // i++ | |
| // } | |
| // if i >= len(f) { | |
| // break | |
| // } | |
| // qvalue := string(f[:i+1]) | |
| // f = f[i+1:] | |
| // if key == name { | |
| // value, err := strconv.Unquote(qvalue) | |
| // if err != nil { | |
| // break | |
| // } | |
| // return value, true | |
| // } | |
| // } | |
| // return "", false | |
| // } | |
| // // Name extracts the initial name key from the struct f (first non-space sequence before any colon/space) | |
| // func (f FormatKind) Name() string { | |
| // // Skip leading spaces | |
| // s := strings.TrimLeft(string(f), " ") | |
| // // Find first invalid key character (space, colon, quote, or control char) | |
| // end := 0 | |
| // for end < len(s) && s[end] > ' ' && s[end] != ':' && s[end] != '"' && s[end] != 0x7f { | |
| // end++ | |
| // } | |
| // if end == 0 { | |
| // return "" | |
| // } | |
| // return s[:end] | |
| // } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment