Skip to content

🐛 Allow setting the type for structs that implement json.Marshaler #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package crd
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strings"

Expand Down Expand Up @@ -109,6 +110,15 @@ func (c *schemaContext) requestSchema(pkgPath, typeName string) {

// infoToSchema creates a schema for the type in the given set of type information.
func infoToSchema(ctx *schemaContext) *apiext.JSONSchemaProps {
// If the obj implements a JSON marshaler and has a marker, use the markers value and do not traverse as
// the marshaler could be doing anything. If there is no marker, fall back to traversing.
if obj := ctx.pkg.Types.Scope().Lookup(ctx.info.Name); obj != nil && implementsJSONMarshaler(obj.Type()) {
schema := &apiext.JSONSchemaProps{}
applyMarkers(ctx, ctx.info.Markers, schema, ctx.info.RawSpec.Type)
if schema.Type != "" {
return schema
}
}
return typeToSchema(ctx, ctx.info.RawSpec.Type)
}

Expand Down Expand Up @@ -431,3 +441,16 @@ func builtinToType(basic *types.Basic, allowDangerousTypes bool) (typ string, fo

return typ, format, nil
}

// Open coded go/types representation of encoding/json.Marshaller
var jsonMarshaler = types.NewInterfaceType([]*types.Func{
types.NewFunc(token.NoPos, nil, "MarshalJSON",
types.NewSignature(nil, nil,
types.NewTuple(
types.NewVar(token.NoPos, nil, "", types.NewSlice(types.Universe.Lookup("byte").Type())),
types.NewVar(token.NoPos, nil, "", types.Universe.Lookup("error").Type())), false)),
}, nil).Complete()

func implementsJSONMarshaler(typ types.Type) bool {
return types.Implements(typ, jsonMarshaler) || types.Implements(types.NewPointer(typ), jsonMarshaler)
}
104 changes: 104 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ package cronjob
import (
"encoding/json"
"fmt"
"net/url"
"time"

batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -247,6 +249,98 @@ func (t *TotallyABool) UnmarshalJSON(in []byte) error {
return nil
}

// +kubebuilder:validation:Type=string
// URL wraps url.URL.
// It has custom json marshal methods that enable it to be used in K8s CRDs
// such that the CRD resource will have the URL but operator code can can work with url.URL struct
type URL struct {
url.URL
}

func (u *URL) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", u.String())), nil
}

func (u *URL) UnmarshalJSON(b []byte) error {
var ref string
if err := json.Unmarshal(b, &ref); err != nil {
return err
}
if ref == "" {
*u = URL{}
return nil
}

r, err := url.Parse(ref)
if err != nil {
return err
} else if r != nil {
*u = URL{*r}
} else {
*u = URL{}
}
return nil
}

func (u *URL) String() string {
if u == nil {
return ""
}
return u.URL.String()
}

// +kubebuilder:validation:Type=string
// URL2 is an alias of url.URL.
// It has custom json marshal methods that enable it to be used in K8s CRDs
// such that the CRD resource will have the URL but operator code can can work with url.URL struct
type URL2 url.URL

func (u *URL2) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", u.String())), nil
}

func (u *URL2) UnmarshalJSON(b []byte) error {
var ref string
if err := json.Unmarshal(b, &ref); err != nil {
return err
}
if ref == "" {
*u = URL2{}
return nil
}

r, err := url.Parse(ref)
if err != nil {
return err
} else if r != nil {
*u = *(*URL2)(r)
} else {
*u = URL2{}
}
return nil
}

func (u *URL2) String() string {
if u == nil {
return ""
}
return (*url.URL)(u).String()
}

// Duration has a custom Marshaler but no markers.
// We want the CRD generation to infer type information
// from the go types and ignore the presense of the Marshaler.
type Duration struct {
Value time.Duration `json:"value"`
}

func (d Duration) MarshalJSON() ([]byte, error) {
type durationWithoutUnmarshaler Duration
return json.Marshal(durationWithoutUnmarshaler(d))
}

var _ json.Marshaler = Duration{}

// ConcurrencyPolicy describes how the job will be handled.
// Only one of the following concurrent policies may be specified.
// If none of the following policies is specified, the default one
Expand Down Expand Up @@ -283,6 +377,16 @@ type CronJobStatus struct {
// with microsecond precision.
// +optional
LastScheduleMicroTime *metav1.MicroTime `json:"lastScheduleMicroTime,omitempty"`

// LastActiveLogURL specifies the logging url for the last started job
// +optional
LastActiveLogURL *URL `json:"lastActiveLogURL,omitempty"`

// LastActiveLogURL2 specifies the logging url for the last started job
// +optional
LastActiveLogURL2 *URL2 `json:"lastActiveLogURL2,omitempty"`

Runtime *Duration `json:"duration,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
Loading