opentofu/internal/command/jsonfunction/parameter.go
Daniel Banck 4fa77727b5
Introduce metadata functions command (#32487)
* Add metadata functions command skeleton

* Export functions as JSON via cli command

* Add metadata command

* Add tests to jsonfunction package

* WIP: Add metadata functions test

* Change return_type & type in JSON to json.RawMessage

This enables easier deserialisation of types when parsing the JSON.

* Skip is_nullable when false

* Update cli docs with metadata command

* Use tfdiags to report function marshal errors

* Ignore map, list and type functions

* Test Marshal function with diags

* Test metadata functions command output

* Simplify type marshaling by using cty.Type

* Add static function signatures for can and try

* Update internal/command/jsonfunction/function_test.go

Co-authored-by: kmoe <5575356+kmoe@users.noreply.github.com>

---------

Co-authored-by: kmoe <5575356+kmoe@users.noreply.github.com>
2023-02-14 14:08:47 +00:00

44 lines
1.0 KiB
Go

package jsonfunction
import (
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
// parameter represents a parameter to a function.
type parameter struct {
// Name is an optional name for the argument.
Name string `json:"name,omitempty"`
// Description is an optional human-readable description
// of the argument
Description string `json:"description,omitempty"`
// IsNullable is true if null is acceptable value for the argument
IsNullable bool `json:"is_nullable,omitempty"`
// A type that any argument for this parameter must conform to.
Type cty.Type `json:"type"`
}
func marshalParameter(p *function.Parameter) *parameter {
if p == nil {
return &parameter{}
}
return &parameter{
Name: p.Name,
Description: p.Description,
IsNullable: p.AllowNull,
Type: p.Type,
}
}
func marshalParameters(parameters []function.Parameter) []*parameter {
ret := make([]*parameter, len(parameters))
for k, p := range parameters {
ret[k] = marshalParameter(&p)
}
return ret
}