Skip to content

Commit

Permalink
Improve client.NewOpenSergoClient API with functional option style
Browse files Browse the repository at this point in the history
Signed-off-by: Jiangnan Jia <[email protected]>
  • Loading branch information
jnan806 committed Dec 6, 2022
1 parent ec731f6 commit 507b174
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 15 deletions.
47 changes: 39 additions & 8 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,69 @@ import (
"github.com/opensergo/opensergo-go/pkg/transport/subscribe"
)

type OpensergoOptions struct {
}

type OpensergoOption func(*OpensergoOptions)

type ClientOptions struct {
connectRetryTimes uint
}

func (opts *ClientOptions) ConnectRetryTimes() uint {
return opts.connectRetryTimes
}

type ClientOption func(*ClientOptions)

func WithConnectRetryTimes(connectRetryTimes uint) ClientOption {
return func(opts *ClientOptions) {
opts.connectRetryTimes = connectRetryTimes
}
}

// SubscribeOptions represents the options of OpenSergo data subscription.
type SubscribeOptions struct {
Subscribers []subscribe.Subscriber
Attachments map[string]interface{}
subscribers []subscribe.Subscriber
attachments map[string]interface{}
}

func (opts *SubscribeOptions) Subscribers() []subscribe.Subscriber {
return opts.subscribers
}

func (opts *SubscribeOptions) Attachments() map[string]interface{} {
return opts.attachments
}

type SubscribeOption func(*SubscribeOptions)

// WithSubscriber provides a subscriber.
func WithSubscriber(subscriber subscribe.Subscriber) SubscribeOption {
return func(opts *SubscribeOptions) {
if opts.Subscribers == nil {
opts.Subscribers = make([]subscribe.Subscriber, 0)
if opts.subscribers == nil {
opts.subscribers = make([]subscribe.Subscriber, 0)
}
opts.Subscribers = append(opts.Subscribers, subscriber)
opts.subscribers = append(opts.subscribers, subscriber)
}
}

// WithAttachment provides an attachment (key-value pair).
func WithAttachment(key string, value interface{}) SubscribeOption {
return func(opts *SubscribeOptions) {
if opts.Attachments == nil {
opts.Attachments = make(map[string]interface{})
if opts.attachments == nil {
opts.attachments = make(map[string]interface{})
}
opts.Attachments[key] = value
opts.attachments[key] = value
}
}

// OpenSergoClient is the universal interface of OpenSergo client.
type OpenSergoClient interface {
// Start the client.
Start() error
// Close the client.
Close() error
// SubscribeConfig subscribes data for given subscribe target.
SubscribeConfig(key model.SubscribeKey, opts ...SubscribeOption) error
// UnsubscribeConfig unsubscribes data for given subscribe target.
Expand Down
16 changes: 16 additions & 0 deletions pkg/global/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022, OpenSergo Authors
//
// 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 global
package global
77 changes: 77 additions & 0 deletions pkg/global/global_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2022, OpenSergo Authors
//
// 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 global

import (
"github.com/opensergo/opensergo-go/pkg/api"
"sync"
)

type GlobalOptions struct {
opensergoOptions *api.OpensergoOptions
clientOptions *api.ClientOptions
}

var globalOptions *GlobalOptions
var globalOptionsOnce sync.Once

func GetGlobalOptions() *GlobalOptions {
globalOptionsOnce.Do(func() {
globalOptions = &GlobalOptions{}
// TODO set default OpensergoOptions
//globalOptions.SetOpensergoOpts()
globalOptions.setDefaultOpensergoOptions()
globalOptions.setDefaultClientOptions()
})
return globalOptions
}

func (g *GlobalOptions) OpensergoOptions() *api.OpensergoOptions {
return g.opensergoOptions
}

func (g *GlobalOptions) SetOpensergoOpts(opts ...api.OpensergoOption) *GlobalOptions {
options := &api.OpensergoOptions{}
if len(opts) > 0 {
for _, opt := range opts {
opt(options)
}
}
g.opensergoOptions = options
return g
}

func (g *GlobalOptions) ClientOptions() *api.ClientOptions {
return g.clientOptions
}

func (g *GlobalOptions) SetClientOptions(opts ...api.ClientOption) *GlobalOptions {
options := &api.ClientOptions{}
if len(opts) > 0 {
for _, opt := range opts {
opt(options)
}
}
g.clientOptions = options
return g
}

func (g *GlobalOptions) setDefaultOpensergoOptions() {

}

func (g *GlobalOptions) setDefaultClientOptions() {
g.SetClientOptions(api.WithConnectRetryTimes(3))
}
12 changes: 5 additions & 7 deletions samples/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
package main

import (
"github.com/opensergo/opensergo-go/pkg/common/logging"
"log"

"github.com/opensergo/opensergo-go/pkg/api"
"github.com/opensergo/opensergo-go/pkg/client"
"github.com/opensergo/opensergo-go/pkg/common/logging"
"github.com/opensergo/opensergo-go/pkg/configkind"
"github.com/opensergo/opensergo-go/pkg/model"
"github.com/opensergo/opensergo-go/samples"
Expand All @@ -32,25 +30,25 @@ func main() {
err := StartAndSubscribeOpenSergoConfig()
if err != nil {
// Handle error here.
log.Printf("Failed to StartAndSubscribeOpenSergoConfig: %s\n", err.Error())
logging.Error(err, "Failed to StartAndSubscribeOpenSergoConfig")
}

select {}
}

func StartAndSubscribeOpenSergoConfig() error {
// Set OpenSergo console logger (optional)
logging.NewConsoleLogger(logging.InfoLevel, logging.SeparateFormat, true)
logging.NewConsoleLogger(logging.DebugLevel, logging.JsonFormat, false)
// Set OpenSergo file logger (optional)
// logging.NewFileLogger("./opensergo-universal-transport-service.log", logging.InfoLevel, logging.JsonFormat, true)

// Create a OpenSergoClient.
openSergoClient, err := client.NewOpenSergoClient("127.0.0.1", 10246)
openSergoClient, err := client.NewOpenSergoClient("127.0.0.1", 10246, api.WithConnectRetryTimes(10))
if err != nil {
return err
}

// Start OpenSergoClient
// start OpenSergoClient
err = openSergoClient.Start()
if err != nil {
return err
Expand Down

0 comments on commit 507b174

Please sign in to comment.