cmp/report_reflect.go
func (opts formatOptions) FormatType(t reflect.Type, s textNode) textNode {
	// Check whether to emit the type or not.
	switch opts.TypeMode {
	case autoType:
		switch t.Kind() {
		case reflect.Struct, reflect.Slice, reflect.Array, reflect.Map:
			if s.Equal(textNil) {
				return s
			}
		default:
			return s
		}
	case elideType:
		return s
	}

	// Determine the type label, applying special handling for unnamed types.
	typeName := t.String()
	if t.Name() == "" {
		// According to Go grammar, certain type literals contain symbols that
		// do not strongly bind to the next lexicographical token (e.g., *T).
		switch t.Kind() {
		case reflect.Chan, reflect.Func, reflect.Ptr:
			typeName = "(" + typeName + ")"
		}
		typeName = strings.Replace(typeName, "struct {", "struct{", -1)
		typeName = strings.Replace(typeName, "interface {", "interface{", -1)
	}

	// Avoid wrap the value in parenthesis if unnecessary.
	if s, ok := s.(textWrap); ok {
		hasParens := strings.HasPrefix(s.Prefix, "(") && strings.HasSuffix(s.Suffix, ")")
		hasBraces := strings.HasPrefix(s.Prefix, "{") && strings.HasSuffix(s.Suffix, "}")
		if hasParens || hasBraces {
			return textWrap{typeName, s, ""}
		}
	}
	return textWrap{typeName + "(", s, ")"}
}
func (opts formatOptions) FormatType(t reflect.Type, s textNode) textNode {
	// Check whether to emit the type or not.
	switch opts.TypeMode {
	case autoType:
		switch t.Kind() {
		case reflect.Struct, reflect.Slice, reflect.Array, reflect.Map:
			if s.Equal(textNil) {
				return s
			}
		default:
			return s
		}
	case elideType:
		return s
	}

	// Determine the type label, applying special handling for unnamed types.
	typeName := t.String()
	if t.Name() == "" {
		// According to Go grammar, certain type literals contain symbols that
		// do not strongly bind to the next lexicographical token (e.g., *T).
		switch t.Kind() {
		case reflect.Chan, reflect.Func, reflect.Ptr:
			typeName = "(" + typeName + ")"
		}
		typeName = strings.Replace(typeName, "struct {", "struct{", -1)
		typeName = strings.Replace(typeName, "interface {", "interface{", -1)
	}

	// Avoid wrap the value in parenthesis if unnecessary.
	if s, ok := s.(textWrap); ok {
		hasParens := strings.HasPrefix(s.Prefix, "(") && strings.HasSuffix(s.Suffix, ")")
		hasBraces := strings.HasPrefix(s.Prefix, "{") && strings.HasSuffix(s.Suffix, "}")
		if hasParens || hasBraces {
			return textWrap{typeName, s, ""}
		}
	}
	return textWrap{typeName + "(", s, ")"}
}
func ( opts formatOptions ) FormatType ( t reflect . Type , s textNode ) textNode { // Check whether to emit the type or not. switch opts . TypeMode { case autoType : switch t . Kind ( ) { case reflect . Struct , reflect . Slice , reflect . Array , reflect . Map : if s . Equal ( textNil ) { return s } default : return s } case elideType : return s } // Determine the type label, applying special handling for unnamed types. typeName := t . String ( ) if t . Name ( ) == " " { // According to Go grammar, certain type literals contain symbols that // do not strongly bind to the next lexicographical token (e.g., *T). switch t . Kind ( ) { case reflect . Chan , reflect . Func , reflect . Ptr : typeName = " " + typeName + " " } typeName = strings . Replace ( typeName , " " , " " , - 1 ) typeName = strings . Replace ( typeName , " " , " " , - 1 ) } // Avoid wrap the value in parenthesis if unnecessary. if s , ok := s . ( textWrap ) ; ok { hasParens := strings . HasPrefix ( s . Prefix , " " ) && strings . HasSuffix ( s . Suffix , " " ) hasBraces := strings . HasPrefix ( s . Prefix , " " ) && strings . HasSuffix ( s . Suffix , " " ) if hasParens || hasBraces { return textWrap { typeName , s , " " } } } return textWrap { typeName + " " , s , " " } }
// FormatType prints the type as if it were wrapping s.
// This may return s as-is depending on the current type and TypeMode mode.
FormatType prints the type as if it were wrapping s . This may return s as - is depending on the current type and TypeMode mode .