From 0e214f8021369c7b6131e3068bbfcc946c238e65 Mon Sep 17 00:00:00 2001 From: ilya-korotya Date: Tue, 10 May 2022 20:14:20 +0300 Subject: [PATCH 1/5] make getSubject public --- claim.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/claim.go b/claim.go index ddd8229..2d6d140 100644 --- a/claim.go +++ b/claim.go @@ -330,7 +330,7 @@ func (c *Claim) setSubject(s Subject) { c.index[0][flagsByteIdx] |= byte(s) } -func (c *Claim) getSubject() Subject { +func (c *Claim) GetSubject() Subject { sbj := c.index[0][flagsByteIdx] // clean all except first 3 bits sbj &= 0b00000111 @@ -422,7 +422,7 @@ func (c *Claim) ResetID() { // Returns error ErrNoID if ID is not set. func (c *Claim) GetID() (ID, error) { var id ID - switch c.getSubject() { + switch c.GetSubject() { case SubjectOtherIdenIndex: return c.getIndexID(), nil case SubjectOtherIdenValue: From 91dc5664ccb273850eb9599d5433bf61b194105f Mon Sep 17 00:00:00 2001 From: ilya-korotya Date: Wed, 11 May 2022 12:26:38 +0300 Subject: [PATCH 2/5] add string implementation for for subject position --- claim.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/claim.go b/claim.go index 2d6d140..1f87beb 100644 --- a/claim.go +++ b/claim.go @@ -129,6 +129,16 @@ type Claim struct { value [4]ElemBytes } +// SubjectPosition string implementation for Subject. +type SubjectPosition string + +const ( + // SubjectPositionIndex for flag SubjectOtherIdenIndex. + SubjectPositionIndex = "index" + // SubjectPositionValue for flag SubjectOtherIdenValue. + SubjectPositionValue = "value" +) + // Subject for the time being describes the location of ID (in index or value // slots or nowhere at all). // @@ -198,6 +208,7 @@ func WithValueID(id ID) Option { // WithID sets ID to claim's index or value depending on `pos`. func WithID(id ID, pos IDPosition) Option { return func(c *Claim) error { + // TODO (illia-korotia): refactor to Subject type. switch pos { case IDPositionIndex: c.SetIndexID(id) @@ -324,13 +335,25 @@ func (c *Claim) GetSchemaHash() SchemaHash { return schemaHash } +// GetSubjectPosition return a position where the subject is stored. +func (c *Claim) GetSubjectPosition() (SubjectPosition, error) { + switch c.getSubject() { + case SubjectOtherIdenIndex: + return SubjectPositionIndex, nil + case SubjectOtherIdenValue: + return SubjectPositionValue, nil + default: + return "", errors.New("claim without ") + } +} + func (c *Claim) setSubject(s Subject) { // clean first 3 bits c.index[0][flagsByteIdx] &= 0b11111000 c.index[0][flagsByteIdx] |= byte(s) } -func (c *Claim) GetSubject() Subject { +func (c *Claim) getSubject() Subject { sbj := c.index[0][flagsByteIdx] // clean all except first 3 bits sbj &= 0b00000111 @@ -422,7 +445,7 @@ func (c *Claim) ResetID() { // Returns error ErrNoID if ID is not set. func (c *Claim) GetID() (ID, error) { var id ID - switch c.GetSubject() { + switch c.getSubject() { case SubjectOtherIdenIndex: return c.getIndexID(), nil case SubjectOtherIdenValue: From 129de62391c5368665da6f79986c69ac505a497d Mon Sep 17 00:00:00 2001 From: ilya-korotya Date: Wed, 11 May 2022 13:23:19 +0300 Subject: [PATCH 3/5] use IDPosition instead of SubjectPosition type --- claim.go | 66 +++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/claim.go b/claim.go index 1f87beb..22263c2 100644 --- a/claim.go +++ b/claim.go @@ -129,38 +129,27 @@ type Claim struct { value [4]ElemBytes } -// SubjectPosition string implementation for Subject. -type SubjectPosition string - -const ( - // SubjectPositionIndex for flag SubjectOtherIdenIndex. - SubjectPositionIndex = "index" - // SubjectPositionValue for flag SubjectOtherIdenValue. - SubjectPositionValue = "value" -) - -// Subject for the time being describes the location of ID (in index or value +// subjectFlag for the time being describes the location of ID (in index or value // slots or nowhere at all). // -// Values SubjectInvalid, SubjectObjectIndex, SubjectObjectValue -// presents for backward compatibility and for now means nothing. -type Subject byte +// Values subjectInvalidFlag presents for backward compatibility and for now means nothing. +type subjectFlag byte const ( - SubjectSelf Subject = iota // 000 - SubjectInvalid // 001 - SubjectOtherIdenIndex // 010 - SubjectOtherIdenValue // 011 - SubjectObjectIndex // 100 - SubjectObjectValue // 101 + subjectSelfFlag subjectFlag = iota // 000 + subjectInvalidFlag // 001 + subjectOtherIdenIndexFlag // 010 + subjectOtherIdenValueFlag // 011 ) type IDPosition uint8 const ( - // IDPositionIndex means ID value is in index slots - IDPositionIndex IDPosition = iota + 1 // value 0 is position undefined - // IDPositionValue means ID value is in value slots + // IDPositionNone means ID value not located in claim. + IDPositionNone IDPosition = iota + // IDPositionIndex means ID value is in index slots. + IDPositionIndex + // IDPositionValue means ID value is in value slots. IDPositionValue ) @@ -208,7 +197,6 @@ func WithValueID(id ID) Option { // WithID sets ID to claim's index or value depending on `pos`. func WithID(id ID, pos IDPosition) Option { return func(c *Claim) error { - // TODO (illia-korotia): refactor to Subject type. switch pos { case IDPositionIndex: c.SetIndexID(id) @@ -335,29 +323,29 @@ func (c *Claim) GetSchemaHash() SchemaHash { return schemaHash } -// GetSubjectPosition return a position where the subject is stored. -func (c *Claim) GetSubjectPosition() (SubjectPosition, error) { +// GetIDPosition returns the position at which the ID is stored. +func (c *Claim) GetIDPosition() IDPosition { switch c.getSubject() { - case SubjectOtherIdenIndex: - return SubjectPositionIndex, nil - case SubjectOtherIdenValue: - return SubjectPositionValue, nil + case subjectOtherIdenIndexFlag: + return IDPositionIndex + case subjectOtherIdenValueFlag: + return IDPositionValue default: - return "", errors.New("claim without ") + return IDPositionNone } } -func (c *Claim) setSubject(s Subject) { +func (c *Claim) setSubject(s subjectFlag) { // clean first 3 bits c.index[0][flagsByteIdx] &= 0b11111000 c.index[0][flagsByteIdx] |= byte(s) } -func (c *Claim) getSubject() Subject { +func (c *Claim) getSubject() subjectFlag { sbj := c.index[0][flagsByteIdx] // clean all except first 3 bits sbj &= 0b00000111 - return Subject(sbj) + return subjectFlag(sbj) } func (c *Claim) setFlagExpiration(val bool) { @@ -401,7 +389,7 @@ func (c *Claim) GetVersion() uint32 { // SetIndexID sets id to index. Removes id from value if any. func (c *Claim) SetIndexID(id ID) { c.resetValueID() - c.setSubject(SubjectOtherIdenIndex) + c.setSubject(subjectOtherIdenIndexFlag) copy(c.index[1][:], id[:]) } @@ -419,7 +407,7 @@ func (c *Claim) getIndexID() ID { // SetValueID sets id to value. Removes id from index if any. func (c *Claim) SetValueID(id ID) { c.resetIndexID() - c.setSubject(SubjectOtherIdenValue) + c.setSubject(subjectOtherIdenValueFlag) copy(c.value[1][:], id[:]) } @@ -438,7 +426,7 @@ func (c *Claim) getValueID() ID { func (c *Claim) ResetID() { c.resetIndexID() c.resetValueID() - c.setSubject(SubjectSelf) + c.setSubject(subjectSelfFlag) } // GetID returns ID from claim's index of value. @@ -446,9 +434,9 @@ func (c *Claim) ResetID() { func (c *Claim) GetID() (ID, error) { var id ID switch c.getSubject() { - case SubjectOtherIdenIndex: + case subjectOtherIdenIndexFlag: return c.getIndexID(), nil - case SubjectOtherIdenValue: + case subjectOtherIdenValueFlag: return c.getValueID(), nil default: return id, ErrNoID From 5bc992d273d42689339d7cab104d91e5c571d391 Mon Sep 17 00:00:00 2001 From: ilya-korotya Date: Wed, 11 May 2022 13:30:55 +0300 Subject: [PATCH 4/5] rename flags --- claim.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/claim.go b/claim.go index 22263c2..bbb811e 100644 --- a/claim.go +++ b/claim.go @@ -132,14 +132,14 @@ type Claim struct { // subjectFlag for the time being describes the location of ID (in index or value // slots or nowhere at all). // -// Values subjectInvalidFlag presents for backward compatibility and for now means nothing. +// Values subjectFlagInvalid presents for backward compatibility and for now means nothing. type subjectFlag byte const ( - subjectSelfFlag subjectFlag = iota // 000 - subjectInvalidFlag // 001 - subjectOtherIdenIndexFlag // 010 - subjectOtherIdenValueFlag // 011 + subjectFlagSelf subjectFlag = iota // 000 + subjectFlagInvalid // 001 + subjectFlagOtherIdenIndex // 010 + subjectFlagOtherIdenValue // 011 ) type IDPosition uint8 @@ -326,9 +326,9 @@ func (c *Claim) GetSchemaHash() SchemaHash { // GetIDPosition returns the position at which the ID is stored. func (c *Claim) GetIDPosition() IDPosition { switch c.getSubject() { - case subjectOtherIdenIndexFlag: + case subjectFlagOtherIdenIndex: return IDPositionIndex - case subjectOtherIdenValueFlag: + case subjectFlagOtherIdenValue: return IDPositionValue default: return IDPositionNone @@ -389,7 +389,7 @@ func (c *Claim) GetVersion() uint32 { // SetIndexID sets id to index. Removes id from value if any. func (c *Claim) SetIndexID(id ID) { c.resetValueID() - c.setSubject(subjectOtherIdenIndexFlag) + c.setSubject(subjectFlagOtherIdenIndex) copy(c.index[1][:], id[:]) } @@ -407,7 +407,7 @@ func (c *Claim) getIndexID() ID { // SetValueID sets id to value. Removes id from index if any. func (c *Claim) SetValueID(id ID) { c.resetIndexID() - c.setSubject(subjectOtherIdenValueFlag) + c.setSubject(subjectFlagOtherIdenValue) copy(c.value[1][:], id[:]) } @@ -426,7 +426,7 @@ func (c *Claim) getValueID() ID { func (c *Claim) ResetID() { c.resetIndexID() c.resetValueID() - c.setSubject(subjectSelfFlag) + c.setSubject(subjectFlagSelf) } // GetID returns ID from claim's index of value. @@ -434,9 +434,9 @@ func (c *Claim) ResetID() { func (c *Claim) GetID() (ID, error) { var id ID switch c.getSubject() { - case subjectOtherIdenIndexFlag: + case subjectFlagOtherIdenIndex: return c.getIndexID(), nil - case subjectOtherIdenValueFlag: + case subjectFlagOtherIdenValue: return c.getValueID(), nil default: return id, ErrNoID From 1f6ffc57295e0e9113212ed370f61a3bdd977189 Mon Sep 17 00:00:00 2001 From: ilya-korotya Date: Wed, 11 May 2022 13:40:31 +0300 Subject: [PATCH 5/5] add nolint comment --- claim.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/claim.go b/claim.go index bbb811e..c2d716a 100644 --- a/claim.go +++ b/claim.go @@ -137,7 +137,7 @@ type subjectFlag byte const ( subjectFlagSelf subjectFlag = iota // 000 - subjectFlagInvalid // 001 + _subjectFlagInvalid // nolint // 001 subjectFlagOtherIdenIndex // 010 subjectFlagOtherIdenValue // 011 )