generated from adrienaury/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add config file loading (#7)
* feat(config): add config file loading * chore: bump devcontainer golang 1.21 * feat: exclude value + coherent source * feat: exclude value + coherent source * style: lint * test: config exclude and coherentWith * chore: reorg tests * chore: test update ci container * Update Dockerfile.ci * Update Dockerfile * feat: constraints validation * test: constraints * feat: exit code 1 if constraint fail * docs: update readme and changelog
- Loading branch information
1 parent
f403d2b
commit 47c0489
Showing
30 changed files
with
855 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
FROM adrienaury/go-devcontainer:v2.0 | ||
FROM adrienaury/go-devcontainer:v3.1 | ||
|
||
USER root | ||
|
||
RUN apk add --update --progress --no-cache make gomplate | ||
|
||
ARG VERSION_GOLICENSE=0.2.0 | ||
ARG VERSION_MILLER=6.2.0 | ||
RUN wget -nv -O- https://github.com/mitchellh/golicense/releases/download/v${VERSION_GOLICENSE}/golicense_${VERSION_GOLICENSE}_linux_x86_64.tar.gz | tar xz -C /usr/bin golicense \ | ||
&& wget -nv -O- https://github.com/johnkerl/miller/releases/download/v${VERSION_MILLER}/miller-${VERSION_MILLER}-linux-amd64.tar.gz | tar xz --strip-components 1 -C /usr/bin miller-${VERSION_MILLER}-linux-amd64/mlr \ | ||
&& chmod +x /usr/bin/golicense /usr/bin/mlr | ||
RUN wget -nv -O- https://github.com/johnkerl/miller/releases/download/v${VERSION_MILLER}/miller-${VERSION_MILLER}-linux-amd64.tar.gz | tar xz --strip-components 1 -C /usr/bin miller-${VERSION_MILLER}-linux-amd64/mlr \ | ||
&& chmod +x /usr/bin/mlr | ||
|
||
ARG VERSION_PIMO=1.19.0 | ||
RUN wget -O- https://github.com/CGI-FR/PIMO/releases/download/v${VERSION_PIMO}/pimo_${VERSION_PIMO}_linux_amd64.tar.gz | tar xz -C /usr/bin pimo | ||
|
||
USER vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
FROM adrienaury/go-devcontainer-ci:v2.0 | ||
FROM adrienaury/go-devcontainer-ci:v3.1 | ||
|
||
USER root | ||
|
||
RUN apk add --update --progress --no-cache make gomplate | ||
|
||
ARG VERSION_GOLICENSE=0.2.0 | ||
ARG VERSION_MILLER=6.2.0 | ||
RUN wget -nv -O- https://github.com/mitchellh/golicense/releases/download/v${VERSION_GOLICENSE}/golicense_${VERSION_GOLICENSE}_linux_x86_64.tar.gz | tar xz -C /usr/bin golicense \ | ||
&& wget -nv -O- https://github.com/johnkerl/miller/releases/download/v${VERSION_MILLER}/miller-${VERSION_MILLER}-linux-amd64.tar.gz | tar xz --strip-components 1 -C /usr/bin miller-${VERSION_MILLER}-linux-amd64/mlr \ | ||
&& chmod +x /usr/bin/golicense /usr/bin/mlr | ||
RUN wget -nv -O- https://github.com/johnkerl/miller/releases/download/v${VERSION_MILLER}/miller-${VERSION_MILLER}-linux-amd64.tar.gz | tar xz --strip-components 1 -C /usr/bin miller-${VERSION_MILLER}-linux-amd64/mlr \ | ||
&& chmod +x /usr/bin/mlr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: "1" | ||
metrics: | ||
- name: "name" | ||
exclude: [""] | ||
coherentWith: ["name"] | ||
constraints: | ||
maskingRate: | ||
shouldEqual: 1 | ||
coherentRate: | ||
shouldBeGreaterThan: 0.5 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (C) 2023 CGI France | ||
// | ||
// This file is part of MIMO. | ||
// | ||
// MIMO is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// MIMO is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with MIMO. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package infra | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cgi-fr/mimo/pkg/mimo" | ||
"github.com/rs/zerolog/log" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Version of the YAML strcuture. | ||
const Version string = "1" | ||
|
||
// YAMLStructure of the file. | ||
type YAMLStructure struct { | ||
Version string `yaml:"version"` | ||
Columns []YAMLColumn `yaml:"metrics,omitempty"` | ||
} | ||
|
||
// YAMLColumn defines how to store a column config in YAML format. | ||
type YAMLColumn struct { | ||
Name string `yaml:"name"` | ||
Exclude []any `yaml:"exclude,omitempty"` | ||
CoherentWith []string `yaml:"coherentWith,omitempty"` | ||
Constraints map[string]YAMLConstraint `yaml:"constraints,omitempty"` | ||
} | ||
|
||
type YAMLConstraint map[string]float64 | ||
|
||
func LoadConfig(filename string) (mimo.Config, error) { | ||
config := &YAMLStructure{ | ||
Version: Version, | ||
Columns: []YAMLColumn{}, | ||
} | ||
|
||
if _, err := os.Stat(filename); os.IsNotExist(err) { | ||
return mimo.NewConfig(), fmt.Errorf("%w: %s", ErrConfigFileNotExists, filename) | ||
} | ||
|
||
log.Debug().Str("file", filename).Msg("loading config from file") | ||
|
||
dat, err := os.ReadFile(filename) | ||
if err != nil { | ||
return mimo.NewConfig(), fmt.Errorf("%w: %s", err, filename) | ||
} | ||
|
||
err = yaml.Unmarshal(dat, config) | ||
if err != nil { | ||
return mimo.NewConfig(), fmt.Errorf("%w: %s", err, filename) | ||
} | ||
|
||
if config.Version != Version { | ||
return mimo.NewConfig(), fmt.Errorf("%w: %s", ErrConfigInvalidVersion, filename) | ||
} | ||
|
||
return CreateConfig(config) | ||
} | ||
|
||
//nolint:cyclop | ||
func CreateConfig(yamlconfig *YAMLStructure) (mimo.Config, error) { | ||
config := mimo.NewConfig() | ||
|
||
for _, yamlcolumn := range yamlconfig.Columns { | ||
column := mimo.ColumnConfig{ | ||
Exclude: yamlcolumn.Exclude, | ||
CoherentWith: yamlcolumn.CoherentWith, | ||
Constraints: []mimo.Constraint{}, | ||
} | ||
|
||
for target, yamlconstraint := range yamlcolumn.Constraints { | ||
for constraintType, value := range yamlconstraint { | ||
constraint := mimo.Constraint{ | ||
Target: 0, | ||
Type: 0, | ||
Value: value, | ||
} | ||
|
||
switch target { | ||
case "maskingRate": | ||
constraint.Target = mimo.MaskingRate | ||
case "coherentRate": | ||
constraint.Target = mimo.CohenrentRate | ||
case "identifiantRate": | ||
constraint.Target = mimo.IdentifiantRate | ||
default: | ||
return config, fmt.Errorf("%w: %s", ErrConfigInvalidConstraintTarget, target) | ||
} | ||
|
||
switch constraintType { | ||
case "shouldEqual": | ||
constraint.Type = mimo.ShouldEqual | ||
case "shouldBeGreaterThan": | ||
constraint.Type = mimo.ShouldBeGreaterThan | ||
case "shouldBeGreaterThanOrEqualTo": | ||
constraint.Type = mimo.ShouldBeGreaterThanOrEqualTo | ||
case "shouldBeLowerThan": | ||
constraint.Type = mimo.ShouldBeLowerThan | ||
case "shouldBeLessThanOrEqualTo": | ||
constraint.Type = mimo.ShouldBeLessThanOrEqualTo | ||
default: | ||
return config, fmt.Errorf("%w: %s", ErrConfigInvalidConstraintType, constraintType) | ||
} | ||
|
||
column.Constraints = append(column.Constraints, constraint) | ||
} | ||
} | ||
|
||
config.ColumnNames = append(config.ColumnNames, yamlcolumn.Name) | ||
config.ColumnConfigs[yamlcolumn.Name] = column | ||
} | ||
|
||
return config, nil | ||
} |
Oops, something went wrong.