Explorar o código

Accept more integer options as strings in the json

Ask Bjørn Hansen %!s(int64=13) %!d(string=hai) anos
pai
achega
3510fd9343
Modificáronse 1 ficheiros con 22 adicións e 5 borrados
  1. 22 5
      config.go

+ 22 - 5
config.go

@@ -118,11 +118,11 @@ func readZoneFile(zoneName, fileName string) (*Zone, error) {
 			case "ttl", "serial", "max_hosts":
 				switch option := k; option {
 				case "ttl":
-					Zone.Options.Ttl = int(v.(float64))
+					Zone.Options.Ttl = valueToInt(v)
 				case "serial":
-					Zone.Options.Serial = int(v.(float64))
+					Zone.Options.Serial = valueToInt(v)
 				case "max_hosts":
-					Zone.Options.MaxHosts = int(v.(float64))
+					Zone.Options.MaxHosts = valueToInt(v)
 				}
 				continue
 
@@ -166,11 +166,11 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
 		label.MaxHosts = Zone.Options.MaxHosts
 
 		if ttl, ok := dv["ttl"]; ok {
-			label.Ttl = int(ttl.(float64))
+			label.Ttl = valueToInt(ttl)
 		}
 
 		if maxHosts, ok := dv["max_hosts"]; ok {
-			label.MaxHosts = int(maxHosts.(float64))
+			label.MaxHosts = valueToInt(maxHosts)
 		}
 
 		for rType, dnsType := range recordTypes {
@@ -346,3 +346,20 @@ func setupSOA(Zone *Zone) {
 	label.Records[dns.TypeSOA][0] = record
 
 }
+
+func valueToInt(v interface{}) (rv int) {
+	switch v.(type) {
+	case string:
+		i, err := strconv.Atoi(v.(string))
+		if err != nil {
+			panic("Error converting weight to integer")
+		}
+		rv = i
+	case float64:
+		rv = int(v.(float64))
+	default:
+		log.Println("Can't convert", v, "to integer")
+		panic("Can't convert value")
+	}
+	return rv
+}