15467: Introduce "StringSet"
authorPeter Amstutz <pamstutz@veritasgenetics.com>
Fri, 26 Jul 2019 20:21:20 +0000 (16:21 -0400)
committerPeter Amstutz <pamstutz@veritasgenetics.com>
Mon, 29 Jul 2019 18:58:16 +0000 (14:58 -0400)
Accepts an array of strings and converts to map for backwards
compatibility.

Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <pamstutz@veritasgenetics.com>

sdk/go/arvados/config.go
sdk/go/arvados/config_test.go

index e04849bb89810b8526244e36f1e469efa832c6b8..bee93046eb8e696f554e31e04a7b6fb0a9fb37dc 100644 (file)
@@ -69,7 +69,7 @@ type Cluster struct {
 
        API struct {
                AsyncPermissionsUpdateInterval Duration
-               DisabledAPIs                   map[string]struct{}
+               DisabledAPIs                   StringSet
                MaxIndexDatabaseRead           int
                MaxItemsPerResponse            int
                MaxRequestAmplification        int
@@ -83,7 +83,7 @@ type Cluster struct {
        AuditLogs struct {
                MaxAge             Duration
                MaxDeleteBatch     int
-               UnloggedAttributes map[string]struct{}
+               UnloggedAttributes StringSet
        }
        Collections struct {
                BlobSigning          bool
@@ -135,10 +135,10 @@ type Cluster struct {
                AutoSetupNewUsers                     bool
                AutoSetupNewUsersWithRepository       bool
                AutoSetupNewUsersWithVmUUID           string
-               AutoSetupUsernameBlacklist            map[string]struct{}
+               AutoSetupUsernameBlacklist            StringSet
                EmailSubjectPrefix                    string
-               NewInactiveUserNotificationRecipients map[string]struct{}
-               NewUserNotificationRecipients         map[string]struct{}
+               NewInactiveUserNotificationRecipients StringSet
+               NewUserNotificationRecipients         StringSet
                NewUsersAreActive                     bool
                UserNotifierEmailFrom                 string
                UserProfileNotificationAddress        string
@@ -148,7 +148,7 @@ type Cluster struct {
                APIClientConnectTimeout          Duration
                APIClientReceiveTimeout          Duration
                APIResponseCompression           bool
-               ApplicationMimetypesWithViewIcon map[string]struct{}
+               ApplicationMimetypesWithViewIcon StringSet
                ArvadosDocsite                   string
                ArvadosPublicDataDocURL          string
                DefaultOpenIdPrefix              string
@@ -267,7 +267,7 @@ type ContainersConfig struct {
        MinRetryPeriod              Duration
        ReserveExtraRAM             ByteSize
        StaleLockTimeout            Duration
-       SupportedDockerImageFormats map[string]struct{}
+       SupportedDockerImageFormats StringSet
        UsePreemptibleInstances     bool
 
        JobsAPI struct {
@@ -300,7 +300,7 @@ type ContainersConfig struct {
                        DNSServerReloadCommand string
                        DNSServerUpdateCommand string
                        ComputeNodeDomain      string
-                       ComputeNodeNameservers map[string]struct{}
+                       ComputeNodeNameservers StringSet
                        AssignNodeHostname     string
                }
        }
@@ -388,6 +388,40 @@ func (it *InstanceTypeMap) UnmarshalJSON(data []byte) error {
        return nil
 }
 
+type StringSet map[string]struct{}
+
+// UnmarshalJSON handles old config files that provide an array of
+// instance types instead of a hash.
+func (ss *StringSet) UnmarshalJSON(data []byte) error {
+       if len(data) > 0 && data[0] == '[' {
+               var arr []string
+               err := json.Unmarshal(data, &arr)
+               if err != nil {
+                       return err
+               }
+               if len(arr) == 0 {
+                       *ss = nil
+                       return nil
+               }
+               *ss = make(map[string]struct{}, len(arr))
+               for _, t := range arr {
+                       (*ss)[t] = struct{}{}
+               }
+               return nil
+       }
+       var hash map[string]struct{}
+       err := json.Unmarshal(data, &hash)
+       if err != nil {
+               return err
+       }
+       *ss = make(map[string]struct{}, len(hash))
+       for t, _ := range hash {
+               (*ss)[t] = struct{}{}
+       }
+
+       return nil
+}
+
 type ServiceName string
 
 const (
index 59c7432686c8fb3246c2aab007eb1f01b4d7fdd6..b984cb5669ce851f2ec1f136a9c96bfb0d06b832 100644 (file)
@@ -14,6 +14,16 @@ var _ = check.Suite(&ConfigSuite{})
 type ConfigSuite struct{}
 
 func (s *ConfigSuite) TestInstanceTypesAsArray(c *check.C) {
+       var cluster Cluster
+       yaml.Unmarshal([]byte(`
+API:
+  DisabledAPIs: [jobs.list]`), &cluster)
+       c.Check(len(cluster.API.DisabledAPIs), check.Equals, 1)
+       _, ok := cluster.API.DisabledAPIs["jobs.list"]
+       c.Check(ok, check.Equals, true)
+}
+
+func (s *ConfigSuite) TestStringSetAsArray(c *check.C) {
        var cluster Cluster
        yaml.Unmarshal([]byte("InstanceTypes:\n- Name: foo\n"), &cluster)
        c.Check(len(cluster.InstanceTypes), check.Equals, 1)