-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
899 additions
and
130 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
23 changes: 23 additions & 0 deletions
23
cmd/layotto_multiple_api/helloworld/component/helloworld.go
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,23 @@ | ||
// | ||
// Copyright 2021 Layotto 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 component | ||
|
||
import ( | ||
"mosn.io/layotto/components/custom" | ||
) | ||
|
||
type HelloWorld interface { | ||
custom.Component | ||
SayHello(name string) (string, error) | ||
} |
38 changes: 38 additions & 0 deletions
38
cmd/layotto_multiple_api/helloworld/component/in_memory.go
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,38 @@ | ||
// | ||
// Copyright 2021 Layotto 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 component | ||
|
||
import ( | ||
"context" | ||
"mosn.io/layotto/components/custom" | ||
) | ||
|
||
type inMemoryHelloWorld struct { | ||
ctx context.Context | ||
config *custom.Config | ||
} | ||
|
||
func (i *inMemoryHelloWorld) Initialize(ctx context.Context, config custom.Config) error { | ||
i.ctx = ctx | ||
i.config = &config | ||
return nil | ||
} | ||
|
||
func (i *inMemoryHelloWorld) SayHello(name string) (string, error) { | ||
return "Hello " + name, nil | ||
} | ||
|
||
func NewInMemoryHelloWorld() custom.Component { | ||
return &inMemoryHelloWorld{} | ||
} |
38 changes: 38 additions & 0 deletions
38
cmd/layotto_multiple_api/helloworld/component/say_goodbye.go
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,38 @@ | ||
// | ||
// Copyright 2021 Layotto 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 component | ||
|
||
import ( | ||
"context" | ||
"mosn.io/layotto/components/custom" | ||
) | ||
|
||
type sayGoodbyeHelloWorld struct { | ||
ctx context.Context | ||
config *custom.Config | ||
} | ||
|
||
func (s *sayGoodbyeHelloWorld) Initialize(ctx context.Context, config custom.Config) error { | ||
s.ctx = ctx | ||
s.config = &config | ||
return nil | ||
} | ||
|
||
func (s *sayGoodbyeHelloWorld) SayHello(name string) (string, error) { | ||
return "Goodbye " + name, nil | ||
} | ||
|
||
func NewSayGoodbyeHelloWorld() custom.Component { | ||
return &sayGoodbyeHelloWorld{} | ||
} |
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,20 @@ | ||
// | ||
// Copyright 2021 Layotto 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 custom | ||
|
||
import "context" | ||
|
||
type Component interface { | ||
Initialize(ctx context.Context, config Config) error | ||
} |
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,19 @@ | ||
// | ||
// Copyright 2021 Layotto 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 custom | ||
|
||
type Config struct { | ||
Version string `json:"version"` | ||
Metadata map[string]string `json:"metadata"` | ||
} |
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,76 @@ | ||
// | ||
// Copyright 2021 Layotto 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 custom | ||
|
||
import ( | ||
"fmt" | ||
"mosn.io/layotto/components/pkg/info" | ||
) | ||
|
||
type Registry interface { | ||
Register(componentType string, factorys ...*ComponentFactory) | ||
Create(componentType, name string) (Component, error) | ||
} | ||
|
||
type ComponentFactory struct { | ||
Name string | ||
FactoryMethod func() Component | ||
} | ||
|
||
func NewComponentFactory(name string, f func() Component) *ComponentFactory { | ||
return &ComponentFactory{ | ||
Name: name, | ||
FactoryMethod: f, | ||
} | ||
} | ||
|
||
type componentRegistry struct { | ||
stores map[string]map[string]func() Component | ||
info *info.RuntimeInfo | ||
} | ||
|
||
func NewRegistry(info *info.RuntimeInfo) Registry { | ||
return &componentRegistry{ | ||
stores: make(map[string]map[string]func() Component), | ||
info: info, | ||
} | ||
} | ||
|
||
func (r *componentRegistry) Register(componentType string, fs ...*ComponentFactory) { | ||
if len(fs) == 0 { | ||
return | ||
} | ||
r.info.AddService(componentType) | ||
// lazy init | ||
if _, ok := r.stores[componentType]; !ok { | ||
r.stores[componentType] = make(map[string]func() Component) | ||
} | ||
// register FactoryMethod | ||
for _, f := range fs { | ||
r.stores[componentType][f.Name] = f.FactoryMethod | ||
r.info.RegisterComponent(componentType, f.Name) | ||
} | ||
} | ||
|
||
func (r *componentRegistry) Create(componentType, name string) (Component, error) { | ||
store, ok := r.stores[componentType] | ||
if !ok { | ||
return nil, fmt.Errorf("custom component type %s is not regsitered", componentType) | ||
} | ||
if f, ok := store[name]; ok { | ||
r.info.LoadComponent(componentType, name) | ||
return f(), nil | ||
} | ||
return nil, fmt.Errorf("custom component %s is not regsitered", name) | ||
} |
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,41 @@ | ||
/* | ||
* Copyright 2021 Layotto 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 custom | ||
|
||
import ( | ||
"mosn.io/layotto/components/pkg/info" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNewRegistry(t *testing.T) { | ||
r := NewRegistry(info.NewRuntimeInfo()) | ||
compType := "my_component" | ||
compName := "etcd" | ||
r.Register(compType, | ||
NewComponentFactory(compName, func() Component { | ||
return nil | ||
}), | ||
) | ||
_, err := r.Create(compType, compName) | ||
if err != nil { | ||
t.Fatalf("create mock store failed: %v", err) | ||
} | ||
if _, err := r.Create(compType, "not exists"); !strings.Contains(err.Error(), "not regsitered") { | ||
t.Fatalf("create mock store failed: %v", err) | ||
} | ||
} |
Oops, something went wrong.