Skip to content
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

FLGO-45 Fix json fields #46

Merged
merged 3 commits into from
Jul 1, 2019
Merged
Changes from 2 commits
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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Reamde
На данный момент репозиторий содержит код API для Фантлаба, написанный на Go. В состоянии активной разработки.

# Порядок работы
## Порядок работы
- Завести issue с описанием задачи/бага
- Создать у себя ветку FLGO-{#issue}
- Написать код
@@ -11,10 +11,9 @@
- ...
- PROFIT

# Protobuf
## Protobuf

```
cd protobuf
protoc --go_out=generated schema/*.proto
```console
$ cd protobuf/
$ protoc --go_out=generated schema/*.proto
```
3 changes: 2 additions & 1 deletion modules/authapi/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package authapi

import (
"net/http"

"fantlab/protobuf/generated/fantlab/pb"
"fantlab/shared"
"fantlab/utils"
"net/http"

"github.com/gin-gonic/gin"
"github.com/segmentio/ksuid"
18 changes: 9 additions & 9 deletions modules/blogsapi/datacontroller.go
Original file line number Diff line number Diff line change
@@ -11,12 +11,12 @@ func getCommunities(dbCommunities []dbCommunity) *pb.Blog_CommunitiesResponse {

for _, dbCommunity := range dbCommunities {
community := &pb.Blog_Community{
Id: dbCommunity.BlogId,
Title: dbCommunity.Name,
CommunityDescription: dbCommunity.Description,
Id: dbCommunity.BlogId,
Title: dbCommunity.Name,
Description: dbCommunity.Description,
Stats: &pb.Blog_Community_Stats{
ArticlesCount: dbCommunity.TopicsCount,
SubscribersCount: dbCommunity.SubscriberCount,
ArticleCount: dbCommunity.TopicsCount,
SubscriberCount: dbCommunity.SubscriberCount,
},
LastArticle: &pb.Blog_LastArticle{
Id: dbCommunity.LastTopicId,
@@ -56,8 +56,8 @@ func getBlogs(dbBlogs []dbBlog) *pb.Blog_BlogsResponse {
},
IsClosed: dbBlog.IsClose,
Stats: &pb.Blog_Blog_Stats{
ArticlesCount: dbBlog.TopicsCount,
SubscribersCount: dbBlog.SubscriberCount,
ArticleCount: dbBlog.TopicsCount,
SubscriberCount: dbBlog.SubscriberCount,
},
LastArticle: &pb.Blog_LastArticle{
Id: dbBlog.LastTopicId,
@@ -81,9 +81,9 @@ func getBlogArticles(dbBlogTopics []dbBlogTopic, urlFormatter utils.UrlFormatter
for _, dbBlogTopic := range dbBlogTopics {
var gender pb.Gender
if dbBlogTopic.Sex == 0 {
gender = pb.Gender_FEMALE
gender = pb.Gender_female
} else {
gender = pb.Gender_MALE
gender = pb.Gender_male
}

avatar := urlFormatter.GetAvatarUrl(dbBlogTopic.UserId, uint32(dbBlogTopic.PhotoNumber))
20 changes: 10 additions & 10 deletions modules/forumapi/datacontroller.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ func getForumBlocks(dbForums []dbForum, dbModerators map[uint32][]dbModerator) *
for _, dbForum := range dbForums {
if dbForum.ForumBlockID != currentForumBlockID {
forumBlock := pb.Forum_ForumBlock{
Id: dbForum.ForumBlockID,
XXX_Id: dbForum.ForumBlockID,
Copy link
Collaborator

Choose a reason for hiding this comment

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

типа эта айдишка не должна светиться?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ага. Параметр технический, раньше вырезать его из json-а было очень просто, теперь приходится костыли городить.

Copy link
Collaborator

Choose a reason for hiding this comment

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

просто, если он не секретный, то может хрен с ним, пускай будет?)

Copy link
Contributor Author

@kenrube kenrube Jun 29, 2019

Choose a reason for hiding this comment

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

Ок. Однако, надобность в аналоге json:"-" может возникнуть и в другом месте, мы просто скрываем проблему.
golang/protobuf#52 - Open

Copy link
Collaborator

Choose a reason for hiding this comment

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

протобаф планируется использовать только для отдачи клиентам (пока что - дальше хз что будет) и при необходимости можно создавать внутренние модели для конвертации и уже из них создавать протобуфные - например если будет сложный алгоритм преобразования дбшных моделей, иногда эффективней потратить больше памяти для промежуточных моделек. В таком случае не понадобится модификация генератора протомоделек) Но вообще не думаю что будет что-то настолько сложное что понадобится извращаться. ИМХО :)

Title: dbForum.ForumBlockName,
Forums: []*pb.Forum_Forum{},
}
@@ -24,7 +24,7 @@ func getForumBlocks(dbForums []dbForum, dbModerators map[uint32][]dbModerator) *

for _, dbForum := range dbForums {
for index := range forumBlocks {
if dbForum.ForumBlockID == forumBlocks[index].GetId() {
if dbForum.ForumBlockID == forumBlocks[index].GetXXX_Id() {
var moderators []*pb.Forum_UserLink

for _, dbModerator := range dbModerators[dbForum.ForumID] {
@@ -36,10 +36,10 @@ func getForumBlocks(dbForums []dbForum, dbModerators map[uint32][]dbModerator) *
}

forum := pb.Forum_Forum{
Id: dbForum.ForumID,
Title: dbForum.Name,
ForumDescription: dbForum.Description,
Moderators: moderators,
Id: dbForum.ForumID,
Title: dbForum.Name,
Description: dbForum.Description,
Copy link
Collaborator

Choose a reason for hiding this comment

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

а мы можем description по другому назвать? :) с type и description есть проблемки в свифте

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Придумаю

Moderators: moderators,
Stats: &pb.Forum_Forum_Stats{
TopicCount: dbForum.TopicCount,
MessageCount: dbForum.MessageCount,
@@ -77,9 +77,9 @@ func getForumTopics(dbTopics []dbForumTopic) *pb.Forum_ForumTopicsResponse {
for _, dbTopic := range dbTopics {
var topicType pb.Forum_Topic_Type
if dbTopic.TopicTypeID == 2 {
topicType = pb.Forum_Topic_POLL
topicType = pb.Forum_Topic_poll
} else {
topicType = pb.Forum_Topic_TOPIC
topicType = pb.Forum_Topic_topic
}

topic := &pb.Forum_Topic{
@@ -130,9 +130,9 @@ func getTopicMessages(dbMessages []dbForumMessage, urlFormatter utils.UrlFormatt

var gender pb.Gender
if dbMessage.Sex == 0 {
gender = pb.Gender_FEMALE
gender = pb.Gender_female
} else {
gender = pb.Gender_MALE
gender = pb.Gender_male
}

avatar := urlFormatter.GetAvatarUrl(dbMessage.UserID, dbMessage.PhotoNumber)
88 changes: 88 additions & 0 deletions protobuf/generated/fantlab/pb/auth_models.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 58 additions & 59 deletions protobuf/generated/fantlab/pb/blog_models.pb.go
94 changes: 21 additions & 73 deletions protobuf/generated/fantlab/pb/common_models.pb.go
141 changes: 71 additions & 70 deletions protobuf/generated/fantlab/pb/forum_models.pb.go
10 changes: 10 additions & 0 deletions protobuf/schema/auth_models.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package fantlab;

option go_package = "fantlab/pb";

message UserSessionResponse {
uint32 user_id = 1;
string session_token = 2;
}
10 changes: 5 additions & 5 deletions protobuf/schema/blog_models.proto
Original file line number Diff line number Diff line change
@@ -30,13 +30,13 @@ message Blog {

message Community {
message Stats {
uint32 articles_count = 1;
uint32 subscribers_count = 2;
uint32 article_count = 1;
uint32 subscriber_count = 2;
}

uint32 id = 1;
string title = 2;
string community_description = 3;
string description = 3;
Stats stats = 4;
LastArticle last_article = 5;
}
@@ -58,8 +58,8 @@ message Blog {

message Blog {
message Stats {
uint32 articles_count = 1;
uint32 subscribers_count = 2;
uint32 article_count = 1;
uint32 subscriber_count = 2;
}

uint32 id = 1;
11 changes: 3 additions & 8 deletions protobuf/schema/common_models.proto
Original file line number Diff line number Diff line change
@@ -5,17 +5,12 @@ package fantlab;
option go_package = "fantlab/pb";

enum Gender {
UNKNOWN = 0;
MALE = 1;
FEMALE = 2;
unknown = 0;
male = 1;
female = 2;
}

message ErrorResponse {
int32 error_code = 1;
string message = 2;
}

message UserSessionResponse {
uint32 user_id = 1;
string session_token = 2;
}
11 changes: 6 additions & 5 deletions protobuf/schema/forum_models.proto
Original file line number Diff line number Diff line change
@@ -50,9 +50,9 @@ message Forum {

message Topic {
enum Type {
UNKNOWN = 0;
TOPIC = 1;
POLL = 2;
unknown = 0;
topic = 1;
poll = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

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

https://developers.google.com/protocol-buffers/docs/style - там раздел про енумы

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Эт все здорово, но как сделать, чтоб в json их строковое представление было в нижнем регистре?

Copy link
Collaborator

Choose a reason for hiding this comment

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

это настолько принципиально? можно указать в маршалере параметр EnumAsInt например

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Это плохо, когда инструмент навязывает собственные решения и не дает альтернатив.
golang/protobuf#555 - Open, как обычно

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Я специально завел строковые значения, чтоб не городить массу непонятных интов для клиентов :) Возвращаться обратно к тому же - не, спасибо

Copy link
Collaborator

Choose a reason for hiding this comment

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

Это плохо, когда инструмент навязывает собственные решения и не дает альтернатив.
golang/protobuf#555 - Open, как обычно

в таком случае поддержку кастомных имен надо добавлять во всех языках которые поддерживают протобуф) в общем такое себе - не вижу большой проблемы в том что кейсы енума будут в апперкейсе) ЗАТО_ИХ_СРАЗУ_ЗАМЕТНО!

}

message Stats {
@@ -78,14 +78,15 @@ message Forum {

uint32 id = 1;
string title = 2;
string forum_description = 3;
string description = 3;
repeated UserLink moderators = 4;
Stats stats = 5;
LastMessage last_message = 6;
}

message ForumBlock {
uint32 id = 1;
// hack to omit field (https://github.com/gogo/protobuf/blob/dadb625850898f31a8e40e83492f4a7132e520a2/jsonpb/jsonpb.go#L286)
uint32 XXX__id = 1;
string title = 2;
repeated Forum forums = 3;
}
2 changes: 1 addition & 1 deletion utils/http.go
Original file line number Diff line number Diff line change
@@ -33,6 +33,6 @@ func ShowProto(ctx *gin.Context, code int, pb proto.Message) {
}

if err := marshaller.Marshal(ctx.Writer, pb); err != nil {
ShowError(ctx, http.StatusInternalServerError, err.Error())
ctx.AbortWithStatus(http.StatusInternalServerError)
}
}