Skip to content

Commit

Permalink
🐛 support null resources (#1726)
Browse files Browse the repository at this point in the history
An example for how this works using the `user` resource and setting its
`group` field to null:

```go
func (x *mqlUser) group(gid int64) (*mqlGroup, error) {
  x.Group.State = plugin.StateIsSet|plugin.StateIsNull
  return nil, nil
}
```

Once you set the field internally (`StateIsSet`), it will ignore the
return values and instead read it from the field that you set. Then it
will see `StateIsNull` and use it on the return data
  • Loading branch information
arlimus authored Sep 14, 2023
1 parent a6232f3 commit b47e80e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 4 additions & 0 deletions cli/printer/mql.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ func (print *Printer) autoExpand(blockRef uint64, data interface{}, bundle *llx.
return res.String()
}

if data == nil {
return print.Secondary("null")
}

m, ok := data.(map[string]interface{})
if !ok {
return "data is not a map to auto-expand"
Expand Down
13 changes: 8 additions & 5 deletions providers-sdk/v1/plugin/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,16 @@ func (x *TValue[T]) ToDataRes(typ types.Type) *DataRes {
return &DataRes{}
}
if x.State&StateIsNull != 0 {
res := &DataRes{
Data: &llx.Primitive{Type: string(typ)},
}
if x.Error != nil {
res.Error = x.Error.Error()
return &DataRes{
Error: x.Error.Error(),
Data: &llx.Primitive{Type: string(typ)},
}
}

return &DataRes{
Data: &llx.Primitive{Type: string(types.Nil)},
}
return res
}
raw := llx.RawData{Type: typ, Value: x.Data, Error: x.Error}
res := raw.Result()
Expand Down

0 comments on commit b47e80e

Please sign in to comment.