Black Lives Matter. Support the Equal Justice Initiative.

Source file src/encoding/json/tags.go

Documentation: encoding/json

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package json
     6  
     7  import (
     8  	"strings"
     9  )
    10  
    11  // tagOptions is the string following a comma in a struct field's "json"
    12  // tag, or the empty string. It does not include the leading comma.
    13  type tagOptions string
    14  
    15  // parseTag splits a struct field's json tag into its name and
    16  // comma-separated options.
    17  func parseTag(tag string) (string, tagOptions) {
    18  	if idx := strings.Index(tag, ","); idx != -1 {
    19  		return tag[:idx], tagOptions(tag[idx+1:])
    20  	}
    21  	return tag, tagOptions("")
    22  }
    23  
    24  // Contains reports whether a comma-separated list of options
    25  // contains a particular substr flag. substr must be surrounded by a
    26  // string boundary or commas.
    27  func (o tagOptions) Contains(optionName string) bool {
    28  	if len(o) == 0 {
    29  		return false
    30  	}
    31  	s := string(o)
    32  	for s != "" {
    33  		var next string
    34  		i := strings.Index(s, ",")
    35  		if i >= 0 {
    36  			s, next = s[:i], s[i+1:]
    37  		}
    38  		if s == optionName {
    39  			return true
    40  		}
    41  		s = next
    42  	}
    43  	return false
    44  }
    45  

View as plain text