Skip to content

Commit 440abb9

Browse files
committed
add nullable hcl string
1 parent fee2d1c commit 440abb9

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

site/genweb/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,16 @@ func TypeMappings(gen *guts.GoParser) error {
9494
Prefix: "",
9595
})
9696
},
97+
"github.com/coder/preview/types.HCLString": func() bindings.ExpressionType {
98+
return bindings.Reference(bindings.Identifier{
99+
Name: "NullHCLString",
100+
Package: nil,
101+
Prefix: "",
102+
})
103+
},
97104
})
98105

99106
err := gen.IncludeCustom(map[string]string{
100-
"github.com/coder/preview/types.HCLString": "string",
101107
// Serpent fields should be converted to their primitive types
102108
"github.com/coder/serpent.Regexp": "string",
103109
"github.com/coder/serpent.StringArray": "string",

site/src/types/preview.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ export interface FriendlyDiagnostic {
1515
readonly detail: string;
1616
}
1717

18+
// From types/value.go
19+
export interface NullHCLString {
20+
readonly value: string;
21+
readonly valid: boolean;
22+
}
23+
1824
// From types/parameter.go
1925
export interface Parameter extends ParameterData {
20-
readonly value: string;
26+
readonly value: NullHCLString;
2127
readonly diagnostics: Diagnostics;
2228
}
2329

@@ -32,7 +38,7 @@ export interface ParameterData {
3238
// empty interface{} type, falling back to unknown
3339
readonly form_type_metadata: unknown;
3440
readonly mutable: boolean;
35-
readonly default_value: string;
41+
readonly default_value: NullHCLString;
3642
readonly icon: string;
3743
readonly options: readonly (ParameterOption)[];
3844
readonly validations: readonly (ParameterValidation)[];
@@ -45,7 +51,7 @@ export interface ParameterData {
4551
export interface ParameterOption {
4652
readonly name: string;
4753
readonly description: string;
48-
readonly value: string;
54+
readonly value: NullHCLString;
4955
readonly icon: string;
5056
}
5157

types/value.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
"github.com/zclconf/go-cty/cty"
1010
)
1111

12+
type NullHCLString struct {
13+
Value string `json:"value"`
14+
Valid bool `json:"valid"`
15+
}
16+
1217
// @typescript-ignore HCLString
1318
type HCLString struct {
1419
Value cty.Value
@@ -36,16 +41,23 @@ func ToHCLString(block *terraform.Block, attr *terraform.Attribute) HCLString {
3641
}
3742

3843
func (s HCLString) MarshalJSON() ([]byte, error) {
39-
return json.Marshal(s.AsString())
44+
return json.Marshal(NullHCLString{
45+
Value: s.AsString(),
46+
Valid: s.Valid() && s.Value.IsKnown(),
47+
})
4048
}
4149

4250
func (s *HCLString) UnmarshalJSON(data []byte) error {
43-
var str string
44-
if err := json.Unmarshal(data, &str); err != nil {
51+
var reduced NullHCLString
52+
if err := json.Unmarshal(data, &reduced); err != nil {
4553
return err
4654
}
55+
if reduced.Valid {
56+
*s = StringLiteral(reduced.Value)
57+
} else {
58+
s.Value = cty.NilVal
59+
}
4760

48-
*s = StringLiteral(str)
4961
return nil
5062
}
5163

0 commit comments

Comments
 (0)