Skip to content

Commit

Permalink
Add metadata helper generator
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 772dcd7 commit 7fd7356
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkg/networkservice/utils/metadata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Metadata helper

Metadata helper generation script has 2 input parameters:
* output - output file name
* type - name<isClient,type>:
* name - prefix for metadata type, constructor
* isClient - metadata.Map(ctx, isClient)
* type - stored type

gen.go:
```go
package connect

//go:generate bash ../../utils/metadata/helper.sh -output metadata_helper.gen.go -type nsClient<false,*github.com/networkservicemesh/api/pkg/api/networkservice.NetworkServiceClient>
```
metadata_helper.gen.go:
```go
// Code generated by "-output metadata_helper.gen.go -type nsClient<false,*github.com/networkservicemesh/api/pkg/api/networkservice.NetworkServiceClient>"; DO NOT EDIT.
package connect

import (
"context"
"sync"

"github.com/networkservicemesh/api/pkg/api/networkservice"

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

const (
_networkservice_NetworkServiceClientKey _networkservice_NetworkServiceClientKeyType = "_networkservice_NetworkServiceClient"
)

type _networkservice_NetworkServiceClientKeyType string

var _nil__networkservice_NetworkServiceClient = func() (val *networkservice.NetworkServiceClient) { return }()

type nsClientMetadataHelper struct {
m *sync.Map
}

func nsClientMetaData(ctx context.Context) *nsClientMetadataHelper {
return &nsClientMetadataHelper{
m: metadata.Map(ctx, false),
}
}

func (h *nsClientMetadataHelper) store(value *networkservice.NetworkServiceClient) {
h.m.Store(_networkservice_NetworkServiceClientKey, value)
}

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

func (h *nsClientMetadataHelper) load() (*networkservice.NetworkServiceClient, bool) {
if raw, ok := h.m.Load(_networkservice_NetworkServiceClientKey); ok {
return raw.(*networkservice.NetworkServiceClient), true
}
return _nil__networkservice_NetworkServiceClient, false
}

func (h *nsClientMetadataHelper) loadAndDelete() (*networkservice.NetworkServiceClient, bool) {
if raw, ok := h.m.LoadAndDelete(_networkservice_NetworkServiceClientKey); ok {
return raw.(*networkservice.NetworkServiceClient), true
}
return _nil__networkservice_NetworkServiceClient, false
}

func (h *nsClientMetadataHelper) delete() {
h.m.Delete(_networkservice_NetworkServiceClientKey)
}
```
134 changes: 134 additions & 0 deletions pkg/networkservice/utils/metadata/helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/bin/bash +x
# shellcheck disable=SC2002,SC2181

# 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.

LINE="$*"

###########################
## Parse arguments
###########################

OUTPUT=
TYPE=
while [[ $# -gt 0 ]]; do
if [[ "$1" == "-output" ]]; then
OUTPUT="$2"
shift 2
elif [[ "$1" == "-type" ]]; then
TYPE="$2"
shift 2
else
echo "usage: $0 -output file.gen.go -type name<isClient,type>" && exit 1
fi
done

if [[ "${OUTPUT}" == "" ]] || [[ "${TYPE}" == "" ]]; then
echo "usage: $0 -output file.gen.go -type name<isClient,type>" && exit 1
fi

###########################
## Parse package
###########################

ls gen.go >/dev/null 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "no gen.go file found" && exit 2
fi
package="$(cat gen.go | grep -e "package .*" | sed -E "s/package (.*)/\1/g")"

###########################
## Prepare types
###########################

name=$(echo "${TYPE}" | sed -E "s/(.*)<.*>/\1/g")
is_client=$(echo "${TYPE}" | sed -E "s/.*<(.*),.*>/\1/g")
type=$(echo "${TYPE}" | sed -E "s/.*<.*,(.*)>/\1/g")

type_package=$(echo "${type}" | sed -E "s/\**(.*\/.*)\..*/\1/g")
type_name=$(echo "${type}" | sed -E "s/(\**).*\/(.*\..*)/\1\2/g")
if [[ "${type_name}" != "${type_package}" ]]; then
type="${type_name}"
fi

type_formatted=$(echo "${type}" | sed -E "s/[\*\.\/]/_/g")

###########################
## Code
###########################

echo \
"// Code generated by \"${LINE}\"; DO NOT EDIT.
package ${package}
import (
\"context\"
\"sync\"" >"${OUTPUT}"
if [[ "${type_name}" != "${type_package}" ]]; then
echo \
"
\"${type_package}\"" >>"${OUTPUT}"
fi
echo \
"
\"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata\"
)
const (
${type_formatted}Key ${type_formatted}KeyType = \"${type_formatted}\"
)
type ${type_formatted}KeyType string
var _nil_${type_formatted} = func() (val ${type}) { return }()
type ${name}MetadataHelper struct {
m *sync.Map
}
func ${name}MetaData(ctx context.Context) *${name}MetadataHelper {
return &${name}MetadataHelper{
m: metadata.Map(ctx, ${is_client}),
}
}
func (h *${name}MetadataHelper) store(value ${type}) {
h.m.Store(${type_formatted}Key, value)
}
func (h *${name}MetadataHelper) loadOrStore(value ${type}) (${type}, bool) {
raw, ok := h.m.LoadOrStore(${type_formatted}Key, value)
return raw.(${type}), ok
}
func (h *${name}MetadataHelper) load() (${type}, bool) {
if raw, ok := h.m.Load(${type_formatted}Key); ok {
return raw.(${type}), true
}
return _nil_${type_formatted}, false
}
func (h *${name}MetadataHelper) loadAndDelete() (${type}, bool) {
if raw, ok := h.m.LoadAndDelete(${type_formatted}Key); ok {
return raw.(${type}), true
}
return _nil_${type_formatted}, false
}
func (h *${name}MetadataHelper) delete() {
h.m.Delete(${type_formatted}Key)
}" >>"${OUTPUT}"

0 comments on commit 7fd7356

Please sign in to comment.