Skip to content

Incorrect ArangoDB API Usage & Idempotency in createCollection Helper #2021

@ayushhhh2999

Description

@ayushhhh2999

Describe the bug
The Person struct, which is used for handling user data, is defined directly within the CreateUserHandler function. This approach restricts its scope and reusability, making the code less modular and harder to maintain if the same Person structure needs to be used elsewhere (e.g., in other handlers, for database operations, or for data serialization/deserialization). It's a code organization and best practice issue rather than a runtime crash.
To Reproduce
Steps to reproduce the behavior, if applicable:
Steps to reproduce the behavior, if applicable:

The code in question is found in the CreateUserHandler function, specifically:

Go

func CreateUserHandler(ctx gofr.Context) (interface{}, error) {
// Create a person document
type Person struct {
Name string json:"name"
Age int json:"age"
}
// ... rest of the handler logic
}
There is no runtime error generated by this specific bug; it's a structural issue. However, if you attempt to use the Person struct outside of this function, it will result in a compilation error:

undefined: Person

Expected behavior
The Person struct should be defined at a package level (e.g., in main.go or a dedicated models.go file within the package). This makes it accessible and reusable across different functions and files in the package, leading to cleaner, more maintainable, and less redundant code.

for example
Go

package main

// Person struct defined at package level
type Person struct {
Name string json:"name"
Age int json:"age"
}

func CreateUserHandler(ctx gofr.Context) (interface{}, error) {
var p Person // Use the package-level struct
// ... rest of the handler logic
}

Screenshots
Image

**Environments **
-OS: Linux (or any OS)

  • gofr version [v1.5.0]
  • go version [1.21]

More description
Defining structs at the package level is a standard Go best practice for data structures that represent domain entities or are used in multiple contexts. This improves code readability, reusability, and makes it easier to manage data models.

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationgood first issueGood for newcomers

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions