Skip to content

Commit

Permalink
fixing lint errors (#140)
Browse files Browse the repository at this point in the history
* fixing lint errors

* using assert.NoError(t, err)

* adding G601 to be ignored by linter because go 1.22 takes care of the possible range loop pointer bug, reverting for loop to use the item instead of index

* one more for loop change
  • Loading branch information
Jiyoon Koo authored Apr 18, 2024
1 parent e1264a1 commit d62037d
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 32 deletions.
14 changes: 4 additions & 10 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
run:
tests: true
timeout: 5m
skip-dirs:
- resources
- old
skip-files:
- cmd/protopkg/main.go

output:
print-issued-lines: false
Expand Down Expand Up @@ -73,12 +68,11 @@ linters:
- nolintlint
- wrapcheck # We might want to re-enable this if we manually wrap all the existing errors with fmt.Errorf
- testableexamples


linters-settings:
exhaustive:
default-signifies-exhaustive: true
govet:
check-shadowing: true
dupl:
threshold: 100
goconst:
Expand All @@ -101,9 +95,9 @@ linters-settings:
desc: "use fmt.Errorf or errors.New"
- pkg: braces.dev/errtrace
desc: "use fmt.Errorf or errors.New"
# wrapcheck:
# ignorePackageGlobs:
# - github.com/TBD54566975/ftl/*
gosec:
excludes:
- G601

issues:
max-same-issues: 0
Expand Down
4 changes: 2 additions & 2 deletions dids/didcore/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Document struct {
// Service expresses ways of communicating with the DID subject or associated entities.
// A service can be any type of service the DID subject wants to advertise.
// spec reference: https://www.w3.org/TR/did-core/#verification-methods
Service []*Service `json:"service,omitempty"`
Service []Service `json:"service,omitempty"`

// AssertionMethod is used to specify how the DID subject is expected to express claims,
// such as for the purposes of issuing a Verifiable Credential.
Expand Down Expand Up @@ -193,7 +193,7 @@ func (d *Document) SelectVerificationMethod(selector VMSelector) (VerificationMe
}

// AddService will append the given Service to the Document.Services array
func (d *Document) AddService(service *Service) {
func (d *Document) AddService(service Service) {
d.Service = append(d.Service, service)
}

Expand Down
5 changes: 2 additions & 3 deletions dids/diddht/diddht.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func CreateWithContext(ctx context.Context, opts ...CreateOption) (did.BearerDID
document := didcore.Document{
Context: []string{"https://www.w3.org/ns/did/v1"},
ID: bdid.URI,
Service: []*didcore.Service{},
Service: []didcore.Service{},
VerificationMethod: []didcore.VerificationMethod{},
}

Expand Down Expand Up @@ -248,8 +248,7 @@ func CreateWithContext(ctx context.Context, opts ...CreateOption) (did.BearerDID
}

for _, service := range o.services {
s := service
document.AddService(&s)
document.AddService(service)
}

// 5. Map the output DID Document to a DNS packet
Expand Down
7 changes: 3 additions & 4 deletions dids/diddht/internal/dns/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ func MarshalDIDDocument(d *didcore.Document) ([]byte, error) {
msg.Answers = append(msg.Answers, resource)

// add verification methods to dns message
for _, v := range d.VerificationMethod {
vm := v
for _, vm := range d.VerificationMethod {
// look for the key after the # in the verification method ID
key, ok := vmIDToK[v.ID]
key, ok := vmIDToK[vm.ID]
if !ok {
// TODO handle error
continue
Expand Down Expand Up @@ -166,7 +165,7 @@ func MarshalVerificationMethod(vm *didcore.VerificationMethod) (string, error) {
}

// MarshalService packs a service into a TXT DNS resource record and adds to the DNS message Answers
func MarshalService(dhtDNSkey string, s *didcore.Service, msg *dnsmessage.Message) error {
func MarshalService(dhtDNSkey string, s didcore.Service, msg *dnsmessage.Message) error {
rawData := fmt.Sprintf("id=%s;t=%s;se=%s", s.ID, s.Type, strings.Join(s.ServiceEndpoint, ","))

resource, err := newResource(fmt.Sprintf("_%s._did.", dhtDNSkey), rawData)
Expand Down
6 changes: 3 additions & 3 deletions dids/diddht/internal/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func (rec *decoder) DIDDocument() (*didcore.Document, error) {
didcore.Purposes(opts...),
)
case strings.HasPrefix(name, "_s"):
var s didcore.Service
if err := UnmarshalService(data, &s); err != nil {
var service didcore.Service
if err := UnmarshalService(data, &service); err != nil {
// TODO handle error
continue
}
document.AddService(&s)
document.AddService(service)
case strings.HasPrefix(name, "_cnt"):
// TODO add controller https://did-dht.com/#controller
// optional field
Expand Down
6 changes: 2 additions & 4 deletions dids/diddht/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func Test_VectorsResolve(t *testing.T) {

r := NewResolver(ts.URL, http.DefaultClient)

for _, v := range vectors {
vector := v
for _, vector := range vectors {
t.Run(vector.Description, func(t *testing.T) {
res, err := r.Resolve(vector.Input.DIDUri)
if vector.Errors {
Expand Down Expand Up @@ -107,8 +106,7 @@ func Test_resolve(t *testing.T) {

r := NewResolver(ts.URL, http.DefaultClient)

for k := range vectors {
did := k
for did := range vectors {
t.Run(did, func(t *testing.T) {
res, err := r.Resolve(did)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dids/didweb/didweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func Create(domain string, opts ...CreateOption) (_did.BearerDID, error) {
}

for _, svc := range options.services {
document.AddService(&svc) //nolint:gosec
document.AddService(svc)
}

return _did.BearerDID{
Expand Down
4 changes: 2 additions & 2 deletions dids/didweb/didweb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func TestCreate_WithOptions(t *testing.T) {
assert.Equal(t, 2, len(document.Service))

pfisvc := document.Service[0]
assert.NotEqual(t, didcore.Service{}, *pfisvc)
assert.NotEqual(t, didcore.Service{}, pfisvc)
assert.Equal(t, "#pfi", pfisvc.ID)
assert.Equal(t, "PFI", pfisvc.Type)
assert.Equal(t, "http://localhost:8080/tbdex", pfisvc.ServiceEndpoint[0])

idvsvc := document.Service[1]
assert.NotEqual(t, didcore.Service{}, *idvsvc)
assert.NotEqual(t, didcore.Service{}, idvsvc)
assert.Equal(t, "#idv", idvsvc.ID)
assert.Equal(t, "IDV", idvsvc.Type)
assert.Equal(t, "http://localhost:8080/idv", idvsvc.ServiceEndpoint[0])
Expand Down
2 changes: 1 addition & 1 deletion jws/jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Decode(jws string, opts ...DecodeOption) (Decoded, error) {
}

if header.KID == "" {
return Decoded{}, errors.New("malformed JWS. Expected header to contain kid.")
return Decoded{}, errors.New("malformed JWS. Expected header to contain kid")
}

signerDID, err := _did.Parse(header.KID)
Expand Down
8 changes: 6 additions & 2 deletions jws/jws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func TestDecode_SuccessWithTestJwtWithPayload(t *testing.T) {
"Z29acnY5czlnUkpOZkhPeTkyNmhkWk50U1lYZ2hhYl9RRmhFNTM3Yk0ifQ", decoded.SignerDID.URI)
var payloadMap map[string]interface{}

json.Unmarshal(decoded.Payload, &payloadMap)
err = json.Unmarshal(decoded.Payload, &payloadMap)
assert.NoError(t, err)

if iss, ok := payloadMap["iss"].(string); ok {
assert.Equal(t, "did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6Imdl"+
"Z29acnY5czlnUkpOZkhPeTkyNmhkWk50U1lYZ2hhYl9RRmhFNTM3Yk0ifQ", iss)
Expand Down Expand Up @@ -105,7 +107,9 @@ func TestDecode_SuccessWithTestJwtWithDetachedPayload(t *testing.T) {
"Z29acnY5czlnUkpOZkhPeTkyNmhkWk50U1lYZ2hhYl9RRmhFNTM3Yk0ifQ", decoded.SignerDID.URI)
var payloadMap map[string]interface{}

json.Unmarshal(decoded.Payload, &payloadMap)
err = json.Unmarshal(decoded.Payload, &payloadMap)
assert.NoError(t, err)

if iss, ok := payloadMap["iss"].(string); ok {
assert.Equal(t, "did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6Imdl"+
"Z29acnY5czlnUkpOZkhPeTkyNmhkWk50U1lYZ2hhYl9RRmhFNTM3Yk0ifQ", iss)
Expand Down

0 comments on commit d62037d

Please sign in to comment.