Skip to content

Commit aaa69d0

Browse files
authored
Merge pull request #3 from nvnieuwk/fix/big-calculations
Fix/big calculations
2 parents 9905510 + 295b7f7 commit aaa69d0

File tree

7 files changed

+116
-92
lines changed

7 files changed

+116
-92
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# bedgovcf changelog
2+
3+
## v0.1.1 - The Second One
4+
5+
### Fixes
6+
7+
1. Big numbers are now printed correctly using `~round`, `~min` and `~sum`
8+
9+
## v0.1.0 - The First One
10+
11+
The first release of bedgovcf

bedgovcf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func main() {
6363
},
6464
Action: func(c *cli.Context) error {
6565
logger := log.New(os.Stderr, "", 0)
66-
err, config := bedgovcf.ReadConfig(c.String("config"))
66+
config, err := bedgovcf.ReadConfig(c.String("config"))
6767
if err != nil {
6868
logger.Fatal(err)
6969
}

convert/config.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package bedgovcf
22

33
import (
4-
"errors"
54
"fmt"
65
"log"
76
"os"
@@ -11,20 +10,20 @@ import (
1110
)
1211

1312
// Read the configuration file, cast it to its struct and validate
14-
func ReadConfig(configString string) (error, Config) {
13+
func ReadConfig(configString string) (Config, error) {
1514
configFile, err := os.ReadFile(configString)
1615
if err != nil {
17-
return errors.New(fmt.Sprintf("Failed to open the config file: %v", err)), Config{}
16+
return Config{}, fmt.Errorf("failed to open the config file: %v", err)
1817
}
1918

2019
var config Config
2120

2221
if err := yaml.Unmarshal(configFile, &config); err != nil {
23-
return errors.New(fmt.Sprintf("Failed to open the config file: %v", err)), Config{}
22+
return Config{}, fmt.Errorf("failed to open the config file: %v", err)
2423
}
2524

2625
config.validate()
27-
return nil, config
26+
return config, nil
2827
}
2928

3029
// Validate the config

convert/resolve.go

+25-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package bedgovcf
22

33
import (
4-
"errors"
54
"fmt"
65
"log"
76
"math"
@@ -10,7 +9,7 @@ import (
109
"strings"
1110
)
1211

13-
func resolveField(configValues []string, bedValues []string, bedHeader []string) (error, string) {
12+
func resolveField(configValues []string, bedValues []string, bedHeader []string) (string, error) {
1413

1514
input := []string{}
1615
for _, v := range configValues {
@@ -33,38 +32,38 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)
3332
if strings.HasPrefix(input[0], "~") {
3433
function = configValues[0][1:]
3534
} else {
36-
return nil, strings.Join(input, " ")
35+
return strings.Join(input, " "), nil
3736
}
3837

3938
switch function {
4039
case "round":
4140
// ~round <value>
4241
float, err := strconv.ParseFloat(input[1], 64)
4342
if err != nil {
44-
return errors.New(fmt.Sprintf("Failed to parse the value (%v) to a float: %v", input[1], err)), ""
43+
return "", fmt.Errorf("failed to parse the value (%v) to a float: %v", input[1], err)
4544
}
4645
round := math.Round(float)
4746
if round == -0 {
4847
round = 0
4948
}
50-
return nil, fmt.Sprintf("%v", round)
49+
return strconv.FormatFloat(round, 'f', -1, 64), nil
5150
case "sum":
5251
// ~sum <value1> <value2> ...
5352
var sum float64
5453
for _, v := range input[1:] {
5554
float, err := strconv.ParseFloat(v, 64)
5655
if err != nil {
57-
return errors.New(fmt.Sprintf("Failed to parse the value (%v) to a float: %v", v, err)), ""
56+
return "", fmt.Errorf("failed to parse the value (%v) to a float: %v", v, err)
5857
}
5958
sum += float
6059
}
6160

62-
return nil, strconv.FormatFloat(sum, 'g', -1, 64)
61+
return strconv.FormatFloat(sum, 'f', -1, 64), nil
6362
case "min":
6463
// ~min <startValue> <valueToSubstract1> <valueToSubstract2> ...
6564
min, err := strconv.ParseFloat(input[1], 64)
6665
if err != nil {
67-
return errors.New(fmt.Sprintf("Failed to parse the value (%v) to a float: %v", input[1], err)), ""
66+
return "", fmt.Errorf("failed to parse the value (%v) to a float: %v", input[1], err)
6867
}
6968
for _, v := range input[2:] {
7069
float, err := strconv.ParseFloat(v, 64)
@@ -73,7 +72,7 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)
7372
}
7473
min -= float
7574
}
76-
return nil, strconv.FormatFloat(min, 'g', -1, 64)
75+
return strconv.FormatFloat(min, 'f', -1, 64), nil
7776
case "if":
7877
// ~if <value1> <operator> <value2> <value_if_true> <value_if_false>
7978
// supported operators: > < >= <= ==
@@ -88,15 +87,15 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)
8887

8988
floatOperators := []string{"<", ">", "<=", ">="}
9089
if slices.Contains(floatOperators, operator) && (err1 != nil || err2 != nil) {
91-
return errors.New(fmt.Sprintf("Failed to parse the values (%v and %v) to a float: %v and %v", v1, v2, err1, err2)), ""
90+
return "", fmt.Errorf("failed to parse the values (%v and %v) to a float: %v and %v", v1, v2, err1, err2)
9291
}
9392

9493
vFalseResolved := ""
9594
var err error
9695
if strings.HasPrefix(vFalse[0], "~") {
97-
err, vFalseResolved = resolveField(vFalse, bedValues, bedHeader)
96+
vFalseResolved, err = resolveField(vFalse, bedValues, bedHeader)
9897
if err != nil {
99-
return err, ""
98+
return "", err
10099
}
101100
} else {
102101
vFalseResolved = strings.Join(vFalse, " ")
@@ -105,43 +104,43 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)
105104
switch operator {
106105
case "<":
107106
if floatV1 < floatV2 {
108-
return nil, vTrue
107+
return vTrue, nil
109108
} else {
110-
return nil, vFalseResolved
109+
return vFalseResolved, nil
111110
}
112111
case ">":
113112
if floatV1 > floatV2 {
114-
return nil, vTrue
113+
return vTrue, nil
115114
} else {
116-
return nil, vFalseResolved
115+
return vFalseResolved, nil
117116
}
118117
case ">=":
119118
if floatV1 >= floatV2 {
120-
return nil, vTrue
119+
return vTrue, nil
121120
} else {
122-
return nil, vFalseResolved
121+
return vFalseResolved, nil
123122
}
124123
case "<=":
125124
if floatV1 <= floatV2 {
126-
return nil, vTrue
125+
return vTrue, nil
127126
} else {
128-
return nil, vFalseResolved
127+
return vFalseResolved, nil
129128
}
130129
case "==":
131130
if v1 == v2 {
132-
return nil, vTrue
131+
return vTrue, nil
133132
} else {
134-
return nil, vFalseResolved
133+
return vFalseResolved, nil
135134
}
136135
case "!=":
137136
if v1 != v2 {
138-
return nil, vTrue
137+
return vTrue, nil
139138
} else {
140-
return nil, vFalseResolved
139+
return vFalseResolved, nil
141140
}
142141
}
143142
}
144143

145-
err := errors.New(fmt.Sprintf("The function %v is not supported", function))
146-
return err, ""
144+
err := fmt.Errorf("the function %v is not supported", function)
145+
return "", err
147146
}

0 commit comments

Comments
 (0)