mirror of
https://github.com/coredns/coredns.git
synced 2025-12-09 03:45:11 -05:00
cleanups: go vet/golint (#331)
Go vet and golint the new code once again. Drop Name from NameTemplate - it's cleaner: nametemplate.Template.
This commit is contained in:
@@ -61,14 +61,16 @@ var requiredSymbols = []string{
|
||||
// symbol consumes the other segments. Most likely this would be the servicename.
|
||||
// Also consider how to handle static strings in the format template.
|
||||
|
||||
type NameTemplate struct {
|
||||
// TODO(infoblox): docs
|
||||
type Template struct {
|
||||
formatString string
|
||||
splitFormat []string
|
||||
// Element is a map of element name :: index in the segmented record name for the named element
|
||||
Element map[string]int
|
||||
}
|
||||
|
||||
func (t *NameTemplate) SetTemplate(s string) error {
|
||||
// TODO: docs
|
||||
func (t *Template) SetTemplate(s string) error {
|
||||
var err error
|
||||
|
||||
t.Element = map[string]int{}
|
||||
@@ -105,7 +107,7 @@ func (t *NameTemplate) SetTemplate(s string) error {
|
||||
// to treat the query string segments as a reverse stack and
|
||||
// step down the stack to find the right element.
|
||||
|
||||
func (t *NameTemplate) GetZoneFromSegmentArray(segments []string) string {
|
||||
func (t *Template) ZoneFromSegmentArray(segments []string) string {
|
||||
index, ok := t.Element["zone"]
|
||||
if !ok {
|
||||
return ""
|
||||
@@ -113,16 +115,16 @@ func (t *NameTemplate) GetZoneFromSegmentArray(segments []string) string {
|
||||
return strings.Join(segments[index:len(segments)], ".")
|
||||
}
|
||||
|
||||
func (t *NameTemplate) GetNamespaceFromSegmentArray(segments []string) string {
|
||||
return t.GetSymbolFromSegmentArray("namespace", segments)
|
||||
func (t *Template) NamespaceFromSegmentArray(segments []string) string {
|
||||
return t.SymbolFromSegmentArray("namespace", segments)
|
||||
}
|
||||
|
||||
func (t *NameTemplate) GetServiceFromSegmentArray(segments []string) string {
|
||||
return t.GetSymbolFromSegmentArray("service", segments)
|
||||
func (t *Template) ServiceFromSegmentArray(segments []string) string {
|
||||
return t.SymbolFromSegmentArray("service", segments)
|
||||
}
|
||||
|
||||
func (t *NameTemplate) GetTypeFromSegmentArray(segments []string) string {
|
||||
typeSegment := t.GetSymbolFromSegmentArray("type", segments)
|
||||
func (t *Template) TypeFromSegmentArray(segments []string) string {
|
||||
typeSegment := t.SymbolFromSegmentArray("type", segments)
|
||||
|
||||
// Limit type to known types symbols
|
||||
if dns_strings.StringInSlice(typeSegment, types) {
|
||||
@@ -132,7 +134,7 @@ func (t *NameTemplate) GetTypeFromSegmentArray(segments []string) string {
|
||||
return typeSegment
|
||||
}
|
||||
|
||||
func (t *NameTemplate) GetSymbolFromSegmentArray(symbol string, segments []string) string {
|
||||
func (t *Template) SymbolFromSegmentArray(symbol string, segments []string) string {
|
||||
index, ok := t.Element[symbol]
|
||||
if !ok {
|
||||
return ""
|
||||
@@ -140,9 +142,9 @@ func (t *NameTemplate) GetSymbolFromSegmentArray(symbol string, segments []strin
|
||||
return segments[index]
|
||||
}
|
||||
|
||||
// GetRecordNameFromNameValues returns the string produced by applying the
|
||||
// RecordNameFromNameValues returns the string produced by applying the
|
||||
// values to the NameTemplate format string.
|
||||
func (t *NameTemplate) GetRecordNameFromNameValues(values NameValues) string {
|
||||
func (t *Template) RecordNameFromNameValues(values NameValues) string {
|
||||
recordName := make([]string, len(t.splitFormat))
|
||||
copy(recordName[:], t.splitFormat)
|
||||
|
||||
@@ -164,8 +166,8 @@ func (t *NameTemplate) GetRecordNameFromNameValues(values NameValues) string {
|
||||
return strings.Join(recordName, ".")
|
||||
}
|
||||
|
||||
func (t *NameTemplate) IsValid() bool {
|
||||
// This is *only* used in a test, for the test this should be a private method.
|
||||
// IsValid returns true if the template has all the required symbols, false otherwise.
|
||||
func (t *Template) IsValid() bool {
|
||||
result := true
|
||||
|
||||
// Ensure that all requiredSymbols are found in NameTemplate
|
||||
@@ -179,6 +181,7 @@ func (t *NameTemplate) IsValid() bool {
|
||||
return result
|
||||
}
|
||||
|
||||
// TODO(infoblox): what's this?
|
||||
type NameValues struct {
|
||||
ServiceName string
|
||||
Namespace string
|
||||
|
||||
@@ -22,7 +22,7 @@ var exampleTemplates = map[string][]int{
|
||||
func TestSetTemplate(t *testing.T) {
|
||||
for s, expectedValue := range exampleTemplates {
|
||||
|
||||
n := new(NameTemplate)
|
||||
n := new(Template)
|
||||
n.SetTemplate(s)
|
||||
|
||||
// check the indexes resulting from calling SetTemplate() against expectedValues
|
||||
@@ -34,9 +34,9 @@ func TestSetTemplate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetServiceFromSegmentArray(t *testing.T) {
|
||||
func TestServiceFromSegmentArray(t *testing.T) {
|
||||
var (
|
||||
n *NameTemplate
|
||||
n *Template
|
||||
formatString string
|
||||
queryString string
|
||||
splitQuery []string
|
||||
@@ -45,37 +45,37 @@ func TestGetServiceFromSegmentArray(t *testing.T) {
|
||||
)
|
||||
|
||||
// Case where template contains {service}
|
||||
n = new(NameTemplate)
|
||||
n = new(Template)
|
||||
formatString = "{service}.{namespace}.{zone}"
|
||||
n.SetTemplate(formatString)
|
||||
|
||||
queryString = "myservice.mynamespace.coredns"
|
||||
splitQuery = strings.Split(queryString, ".")
|
||||
expectedService = "myservice"
|
||||
actualService = n.GetServiceFromSegmentArray(splitQuery)
|
||||
actualService = n.ServiceFromSegmentArray(splitQuery)
|
||||
|
||||
if actualService != expectedService {
|
||||
t.Errorf("Expected service name '%v', instead got service name '%v' for query string '%v' and format '%v'", expectedService, actualService, queryString, formatString)
|
||||
}
|
||||
|
||||
// Case where template does not contain {service}
|
||||
n = new(NameTemplate)
|
||||
n = new(Template)
|
||||
formatString = "{namespace}.{zone}"
|
||||
n.SetTemplate(formatString)
|
||||
|
||||
queryString = "mynamespace.coredns"
|
||||
splitQuery = strings.Split(queryString, ".")
|
||||
expectedService = ""
|
||||
actualService = n.GetServiceFromSegmentArray(splitQuery)
|
||||
actualService = n.ServiceFromSegmentArray(splitQuery)
|
||||
|
||||
if actualService != expectedService {
|
||||
t.Errorf("Expected service name '%v', instead got service name '%v' for query string '%v' and format '%v'", expectedService, actualService, queryString, formatString)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetZoneFromSegmentArray(t *testing.T) {
|
||||
func TestZoneFromSegmentArray(t *testing.T) {
|
||||
var (
|
||||
n *NameTemplate
|
||||
n *Template
|
||||
formatString string
|
||||
queryString string
|
||||
splitQuery []string
|
||||
@@ -84,42 +84,42 @@ func TestGetZoneFromSegmentArray(t *testing.T) {
|
||||
)
|
||||
|
||||
// Case where template contains {zone}
|
||||
n = new(NameTemplate)
|
||||
n = new(Template)
|
||||
formatString = "{service}.{namespace}.{zone}"
|
||||
n.SetTemplate(formatString)
|
||||
|
||||
queryString = "myservice.mynamespace.coredns"
|
||||
splitQuery = strings.Split(queryString, ".")
|
||||
expectedZone = "coredns"
|
||||
actualZone = n.GetZoneFromSegmentArray(splitQuery)
|
||||
actualZone = n.ZoneFromSegmentArray(splitQuery)
|
||||
|
||||
if actualZone != expectedZone {
|
||||
t.Errorf("Expected zone name '%v', instead got zone name '%v' for query string '%v' and format '%v'", expectedZone, actualZone, queryString, formatString)
|
||||
}
|
||||
|
||||
// Case where template does not contain {zone}
|
||||
n = new(NameTemplate)
|
||||
n = new(Template)
|
||||
formatString = "{service}.{namespace}"
|
||||
n.SetTemplate(formatString)
|
||||
|
||||
queryString = "mynamespace.coredns"
|
||||
splitQuery = strings.Split(queryString, ".")
|
||||
expectedZone = ""
|
||||
actualZone = n.GetZoneFromSegmentArray(splitQuery)
|
||||
actualZone = n.ZoneFromSegmentArray(splitQuery)
|
||||
|
||||
if actualZone != expectedZone {
|
||||
t.Errorf("Expected zone name '%v', instead got zone name '%v' for query string '%v' and format '%v'", expectedZone, actualZone, queryString, formatString)
|
||||
}
|
||||
|
||||
// Case where zone is multiple segments
|
||||
n = new(NameTemplate)
|
||||
n = new(Template)
|
||||
formatString = "{service}.{namespace}.{zone}"
|
||||
n.SetTemplate(formatString)
|
||||
|
||||
queryString = "myservice.mynamespace.coredns.cluster.local"
|
||||
splitQuery = strings.Split(queryString, ".")
|
||||
expectedZone = "coredns.cluster.local"
|
||||
actualZone = n.GetZoneFromSegmentArray(splitQuery)
|
||||
actualZone = n.ZoneFromSegmentArray(splitQuery)
|
||||
|
||||
if actualZone != expectedZone {
|
||||
t.Errorf("Expected zone name '%v', instead got zone name '%v' for query string '%v' and format '%v'", expectedZone, actualZone, queryString, formatString)
|
||||
|
||||
Reference in New Issue
Block a user