Skip to content

Instantly share code, notes, and snippets.

@ejoubaud
Last active September 22, 2020 11:16
Show Gist options
  • Save ejoubaud/c08205b399634119b67d328570cb302e to your computer and use it in GitHub Desktop.
Save ejoubaud/c08205b399634119b67d328570cb302e to your computer and use it in GitHub Desktop.
GRPC Composition service: Go vs Rb (verbosity)
func (api *AppointmentServiceV1Alpha1) ListAdvisers(ctx context.Context, req *v1alpha1.ListAdvisersRequest) (*v1alpha1.ListAdvisersResponse, error) {
ctx = forwardGRPCMetadata(ctx)
grpcClient, err := api.Schoolbackoffice.Service.KnownServiceGRPCClient("appointment")
if err != nil {
return nil, grpc.InternalError("missing appointment grpc client: %v", err)
}
appointmentClient := jobteaser_appointment_v1alpha1.NewAdviserServiceClient(grpcClient.Conn)
listAdvisersResponse, err := appointmentClient.ListAdvisers(ctx, &jobteaser_appointment_v1alpha1.ListAdvisersRequest{
PageSize: req.GetPageSize(),
PageToken: req.GetPageToken(),
})
totalCountAdvisersRequest := jobteaser_appointment_v1alpha1.CountAdvisersRequest{}
totalCountAdvisersResponse, err := appointmentClient.CountAdvisers(ctx, &totalCountAdvisersRequest)
if err != nil {
return nil, err
}
enabledTotalCountAdvisersRequest := jobteaser_appointment_v1alpha1.CountAdvisersRequest{WithoutDisabled: true}
enabledTotalCountAdvisersResponse, err := appointmentClient.CountAdvisers(ctx, &enabledTotalCountAdvisersRequest)
if err != nil {
return nil, err
}
if err != nil {
return nil, grpc.InternalError("cannot list advisers: %v", err)
}
// collect user ids
set := make(map[string]struct{})
var userIds []string
for _, adviser := range listAdvisersResponse.GetAdvisers() {
if _, ok := set[adviser.GetUserId()]; !ok {
set[adviser.GetUserId()] = struct{}{}
userIds = append(userIds, adviser.GetUserId())
}
for _, id := range adviser.GetNotifyAdminIds() {
if _, ok := set[id]; !ok {
set[id] = struct{}{}
userIds = append(userIds, id)
}
}
}
grpcClient, err = api.Schoolbackoffice.Service.KnownServiceGRPCClient("user")
if err != nil {
return nil, grpc.InternalError("cannot get user grpc client connection")
}
userClient := jobteaser_user_v2alpha1.NewUserServiceClient(grpcClient.Conn)
var batchGetUsersResponse *jobteaser_user_v2alpha1.BatchGetUsersResponse
if len(userIds) > 0 {
batchGetUsersRequest := jobteaser_user_v2alpha1.BatchGetUsersRequest{Ids: userIds}
batchGetUsersResponse, err = userClient.BatchGetUsers(ctx, &batchGetUsersRequest)
if err != nil {
return nil, err
}
}
table := make(map[string]*jobteaser_user_v2alpha1.User)
for _, user := range batchGetUsersResponse.GetUsers() {
table[user.GetId()] = user
}
lookupUsers := func(userIds []string, table map[string]*jobteaser_user_v2alpha1.User) []*jobteaser_user_v2alpha1.User {
var data []*jobteaser_user_v2alpha1.User
for _, userId := range userIds {
user := table[userId]
if user != nil {
data = append(data, user)
}
}
return data
}
lookupUser := func(userId string, table map[string]*jobteaser_user_v2alpha1.User) *jobteaser_user_v2alpha1.User {
users := lookupUsers([]string{userId}, table)
if len(users) == 1 {
return users[0]
}
return nil
}
var advisers []*v1alpha1.Adviser
for _, adviser := range listAdvisersResponse.GetAdvisers() {
advisers = append(advisers, &v1alpha1.Adviser{
Adviser: adviser,
User: lookupUser(adviser.GetUserId(), table),
NotifyAdmins: lookupUsers(adviser.GetNotifyAdminIds(), table),
})
}
return &v1alpha1.ListAdvisersResponse{
TotalCount: totalCountAdvisersResponse.GetCount(),
EnabledTotalCount: enabledTotalCountAdvisersResponse.GetCount(),
NextPageToken: listAdvisersResponse.GetNextPageToken(),
Advisers: advisers,
}, nil
}
def list_advisers(req, call)
resp = @adviser_client.list_advisers(req.to_h)
count = @adviser_client.count_advisers.count
enabled_count = @adviser_client.count_advisers(without_disabled: true).count
user_ids = resp.advisers.reduce(Set.new) do |uids, a|
uids += a.notify_admin_ids + [a.user_id]
end
users = @user_client.batch_get_users(ids: user_ids).users
users_by_id = users.map { |u| [u.id, u] }.to_h
ProtoNS::ListAdvisersResponse.new(
total_count: count,
enabled_total_count: enabled_count,
next_page_token: resp.next_page_token,
advisors: resp.advisors.map { |a| {
adviser: a,
user: users_by_id[a.user_id],
notify_admins: a.notify_admin_ids.map { |id| users_by_id[id] }
}},
)
end
func (api *CourseServiceV1Alpha1) AutocompleteCourses(ctx context.Context, req *v1alpha1.AutocompleteCoursesRequest) (*v1alpha1.AutocompleteCoursesResponse, error) {
ctx = forwardGRPCMetadata(ctx)
grpcClient, err := api.Schoolbackoffice.Service.KnownServiceGRPCClient("course")
if err != nil {
return nil, grpc.InternalError("cannot get course grpc client connection: %v", err)
}
courseClient := jobteaser_course_v1.NewCourseServiceClient(grpcClient.Conn)
autocompleteCoursesRequest := jobteaser_course_v1.AutocompleteCoursesRequest{
Text: req.GetText(),
PageToken: req.GetPageToken(),
PageSize: req.GetPageSize(),
Locale: req.GetLocale(),
}
autocompleteCoursesResponse, err := courseClient.AutocompleteCourses(ctx, &autocompleteCoursesRequest)
if err != nil {
return nil, err
}
var courses []*v1alpha1.CourseSearch
for _, course := range autocompleteCoursesResponse.GetCourses() {
courses = append(courses, &v1alpha1.CourseSearch{
Course: course,
})
}
return &v1alpha1.AutocompleteCoursesResponse{
Courses: courses,
NextPageToken: autocompleteCoursesResponse.GetNextPageToken(),
}, nil
}
def autocomplete_courses(req, call)
resp = @course_client.autocomplete_courses(req.to_h)
ProtoNS::AutocompleteCoursesResponse.new(
courses: resp.courses.map { |c| { course: c } },
next_page_token: resp.next_page_token,
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment