Skip to content

Commit 07cfded

Browse files
committed
Document plain config fields
Go SDK docs now show ordinary config structs, Defaults(), and Constraints() instead of cfg value field types.
1 parent 95038ae commit 07cfded

2 files changed

Lines changed: 55 additions & 29 deletions

File tree

docs/go-sdk/configuration.md

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,65 @@ A Go library can declare configuration that applies to every node under an impor
44

55
```go
66
type Configuration struct {
7-
Region cfg.String
8-
Prefix *cfg.String
7+
Region string
8+
Prefix string
9+
MaxAttempts int64 `ub:"max-attempts"`
10+
}
11+
12+
func (c Configuration) Defaults() []defaults.Default {
13+
return []defaults.Default{
14+
defaults.Value(c.Prefix, ""),
15+
defaults.Value(c.MaxAttempts, int64(3)),
16+
}
17+
}
18+
19+
func (c Configuration) Constraints() []constraint.Constraint {
20+
return []constraint.Constraint{
21+
constraint.Must(constraint.NotEmpty(c.Region)).Message("region is required"),
22+
constraint.Must(constraint.AtLeast(c.MaxAttempts, 1)).
23+
Message("max-attempts must be positive"),
24+
}
925
}
1026

1127
func Library() *runtime.Library {
1228
return &runtime.Library{
1329
Name: "cloud",
1430
Configuration: &cfg.ConfigurationType[*Configuration]{
1531
Description: "Cloud connection settings.",
16-
New: func() *Configuration {
17-
return &Configuration{
18-
Prefix: &cfg.String{Default: ""},
19-
}
20-
},
32+
New: func() *Configuration { return &Configuration{} },
2133
},
2234
}
2335
}
2436
```
2537

2638
Use `runtime.NoConfig` as the config type parameter when a library has no configuration.
2739

28-
## Wrapper types
40+
## Field model
2941

30-
Configuration structs use wrapper types from `pkg/sdk/cfg`:
42+
Configuration structs use the same ordinary Go field model as resource, data source,
43+
and action input structs:
3144

32-
- `cfg.String`
33-
- `cfg.Integer`
34-
- `cfg.Number`
35-
- `cfg.Boolean`
36-
- `cfg.Null`
37-
- `cfg.Any`
38-
- `cfg.List[T]`
39-
- `cfg.Map[T]`
40-
- `cfg.Object[T]`
45+
- `string`, `bool`, `int64`, `float64`, and `any` map to UB scalar types.
46+
- `[]T` maps to `list(T)`.
47+
- `map[string]T` maps to `map(T)`.
48+
- Nested structs map to UB objects.
49+
- `*T` makes a field optional.
50+
- `ub:"name"` changes the UB field name.
51+
- `ub:"-"` omits a Go field from the UB schema.
4152

42-
A pointer field makes a nested value optional. Wrapper fields can set descriptions, defaults, and validators.
53+
Use `Defaults()` for non-pointer fields that may be omitted. Use `Constraints()`
54+
for config validation. Defaults are applied before constraints and before the
55+
decoded config reaches resources, data sources, actions, or functions.
4356

4457
## Source use
4558

4659
A factory input can use the configuration schema:
4760

48-
```
61+
```ub
4962
inputs: {
5063
cloud: {
5164
type: library-config('github.com/example/cloud')
52-
default: {}
65+
default: { region: 'us-west-2' }
5366
}
5467
}
5568
@@ -60,4 +73,5 @@ library-configs: {
6073
}
6174
```
6275

63-
Every resource, data source, action, and function under the `cloud` alias receives that config type at runtime.
76+
Every resource, data source, action, and function under the `cloud` alias receives
77+
that config type at runtime.

docs/go-sdk/schemas-constraints-defaults.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Schemas, constraints, defaults
22

3-
The compiler reads Go library source to derive input schemas, output fields, constraints, and defaults.
3+
The compiler reads Go library source to derive input schemas, configuration schemas,
4+
output fields, constraints, and defaults.
45

56
## Struct fields
67

7-
Input structs become UB object fields. Field names use kebab case by default, and `ub` tags can set the UB name:
8+
Input and configuration structs become UB object fields. Field names use kebab case by
9+
default, and `ub` tags can set the UB name:
810

911
```go
1012
type File struct {
@@ -14,7 +16,9 @@ type File struct {
1416
}
1517
```
1618

17-
Pointer fields are optional. Output structs use the same field naming rules. An output field can be marked sensitive:
19+
Pointer fields are optional. For non-pointer fields that may be omitted, declare
20+
`Defaults()` with `defaults.Value` or `defaults.Optional`. Output structs use the same
21+
field naming rules. An output field can be marked sensitive:
1822

1923
```go
2024
type SecretOutput struct {
@@ -36,7 +40,9 @@ func (f File) Defaults() []defaults.Default {
3640
}
3741
```
3842

39-
`defaults.Value` fills a value before the type's runtime method runs. `defaults.Optional` says the field may be omitted and the zero value is acceptable.
43+
`defaults.Value` fills a value before the type's runtime method runs.
44+
`defaults.Optional` says the field may be omitted and the zero value is acceptable.
45+
The same defaults method model applies to library configuration structs.
4046

4147
## Constraints
4248

@@ -51,10 +57,16 @@ func (f File) Constraints() []constraint.Constraint {
5157
}
5258
```
5359

54-
Set constraints include `ExactlyOneOf`, `AtLeastOneOf`, `AtMostOneOf`, `RequiredTogether`, `RequiredWith`, and `ForbiddenWith`. Predicate constraints use `Must` or `When(...).Require(...)`.
60+
Set constraints include `ExactlyOneOf`, `AtLeastOneOf`, `AtMostOneOf`,
61+
`RequiredTogether`, `RequiredWith`, and `ForbiddenWith`. Predicate constraints use
62+
`Must` or `When(...).Require(...)`. Library configuration structs use the same
63+
constraints method model.
5564

5665
## Check timing
5766

58-
Deep schema and constraint checks happen at compile time when the source and selected libraries are known. The compiled factory trusts those checks and decodes runtime values into the registered Go types.
67+
Deep schema and constraint checks happen at compile time when the source and selected
68+
libraries are known. The compiled factory trusts those checks and decodes runtime
69+
values into the registered Go types.
5970

60-
For checks that need live configuration or external state, a resource can also implement `runtime.InputValidator`; see [Resources](resources.md#apply-time-input-validation).
71+
For checks that need live configuration or external state, a resource can also implement
72+
`runtime.InputValidator`; see [Resources](resources.md#apply-time-input-validation).

0 commit comments

Comments
 (0)