mirror of
https://github.com/foomo/foomo-docs.git
synced 2025-10-16 12:35:40 +00:00
feat: extend gtsrpc value object docs
This commit is contained in:
parent
63ad8cc9bc
commit
d0dc058b03
@ -2,12 +2,17 @@
|
|||||||
sidebar_position: 1
|
sidebar_position: 1
|
||||||
toc_max_heading_level: 5
|
toc_max_heading_level: 5
|
||||||
---
|
---
|
||||||
|
|
||||||
# Value Objects
|
# Value Objects
|
||||||
|
|
||||||
Typically value objects will be serialized / marshalled as JSON. Please refer to this documentation:
|
Typically value objects will be serialized / marshalled as JSON. Please refer to this documentation:
|
||||||
|
|
||||||
[https://pkg.go.dev/encoding/json#Marshal](https://pkg.go.dev/encoding/json#Marshal)
|
[https://pkg.go.dev/encoding/json#Marshal](https://pkg.go.dev/encoding/json#Marshal)
|
||||||
|
|
||||||
|
:::note
|
||||||
|
This is just a few basic examples, that show the basics of mapping Go types to idiomatic TypeScript types.
|
||||||
|
:::
|
||||||
|
|
||||||
## Scalar types
|
## Scalar types
|
||||||
|
|
||||||
### Supported in Go and TypeScript
|
### Supported in Go and TypeScript
|
||||||
@ -25,13 +30,11 @@ Typically value objects will be serialized / marshalled as JSON. Please refer to
|
|||||||
|
|
||||||
### Type Aliases
|
### Type Aliases
|
||||||
|
|
||||||
Go
|
```go title="Go"
|
||||||
```go
|
|
||||||
type Greeting string
|
type Greeting string
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript
|
```typescript title="TypeScript"
|
||||||
```typescript
|
|
||||||
type Greeting = string;
|
type Greeting = string;
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -39,8 +42,7 @@ type Greeting = string;
|
|||||||
|
|
||||||
Go does not support enumerations, but `gotsrpc` will translate constants to TypeScripts enums:
|
Go does not support enumerations, but `gotsrpc` will translate constants to TypeScripts enums:
|
||||||
|
|
||||||
Go
|
```go title="Go"
|
||||||
```go
|
|
||||||
type Pet string
|
type Pet string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -58,134 +60,216 @@ const (
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript
|
```typescript title="TypeScript"
|
||||||
```typescript
|
|
||||||
export enum Pet {
|
export enum Pet {
|
||||||
Cat = "cat",
|
Cat = "cat",
|
||||||
Dog = "dog",
|
Dog = "dog",
|
||||||
Fish = "fish",
|
Fish = "fish",
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum SeatCount {
|
export enum SeatCount {
|
||||||
FiveSeats = 5,
|
FiveSeats = 5,
|
||||||
SevenSeats = 7,
|
SevenSeats = 7,
|
||||||
TwoSeats = 2,
|
TwoSeats = 2,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Structs / Interfaces
|
|
||||||
|
|
||||||
```go
|
|
||||||
type Car struct {
|
|
||||||
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
interface Car {
|
|
||||||
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Slices
|
## Slices
|
||||||
|
|
||||||
Slices are nilable in Go, thus they can be null in TypeScript. They translate to `Array<T>|null` in TypeScript.
|
Slices are nilable in Go, thus they can be null in TypeScript. They translate to `Array<T>|null` in TypeScript.
|
||||||
|
|
||||||
### Scalar types
|
### Scalar types
|
||||||
|
|
||||||
```go
|
```go title="Go"
|
||||||
[]string
|
[]string
|
||||||
[]int
|
[]int
|
||||||
// other numeric types
|
// other numeric types
|
||||||
[]bool
|
[]bool
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript title="TypeScript"
|
||||||
Array<string>|null
|
Array<string> | null;
|
||||||
Array<number>|null
|
Array<number> | null;
|
||||||
// all numeric types are numbers
|
// all numeric types are numbers
|
||||||
Array<boolean>|null
|
Array<boolean> | null;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Other slice type examples
|
### Other slice type examples
|
||||||
|
|
||||||
|
#### Structs
|
||||||
|
|
||||||
#### structs
|
```go title="Go"
|
||||||
```go
|
|
||||||
[]Car
|
[]Car
|
||||||
[]*Car
|
[]*Car
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript title="TypeScript"
|
||||||
Array<Car>|null
|
Array<Car> | null;
|
||||||
Array<Car|null>|null
|
Array<Car | null> | null;
|
||||||
```
|
```
|
||||||
#### nested slices
|
|
||||||
```go
|
#### Nested slices
|
||||||
|
|
||||||
|
```go title="Go"
|
||||||
[][]string
|
[][]string
|
||||||
[][]int
|
[][]int
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript title="TypeScript"
|
||||||
Array<Array<string>|null>|null
|
Array<Array<string> | null> | null;
|
||||||
Array<Array<number>|null>|null
|
Array<Array<number> | null> | null;
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Maps / Records
|
## Maps / Records
|
||||||
|
|
||||||
Like slices Go maps are nilable. They translate to `Record<K extends keyof any, T>|null` in TypeScript.
|
Like slices Go maps are nilable. They translate to `Record<K extends keyof any, T>|null` in TypeScript.
|
||||||
|
|
||||||
|
### Scalars
|
||||||
|
|
||||||
### scalars
|
```go title="Go"
|
||||||
|
|
||||||
```go
|
|
||||||
map[string]string
|
map[string]string
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript title="TypeScript"
|
||||||
Record<string,string>|null
|
Record<string, string> | null;
|
||||||
```
|
```
|
||||||
|
|
||||||
### structs
|
### Structs
|
||||||
|
|
||||||
```go
|
```go title="Go"
|
||||||
map[string]*Car
|
map[string]*Car
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript title="TypeScript"
|
||||||
Record<string,Car|null>|null
|
Record<string, Car | null> | null;
|
||||||
```
|
```
|
||||||
|
|
||||||
### slices
|
### Slices
|
||||||
|
|
||||||
```go
|
```go title="Go"
|
||||||
map[string][]*Car
|
map[string][]*Car
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript title="TypeScript"
|
||||||
Record<string,Array<Car|null>|null>|null
|
Record<string, Array<Car | null> | null> | null;
|
||||||
```
|
```
|
||||||
|
|
||||||
## custom map types
|
## Map types
|
||||||
|
|
||||||
Go and TypeScript support map / Record types:
|
Go and TypeScript support map / Record types:
|
||||||
|
|
||||||
:::note
|
:::tip
|
||||||
Scalar types / type aliases are of particular value when using maps, because they can add strong semantics:
|
Scalar types / type aliases are of particular value when using maps, because they can add strong semantics:
|
||||||
:::
|
:::
|
||||||
|
|
||||||
```go
|
```go title="Go"
|
||||||
type CarDirectory map[ProductID]*Car
|
type CarDirectory map[ProductID]*Car
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript title="TypeScript"
|
||||||
type CarDirectory = Record<ProductID,Car|null>|null
|
type CarDirectory = Record<ProductID, Car | null> | null;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Nested map types
|
||||||
|
|
||||||
|
```go title="Go"
|
||||||
|
type BrandID string
|
||||||
|
type BrandCarDirectory map[BrandID]map[ProductID]*Car
|
||||||
|
// or
|
||||||
|
type BrandCarDirectory map[BrandID]CarDirectory
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript title="TypeScript"
|
||||||
|
type BrandID = string;
|
||||||
|
type BrandCarDirectory = Record<
|
||||||
|
BrandID,
|
||||||
|
Record<ProductID, Car | null> | null
|
||||||
|
> | null;
|
||||||
|
// or
|
||||||
|
type BrandCarDirectory = Record<BrandID, CarDirectory> | null;
|
||||||
|
```
|
||||||
|
## Structs / Interfaces
|
||||||
|
|
||||||
|
Arbitrary Types can be composed in structs.
|
||||||
|
|
||||||
|
### Field names
|
||||||
|
|
||||||
|
Naming conventions are different between Go and TypeScript. In order to bridge the gap between Go and TypeScript Go struct fields can be [annotated with tags](https://pkg.go.dev/encoding/json#Marshal). In this way idiomatic naming of fields can be can be provided for both languages and the translation will be automatic.
|
||||||
|
|
||||||
|
#### Default Go => TypeScript
|
||||||
|
|
||||||
|
Without json tags TypeScript field names will be like in Go, which is not idiomatic for TypeScript.
|
||||||
|
|
||||||
|
```go title="Go"
|
||||||
|
type Car struct {
|
||||||
|
GoCase string
|
||||||
|
CamelCase string
|
||||||
|
SnakeCase string
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript title="TypeScript"
|
||||||
|
interface Car {
|
||||||
|
GoCase:string;
|
||||||
|
CamelCase:string;
|
||||||
|
SnakeCase:string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Idiomatic field names
|
||||||
|
|
||||||
|
Json tags allow controlling the name in TypeScript.
|
||||||
|
|
||||||
|
```go title="Go"
|
||||||
|
type Car struct {
|
||||||
|
CamelCase string `json:"camelCase"`
|
||||||
|
SnakeCase string `json:"snake_case"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript title="TypeScript"
|
||||||
|
interface Car {
|
||||||
|
camelCase:string;
|
||||||
|
snake_case:string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Optional and nullable fields
|
||||||
|
|
||||||
|
```go title="Go"
|
||||||
|
type Basic struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
OptionalValue string `json:"optionalValue,omitempty"`
|
||||||
|
NullableValue *string `json:"nullableValue"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript title="TypeScript"
|
||||||
|
type Basic interface {
|
||||||
|
value:string;
|
||||||
|
optionalValue?:string;
|
||||||
|
nullableValue:string|null;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Hiding values from the client
|
||||||
|
|
||||||
|
The Go json tag ``` `json:"-"` ``` on a struct allows it to:
|
||||||
|
|
||||||
|
- hide fields from clients
|
||||||
|
- prevents clients from setting them in JSON Unmarshalling
|
||||||
|
|
||||||
|
```go title="Go"
|
||||||
|
type Basic struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
Secret string `json:"-"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript title="TypeScript"
|
||||||
|
type Basic interface {
|
||||||
|
value:string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### nested maps
|
|
||||||
Loading…
Reference in New Issue
Block a user