-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significant refactor of extension key-value handling
- Loading branch information
Showing
24 changed files
with
419 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cbc | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/invopop/jsonschema" | ||
) | ||
|
||
// KeyOrCode is a special type that can be either a Key or a Code. This is meant | ||
// for situations when the value to be used has to be flexible enough | ||
// to either a key defined by GOBL, or a code usually defined by an external | ||
// entity. | ||
type KeyOrCode string | ||
|
||
// String provides the string representation. | ||
func (kc KeyOrCode) String() string { | ||
return string(kc) | ||
} | ||
|
||
// Key returns the key value or empty if the value is a Code. | ||
func (kc KeyOrCode) Key() Key { | ||
k := Key(kc) | ||
if err := k.Validate(); err == nil { | ||
return k | ||
} | ||
return KeyEmpty | ||
} | ||
|
||
// Code returns the code value or empty if the value is a Key. | ||
func (kc KeyOrCode) Code() Code { | ||
c := Code(kc) | ||
if err := c.Validate(); err == nil { | ||
return c | ||
} | ||
return CodeEmpty | ||
} | ||
|
||
// Validate ensures the value is either a key or a code. | ||
func (kc KeyOrCode) Validate() error { | ||
if err := Key(kc).Validate(); err == nil { | ||
return nil | ||
} | ||
if err := Code(kc).Validate(); err == nil { | ||
return nil | ||
} | ||
return errors.New("value is not a key or code") | ||
} | ||
|
||
func (KeyOrCode) JSONSchemaExtend(schema *jsonschema.Schema) { | ||
schema.OneOf = []*jsonschema.Schema{ | ||
KeyEmpty.JSONSchema(), | ||
CodeEmpty.JSONSchema(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cbc_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/invopop/gobl/cbc" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeyOrCode(t *testing.T) { | ||
kc := cbc.KeyOrCode("IT") | ||
assert.Equal(t, "IT", kc.String()) | ||
assert.NoError(t, kc.Validate()) | ||
assert.Equal(t, cbc.Code("IT"), kc.Code()) | ||
assert.Equal(t, cbc.KeyEmpty, kc.Key()) | ||
|
||
kc = cbc.KeyOrCode("testing") | ||
assert.Equal(t, "testing", kc.String()) | ||
assert.NoError(t, kc.Validate()) | ||
assert.Equal(t, cbc.Key("testing"), kc.Key()) | ||
assert.Equal(t, cbc.CodeEmpty, kc.Code()) | ||
|
||
kc = cbc.KeyOrCode("INvalid") | ||
if assert.Error(t, kc.Validate()) { | ||
assert.Contains(t, kc.Validate().Error(), "value is not a key or code") | ||
} | ||
assert.Equal(t, cbc.CodeEmpty, kc.Code()) | ||
assert.Equal(t, cbc.KeyEmpty, kc.Key()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.