Skip to content
Snippets Groups Projects
Commit 74cfd2cf authored by Anton Sattarov's avatar Anton Sattarov :cucumber: Committed by Pavel Antonov
Browse files

Добавлены функции установки и сброса переводов

parent e85e37a7
No related branches found
No related tags found
No related merge requests found
...@@ -217,57 +217,83 @@ func (i *Item) Localize(localizer *localizer.Localizer) (err error) { ...@@ -217,57 +217,83 @@ func (i *Item) Localize(localizer *localizer.Localizer) (err error) {
return nil return nil
} }
func (i Item) Encode(ctx context.Context, s *schema.Schema) (*Item, error) { // UnsetTranslation устанавливает значение перевода в nil, для сброса перевода для языка
// "localeID":map{...}, "*":nil} - установка перевода для языка, сброс остальных переводов
// {"*":nil} -сброс всех переводов
func (i *Item) UnsetTranslation(localeID string) {
if i.Translations == nil {
i.Translations = make(map[string]map[string]interface{})
}
i.Translations[localeID] = nil
}
// SetTranslation устанавливает перевод для языка
func (i *Item) SetTranslation(dt map[string]interface{}, localeID string) {
if i.Translations == nil {
i.Translations = make(map[string]map[string]interface{})
}
i.Translations[localeID] = dt
}
func (i *Item) Encode(ctx context.Context, s *schema.Schema) (*Item, error) {
res := *i
if i.Data != nil { if i.Data != nil {
dt, err := schema.Encode(ctx, s, i.Data) dt, err := schema.Encode(ctx, s, i.Data)
if err != nil { if err != nil {
// return errors.WithField(err, "data") // return errors.WithField(err, "data")
return nil, err return nil, err
} }
i.Data = dt.(map[string]interface{}) res.Data = dt.(map[string]interface{})
} }
if len(i.Translations) > 0 { if len(i.Translations) > 0 {
res.Translations = make(map[string]map[string]interface{}, len(i.Translations))
for l, v := range i.Translations { for l, v := range i.Translations {
dt, err := schema.Encode(ctx, s, v) dt, err := schema.Encode(ctx, s, v)
if err != nil { if err != nil {
// return errors.WithField(err, fmt.Sprintf("translations.%s", l)) // return errors.WithField(err, fmt.Sprintf("translations.%s", l))
return nil, err return nil, err
} }
i.Translations[l] = dt.(map[string]interface{}) res.Translations[l] = dt.(map[string]interface{})
} }
} }
return &i, nil return &res, nil
} }
func (i Item) Decode(ctx context.Context, s *schema.Schema) (res *Item, err error) { func (i *Item) Decode(ctx context.Context, s *schema.Schema) (*Item, error) {
res := *i
var err error
if i.Data != nil { if i.Data != nil {
i.Data, err = s.Decode(ctx, i.Data) res.Data, err = s.Decode(ctx, i.Data)
if err != nil { if err != nil {
return nil, err return nil, err
// return errors.WithField(err, "data") // return errors.WithField(err, "data")
} }
} }
if len(i.Translations) > 0 { if len(i.Translations) > 0 {
res.Translations = make(map[string]map[string]interface{}, len(i.Translations))
for l, v := range i.Translations { for l, v := range i.Translations {
dt, err := schema.Decode(ctx, s, v) dt, err := schema.Decode(ctx, s, v)
if err != nil { if err != nil {
return nil, err return nil, err
} }
i.Translations[l] = dt.(map[string]interface{}) res.Translations[l] = dt.(map[string]interface{})
} }
} }
return &i, nil return &res, nil
} }
type ProcessDataFunc func(ctx context.Context, sch *schema.Schema, data map[string]interface{}) (map[string]interface{}, error) type ProcessDataFunc func(ctx context.Context, sch *schema.Schema, data map[string]interface{}) (map[string]interface{}, error)
func (i Item) ProcessData(ctx context.Context, sch *schema.Schema, fn ProcessDataFunc, locales ...*locales.Locale) (*Item, error) { func (i *Item) ProcessData(ctx context.Context, sch *schema.Schema, fn ProcessDataFunc, locales ...*locales.Locale) (*Item, error) {
res := *i
if i.Data != nil { if i.Data != nil {
dt, err := fn(ctx, sch, i.Data) dt, err := fn(ctx, sch, res.Data)
if err != nil { if err != nil {
return nil, errors.WithField(err, "data") return nil, errors.WithField(err, "data")
} }
i.Data = dt res.Data = dt
} }
tr := make(map[string]map[string]interface{}) tr := make(map[string]map[string]interface{})
...@@ -288,15 +314,14 @@ func (i Item) ProcessData(ctx context.Context, sch *schema.Schema, fn ProcessDat ...@@ -288,15 +314,14 @@ func (i Item) ProcessData(ctx context.Context, sch *schema.Schema, fn ProcessDat
return nil, errors.WithField(err, fmt.Sprintf("translations.%s", l.ID)) return nil, errors.WithField(err, fmt.Sprintf("translations.%s", l.ID))
} }
tr[l.ID] = dt tr[l.ID] = dt
} }
i.Translations = nil res.Translations = nil
if len(tr) > 0 { if len(tr) > 0 {
i.Translations = tr res.Translations = tr
} }
return &i, nil return &res, nil
} }
// IsSystemField возвращает являться ли поле системным // IsSystemField возвращает являться ли поле системным
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment