2021-08-09 08:01:20 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/metadata"
|
2021-08-09 08:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type MetadataWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Key string
|
|
|
|
Value []byte
|
|
|
|
State domain.MetadataState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MetadataWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *metadata.SetEvent:
|
|
|
|
if wm.Key != e.Key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Value = e.Value
|
|
|
|
wm.State = domain.MetadataStateActive
|
|
|
|
case *metadata.RemovedEvent:
|
2022-10-07 10:44:28 +01:00
|
|
|
if wm.Key != e.Key {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-09 08:01:20 +02:00
|
|
|
wm.State = domain.MetadataStateRemoved
|
2021-08-12 16:10:01 +02:00
|
|
|
case *metadata.RemovedAllEvent:
|
|
|
|
wm.State = domain.MetadataStateRemoved
|
2021-08-09 08:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
type MetadataListWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
metadataList map[string][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MetadataListWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *metadata.SetEvent:
|
|
|
|
wm.metadataList[e.Key] = e.Value
|
|
|
|
case *metadata.RemovedEvent:
|
|
|
|
delete(wm.metadataList, e.Key)
|
2021-08-12 16:10:01 +02:00
|
|
|
case *metadata.RemovedAllEvent:
|
|
|
|
wm.metadataList = make(map[string][]byte)
|
2021-08-09 08:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|