This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
/
exporter.go
73 lines (55 loc) · 1.59 KB
/
exporter.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2016 Paul Jolly <paul@myitcv.org.uk>, all rights reserved.
// Use of this document is governed by a license found in the LICENSE document.
package main
import (
"text/template"
"unicode"
"unicode/utf8"
)
var exportMap = template.FuncMap{
"Export": export,
"Capitalise": capitalise,
"Choose": chooseFirst,
}
var unexportMap = template.FuncMap{
"Export": unexport,
"Capitalise": capitalise,
"Choose": chooseSecond,
}
// exporter returns a template.FuncMap with
// "Export" mapped to an appropriate function
// depending on the initial capitalisation of
// name
func exporter(name string) template.FuncMap {
r, _ := utf8.DecodeRuneInString(name)
// Note: we are choosing to ignore the situation where we decode utf8.RuneError
// this situation would only happen if the source contained an invalid utf8 code point...
// which is impossible because it won't compile
// But we unexport for "safety", even though this doesn't mean anything
if unicode.IsUpper(r) {
return exportMap
}
return unexportMap
}
func export(name string) string {
return capitalise(name)
}
func unexport(name string) string {
return uncapitalise(name)
}
func capitalise(name string) string {
r, n := utf8.DecodeRuneInString(name)
// again, choosing to ignore error
return string(unicode.ToUpper(r)) + name[n:]
}
func uncapitalise(name string) string {
r, n := utf8.DecodeRuneInString(name)
// again, choosing to ignore error
return string(unicode.ToLower(r)) + name[n:]
}
func chooseFirst(s1, s2 string) string {
return s1
}
func chooseSecond(s1, s2 string) string {
return s2
}