forked from google/trillian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.go
142 lines (132 loc) · 4.71 KB
/
conversions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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 quotaapi
import (
"fmt"
"github.com/google/trillian/quota/etcd/quotapb"
"github.com/google/trillian/quota/etcd/storagepb"
"google.golang.org/genproto/protobuf/field_mask"
)
const (
statePath = "state"
maxTokensPath = "max_tokens"
sequencingBasedPath = "sequencing_based"
timeBasedPath = "time_based"
)
var (
commonMask = &field_mask.FieldMask{Paths: []string{statePath, maxTokensPath}}
sequencingBasedMask = &field_mask.FieldMask{Paths: append(commonMask.Paths, sequencingBasedPath)}
timeBasedMask = &field_mask.FieldMask{Paths: append(commonMask.Paths, timeBasedPath)}
errBothReplenishmentStrategies = fmt.Errorf("both %q and %q paths specified, please pick a single replenishment strategy", sequencingBasedPath, timeBasedPath)
)
// validateMask returns nil if mask is a valid Config mask, error otherwise.
// Paths must match the proto name of the fields (e.g., "time_based", not "TimeBased").
// If fields belong to a oneof (such as "sequencing_based" and "time_based"), then only one field of
// the oneof may be specified.
func validateMask(mask *field_mask.FieldMask) error {
sequencingBasedFound := false
timeBasedFound := false
for _, path := range mask.Paths {
switch path {
case statePath, maxTokensPath:
// OK
case sequencingBasedPath:
if timeBasedFound {
return errBothReplenishmentStrategies
}
sequencingBasedFound = true
case timeBasedPath:
if sequencingBasedFound {
return errBothReplenishmentStrategies
}
timeBasedFound = true
default:
return fmt.Errorf("invalid field path for Config: %q", path)
}
}
return nil
}
// applyMask copies the fields specified by mask from src to dest. The mask must be first validated
// by validateMask(), as unknown paths are simply ignored by applyMask.
// Paths must match the proto name of the fields (e.g., "time_based", not "TimeBased").
func applyMask(src *quotapb.Config, dest *storagepb.Config, mask *field_mask.FieldMask) {
for _, path := range mask.Paths {
switch path {
case statePath:
dest.State = storagepb.Config_State(storagepb.Config_State_value[src.State.String()])
case maxTokensPath:
dest.MaxTokens = src.MaxTokens
case sequencingBasedPath:
if src.GetSequencingBased() == nil {
dest.ReplenishmentStrategy = nil
} else {
dest.ReplenishmentStrategy = &storagepb.Config_SequencingBased{
SequencingBased: &storagepb.SequencingBasedStrategy{},
}
}
case timeBasedPath:
if tb := src.GetTimeBased(); tb == nil {
dest.ReplenishmentStrategy = nil
} else {
dest.ReplenishmentStrategy = &storagepb.Config_TimeBased{
TimeBased: &storagepb.TimeBasedStrategy{
TokensToReplenish: tb.GetTokensToReplenish(),
ReplenishIntervalSeconds: tb.GetReplenishIntervalSeconds(),
},
}
}
}
}
}
// convertToAPI returns the API representation of a storagepb.Config proto.
func convertToAPI(src *storagepb.Config) *quotapb.Config {
dest := "apb.Config{
Name: src.Name,
State: quotapb.Config_State(quotapb.Config_State_value[src.State.String()]),
MaxTokens: src.MaxTokens,
}
sb := src.GetSequencingBased()
tb := src.GetTimeBased()
switch {
case sb != nil:
dest.ReplenishmentStrategy = "apb.Config_SequencingBased{
SequencingBased: "apb.SequencingBasedStrategy{},
}
case tb != nil:
dest.ReplenishmentStrategy = "apb.Config_TimeBased{
TimeBased: "apb.TimeBasedStrategy{
TokensToReplenish: tb.TokensToReplenish,
ReplenishIntervalSeconds: tb.ReplenishIntervalSeconds,
},
}
}
return dest
}
// convertToStorage returns the storage representation of a quotapb.Config proto.
func convertToStorage(src *quotapb.Config) *storagepb.Config {
// Instead of potentially duplicating logic, let's take advantage of applyMask by picking a
// pre-made mask that contains all fields we care about and apply it to a new proto.
var mask *field_mask.FieldMask
switch {
case src.GetSequencingBased() != nil:
mask = sequencingBasedMask
case src.GetTimeBased() != nil:
mask = timeBasedMask
default:
mask = commonMask
}
dest := &storagepb.Config{Name: src.Name}
applyMask(src, dest, mask)
return dest
}