Skip to content

Commit

Permalink
Identity 'key' property is back
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Nov 21, 2023
1 parent 5d7dce0 commit f10ffeb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
32 changes: 8 additions & 24 deletions org/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org

import (
"context"
"encoding/json"
"fmt"

"github.com/invopop/gobl/cbc"
Expand All @@ -17,6 +16,8 @@ type Identity struct {
UUID *uuid.UUID `json:"uuid,omitempty" jsonschema:"title=UUID"`
// Optional label useful for non-standard identities to give a bit more context.
Label string `json:"label,omitempty" jsonschema:"title=Label"`
// Uniquely classify this identity using a key instead of a code.
Key cbc.Key `json:"key,omitempty" jsonschema:"title=Key"`
// The type of Code being represented and usually specific for
// a particular context, country, or tax regime, and cannot be used
// alongside the key.
Expand All @@ -25,11 +26,6 @@ type Identity struct {
Code cbc.Code `json:"code" jsonschema:"title=Code"`
// Description adds details about what the code could mean or imply
Description string `json:"description,omitempty" jsonschema:"title=Description"`

// Key was previously available, but has now been migrated to extensions.
// This should not appear in schemas.
// Deprecated: Since 2023-08-25, use extensions (ext) instead.
Key cbc.Key `json:"-"`
}

// Validate ensures the identity looks valid.
Expand All @@ -41,30 +37,18 @@ func (i *Identity) Validate() error {
func (i *Identity) ValidateWithContext(ctx context.Context) error {
return tax.ValidateStructWithRegime(ctx, i,
validation.Field(&i.Label),
validation.Field(&i.Type),
validation.Field(&i.Key),
validation.Field(&i.Type,
validation.When(i.Key != "",
validation.Empty,
),
),
validation.Field(&i.Code,
validation.Required,
),
)
}

// UnmarshalJSON overrides the default to help extract the key value which is no
// longer used.
func (i *Identity) UnmarshalJSON(data []byte) error {
type Alias Identity
a := &struct {
*Alias
Key cbc.Key `json:"key,omitempty"`
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &a); err != nil {
return err
}
i.Key = a.Key
return nil
}

// HasIdentityType provides a validation rule that will determine if at least one
// of the identities defined includes one with the defined type.
func HasIdentityType(typ cbc.Code) validation.Rule {
Expand Down
11 changes: 0 additions & 11 deletions org/identity_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org_test

import (
"encoding/json"
"testing"

"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/org"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAddIdentity(t *testing.T) {
Expand All @@ -29,12 +27,3 @@ func TestAddIdentity(t *testing.T) {
assert.Len(t, st.Identities, 1)
assert.Equal(t, "BARDOM", st.Identities[0].Code.String())
}

func TestIdentityKeyExtraction(t *testing.T) {
data := `{"key":"example-key","code":"BAR"}`
i := new(org.Identity)
err := json.Unmarshal([]byte(data), i)
require.NoError(t, err)
assert.Equal(t, "example-key", i.Key.String())
assert.Equal(t, "BAR", i.Code.String())
}
10 changes: 9 additions & 1 deletion regimes/mx/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ func validItemExtensions(value interface{}) error {
return nil
}

// extension keys that have been migrated from identities to
// extensions.
var migratedExtensionKeys = []cbc.Key{
ExtKeyCFDIProdServ,
ExtKeyCFDIFiscalRegime,
ExtKeyCFDIUse,
}

func normalizeItem(item *org.Item) error {
// 2023-08-25: Migrate identities to extensions
// Pending removal after migrations completed.
idents := make([]*org.Identity, 0)
for _, v := range item.Identities {
if v.Key != "" {
if v.Key.In(migratedExtensionKeys...) {
if item.Ext == nil {
item.Ext = make(cbc.CodeMap)
}
Expand Down

0 comments on commit f10ffeb

Please sign in to comment.