Skip to content

Commit fb8e04d

Browse files
committed
seiralise NanoCPUs as string
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 7218685 commit fb8e04d

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

types/cpus.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package types
18+
19+
import (
20+
"fmt"
21+
"strconv"
22+
)
23+
24+
type NanoCPUs float32
25+
26+
func (n *NanoCPUs) DecodeMapstructure(a any) error {
27+
switch v := a.(type) {
28+
case string:
29+
f, err := strconv.ParseFloat(v, 64)
30+
if err != nil {
31+
return err
32+
}
33+
*n = NanoCPUs(f)
34+
case int:
35+
*n = NanoCPUs(v)
36+
case float32:
37+
*n = NanoCPUs(v)
38+
case float64:
39+
*n = NanoCPUs(v)
40+
default:
41+
return fmt.Errorf("unexpected value type %T for cpus", v)
42+
}
43+
return nil
44+
}
45+
46+
func (n *NanoCPUs) Value() float32 {
47+
return float32(*n)
48+
}

types/types.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"sort"
23-
"strconv"
2423
"strings"
2524

2625
"github.com/docker/go-connections/nat"
@@ -391,30 +390,6 @@ type Resource struct {
391390
Extensions Extensions `yaml:"#extensions,inline,omitempty" json:"-"`
392391
}
393392

394-
type NanoCPUs float32
395-
396-
func (n *NanoCPUs) DecodeMapstructure(a any) error {
397-
switch v := a.(type) {
398-
case string:
399-
f, err := strconv.ParseFloat(v, 64)
400-
if err != nil {
401-
return err
402-
}
403-
*n = NanoCPUs(f)
404-
case float32:
405-
*n = NanoCPUs(v)
406-
case float64:
407-
*n = NanoCPUs(v)
408-
default:
409-
return fmt.Errorf("unexpected value type %T for cpus", v)
410-
}
411-
return nil
412-
}
413-
414-
func (n *NanoCPUs) Value() float32 {
415-
return float32(*n)
416-
}
417-
418393
// GenericResource represents a "user defined" resource which can
419394
// only be an integer (e.g: SSD=3) for a service
420395
type GenericResource struct {

0 commit comments

Comments
 (0)