Skip to content
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

Adds backing store support #54

Merged
merged 12 commits into from
Jan 26, 2023
107 changes: 75 additions & 32 deletions api_client_builder.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,75 @@
package abstractions

import (
sync "sync"

s "github.com/microsoft/kiota-abstractions-go/serialization"
)

var serializerMutex sync.Mutex
var deserializerMutex sync.Mutex

// RegisterDefaultSerializer registers the default serializer to the registry singleton to be used by the request adapter.
func RegisterDefaultSerializer(metaFactory func() s.SerializationWriterFactory) {
factory := metaFactory()
contentType, err := factory.GetValidContentType()
if err == nil && contentType != "" {
serializerMutex.Lock()
s.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories[contentType] = factory
serializerMutex.Unlock()
}
}

// RegisterDefaultDeserializer registers the default deserializer to the registry singleton to be used by the request adapter.
func RegisterDefaultDeserializer(metaFactory func() s.ParseNodeFactory) {
factory := metaFactory()
contentType, err := factory.GetValidContentType()
if err == nil && contentType != "" {
deserializerMutex.Lock()
s.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories[contentType] = factory
deserializerMutex.Unlock()
}
}
package abstractions

import (
"github.com/microsoft/kiota-abstractions-go/store"
sync "sync"

s "github.com/microsoft/kiota-abstractions-go/serialization"
)

var serializerMutex sync.Mutex
var deserializerMutex sync.Mutex

// RegisterDefaultSerializer registers the default serializer to the registry singleton to be used by the request adapter.
func RegisterDefaultSerializer(metaFactory func() s.SerializationWriterFactory) {
factory := metaFactory()
contentType, err := factory.GetValidContentType()
if err == nil && contentType != "" {
serializerMutex.Lock()
s.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories[contentType] = factory
serializerMutex.Unlock()
}
}

// RegisterDefaultDeserializer registers the default deserializer to the registry singleton to be used by the request adapter.
func RegisterDefaultDeserializer(metaFactory func() s.ParseNodeFactory) {
factory := metaFactory()
contentType, err := factory.GetValidContentType()
if err == nil && contentType != "" {
deserializerMutex.Lock()
s.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories[contentType] = factory
deserializerMutex.Unlock()
}
}

// EnableBackingStoreForSerializationWriterFactory Enables the backing store on default serialization writers and the given serialization writer.
func EnableBackingStoreForSerializationWriterFactory(factory s.SerializationWriterFactory) s.SerializationWriterFactory {
var result s.SerializationWriterFactory
switch v := factory.(type) {
case *s.SerializationWriterFactoryRegistry:
enableBackingStoreForSerializationRegistry(v)
default:
result = store.NewBackingStoreSerializationWriterProxyFactory(factory)
enableBackingStoreForSerializationRegistry(s.DefaultSerializationWriterFactoryInstance)
}
return result
}

func enableBackingStoreForSerializationRegistry(registry *s.SerializationWriterFactoryRegistry) {
for key, value := range registry.ContentTypeAssociatedFactories {
if _, ok := value.(*store.BackingStoreSerializationWriterProxyFactory); !ok {
registry.ContentTypeAssociatedFactories[key] = store.NewBackingStoreSerializationWriterProxyFactory(value)
}
}
}

// EnableBackingStoreForParseNodeFactory Enables the backing store on default parse nodes factories and the given parse node factory.
func EnableBackingStoreForParseNodeFactory(factory s.ParseNodeFactory) s.ParseNodeFactory {
var result s.ParseNodeFactory
switch v := factory.(type) {
case *s.ParseNodeFactoryRegistry:
enableBackingStoreForParseNodeRegistry(v)
default:
result = store.NewBackingStoreParseNodeFactory(factory)
enableBackingStoreForParseNodeRegistry(s.DefaultParseNodeFactoryInstance)
}
return result
}

func enableBackingStoreForParseNodeRegistry(registry *s.ParseNodeFactoryRegistry) {
for key, value := range registry.ContentTypeAssociatedFactories {
if _, ok := value.(*store.BackingStoreParseNodeFactory); !ok {
registry.ContentTypeAssociatedFactories[key] = store.NewBackingStoreParseNodeFactory(value)
}
}
}
Loading