Skip to content

Commit

Permalink
prefer canonical transform vs DecodeMapStructure
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Oct 4, 2024
1 parent 48d3a5b commit 8158217
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 55 deletions.
4 changes: 2 additions & 2 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,7 @@ services:
count: 2
device_ids: ["my-device-id"]
`)
assert.ErrorContains(t, err, `invalid "count" and "device_ids" are attributes are exclusive`)
assert.ErrorContains(t, err, `"count" and "device_ids" attributes are exclusive`)
}

func TestServiceDeviceRequestCapabilitiesMandatory(t *testing.T) {
Expand All @@ -2221,7 +2221,7 @@ services:
- driver: nvidia
count: 2
`)
assert.ErrorContains(t, err, `"capabilities" attribute is mandatory for device request definition`)
assert.ErrorContains(t, err, `capabilities is required`)
}

func TestServicePullPolicy(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,10 @@
"options":{"$ref": "#/definitions/list_or_dict"}
},
"additionalProperties": false,
"patternProperties": {"^x-": {}}
"patternProperties": {"^x-": {}},
"required": [
"capabilities"
]
}
},

Expand Down
1 change: 1 addition & 0 deletions transform/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
transformers["services.*.build.secrets.*"] = transformFileMount
transformers["services.*.build.additional_contexts"] = transformKeyValue
transformers["services.*.depends_on"] = transformDependsOn
transformers["services.*.deploy.resources.reservations.devices.*"] = transformDeviceRequest
transformers["services.*.env_file"] = transformEnvFile
transformers["services.*.extends"] = transformExtends
transformers["services.*.networks"] = transformServiceNetworks
Expand Down
40 changes: 40 additions & 0 deletions transform/devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transform

import (
"fmt"

"github.com/compose-spec/compose-go/v2/tree"
)

func transformDeviceRequest(data any, p tree.Path, ignoreParseError bool) (any, error) {
switch v := data.(type) {
case map[string]any:
_, hasCount := v["count"]
_, hasIds := v["device_ids"]
if hasCount && hasIds {
return nil, fmt.Errorf(`%s: "count" and "device_ids" attributes are exclusive`, p)
}
if !hasCount && !hasIds {
v["count"] = "all"
}
return transformMapping(v, p, ignoreParseError)
default:
return data, fmt.Errorf("%s: invalid type %T for device request", p, v)
}
}
52 changes: 0 additions & 52 deletions types/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,3 @@ func (c *DeviceCount) DecodeMapstructure(value interface{}) error {
}
return nil
}

func (d *DeviceRequest) DecodeMapstructure(value interface{}) error {
v, ok := value.(map[string]any)
if !ok {
return fmt.Errorf("invalid device request type %T", value)
}
if _, okCaps := v["capabilities"]; !okCaps {
return fmt.Errorf(`"capabilities" attribute is mandatory for device request definition`)
}
if _, okCount := v["count"]; okCount {
if _, okDeviceIds := v["device_ids"]; okDeviceIds {
return fmt.Errorf(`invalid "count" and "device_ids" are attributes are exclusive`)
}
}
d.Count = DeviceCount(-1)

capabilities := v["capabilities"]
caps := StringList{}
if err := caps.DecodeMapstructure(capabilities); err != nil {
return err
}
d.Capabilities = caps
if driver, ok := v["driver"]; ok {
if val, ok := driver.(string); ok {
d.Driver = val
} else {
return fmt.Errorf("invalid type for driver value: %T", driver)
}
}
if count, ok := v["count"]; ok {
if err := d.Count.DecodeMapstructure(count); err != nil {
return err
}
}
if deviceIDs, ok := v["device_ids"]; ok {
ids := StringList{}
if err := ids.DecodeMapstructure(deviceIDs); err != nil {
return err
}
d.IDs = ids
d.Count = DeviceCount(len(ids))
}

d.Options = Mapping{}
if options, ok := v["options"].(map[string]any); ok {
for k, v := range options {
d.Options[k] = fmt.Sprint(v)
}
}
return nil

}

0 comments on commit 8158217

Please sign in to comment.