12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package service
- import (
- "context"
- "errors"
- "strings"
- )
- // Service constants
- const (
- StrMaxSize = 1024
- )
- // Service errors
- var (
- ErrMaxSize = errors.New("maximum size of 1024 bytes exceeded")
- ErrStrValue = errors.New("error str value to Integer")
- )
- // Service Define a service interface
- type Service interface {
- // Concat a and b
- Concat(a, b string) (string, error)
- // a,b pkg string value
- Diff(ctx context.Context, a, b string) (string, error)
- // HealthCheck check service health status
- HealthCheck() bool
- }
- //ArithmeticService implement Service interface
- type StringService struct {
- }
- func (s StringService) Concat(a, b string) (string, error) {
- // test for length overflow
- if len(a)+len(b) > StrMaxSize {
- return "", ErrMaxSize
- }
- return a + b, nil
- }
- func (s StringService) Diff(ctx context.Context, a, b string) (string, error) {
- if len(a) < 1 || len(b) < 1 {
- return "", nil
- }
- res := ""
- if len(a) >= len(b) {
- for _, char := range b {
- if strings.Contains(a, string(char)) {
- res = res + string(char)
- }
- }
- } else {
- for _, char := range a {
- if strings.Contains(b, string(char)) {
- res = res + string(char)
- }
- }
- }
- return res, nil
- }
- // HealthCheck implement Service method
- // 用于检查服务的健康状态,这里仅仅返回true。
- func (s StringService) HealthCheck() bool {
- return true
- }
- // ServiceMiddleware define service middleware
- type ServiceMiddleware func(Service) Service
|