Skip to content

Commit

Permalink
refactor: repair lint warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao-Ma5566 committed Apr 23, 2024
1 parent cfaa587 commit 79d86df
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
9 changes: 5 additions & 4 deletions cmd/sigo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Copyright (C) 2022 CGI France \n License GPLv3: GNU GPL version 3 <https://gnu.o
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.`, version, commit, buildDate, builtBy),
Run: func(cmd *cobra.Command, args []string) {
// nolint: exhaustivestruct
//nolint: exhaustivestruct
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

definition.flagIsSet(*cmd)
Expand All @@ -94,7 +94,7 @@ There is NO WARRANTY, to the extent permitted by law.`, version, commit, buildDa
rootCmd.PersistentFlags().
BoolVar(&logs.jsonlog, "log-json", false, "output logs in JSON format")
rootCmd.PersistentFlags().StringVar(&logs.colormode, "color", "auto", "use colors in log outputs : yes, no or auto")
// nolint: gomnd
//nolint: gomnd
rootCmd.PersistentFlags().IntVarP(&definition.k, "k-value", "k", 3, "k-value for k-anonymization")
rootCmd.PersistentFlags().IntVarP(&definition.l, "l-value", "l", 1, "l-value for l-diversity")
rootCmd.PersistentFlags().
Expand All @@ -121,6 +121,7 @@ There is NO WARRANTY, to the extent permitted by law.`, version, commit, buildDa
}
}

//nolint: funlen
func run(definition pdef, logs logs) {
initLog(logs, definition.entropy)

Expand Down Expand Up @@ -185,7 +186,7 @@ func run(definition pdef, logs logs) {
}
}

// nolint: cyclop
//nolint: cyclop
func initLog(logs logs, entropy bool) {
color := false

Expand All @@ -202,7 +203,7 @@ func initLog(logs logs, entropy bool) {
if logs.jsonlog {
logger = zerolog.New(os.Stderr)
} else {
// nolint: exhaustivestruct
//nolint: exhaustivestruct
logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: !color})
}

Expand Down
1 change: 0 additions & 1 deletion internal/infra/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func (jlr JSONLineRecord) QuasiIdentifer() ([]float64, error) {
// err := fmt.Errorf("unsupported type: %T", v)
// return []float64{}, err
// }

}

return result, nil
Expand Down
27 changes: 11 additions & 16 deletions pkg/sigo/anonymizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (a GeneralAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) R

// ComputeGeneralization calculates the min and max values of the cluster for each qi.
func (a GeneralAnonymizer) ComputeGeneralization(clus Cluster, qi []string) {
values, _ := listValues(clus, qi)
values := listValues(clus, qi)

boundsVal := make(map[string]bounds)

Expand Down Expand Up @@ -176,7 +176,7 @@ func (a AggregationAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []strin
// ComputeAggregation calculates the mean (method meanAggreagtion)
// or median (method medianAggregation) value of the cluster for each qi.
func (a AggregationAnonymizer) ComputeAggregation(clus Cluster, qi []string) {
values, _ := listValues(clus, qi)
values := listValues(clus, qi)

valAggregation := make(map[string]float64)

Expand All @@ -197,7 +197,7 @@ func (a AggregationAnonymizer) ComputeAggregation(clus Cluster, qi []string) {
// if the record is > Q3 then it takes the Q3 value
// if the record is < Q1 then it takes the Q1 value.
func (a CodingAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
values, _ := listValues(clus, qi)
values := listValues(clus, qi)
mask := map[string]interface{}{}

for i, key := range qi {
Expand All @@ -206,11 +206,8 @@ func (a CodingAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Re
bottom := q.Q1
top := q.Q3

recVals, err := rec.QuasiIdentifer()
if err != nil {
fmt.Println(err)
break
}
recVals, _ := rec.QuasiIdentifer()

val := recVals[i]

switch {
Expand All @@ -230,15 +227,17 @@ func (a CodingAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Re
// the record takes as value the original value added to a Laplacian or Gaussian noise
// the anonymized value stays within the bounds of the cluster.
func (a NoiseAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
values, _ := listValues(clus, qi)
values := listValues(clus, qi)
mask := map[string]interface{}{}

for i, key := range qi {
recVals, err := rec.QuasiIdentifer()
if err != nil {
fmt.Println(err)

break
}

val := recVals[i]

laplaceVal := Scaling(val, values[key], laplace)
Expand Down Expand Up @@ -292,7 +291,7 @@ func (a SwapAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Reco

func (a SwapAnonymizer) Swap(clus Cluster, qi []string) {
// retrieve the cluster values for each qi
values, _ := listValues(clus, qi)
values := listValues(clus, qi)

swapVal := make(map[string][]float64)

Expand Down Expand Up @@ -461,20 +460,16 @@ func (r Reidentification) ComputeSimilarity(rec Record, clus Cluster,
}

// Returns the list of values present in the cluster for each qi.
func listValues(clus Cluster, qi []string) (mapValues map[string][]float64, err error) {
func listValues(clus Cluster, qi []string) (mapValues map[string][]float64) {
mapValues = make(map[string][]float64)

for _, record := range clus.Records() {
for i, key := range qi {
vals, _ := record.QuasiIdentifer()
// if err != nil {
// fmt.Println(err)
// return map[string][]float64{}, err
// }
val := vals[i]
mapValues[key] = append(mapValues[key], val)
}
}

return mapValues, nil
return mapValues
}
1 change: 1 addition & 0 deletions pkg/sigo/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Anonymize(source RecordSource, factory GeneralizerFactory,
count := 0

log.Info().Msg("Reading source")

for source.Next() {
if source.Err() != nil {
return fmt.Errorf("%w", source.Err())
Expand Down
1 change: 0 additions & 1 deletion pkg/sigo/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func BenchmarkLongClustering(b *testing.B) {
func TestNullValueShouldReturnError(t *testing.T) {
t.Parallel()

// nolint: goconst
sourceText := `{"x":0, "y":0, "foo":"bar"}
{"x":null, "y":1, "foo":"bar"}
{"x":0, "y":null, "foo":"bar"}
Expand Down
13 changes: 12 additions & 1 deletion pkg/sigo/kdtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (n *node) build() error {
Int("Size", len(n.cluster)).
Msg("Cluster:")

//nolint: nestif
if n.isValid() && len(n.cluster) >= 2*n.tree.k {
// rollback to simple node
var (
Expand All @@ -130,7 +131,9 @@ func (n *node) build() error {
lower, upper, valide, err = n.split()
if err != nil {
return err
} else if !valide {
}

if !valide {
n.incRot()
} else {
break
Expand All @@ -151,9 +154,11 @@ func (n *node) build() error {

n.cluster = nil
err = n.subNodes[0].build()

if err != nil {
return err
}

err = n.subNodes[1].build()
if err != nil {
return err
Expand All @@ -173,17 +178,22 @@ func (n *node) split() (node, node, bool, error) {
if err != nil {
// Stocker l'erreur dans la variable globale
globalError = err

return false
}

valueJ, err := n.cluster[j].QuasiIdentifer()
if err != nil {
globalError = err

return false
}

return valueI[n.rot] < valueJ[n.rot]
}

sort.SliceStable(n.cluster, less)

if globalError != nil {
return node{}, node{}, false, globalError
}
Expand Down Expand Up @@ -243,6 +253,7 @@ func (n *node) Clusters() []Cluster {
func (n *node) string(offset int) string {
if n.cluster != nil {
result := "["

for _, rec := range n.cluster {
// result += fmt.Sprintf("%v ", rec.QuasiIdentifer()[n.rot])
recValue, _ := rec.QuasiIdentifer()
Expand Down
12 changes: 7 additions & 5 deletions pkg/sigo/kdtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestAddRecord(t *testing.T) {
assert.Equal(t, 0, clusters[0].Records()[0].Row()["x"])
}

// nolint: funlen
//nolint: funlen
func TestAddNRecords(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -112,7 +112,7 @@ func TestAddNRecords(t *testing.T) {
{k: 6, n: 1000, d: 6, s: 5},
}

// nolint: paralleltest
//nolint: paralleltest
for i, tc := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
t.Parallel()
Expand All @@ -127,9 +127,9 @@ func TestAddNRecords(t *testing.T) {
rows := []jsonline.Row{}

for i := 0; i < N; i++ {
// nolint: gosec
//nolint: gosec
x := rand.Intn(N)
// nolint: gosec
//nolint: gosec
y := rand.Intn(N)
for j := 0; j < D; j++ {
row := jsonline.NewRow()
Expand Down Expand Up @@ -186,7 +186,9 @@ func TestAddClusterInfos(t *testing.T) {
kdtree.Add(record)
}

kdtree.Build()
err := kdtree.Build()
assert.Nil(t, err)

clusters := kdtree.Clusters()

for _, cluster := range clusters {
Expand Down

0 comments on commit 79d86df

Please sign in to comment.