-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
157 lines (140 loc) · 4.54 KB
/
validate.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2023 Dolthub, Inc.
//
// 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 doltswarm
import (
"fmt"
remotesapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/remotesapi/v1alpha1"
"github.com/dolthub/dolt/go/store/hash"
"github.com/dolthub/dolt/go/store/types"
)
func validateRepoRequest(req repoRequest) error {
if req.GetRepoPath() == "" && req.GetRepoId() == nil {
return fmt.Errorf("expected repo_path or repo_id, got neither")
} else if req.GetRepoPath() == "" {
id := req.GetRepoId()
if id.Org == "" || id.RepoName == "" {
return fmt.Errorf("expected repo_id.org and repo_id.repo_name, missing at least one")
}
}
return nil
}
func validateChunkTableInfo(field string, ctis []*remotesapi.ChunkTableInfo) error {
for i, cti := range ctis {
if len(cti.Hash) != hash.ByteLen {
return fmt.Errorf("expected %s[%d].Hash to be %d bytes long, was %d", field, i, hash.ByteLen, len(cti.Hash))
}
if cti.ChunkCount == 0 {
return fmt.Errorf("expected %s[%d].ChunkCount to be non-zero", field, i)
}
}
return nil
}
func validateHash(field string, h []byte) error {
if len(h) != hash.ByteLen {
return fmt.Errorf("expected %s hash to be %d bytes long, was %d", field, hash.ByteLen, len(h))
}
return nil
}
func validateHashes(field string, hashes [][]byte) error {
for i, bs := range hashes {
if len(bs) != hash.ByteLen {
return fmt.Errorf("expected %s[%d] hash to be %d bytes long, was %d", field, i, hash.ByteLen, len(bs))
}
}
return nil
}
func ValidateGetRepoMetadataRequest(req *remotesapi.GetRepoMetadataRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
if req.ClientRepoFormat == nil {
return fmt.Errorf("expected non-nil client_repo_format")
}
if _, err := types.GetFormatForVersionString(req.ClientRepoFormat.NbfVersion); err != nil {
return fmt.Errorf("unsupported value for client_repo_format.nbf_version: %w", err)
}
if req.ClientRepoFormat.NbsVersion != "4" && req.ClientRepoFormat.NbsVersion != "5" {
return fmt.Errorf("unsupported value for client_repo_format.nbs_version: %v; expected \"4\" or \"5\"", req.ClientRepoFormat.NbsVersion)
}
return nil
}
func ValidateHasChunksRequest(req *remotesapi.HasChunksRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
return validateHashes("hashes", req.Hashes)
}
func ValidateGetDownloadLocsRequest(req *remotesapi.GetDownloadLocsRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
return validateHashes("chunk_hashes", req.ChunkHashes)
}
func ValidateGetUploadLocsRequest(req *remotesapi.GetUploadLocsRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
return validateHashes("table_file_hashes", req.TableFileHashes)
}
func ValidateRebaseRequest(req *remotesapi.RebaseRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
return nil
}
func ValidateRootRequest(req *remotesapi.RootRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
return nil
}
func ValidateCommitRequest(req *remotesapi.CommitRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
if err := validateHash("current", req.Current); err != nil {
return err
}
if err := validateHash("last", req.Last); err != nil {
return err
}
if err := validateChunkTableInfo("chunk_table_info", req.ChunkTableInfo); err != nil {
return err
}
return nil
}
func ValidateListTableFilesRequest(req *remotesapi.ListTableFilesRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
return nil
}
func ValidateRefreshTableFileUrlRequest(req *remotesapi.RefreshTableFileUrlRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
if req.FileId == "" {
return fmt.Errorf("expected file_id")
}
return nil
}
func ValidateAddTableFilesRequest(req *remotesapi.AddTableFilesRequest) error {
if err := validateRepoRequest(req); err != nil {
return err
}
if err := validateChunkTableInfo("chunk_table_info", req.ChunkTableInfo); err != nil {
return err
}
return nil
}