xref: /aosp_15_r20/external/bazelbuild-rules_go/tests/integration/googleapis/color_service.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package color_service
16
17import (
18	"context"
19	"sync"
20
21	cspb "github.com/bazelbuild/rules_go/tests/integration/googleapis/color_service_proto"
22	"google.golang.org/genproto/googleapis/rpc/code"
23	"google.golang.org/genproto/googleapis/rpc/status"
24	"google.golang.org/genproto/googleapis/type/color"
25)
26
27type colorServer struct {
28	colors sync.Map
29}
30
31func New() cspb.ColorServiceServer {
32	return &colorServer{}
33}
34
35func (s *colorServer) SetColor(ctx context.Context, r *cspb.SetColorRequest) (*cspb.SetColorResponse, error) {
36	_, loaded := s.colors.LoadOrStore(r.Name, r.Color)
37	if loaded {
38		return &cspb.SetColorResponse{Status: &status.Status{Code: int32(code.Code_ALREADY_EXISTS)}}, nil
39	}
40	return &cspb.SetColorResponse{}, nil
41}
42
43func (s *colorServer) GetColor(ctx context.Context, r *cspb.GetColorRequest) (*cspb.GetColorResponse, error) {
44	value, ok := s.colors.Load(r.Name)
45	if !ok {
46		return &cspb.GetColorResponse{Status: &status.Status{Code: int32(code.Code_NOT_FOUND)}}, nil
47	}
48	return &cspb.GetColorResponse{Color: value.(*color.Color)}, nil
49}
50