Source file src/encoding/json/tags.go

     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  	tag, opt, _ := strings.Cut(tag, ",")
    19  	return tag, tagOptions(opt)
    20  }
    21  
    22  // Contains reports whether a comma-separated list of options
    23  // contains a particular substr flag. substr must be surrounded by a
    24  // string boundary or commas.
    25  func (o tagOptions) Contains(optionName string) bool {
    26  	if len(o) == 0 {
    27  		return false
    28  	}
    29  	s := string(o)
    30  	for s != "" {
    31  		var name string
    32  		name, s, _ = strings.Cut(s, ",")
    33  		if name == optionName {
    34  			return true
    35  		}
    36  	}
    37  	return false
    38  }
    39  

View as plain text