Skip to content

Commit

Permalink
Rework metadata helper with genny
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Dec 3, 2020
1 parent 199182c commit 0f92803
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 208 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ issues:
linters:
- dupl
- golint
- goheader
- path: .*\.template\.go
linters:
- dupl
- deadcode
- path: .*registry.*.go
linters:
- dupl
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/HdrHistogram/hdrhistogram-go v1.0.0 // indirect
github.com/RoaringBitmap/roaring v0.4.23
github.com/cheekybits/genny v1.0.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/edwarnicke/exechelper v1.0.2
github.com/edwarnicke/grpcfd v0.0.0-20200920223154-d5b6e1f19bd0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ github.com/bombsimon/wsl/v3 v3.1.0/go.mod h1:st10JtZYLE4D5sC7b8xV4zTKZwAQjCH/Hy2
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE=
github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down
74 changes: 0 additions & 74 deletions pkg/networkservice/utils/metadata/README.md

This file was deleted.

134 changes: 0 additions & 134 deletions pkg/networkservice/utils/metadata/helper.sh

This file was deleted.

69 changes: 69 additions & 0 deletions pkg/networkservice/utils/metadata/helper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Metadata helper

Metadata helper is a [genny](https://github.com/cheekybits/genny) template to generate a helper class for the metadata
usage. Temlate parameters are:
* prefix - prefix for the metadata type and constructor;
* valueType - stored type.
Generated helpers can both be exported (**P**refix) and unexported (**p**refix).

gen.go:
```go
package connect

//go:generate genny -in=../../utils/metadata/helper/meta_data.template.go -out=client_meta_data.gen.go -pkg=connect gen "prefix=client valueType=networkservice.NetworkServiceClient"
```
client_meta_data.gen.go:
```go
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny

package connect

import (
"context"
"sync"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type _ClientKeyType struct{}

type clientMetadataHelper struct {
m *sync.Map
}

func clientMetadata(ctx context.Context, isClient bool) *clientMetadataHelper {
return &clientMetadataHelper{
m: metadata.Map(ctx, isClient),
}
}

func (h *clientMetadataHelper) Store(value networkservice.NetworkServiceClient) {
h.m.Store(_ClientKeyType{}, value)
}

func (h *clientMetadataHelper) LoadOrStore(value networkservice.NetworkServiceClient) (networkservice.NetworkServiceClient, bool) {
raw, ok := h.m.LoadOrStore(_ClientKeyType{}, value)
return raw.(networkservice.NetworkServiceClient), ok
}

func (h *clientMetadataHelper) Load() (networkservice.NetworkServiceClient, bool) {
if raw, ok := h.m.Load(_ClientKeyType{}); ok {
return raw.(networkservice.NetworkServiceClient), true
}
return func() (v networkservice.NetworkServiceClient) { return }(), false
}

func (h *clientMetadataHelper) LoadAndDelete() (networkservice.NetworkServiceClient, bool) {
if raw, ok := h.m.LoadAndDelete(_ClientKeyType{}); ok {
return raw.(networkservice.NetworkServiceClient), true
}
return func() (v networkservice.NetworkServiceClient) { return }(), false
}

func (h *clientMetadataHelper) Delete() {
h.m.Delete(_ClientKeyType{})
}
```
18 changes: 18 additions & 0 deletions pkg/networkservice/utils/metadata/helper/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 helper provides template for generating metadata helpers.
package helper
19 changes: 19 additions & 0 deletions pkg/networkservice/utils/metadata/helper/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 helper

//go:generate genny -in=meta_data.template.go -out=int_meta_data.gen.go gen "prefix=Int valueType=int"
Loading

0 comments on commit 0f92803

Please sign in to comment.