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  //go:build !goexperiment.jsonv2
     6  
     7  package json
     8  
     9  import (
    10  	"strings"
    11  )
    12  
    13  // tagOptions is the string following a comma in a struct field's "json"
    14  // tag, or the empty string. It does not include the leading comma.
    15  type tagOptions string
    16  
    17  // parseTag splits a struct field's json tag into its name and
    18  // comma-separated options.
    19  func parseTag(tag string) (string, tagOptions) {
    20  	tag, opt, _ := strings.Cut(tag, ",")
    21  	return tag, tagOptions(opt)
    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 name string
    34  		name, s, _ = strings.Cut(s, ",")
    35  		if name == optionName {
    36  			return true
    37  		}
    38  	}
    39  	return false
    40  }
    41  

View as plain text