Documentation ¶
Overview ¶
Package text contains utility functions for manipulating raw text strings
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GoIdentifierFrom ¶
GoIdentifierFrom provides a mechanism to mutate an arbitrary descriptive string (name) into a Go identifier (variable name, function name, etc) that e.g. can be used in generated code, taking into account a blacklist of names that should not be used, plus the blacklist of the go language reserved key words (https://golang.org/ref/spec#Keywords), in order to guarantee that a new name is created which will not conflict with an existing type.
Identifier syntax: https://golang.org/ref/spec#Identifiers
Strategy to convert arbitrary unicode string to a valid identifier:
1) Ensure name is valid UTF-8; if not, replace it with empty string
2) Split name into arrays of allowed runes (words), by considering a run of disallowed unicode characters to act as a separator, where allowed runes include unicode letters, unicode numbers, and '_' character (disallowed runes are discarded)
3) Split words further into sub words, by decomposing camel case words as per https://github.com/fatih/camelcase#usage-and-examples
4) Designate the case of all subwords of all words to be uppercase, with the exception of the first subword of the first word, which should be lowercase if exported is false, otherwise uppercase
5) For each subword of each word, adjust as follows: if designated as lowercase, lowercase all characters of the subword; if designated as uppercase, then if recognised as a common "initialism", then uppercase all the characters of the subword, otherwise uppercase only the first character of the subword. Common "Initialisms" are defined as per: https://github.com/golang/lint/blob/32a87160691b3c96046c0c678fe57c5bef761456/lint.go#L702
6) Rejoin subwords to form a single word
7) Rejoin words into a single string
8) If the string starts with a number, add a leading `_`
9) If the string is the empty string or "_", set as "Identifier"
10) If the resulting identifier is in the given blacklist, or the list of reserved key words (https://golang.org/ref/spec#Keywords), append the lowest integer possible, >= 1, that results in no blacklist conflict
11) Add the new name to the given blacklist
Note, the `map[string]bool` construction is simply a mechanism to implement set semantics; a value of `true` signifies inclusion in the set. Non-existence is equivalent to existence with a value of `false`; therefore it is recommended to only store `true` values.
Example ¶
package main import ( "fmt" "github.com/taskcluster/taskcluster/v54/tools/jsonschema2go/text" ) func main() { blacklist := make(map[string]bool) fmt.Println(text.GoIdentifierFrom("Azure Artifact Request", true, blacklist)) fmt.Println(text.GoIdentifierFrom("AzureArtifactRequest", true, blacklist)) fmt.Println(text.GoIdentifierFrom("Azure artifact request", false, blacklist)) fmt.Println(text.GoIdentifierFrom("azure-artifact request", false, blacklist)) fmt.Println(text.GoIdentifierFrom("azure-artifact request", true, blacklist)) fmt.Println(text.GoIdentifierFrom("List Artifacts Response", true, blacklist)) fmt.Println(text.GoIdentifierFrom("hello, 世;;;((```[]!@#$界", false, blacklist)) fmt.Println(text.GoIdentifierFrom(".-4$sjdb2##f \n\txxßßß", true, blacklist)) fmt.Println(text.GoIdentifierFrom("", false, blacklist)) fmt.Println(text.GoIdentifierFrom("", true, blacklist)) fmt.Println(text.GoIdentifierFrom("_", false, blacklist)) fmt.Println(text.GoIdentifierFrom("grüß", true, blacklist)) fmt.Println(text.GoIdentifierFrom("333", false, blacklist)) fmt.Println(text.GoIdentifierFrom("3_33", true, blacklist)) fmt.Println(text.GoIdentifierFrom("ü", true, blacklist)) fmt.Println(text.GoIdentifierFrom("üö33", true, blacklist)) fmt.Println(text.GoIdentifierFrom("Üö33", false, blacklist)) fmt.Println(text.GoIdentifierFrom("Üö33", true, blacklist)) fmt.Println(text.GoIdentifierFrom("\xe2\x28\xa1", true, blacklist)) fmt.Println(text.GoIdentifierFrom("provisioner id", true, blacklist)) fmt.Println(text.GoIdentifierFrom("provisioner ide", true, blacklist)) fmt.Println(text.GoIdentifierFrom("provisionerId", true, blacklist)) fmt.Println(text.GoIdentifierFrom("provisionerId parent", true, blacklist)) fmt.Println(text.GoIdentifierFrom("provisionerId parent ", true, blacklist)) fmt.Println(text.GoIdentifierFrom("urlEndpoint", false, blacklist)) fmt.Println(text.GoIdentifierFrom("uRLEndpoint", false, blacklist)) fmt.Println(text.GoIdentifierFrom("URLEndpoint", true, blacklist)) fmt.Println(text.GoIdentifierFrom("UrlEndpoint", true, blacklist)) fmt.Println(text.GoIdentifierFrom("UrlEndpoint", false, blacklist)) fmt.Println(text.GoIdentifierFrom("PDFDocument", false, blacklist)) fmt.Println(text.GoIdentifierFrom("continue", false, blacklist)) }
Output: AzureArtifactRequest AzureArtifactRequest1 azureArtifactRequest azureArtifactRequest1 AzureArtifactRequest2 ListArtifactsResponse hello世界 _4Sjdb2FXxßßß Identifier Identifier1 Identifier2 Grüß _333 _3_33 Ü Üö33 üö33 Üö331 Identifier3 ProvisionerID ProvisionerIde ProvisionerID1 ProvisionerIDParent ProvisionerIDParent1 urlEndpoint uRLEndpoint URLEndpoint URLEndpoint1 urlEndpoint1 pdfDocument continue1
func IndefiniteArticle ¶
Returns the indefinite article (in English) for a the given noun, which is 'an' for nouns beginning with a vowel, otherwise 'a'.
Example ¶
package main import ( "fmt" "github.com/taskcluster/taskcluster/v54/tools/jsonschema2go/text" ) func main() { for _, noun := range []string{ "ant", "dog", "emu", "fish", "gopher", "hippopotamus", "owl", } { fmt.Println(text.IndefiniteArticle(noun), noun) } }
Output: an ant a dog an emu a fish a gopher a hippopotamus an owl
func Indent ¶
Indent indents a block of text with an indent string. It does this by placing the given indent string at the front of every line, except on the last line, if the last line has no characters. This special treatment simplifies the generation of nested text structures.
Example (Basic) ¶
package main import ( "fmt" "github.com/taskcluster/taskcluster/v54/tools/jsonschema2go/text" ) func main() { fmt.Println("1.") fmt.Println(text.Indent("", "....")) fmt.Println("2.") fmt.Println(text.Indent("\n", "....")) fmt.Println("3.") fmt.Println(text.Indent("line one\nline two", "....")) fmt.Println("4.") fmt.Println(text.Indent("line one\nline two\n", "....")) fmt.Println("5.") fmt.Println(text.Indent("line one\nline two\n\n", "....")) fmt.Println("Done") }
Output: 1. 2. .... 3. ....line one ....line two 4. ....line one ....line two 5. ....line one ....line two .... Done
Example (Nested) ¶
package main import ( "fmt" "github.com/taskcluster/taskcluster/v54/tools/jsonschema2go/text" ) func main() { fmt.Println(text.Indent("func A(foo string) {\n"+text.Indent("a := []string{\n"+text.Indent("\"x\",\n\"y\",\n\"z\",\n", "\t")+"}\n", "\t")+"}\n", "=> ")) fmt.Println("Done") }
Output: => func A(foo string) { => a := []string{ => "x", => "y", => "z", => } => } Done
func Underline ¶
Underline returns the provided text together with a new line character and a line of "=" characters whose length is equal to the maximum line length in the provided text, followed by a final newline character.
Example (Basic) ¶
package main import ( "fmt" "github.com/taskcluster/taskcluster/v54/tools/jsonschema2go/text" ) func main() { fmt.Println(text.Underline("Taskcluster Client") + "Please see https://docs.taskcluster.net/manual/tools/clients") }
Output: Taskcluster Client ================== Please see https://docs.taskcluster.net/manual/tools/clients
Example (Multiline) ¶
package main import ( "fmt" "github.com/taskcluster/taskcluster/v54/tools/jsonschema2go/text" ) func main() { fmt.Println(text.Underline("Taskcluster Client\nGo (golang) Implementation\n13 Jan 2016") + "Please see http://taskcluster.github.io/taskcluster-client-go") }
Output: Taskcluster Client Go (golang) Implementation 13 Jan 2016 ========================== Please see http://taskcluster.github.io/taskcluster-client-go
Types ¶
This section is empty.