UnmarshalMap using aws-go-sdk - go

// UserInfo 用来解构返回的数据
type UserInfo struct {
gender string `dynamo:"gender"`
product string `dynamo:"product"`
id string `dynamo:"id"`
createTime int `dynamo:"create_time"`
name string `dynamo:"name"`
}
// GetUserInfoByID 根据userId在supe_user表取回用户信息
func GetUserInfoByID(userId string) (UserInfo, error) {
queryInput := dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"userId": {
S: aws.String(userId),
},
},
TableName: aws.String("user"),
}
result, err := dbsession.DynamoDB.GetItem(&queryInput)
userInfo := UserInfo{}
if err != nil {
fmt.Println(err.Error())
return userInfo, err
}
unmarshalMapErr := dynamodbattribute.UnmarshalMap(result.Item, &userInfo)
if unmarshalMapErr != nil {
return userInfo, err
}
fmt.Println(result.Item)
fmt.Println(userInfo.name)
return userInfo, nil
}
Why is this not working? It did not throw any error, just not working...
My guess is something wrong with my UserInfo type, but can't figure the right way to do this, help, please.

In Go, a name is exported if it begins with a capital letter. You should make first letters of fields UPPERCASED to make sure they're exported, like:
type UserInfo struct {
Gender string `dynamo:"gender"`
Product string `dynamo:"product"`
Id string `dynamo:"id"`
CreateTime int `dynamo:"create_time"`
Name string `dynamo:"name"`
}
more info: https://www.goinggo.net/2014/03/exportedunexported-identifiers-in-go.html

Related

i am working on supply chain DApps .here i want to add budget with ownername only not with the whole block

type Product struct {
ID string `json:"id"`
Name string `json:"name"`
Area string `json:"area"`
OwnerName string `json:"ownerName"`
Value int `json:"cost"`
Budget int `json:"budget"`
}
func (pc *ProductTransferSmartContract) AddProduct(ctx contractapi.TransactionContextInterface, id string, name string, area string, ownerName string, cost int, budget int) error {
productJSON, err := ctx.GetStub().GetState(id)
if err != nil {
return fmt.Errorf("Failed to read the data from world state", err)
}
if productJSON != nil {
return fmt.Errorf("the product %s already exists", id)
}
prop := Product{
ID: id,
Name: name,
Area: area,
OwnerName: ownerName,
Value: cost,
}
productBytes, err := json.Marshal(prop)
if err != nil {
return err
}
return ctx.GetStub().PutState(id, productBytes)
}
here i want to attach budget only with owner name so that if owner got changed i can change the budget too. Here i have written chaincode in golang.
If requirement is to restrict budget access only for owner, then private data collection can be used. Corresponding APIs are getPrivateData and putPrivateData.

Error:interface must be a pointer to struct Error Returned in fiber (golang), how to solve this?

I'm new in Golang Programming
I'm Faceing an issue..
I'm trying to acces my sent body data by "BodyParser"functtion
But I got an error
schema: interface must be a pointer to struct
I'm Giving the Function Bellow
func CreateService(c *fiber.Ctx) error {
if c.Locals("user_type") != "1" {
return c.SendString("Wrong One")
}
file, err := c.FormFile("image")
// Check for errors:
if err != nil {
fmt.Println(err.Error())
return c.JSON("Something error")
}
// 👷 Save file to root directory:
c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
// 👷 Save file inside uploads folder under current working directory:
c.SaveFile(file, fmt.Sprintf("./uploads/%s", file.Filename))
// 👷 Save file using a relative path:
c.SaveFile(file, fmt.Sprintf("/tmp/uploads_relative/%s", file.Filename))
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return err
}
service := models.Services{
Title: data["title"],
Src: PORT + "/" + file.Filename,
}
database.DB.Create(&service)
return c.JSON(service)
}
model.Services is
type Services struct {
Id uint `json:"id"`
Title string `json:"title"`
Src string `json:"src"`
}
Please Help me out. Thanks a lot in advance!!!
We need to provide pointer to a struct in GoFiber_v2 not *map[string]string. This is causing the issue.
Below Function for getting data from GoFiber_v2 BodyParser:
func Register(c *fiber.Ctx) error {
var user models.User
err := c.BodyParser(&user)
if err != nil {
return err
}
fmt.Printf("%#+v", user)
}
Model Package
package models
type User struct {
Id uint `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Password string `json:"password"`
}

Replace incoming post request data before bind with Go Gin?

I made a simple post API to store articles in database using Gorm And Go Gin.
problem with API showing when I tried to post the category name instead of the category id because the struct declaring it as int32 type
I made a simple function to getting the id of the category instead of the name but i don't know how to inject it within the incoming request
The article model
package models
import (
"gorm.io/gorm"
)
type News struct {
Id int `gorm:"primary_key;auto_increment;not_null" json:"id"`
Category int32 `gorm:"category" json:"category" binding:"required"`
CountryID int32 `gorm:"country_id" json:"country_id" binding:"required"`
LangID int32 `gorm:"lang_id" json:"lang_id" binding:"required"`
SourceId int32 `gorm:"source_id" json:"source_id" binding:"required"`
HashtagId int32 `gorm:"hashtag_id" json:"hashtag_id" binding:"required"`
Title string `gorm:"title" json:"title" binding:"required"`
Content string `gorm:"content" json:"content" binding:"required"`
Description string `gorm:"description" json:"description" binding:"required"`
Summery string `gorm:"summery" json:"summery" binding:"required"`
ImageUrl string `gorm:"image_url" json:"image_url" binding:"required"`
SourceUrl string `gorm:"source_url" json:"source_url" binding:"required"`
Author string `gorm:"author" json:"author" `
}
func CreateArticle(db *gorm.DB, article *News) (err error) {
err = db.Create(article).Error
if err != nil {
return err
}
return nil
}
Category Model
package models
import "gorm.io/gorm"
type Category struct {
Id int32 `gorm:"primary_key;auto_increment;not_null" json:"id"`
Name string `gorm:"category_name" json:"category" binding:"required"`
Parent int32 `gorm:"parent" json:"parent"`
}
func GetCategoryByName(db *gorm.DB, category *Category, name string) (err error) {
err = db.Where("LOWER(category_name) Like LOWER(?)", "%"+name+"%").First(category).Error
if err != nil {
return err
}
return nil
}
News Controller which implement the models
package controllers
import (
"errors"
"net/http"
"github.com/fouad82/news-app-go/models"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"gorm.io/gorm"
)
type NewsRepo struct {
db *gorm.DB
}
func NewArticle() *NewsRepo {
db := models.InitDb()
return &NewsRepo{db: db}
}
// CreateArticle Create Article
func (repository *NewsRepo) CreateArticle(c *gin.Context) {
var Category models.Category
if err := c.ShouldBindBodyWith(&Category, binding.JSON); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": &Category})
return
}
err := models.GetCategoryByName(repository.db, &Category, Category.Name)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err})
return
}
var CategoryId = Category.Id
var Article models.News
Article.Category = CategoryId
if err := c.ShouldBindBodyWith(&Article, binding.JSON); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": err})
return
}
err = models.GetArticleByTitle(repository.db, &Article, Article.Title)
if err == nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"status": "error", "error": "Title Created Before"})
return
}
createArticle := models.CreateArticle(repository.db, &Article)
if createArticle != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"status": "error", "error": createArticle})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok", "result": "Article Created"})
}
in this line i tried to inject the int value within the article struct abut it still getting error that im passing string value not int and this because it still read from the request json not the injected parameter
var CategoryId = Category.Id
var Article models.News
Article.Category = CategoryId
I believe the function you are using is out of request scope, so it's basically can not affect the gin.Context.
What I suggest is to copy the context in order to modify it with your changed data. Article.Category in specific.
...
c.Copy
...
The other thing is about the function - it's evaluating itself - I would suggest to exclude it from CreateArticle function
createArticle := models.CreateArticle(repository.db, &Article)
What I also suggest is to return changed struct and work with it separately from request payload - probably you need to create another function for that:
func (repository *NewsRepo) ChangedArticle(c *gin.Context)News{
changedNews := News{
Id: 0,
Category: 0, // Here is where you change it
CountryID: 0,
LangID: 0,
SourceId: 0,
HashtagId: 0,
Title: "",
Content: "",
Description: "",
Summery: "",
ImageUrl: "",
SourceUrl: "",
Author: "",
}
return changedNews
}
and than in your main function - just use it:
...
createArticle := models.ChangedArticle
...
I could miss some details, but I hope this top level explanation helps.

Can't set up has-many association in GORM

I'm trying to set up an association between Users and PredictionsBags. My problem is that everything works OK if I use GORM's assumed names for referring objects, but I'd like to change the names a bit.
type User struct {
gorm.Model
// We’ll try not using usernames for now
Email string `gorm:"not null;unique_index"`
Password string `gorm:"-"`
PasswordHash string `gorm:"not null"`
Remember string `gorm:"-"` // A user’s remember token.
RememberHash string `gorm:"not null;unique_index"`
Bags []PredictionsBag
}
Every user, of course, owns zero or more PredictionsBags:
type PredictionsBag struct {
gorm.Model
UserID uint // I want this to be "OwnerID"
Title string
NotesPublic string `gorm:"not null"` // Markdown field. May be published.
NotesPrivate string `gorm:"not null"` // Markdown field. Only for (private) viewing and export.
Predictions []Prediction
}
And I'd like to have .Related() work in the usual way:
func (ug *userGorm) ByEmail(email string) (*User, error) {
var ret User
matchingEmail := ug.db.Where("email = ?", email)
err := first(matchingEmail, &ret)
if err != nil {
return nil, err
}
var bags []PredictionsBag
if err := ug.db.Model(&ret).Related(&bags).Error; err != nil {
return nil, err
}
ret.Bags = bags
return &ret, nil
}
My problem is that I can't find a way to change PredictionsBag.UserID to anything else and still have GORM figure out the relationships involved. I've been reading http://gorm.io/docs/has_many.html#Foreign-Key and if I change the relevant lines to
type User struct {
// …
Bags []PredictionsBag `gorm:"foreignkey:OwnerID"`
}
and
type PredictionsBag struct {
// …
OwnerID uint
// …
}
I get this error:
[2019-07-28 14:23:49] invalid association []
What am I doing wrong? I've also been reading http://gorm.io/docs/belongs_to.html, but I'm not sure which page to follow more closely.
I'll have to Check Related() when I get home, but I think what you're looking for is Preload() This is my example that works with what you want.
package main
import (
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"log"
)
var DB *gorm.DB
func init() {
var err error
DB, err = gorm.Open("mysql", fmt.Sprintf("%s:%s#tcp(%s:3306)/%s?&parseTime=True&loc=Local", "root", "root", "localhost", "testing"))
if err != nil {
log.Fatal(err)
}
DB.DropTableIfExists(&User{}, &PredictionsBag{})
DB.AutoMigrate(&User{}, &PredictionsBag{})
user := User{Email:"dave#example.com"}
user.Bags = append(user.Bags, PredictionsBag{OwnerID: user.ID, NotesPrivate: "1", NotesPublic: "1"})
DB.Create(&user)
}
func main() {
user := User{Email:"dave#example.com"}
err := user.ByEmail()
if err != nil {
log.Println(err)
}
fmt.Println(user.ID, user.Email, "Bags:", len(user.Bags))
DB.Close()
}
type User struct {
gorm.Model
// We’ll try not using usernames for now
Email string `gorm:"not null;unique_index"`
Password string `gorm:"-"`
PasswordHash string `gorm:"not null"`
Remember string `gorm:"-"` // A user’s remember token.
RememberHash string `gorm:"not null;unique_index"`
Bags []PredictionsBag `gorm:"foreignkey:OwnerID"`
}
type PredictionsBag struct {
gorm.Model
OwnerID uint
Title string
NotesPublic string `gorm:"not null"` // Markdown field. May be published.
NotesPrivate string `gorm:"not null"` // Markdown field. Only for (private) viewing and export.
}
func (ug *User) ByEmail() error {
DB.Where("email = ?", ug.Email).Preload("Bags").Limit(1).Find(&ug)
if ug.ID == 0 {
return errors.New("no user found")
}
return nil
}
Using this might work with related, but I'm not sure what else needs to be changed:
Bags []PredictionsBag `gorm:"foreignkey:OwnerID;association_foreignkey:ID"`
Update:
I can get the Related() method to work, if you state the ForeignKey like the following:
DB.Where("email = ?", ug.Email).Limit(1).Find(&ug)
if ug.ID == 0 {
return errors.New("no user found")
}
if err := DB.Model(&ug).Related(&ug.Bags, "owner_id").Error; err != nil {
return err
}

How to access struct with a variable?

I'm new to Go and I'm facing issues in accessing a struct with a variable
I have this function decodeUser. My task is to check whether the keys are present in the request. So this is what I did. I've added a comment where I got the error.
func decodeUser(r *http.Request) (root.User, []string, error) {
var u root.User
if r.Body == nil {
return u, []string{}, errors.New("no request body")
}
decoder := json.NewDecoder(r.Body)
checks := []string{
"UserName",
"FirstName",
"LastName",
"Email",
}
emptyFields := []string{}
for _, check := range checks {
// i'm having problem over here `u[check]` it's showing (invalid
operation: u[check] (type root.User does not support
indexing))
if u[check] == nil {
emptyFields = append(emptyFields, check)
}
}
err := decoder.Decode(&u)
return u, emptyFields, err
}
Just in case I added root.User here's structure for it
type User struct {
ID string
Username string
Password string
FirstName string
LastName string
Email string
PhoneNumber string
PhoneNumberExtension string
DOB time.Time
AboutMe string
}
The problem occurs as it doesn't allow me to access struct by a variable and I can't use this method which is u.check. So basically how should I make u[check] work?
I would suggest you manually check for zero values since it seems that you already know the fields that needs to be non-zero at compile time. However, if that is not the case, here is a simple function (using reflection) that will check for zero values in a struct.
func zeroFields(v interface{}, fields ...string) []string {
val := reflect.Indirect(reflect.ValueOf(v))
if val.Kind() != reflect.Struct {
return nil
}
var zeroes []string
for _, name := range fields {
field := val.FieldByName(name)
if !field.IsValid() {
continue
}
zero := reflect.Zero(field.Type())
// check for zero value
if reflect.DeepEqual(zero.Interface(), field.Interface()) {
zeroes = append(zeroes, name)
}
}
return zeroes
}
func main() {
x := User{
Email: "not#nil",
}
fmt.Println(zeroFields(&x, "ID", "Username", "Email", "Something", "DOB"))
}
Which would output:
[ID Username DOB]
Playground
This is what worked for me
for _, check := range checks {
temp := reflect.Indirect(reflect.ValueOf(&u))
fieldValue := temp.FieldByName(string(check))
if (fieldValue.Type().String() == "string" && fieldValue.Len() == 0) || (fieldValue.Type().String() != "string" && fieldValue.IsNil()) {
fmt.Println("EMPTY->", check)
emptyFields = append(emptyFields, check)
}
}

Resources