Skip to content

Remarshal structs#7

Merged
LightJack05 merged 6 commits intomainfrom
remarshal-structs
Jan 16, 2026
Merged

Remarshal structs#7
LightJack05 merged 6 commits intomainfrom
remarshal-structs

Conversation

@LightJack05
Copy link
Contributor

No description provided.

Copilot AI review requested due to automatic review settings January 16, 2026 15:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the product database queries to return structured Go types instead of pre-marshaled JSON strings, eliminating unnecessary BSON-to-JSON conversion overhead. The changes introduce a new Product struct and update both the database query layer and HTTP handlers to work with Go structs that are marshaled to JSON by the Gin framework.

Changes:

  • Added a comprehensive Product struct definition with BSON and JSON tags
  • Modified GetProductByID and SearchProductByName to return *Product and *[]Product respectively instead of JSON strings
  • Updated handlers to work with struct pointers and rely on Gin's JSON marshaling

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
internal/database/product/product.go New file defining the Product struct with all fields matching the MongoDB schema
internal/database/product/queries.go Refactored to decode MongoDB results directly into Go structs instead of marshaling to JSON strings
internal/handlers/product/getId.go Updated to work with *Product pointer and check for nil instead of empty string
internal/handlers/product/get.go Updated to work with *[]Product pointer and dereference the slice for JSON response

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

package product

type Product struct {
ID_ string `json:"_id" bson:"_id"`
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The field name ID_ with a trailing underscore is unconventional in Go. Consider renaming to ID or MongoID for clarity, as the JSON/BSON tags already specify the _id mapping.

Suggested change
ID_ string `json:"_id" bson:"_id"`
MongoID string `json:"_id" bson:"_id"`

Copilot uses AI. Check for mistakes.
Comment on lines +39 to +43
if products == nil {
log.Println("Products was nil?!")
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "products was nil."})
return
}
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

This nil check is ineffective. The SearchProductByName function returns &results where results is a slice initialized as var results []Product. Taking the address of a local slice variable can never return nil - it will always return a valid pointer, even if the slice is empty. If you want to check for empty results, check len(*products) == 0 instead.

Suggested change
if products == nil {
log.Println("Products was nil?!")
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "products was nil."})
return
}

Copilot uses AI. Check for mistakes.
}

func SearchProductByName(db *mongo.Client, query string, limit int) (string, error) {
func SearchProductByName(db *mongo.Client, query string, limit int) (*[]Product, error) {
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The return type *[]Product (pointer to slice) is unconventional in Go. Slices are already reference types, so returning a pointer to a slice is rarely needed. Consider returning []Product directly instead. This would eliminate the need for nil checking in the handler and the dereferencing when returning the JSON response.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 16, 2026 15:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

func GetProductByID(db *mongo.Client, id string) (string, error) {
bsonResult, err := db.Database("off").Collection("products").FindOne(context.TODO(), bson.M{"_id": id}).Raw()
func GetProductByID(db *mongo.Client, id string) (*Product, error) {
var product *Product = new(Product)
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The variable initialization var product *Product = new(Product) is redundant. In Go, you should simply use var product Product and pass &product to Decode, or use product := &Product{}. The current approach allocates memory unnecessarily and makes the code less idiomatic.

Suggested change
var product *Product = new(Product)
product := &Product{}

Copilot uses AI. Check for mistakes.
@LightJack05 LightJack05 merged commit c032dac into main Jan 16, 2026
12 checks passed
@LightJack05 LightJack05 deleted the remarshal-structs branch January 16, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The json from mongo should be re-marshalled from bson to struct to json to have it properly formatted.

1 participant