field.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package opencode
  2. import (
  3. "github.com/sst/opencode-sdk-go/internal/param"
  4. "io"
  5. )
  6. // F is a param field helper used to initialize a [param.Field] generic struct.
  7. // This helps specify null, zero values, and overrides, as well as normal values.
  8. // You can read more about this in our [README].
  9. //
  10. // [README]: https://pkg.go.dev/github.com/sst/opencode-sdk-go#readme-request-fields
  11. func F[T any](value T) param.Field[T] { return param.Field[T]{Value: value, Present: true} }
  12. // Null is a param field helper which explicitly sends null to the API.
  13. func Null[T any]() param.Field[T] { return param.Field[T]{Null: true, Present: true} }
  14. // Raw is a param field helper for specifying values for fields when the
  15. // type you are looking to send is different from the type that is specified in
  16. // the SDK. For example, if the type of the field is an integer, but you want
  17. // to send a float, you could do that by setting the corresponding field with
  18. // Raw[int](0.5).
  19. func Raw[T any](value any) param.Field[T] { return param.Field[T]{Raw: value, Present: true} }
  20. // Int is a param field helper which helps specify integers. This is
  21. // particularly helpful when specifying integer constants for fields.
  22. func Int(value int64) param.Field[int64] { return F(value) }
  23. // String is a param field helper which helps specify strings.
  24. func String(value string) param.Field[string] { return F(value) }
  25. // Float is a param field helper which helps specify floats.
  26. func Float(value float64) param.Field[float64] { return F(value) }
  27. // Bool is a param field helper which helps specify bools.
  28. func Bool(value bool) param.Field[bool] { return F(value) }
  29. // FileParam is a param field helper which helps files with a mime content-type.
  30. func FileParam(reader io.Reader, filename string, contentType string) param.Field[io.Reader] {
  31. return F[io.Reader](&file{reader, filename, contentType})
  32. }
  33. type file struct {
  34. io.Reader
  35. name string
  36. contentType string
  37. }
  38. func (f *file) ContentType() string { return f.contentType }
  39. func (f *file) Filename() string { return f.name }