Skip to content

add proto_util.h for some protobuf utility functions #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions encoding/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,26 @@ proto_library(
cc_library(
name = 'proto_json_format',
srcs = 'proto_json_format.cpp',
deps = ['//thirdparty/glog:glog',
'//thirdparty/jsoncpp:jsoncpp',
'//thirdparty/protobuf:protobuf',
],
deps = [
'//thirdparty/glog:glog',
'//thirdparty/jsoncpp:jsoncpp',
'//thirdparty/protobuf:protobuf',
]
)

cc_test(
name = 'proto_json_format_unittest',
srcs = 'proto_json_format_unittest.cpp',
deps = [':proto_json_format',
':unittest_proto',
'//toft/storage/file:file',
],
testdata = ['debug_string.txt',
'json_fast_string.txt',
'json_styled_string.txt',
]
deps = [
':proto_json_format',
':unittest_proto',
'//toft/storage/file:file',
],
testdata = [
'debug_string.txt',
'json_fast_string.txt',
'json_styled_string.txt',
]
)

cc_test(
Expand Down
9 changes: 5 additions & 4 deletions encoding/proto_json_format_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
#include <stdlib.h>
#include <limits>

#include "toft/encoding/proto_util.h"
#include "toft/encoding/unittest.pb.h"
#include "toft/storage/file/file.h"

#include "thirdparty/glog/logging.h"
#include "thirdparty/google/protobuf/text_format.h"
#include "thirdparty/gtest/gtest.h"
#include "thirdparty/jsoncpp/json.h"
#include "toft/encoding/unittest.pb.h"
#include "toft/storage/file/file.h"

namespace toft {

Expand Down Expand Up @@ -54,7 +55,7 @@ void TestJsonString(const std::string& json_file) {
std::string content;
std::string path = "debug_string.txt";
toft::File::ReadAll(path, &content);
google::protobuf::TextFormat::ParseFromString(content, &expected_pb);
ProtoUtil::ParseFromASCIIString(content, &expected_pb);
}
EXPECT_EQ(pb.SerializeAsString(), expected_pb.SerializeAsString());
}
Expand Down
28 changes: 28 additions & 0 deletions encoding/proto_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2013, The Toft Authors.
// All rights reserved.
//
// Author: Ye Shunping <yeshunping@gmail.com>

#ifndef TOFT_ENCODING_PROTO_UTIL_H_
#define TOFT_ENCODING_PROTO_UTIL_H_

#include <string>

#include "toft/base/static_class.h"

#include "thirdparty/protobuf/text_format.h"

namespace toft {

struct ProtoUtil {
TOFT_DECLARE_STATIC_CLASS(ProtoUtil);

public:
static bool ParseFromASCIIString(const std::string& debug_str, google::protobuf::Message* out) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要这个函数,有现成的:
static bool TextFormat::ParseFromString(const string & input, Message * output)

google::protobuf::TextFormat::Parser parser;
return parser.ParseFromString(debug_str, out);
}
};

} // namespace toft
#endif // TOFT_ENCODING_PROTO_UTIL_H_