1 /* Copyright 2018 The TensorFlow 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 ==============================================================================*/
15
16 #include "tensorflow/core/util/proto/proto_utils.h"
17
18 #include "absl/strings/string_view.h"
19 #include "absl/strings/substitute.h"
20 #include "tensorflow/core/framework/types.h"
21 #include "tensorflow/core/platform/logging.h"
22 #include "tensorflow/core/platform/protobuf.h"
23
24 namespace tensorflow {
25 namespace proto_utils {
26
27 using tensorflow::protobuf::FieldDescriptor;
28 using tensorflow::protobuf::internal::WireFormatLite;
29
IsCompatibleType(FieldDescriptor::Type field_type,DataType dtype)30 bool IsCompatibleType(FieldDescriptor::Type field_type, DataType dtype) {
31 switch (field_type) {
32 case WireFormatLite::TYPE_DOUBLE:
33 return dtype == tensorflow::DT_DOUBLE;
34 case WireFormatLite::TYPE_FLOAT:
35 return dtype == tensorflow::DT_FLOAT || dtype == tensorflow::DT_DOUBLE;
36 case WireFormatLite::TYPE_INT64:
37 return dtype == tensorflow::DT_INT64;
38 case WireFormatLite::TYPE_UINT64:
39 return dtype == tensorflow::DT_UINT64;
40 case WireFormatLite::TYPE_INT32:
41 return dtype == tensorflow::DT_INT32 || dtype == tensorflow::DT_INT64;
42 case WireFormatLite::TYPE_FIXED64:
43 return dtype == tensorflow::DT_UINT64;
44 case WireFormatLite::TYPE_FIXED32:
45 return dtype == tensorflow::DT_UINT32 || dtype == tensorflow::DT_UINT64;
46 case WireFormatLite::TYPE_BOOL:
47 return dtype == tensorflow::DT_BOOL;
48 case WireFormatLite::TYPE_STRING:
49 return dtype == tensorflow::DT_STRING;
50 case WireFormatLite::TYPE_GROUP:
51 return dtype == tensorflow::DT_STRING;
52 case WireFormatLite::TYPE_MESSAGE:
53 return dtype == tensorflow::DT_STRING;
54 case WireFormatLite::TYPE_BYTES:
55 return dtype == tensorflow::DT_STRING;
56 case WireFormatLite::TYPE_UINT32:
57 return dtype == tensorflow::DT_UINT32 || dtype == tensorflow::DT_UINT64;
58 case WireFormatLite::TYPE_ENUM:
59 return dtype == tensorflow::DT_INT32;
60 case WireFormatLite::TYPE_SFIXED32:
61 return dtype == tensorflow::DT_INT32 || dtype == tensorflow::DT_INT64;
62 case WireFormatLite::TYPE_SFIXED64:
63 return dtype == tensorflow::DT_INT64;
64 case WireFormatLite::TYPE_SINT32:
65 return dtype == tensorflow::DT_INT32 || dtype == tensorflow::DT_INT64;
66 case WireFormatLite::TYPE_SINT64:
67 return dtype == tensorflow::DT_INT64;
68 // default: intentionally omitted in order to enable static checking.
69 }
70 }
71
ParseTextFormatFromString(absl::string_view input,protobuf::Message * output)72 Status ParseTextFormatFromString(absl::string_view input,
73 protobuf::Message* output) {
74 DCHECK(output != nullptr) << "output must be non NULL";
75 // When checks are disabled, instead log the error and return an error status.
76 if (output == nullptr) {
77 LOG(ERROR) << "output must be non NULL";
78 return Status(error::INVALID_ARGUMENT, "output must be non NULL");
79 }
80 string err;
81 StringErrorCollector err_collector(&err, /*one-indexing=*/true);
82 protobuf::TextFormat::Parser parser;
83 parser.RecordErrorsTo(&err_collector);
84 if (!parser.ParseFromString(string(input), output)) {
85 return Status(error::INVALID_ARGUMENT, err);
86 }
87 return Status::OK();
88 }
89
StringErrorCollector(string * error_text)90 StringErrorCollector::StringErrorCollector(string* error_text)
91 : StringErrorCollector(error_text, false) {}
92
StringErrorCollector(string * error_text,bool one_indexing)93 StringErrorCollector::StringErrorCollector(string* error_text,
94 bool one_indexing)
95 : error_text_(error_text), index_offset_(one_indexing ? 1 : 0) {
96 DCHECK(error_text_ != nullptr) << "error_text must be non NULL";
97 // When checks are disabled, just log and then ignore added errors/warnings.
98 if (error_text_ == nullptr) {
99 LOG(ERROR) << "error_text must be non NULL";
100 }
101 }
102
AddError(int line,int column,const string & message)103 void StringErrorCollector::AddError(int line, int column,
104 const string& message) {
105 if (error_text_ != nullptr) {
106 absl::SubstituteAndAppend(error_text_, "$0($1): $2\n", line + index_offset_,
107 column + index_offset_, message);
108 }
109 }
110
AddWarning(int line,int column,const string & message)111 void StringErrorCollector::AddWarning(int line, int column,
112 const string& message) {
113 AddError(line, column, message);
114 }
115
116 } // namespace proto_utils
117 } // namespace tensorflow
118