https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/serializers.go

序列化定义

package users

import (
"gopkg.in/gin-gonic/gin.v1" "github.com/wangzitian0/golang-gin-starter-kit/common"
) // 用户简介序列化结构体
type ProfileSerializer struct {
C *gin.Context
UserModel
} // 用户简介响应json结构体
type ProfileResponse struct {
ID uint `json:"-"`
Username string `json:"username"`
Bio string `json:"bio"`
Image *string `json:"image"`
Following bool `json:"following"`
} // 用户简介的内置方法Response
func (self *ProfileSerializer) Response() ProfileResponse {
//中间件请求c.MustGet
myUserModel := self.C.MustGet("my_user_model").(UserModel)
profile := ProfileResponse{
ID: self.ID,
Username: self.Username,
Bio: self.Bio,
Image: self.Image,
Following: myUserModel.isFollowing(self.UserModel),
}
return profile
} // 用户信息结构体
type UserSerializer struct {
c *gin.Context
} // 用户信息响应json结构体
type UserResponse struct {
Username string `json:"username"`
Email string `json:"email"`
Bio string `json:"bio"`
Image *string `json:"image"`
Token string `json:"token"`
} // 用户信息的内置方法Response
func (self *UserSerializer) Response() UserResponse {
//中间件请求c.MustGet
myUserModel := self.c.MustGet("my_user_model").(UserModel)
user := UserResponse{
Username: myUserModel.Username,
Email: myUserModel.Email,
Bio: myUserModel.Bio,
Image: myUserModel.Image,
Token: common.GenToken(myUserModel.ID),
}
return user
}

从golang-gin-realworld-example-app项目学写httpapi (三)的更多相关文章

  1. 从golang-gin-realworld-example-app项目学写httpapi (八)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/common/unit_test.go 单元测试 ...

  2. 从golang-gin-realworld-example-app项目学写httpapi (七)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/hello.go main调用 package ...

  3. 从golang-gin-realworld-example-app项目学写httpapi (六)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/validators.go 验证器 ...

  4. 从golang-gin-realworld-example-app项目学写httpapi (五)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/middlewares.go 中间件 ...

  5. 从golang-gin-realworld-example-app项目学写httpapi (四)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/routers.go 路由定义 pa ...

  6. 从golang-gin-realworld-example-app项目学写httpapi (二)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/models.go 模型定义 use ...

  7. 从golang-gin-realworld-example-app项目学写httpapi (一)

    https://wangzitian0.github.io/2013/06/29/zero-to-one-1/ https://github.com/gothinkster/golang-gin-re ...

  8. 【饿了么】—— Vue2.0高仿饿了么核心模块&移动端Web App项目爬坑(三)

    前言:接着上一篇项目总结,这一篇是学习过程记录的最后一篇,这里会梳理:评论组件.商家组件.优化.打包.相关资料链接.项目github地址:https://github.com/66Web/ljq_el ...

  9. Golang Gin 项目包依赖管理 godep 使用

    Golang Gin 项目包依赖管理 godep 使用 标签(空格分隔): Go 在按照github.com/tools/godep文档go get完包以后,调整项目结构为$GOPATH/src/$P ...

随机推荐

  1. 查看tomcat部署的项目名

    Myeclips的查看方法 项目名右键-->properties-->Myeclips-->deployment 这里虽然可以改这个路径的项目名 但是一般不建议更改 避免出现未知错误 ...

  2. 控制反转(IOC) 和依赖注入(DI) 的理解

    1.      IOC(控制反转) inverseof control是spring容器的内核,AOP.声明事务等功能在此基础上开花结果. 2.      通过实例理解IOC概念: 实例:<墨攻 ...

  3. javac之Method Invocation Expressions

    15.12.1. Compile-Time Step 1: Determine Class or Interface to Search 15.12.2. Compile-Time Step 2: D ...

  4. 1-2 Mobx 入门实践之TodoList(官方Demo)

    第一步:导入模块 import React, { Component } from 'react'; import { observable, autorun,computed } from 'mob ...

  5. strcpy和strncpy

    在c语言中,对于简单变量,如int型.double型,直接使用赋值符号“=”,即可完成赋值,如 int a=10: int b: b=a: 即可完成用a给b赋值. 但是对于字符串,这样赋值是不准确的. ...

  6. You have not concluded your merge. (MERGE_HEAD exists)。(转)

    自己简直就是一个git小白,碰到问题,一点点的解决吧,可能不太系统,但也只能勤能补拙了 Git本地有修改如何强制更新 本地有修改和提交,如何强制用远程的库更新更新.我尝试过用git pull -f,总 ...

  7. 什么是SharePoint?

    在聊SharePoint开发之前,有必要说下什么是SharePoint. 在我工作的过程中,经常遇到客户对SharePoint不太了解的情况.有客户说,SharePoint太烂了,DropBox能做到 ...

  8. iOS 真机测试错误“The application could not be verified”

    iOS 真机测试错误"The application could not be verified" 真机测试的时候报错:"The application could no ...

  9. 我和ARM的那些事儿3 beep之旅,最详细的ARM裸机工程设置

    前言 在博客园里面我搜索了老半天,找相应的mini2440的裸机开发的程序,让我挺失望的就是居然没有人对做arm这块做详细的解答,到底如何去做,到底如何去配置,都不清楚,让我很纠结,那么我花了近一星期 ...

  10. android studio导入android studio工程

    在导入其他android studio工程的时候因为gradle和sdk.tool等版本不一样,会导致android studio自动去后台下载,导致占用硬盘越来越大,最主要的时候会等待很久,不知道要 ...