aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlparser/validator/messaging.go
blob: f1ab5873f339356b60410ff9eb362911d1861b83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package validator

import "bytes"

// Given [ A, B, C ] return '"A", "B", or "C"'.
func QuotedOrList(items ...string) string {
	itemsQuoted := make([]string, len(items))
	for i, item := range items {
		itemsQuoted[i] = `"` + item + `"`
	}
	return OrList(itemsQuoted...)
}

// Given [ A, B, C ] return 'A, B, or C'.
func OrList(items ...string) string {
	var buf bytes.Buffer

	if len(items) > 5 {
		items = items[:5]
	}
	if len(items) == 2 {
		buf.WriteString(items[0])
		buf.WriteString(" or ")
		buf.WriteString(items[1])
		return buf.String()
	}

	for i, item := range items {
		if i != 0 {
			if i == len(items)-1 {
				buf.WriteString(", or ")
			} else {
				buf.WriteString(", ")
			}
		}
		buf.WriteString(item)
	}
	return buf.String()
}