Skip to content

Commit

Permalink
Merge pull request #28 from Genez-io/dev
Browse files Browse the repository at this point in the history
Release v0.8.0
  • Loading branch information
radu1122 authored Feb 20, 2024
2 parents 00a42d5 + 914c93d commit 05058dc
Show file tree
Hide file tree
Showing 38 changed files with 1,072 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/cli-tool/cli-commands/genezio-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ These options work only when combined with `genezio create frontend` subcommand.
`--frontend <frontend-template>`: Starter template for frontend

- Required, asked interactively if not provided
- Choices: `react-ts`, `react-js`, `vue-ts`, `vue-js`, `vanilla-js`, `none`
- Choices: `react-ts`, `react-js`, `vue-ts`, `vue-js`, `svelte-ts`, `svelte-js`, `vanilla-js`, `none`
- If `none` is selected, no client folder will be created, but the project will still have a fullstack-like strucure.

`--multirepo`: If present, the project will create separate repositories for backend and frontend
Expand Down
11 changes: 11 additions & 0 deletions docs/examples/go-experimental/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
sidebar_position: 5
---

# Go (Experimental)

The support for Go is newly launched and still in experimental mode. Don't hesitate to contact us on the [support chat](https://app.genez.io) or on our [Discord server](https://discord.com/invite/uc9H5YKjXv) to help you out if you encounter any problems.

| Example | Short Description |
| ---------------------------------- | --------------------------------------- |
| [Getting Started](getting-started) | A guide to create your first Go project |
118 changes: 118 additions & 0 deletions docs/examples/go-experimental/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
sidebar_position: 1
---

# Getting Started

This page will explain how to structure your genezio project when using Go for the backend language.

### Project Structure

Because Go declarations belong to packages instead of files, genezio allows one deployable genezio class per package. This means that for a project with 2 genezio classes, a valid project structure should look like this:

```
.
├── genezio.yaml
├── go.mod
├── users
│   └── users.go
└── tasks
└── tasks.go
```

Each class has its dedicated folder (package). The package can contain multiple files and even have functions or types distributed in many files of the same package, as Go sees them in the same scope.

### The YAML

Go doesn’t support decorators, so indicating which classes should be deployed is done using the `genezio.yaml` configuration file (see [Configuration File - Classes](../../project-structure/genezio-configuration-file#classes)). A YAML configuration for the project structure mentioned above would look like this:

```yaml
name: test-go
region: eu-central-1
classes:
- path: ./users/users.go
type: jsonrpc
- path: ./tasks/tasks.go
type: jsonrpc
```
### “Classes”
Go doesn’t have the notion of “classes”. We have, thankfully, an alternative. Structs with attached methods. For the rest of this page, we will refer to them as `genezio classes`, or simply `classes`. Our parser will choose and analyze the first struct it finds (not necessarily the first declared) that has the following properties:

- is exported (starts with a capital letter)
- has at least one method attached
- the package exports a method named `New` which takes no parameters and returns an instance of the struct

### Methods

Each method that will eventually be deployed needs to respect some signature rules:

- a method can take any number of parameters as long as they are serializable (see [Serialization rules](#serialization-rules))
- it returns an error or a tuple formed by a type and an error (error can be either the standard interface defined in the standard library, or any custom type implementing that interface)

```go
func (u UserService) Get(id int) (*User, error) // valid
func (u *UserService) Delete(user User) error // valid
func (u *UserService) GetAge(user *User) int // NOT VALID, shlould return (int, error)
```

Notice that we can attach methods to the value of a struct as well as to a pointer of that struct. You can use and mix those kinds of declarations as you wish. It makes no difference for the SDK generation or the way your code runs on the genezio infrastructure.

Example of a valid “genezio class” in Go:

```go
package users
import (
"fmt"
"go-example/models"
)
// the genezio class
type UserService struct{}
func New() UserService {
return UserService{}
}
func (u UserService) GetAge(name string, age *int) (int, error) {
fmt.Println("Get Age for", name)
if age == nil {
return 0, fmt.Errorf("age is nil")
}
return *age, nil
}
func (u *UserService) Unimplemented() error {
fmt.Println("Unimplemented")
return fmt.Errorf("not implemented")
}
func (u *UserService) Delete(user *models.User) (*models.User, error) {
if user == nil {
return nil, fmt.Errorf("user is nil")
}
fmt.Println("Deleted user")
return user, nil
}
```

Notice you can import types from other packages. They also have to satisfy the same rules for serialization.

### Serialization rules

- primitive types (int, float64, bool, string), are serializable
- structs are serializable
- arrays are serializable
- maps are serializable as long as the key is `string` and the value type is serializable
- if an argument is a pointer, that means it is an optional argument (can be null)
- if a struct property is a pointer, it means it is an optional property (can be null)
- interfaces are NOT serializable

### Current limitations

- json tags on struct properties currently not supported
- no support for enums
- http methods currently not supported, only jsonrpc and crons methods are available
Loading

0 comments on commit 05058dc

Please sign in to comment.