Add WithSerializer option to add serializers to CodecFactory.

This commit is contained in:
Ben Luddy 2024-10-16 17:41:00 -04:00
parent 66a14268c5
commit db1239d354
No known key found for this signature in database
GPG Key ID: A6551E73A5974C30

View File

@ -89,6 +89,10 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory, option
},
}
for _, f := range options.serializers {
serializers = append(serializers, f(scheme, scheme))
}
return serializers
}
@ -108,6 +112,8 @@ type CodecFactoryOptions struct {
Strict bool
// Pretty includes a pretty serializer along with the non-pretty one
Pretty bool
serializers []func(runtime.ObjectCreater, runtime.ObjectTyper) runtime.SerializerInfo
}
// CodecFactoryOptionsMutator takes a pointer to an options struct and then modifies it.
@ -134,6 +140,13 @@ func DisableStrict(options *CodecFactoryOptions) {
options.Strict = false
}
// WithSerializer configures a serializer to be supported in addition to the default serializers.
func WithSerializer(f func(runtime.ObjectCreater, runtime.ObjectTyper) runtime.SerializerInfo) CodecFactoryOptionsMutator {
return func(options *CodecFactoryOptions) {
options.serializers = append(options.serializers, f)
}
}
// NewCodecFactory provides methods for retrieving serializers for the supported wire formats
// and conversion wrappers to define preferred internal and external versions. In the future,
// as the internal version is used less, callers may instead use a defaulting serializer and