diff --git a/astool/codegen/method.go b/astool/codegen/method.go index 6c56953..6e4d626 100644 --- a/astool/codegen/method.go +++ b/astool/codegen/method.go @@ -195,7 +195,7 @@ func (m Method) Definition() jen.Code { if len(m.function.comment) > 0 { comment = jen.Commentf(insertNewlines(m.function.comment)).Line() } - funcDef := jen.Empty() + var funcDef *jen.Statement switch m.member { case pointerMember: funcDef = jen.Func().Params( diff --git a/astool/codegen/struct.go b/astool/codegen/struct.go index 2d58b3d..7303ad8 100644 --- a/astool/codegen/struct.go +++ b/astool/codegen/struct.go @@ -69,8 +69,8 @@ func (s *Struct) Definition() jen.Code { for _, m := range s.methods { ms = append(ms, m.Name()) } - sort.Sort(sort.StringSlice(fs)) - sort.Sort(sort.StringSlice(ms)) + sort.Strings(fs) + sort.Strings(ms) // Add the functions and methods in order. for _, c := range fs { def = def.Line().Line().Add(s.constructors[c].Definition()) diff --git a/astool/codegen/typedef.go b/astool/codegen/typedef.go index 94bb97c..3eb6e30 100644 --- a/astool/codegen/typedef.go +++ b/astool/codegen/typedef.go @@ -59,8 +59,8 @@ func (t *Typedef) Definition() jen.Code { for _, m := range t.methods { ms = append(ms, m.Name()) } - sort.Sort(sort.StringSlice(fs)) - sort.Sort(sort.StringSlice(ms)) + sort.Strings(fs) + sort.Strings(ms) // Add the functions and methods in order for _, c := range fs { def = def.Line().Line().Add(t.constructors[c].Definition()) diff --git a/astool/convert/convert.go b/astool/convert/convert.go index f888ec1..bc8fa4b 100644 --- a/astool/convert/convert.go +++ b/astool/convert/convert.go @@ -14,7 +14,6 @@ import ( const ( interfacePkg = "vocab" - resolverPkg = "resolver" typePropertyName = "type" ) @@ -43,11 +42,11 @@ type vocabulary struct { // newVocabulary creates a vocabulary with maps already made. func newVocabulary() vocabulary { return vocabulary{ - Values: make(map[string]*gen.Kind, 0), - FProps: make(map[string]*gen.FunctionalPropertyGenerator, 0), - NFProps: make(map[string]*gen.NonFunctionalPropertyGenerator, 0), - Types: make(map[string]*gen.TypeGenerator, 0), - References: make(map[string]*vocabulary, 0), + Values: make(map[string]*gen.Kind), + FProps: make(map[string]*gen.FunctionalPropertyGenerator), + NFProps: make(map[string]*gen.NonFunctionalPropertyGenerator), + Types: make(map[string]*gen.TypeGenerator), + References: make(map[string]*vocabulary), } } @@ -274,7 +273,7 @@ func (c *Converter) Convert(p *rdf.ParsedVocabulary) (f []*File, e error) { // all vocabularies and results in a flattened converted map. func (c *Converter) convertReferenceVocabularyRecursively(skip map[string]bool, p *rdf.ParsedVocabulary, refs map[string]*vocabulary) (v map[string]*vocabulary, e error) { v = make(map[string]*vocabulary) - for k, _ := range p.References { + for k := range p.References { if skip[k] { continue } diff --git a/astool/gen/funcprop.go b/astool/gen/funcprop.go index dbc8f08..df853c7 100644 --- a/astool/gen/funcprop.go +++ b/astool/gen/funcprop.go @@ -140,7 +140,7 @@ func (p *FunctionalPropertyGenerator) multiTypeClearNonLanguageMapMembers() []je // funcs produces the methods needed for the functional property. func (p *FunctionalPropertyGenerator) funcs() []*codegen.Method { kindIndexFns := make([]jen.Code, 0, len(p.kinds)+1) - for i, _ := range p.kinds { + for i := range p.kinds { kindIndexFns = append(kindIndexFns, jen.If( jen.Id(codegen.This()).Dot(p.isMethodName(i)).Call(), ).Block( diff --git a/astool/gen/manager.go b/astool/gen/manager.go index 2b99fa9..c7d4a2e 100644 --- a/astool/gen/manager.go +++ b/astool/gen/manager.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/dave/jennifer/jen" "github.com/go-fed/activity/astool/codegen" - "sync" ) const ( @@ -52,9 +51,6 @@ type ManagerGenerator struct { tgManagedMethods map[*TypeGenerator]*managedMethods fpManagedMethods map[*FunctionalPropertyGenerator]*managedMethods nfpManagedMethods map[*NonFunctionalPropertyGenerator]*managedMethods - // Cached during manager code generation. - cacheOnce sync.Once - cachedStruct *codegen.Struct } // managedMethods caches the specific methods and interfaces mapped to specific diff --git a/astool/gen/nonfuncprop.go b/astool/gen/nonfuncprop.go index e7b7359..1fe78c7 100644 --- a/astool/gen/nonfuncprop.go +++ b/astool/gen/nonfuncprop.go @@ -105,7 +105,7 @@ func (p *NonFunctionalPropertyGenerator) Definitions() (*codegen.Struct, *codege // iteratorInterfaceName gets the interface name for the iterator. func (p *NonFunctionalPropertyGenerator) iteratorInterfaceName() string { - return fmt.Sprintf("%s", strings.Title(p.iteratorTypeName().CamelName)) + return strings.Title(p.iteratorTypeName().CamelName) } // elementTypeGenerator produces a FunctionalPropertyGenerator for the iterator diff --git a/astool/gen/pkg.go b/astool/gen/pkg.go index cf1178f..7416ffd 100644 --- a/astool/gen/pkg.go +++ b/astool/gen/pkg.go @@ -447,7 +447,7 @@ func genInit(pkg Package, ) callInitsSlice = append(callInitsSlice, key) } - sort.Sort(sort.StringSlice(callInitsSlice)) + sort.Strings(callInitsSlice) callInits := make([]jen.Code, 0, len(callInitsSlice)) for _, c := range callInitsSlice { callInits = append(callInits, callInitsMap[c]) @@ -464,7 +464,7 @@ func genInit(pkg Package, ) callInitsSlice = append(callInitsSlice, key) } - sort.Sort(sort.StringSlice(callInitsSlice)) + sort.Strings(callInitsSlice) for _, c := range callInitsSlice { callInits = append(callInits, callInitsMap[c]) } diff --git a/astool/gen/property.go b/astool/gen/property.go index e5b02a3..502e360 100644 --- a/astool/gen/property.go +++ b/astool/gen/property.go @@ -290,13 +290,13 @@ func (p *PropertyGenerator) iteratorTypeName() Identifier { // InterfaceName returns the interface name of the property type. func (p *PropertyGenerator) InterfaceName() string { - return fmt.Sprintf("%s", p.StructName()) + return p.StructName() } // parentTypeInterfaceName is useful for iterators that need the base property // type's interface name. func (p *PropertyGenerator) parentTypeInterfaceName() string { - return fmt.Sprintf("%s", strings.TrimSuffix(p.StructName(), "Iterator")) + return strings.TrimSuffix(p.StructName(), "Iterator") } // PropertyName returns the name of this property, as defined in diff --git a/astool/gen/resolver.go b/astool/gen/resolver.go index 7e7261c..d7e816f 100644 --- a/astool/gen/resolver.go +++ b/astool/gen/resolver.go @@ -24,7 +24,6 @@ const ( errorUnhandled = "ErrUnhandledType" errorPredicateUnmatched = "ErrPredicateUnmatched" errorCannotTypeAssert = "errCannotTypeAssertType" - errorCannotTypeAssertPredicate = "errCannotTypeAssertPredicate" isUnFnName = "IsUnmatchedErr" toAliasMapFnName = "toAliasMap" ) @@ -32,21 +31,20 @@ const ( // ResolverGenerator generates the code required for the TypeResolver and the // PredicateTypeResolver. type ResolverGenerator struct { - pkg Package - types []*TypeGenerator - manGen *ManagerGenerator - cacheOnce sync.Once - cachedJSON *codegen.Struct - cachedTypePredicate *codegen.Struct - cachedType *codegen.Struct - cachedErrNoMatch jen.Code - cachedErrUnhandled jen.Code - cachedErrPredicateUnmatched jen.Code - cachedErrCannotTypeAssert jen.Code - cachedErrCannotTypeAssertPredicate jen.Code - cachedFns []*codegen.Function - cachedASInterface *codegen.Interface - cachedResolverInterface *codegen.Interface + pkg Package + types []*TypeGenerator + manGen *ManagerGenerator + cacheOnce sync.Once + cachedJSON *codegen.Struct + cachedTypePredicate *codegen.Struct + cachedType *codegen.Struct + cachedErrNoMatch jen.Code + cachedErrUnhandled jen.Code + cachedErrPredicateUnmatched jen.Code + cachedErrCannotTypeAssert jen.Code + cachedFns []*codegen.Function + cachedASInterface *codegen.Interface + cachedResolverInterface *codegen.Interface } // Creates a new ResolverGenerator for generating all the methods, functions, @@ -138,7 +136,6 @@ func (r *ResolverGenerator) Definition() (jsonRes, typeRes, typePredRes *codegen r.cachedErrUnhandled = r.errorUnhandled() r.cachedErrPredicateUnmatched = r.errorPredicateUnmatched() r.cachedErrCannotTypeAssert = r.errorCannotTypeAssert() - r.cachedErrCannotTypeAssertPredicate = r.errorCannotTypeAssertPredicate() r.cachedFns = r.fns() r.cachedASInterface = r.asInterface() r.cachedResolverInterface = r.resolverInterface() @@ -148,7 +145,6 @@ func (r *ResolverGenerator) Definition() (jsonRes, typeRes, typePredRes *codegen r.cachedErrUnhandled, r.cachedErrPredicateUnmatched, r.cachedErrCannotTypeAssert, - r.cachedErrCannotTypeAssertPredicate, }, r.cachedFns, []*codegen.Interface{ r.cachedASInterface, r.cachedResolverInterface, @@ -184,16 +180,6 @@ func (r *ResolverGenerator) errorCannotTypeAssert() jen.Code { ).Line().Var().Id(errorCannotTypeAssert).Error().Op("=").Qual("errors", "New").Call(jen.Lit("activity stream type cannot be asserted to its interface")) } -// errorCannotTypeAssertPredicate returns the declaration for the -// errCannotTypeAssert global value. -func (r *ResolverGenerator) errorCannotTypeAssertPredicate() jen.Code { - return jen.Commentf( - "%s indicates that a predicate cannot be type-casted to an "+ - "expected function signature.", - errorCannotTypeAssertPredicate, - ).Line().Var().Id(errorCannotTypeAssertPredicate).Error().Op("=").Qual("errors", "New").Call(jen.Lit("predicate cannot be type asserted to a known function type")) -} - // errorPredicateUnmatched returns the declaration for the ErrPredicateUnmatched // global value. func (r *ResolverGenerator) errorPredicateUnmatched() jen.Code { @@ -307,10 +293,7 @@ func (r *ResolverGenerator) jsonResolverMethods() (m []*codegen.Method) { jen.If( jen.Op("!").Id("ok"), ).Block( - jen.List( - jen.Id(vocabId), - jen.Id("_"), - ).Op("=").Id("aliasMap").Index( + jen.Id(vocabId).Op("=").Id("aliasMap").Index( jen.Lit(vocabHttp.String()), ), ), diff --git a/astool/gen/type.go b/astool/gen/type.go index a156877..d02d669 100644 --- a/astool/gen/type.go +++ b/astool/gen/type.go @@ -304,7 +304,7 @@ func (t *TypeGenerator) StructName() string { // InterfaceName returns the interface name for this type. func (t *TypeGenerator) InterfaceName() string { - return fmt.Sprintf("%s", t.StructName()) + return t.StructName() } // Extends returns the generators of types that this ActivityStreams type @@ -450,11 +450,11 @@ func (t *TypeGenerator) allProperties() []Property { } } for ext := range extends { - for k, _ := range ext.WithoutProperties() { + for k := range ext.WithoutProperties() { delete(p, k) } } - for k, _ := range t.WithoutProperties() { + for k := range t.WithoutProperties() { delete(p, k) } // Sort the properties into a stable order -- this is important for @@ -546,7 +546,7 @@ func (t *TypeGenerator) extendsDefinition() (*codegen.Function, *codegen.Method) for _, name := range extends { extendsStr = append(extendsStr, name) } - sort.Sort(sort.StringSlice(extendsStr)) + sort.Strings(extendsStr) extensions := make([]jen.Code, 0, len(extendsStr)) for _, name := range extendsStr { extensions = append(extensions, jen.Lit(name)) @@ -595,7 +595,7 @@ func (t *TypeGenerator) getAllChildrenExtendedBy(s []string, tg *TypeGenerator) s = append(s, e.TypeName()) s = t.getAllChildrenExtendedBy(s, e) } - sort.Sort(sort.StringSlice(s)) + sort.Strings(s) return s } @@ -1119,7 +1119,7 @@ func (t *TypeGenerator) constructorFn() *codegen.Function { jen.Op("&").Qual(t.PrivatePackage().Path(), t.StructName()).Values( jen.Dict{ jen.Id(aliasMember): jen.Lit(t.vocabAlias), - jen.Id(unknownMember): jen.Make(jen.Map(jen.String()).Interface(), jen.Lit(0)), + jen.Id(unknownMember): jen.Make(jen.Map(jen.String()).Interface()), }, ), ), @@ -1132,7 +1132,7 @@ func (t *TypeGenerator) constructorFn() *codegen.Function { jen.Op("&").Qual(t.PrivatePackage().Path(), t.StructName()).Values( jen.Dict{ jen.Id(aliasMember): jen.Lit(t.vocabAlias), - jen.Id(unknownMember): jen.Make(jen.Map(jen.String()).Interface(), jen.Lit(0)), + jen.Id(unknownMember): jen.Make(jen.Map(jen.String()).Interface()), jen.Id(typeMember): jen.Id("typeProp"), }, ), diff --git a/astool/main.go b/astool/main.go index 4e95b0a..dda61c5 100644 --- a/astool/main.go +++ b/astool/main.go @@ -12,6 +12,7 @@ import ( "github.com/go-fed/activity/astool/rdf/rfc" "github.com/go-fed/activity/astool/rdf/schema" "github.com/go-fed/activity/astool/rdf/xsd" + "io" "io/ioutil" "os" "strings" @@ -166,9 +167,7 @@ func mustAddOntology(o rdf.Ontology) { // into the registry, before main executes. func init() { flag.Usage = func() { - fmt.Fprintf( - flag.CommandLine.Output(), - helpText) + _, _ = io.WriteString(flag.CommandLine.Output(), helpText) flag.PrintDefaults() } mustAddOntology(&xsd.XMLOntology{Package: "xml"}) @@ -278,8 +277,7 @@ func (c *CommandLineFlags) detectPath() error { } c.pathAutoDetected = true gopath = strings.Join([]string{gopath, "src", ""}, "/") - c.path.Set(strings.TrimPrefix(pwd, gopath)) - return nil + return c.path.Set(strings.TrimPrefix(pwd, gopath)) } // Validate applies custom validation logic to flags and returns an error if any diff --git a/astool/rdf/data.go b/astool/rdf/data.go index 6f21fc7..d31d3ba 100644 --- a/astool/rdf/data.go +++ b/astool/rdf/data.go @@ -59,7 +59,7 @@ func (p *ParsedVocabulary) GetReference(uri string) (*Vocabulary, error) { return nil, err } if p.References == nil { - p.References = make(map[string]*Vocabulary, 0) + p.References = make(map[string]*Vocabulary) } if v, ok := p.References[httpSpec]; ok { return v, nil diff --git a/astool/rdf/ontology.go b/astool/rdf/ontology.go index 602c64f..e30f38c 100644 --- a/astool/rdf/ontology.go +++ b/astool/rdf/ontology.go @@ -270,12 +270,8 @@ func (l *langstring) Apply(key string, value interface{}, ctx *ParsingContext) ( jen.Id("k"), ), ), - jen.Qual("sort", "Sort").Call( - jen.Qual("sort", "StringSlice").Call(jen.Id("lk")), - ), - jen.Qual("sort", "Sort").Call( - jen.Qual("sort", "StringSlice").Call(jen.Id("rk")), - ), + jen.Qual("sort", "Strings").Call(jen.Id("lk")), + jen.Qual("sort", "Strings").Call(jen.Id("rk")), jen.For( jen.Id("i").Op(":=").Lit(0), jen.Id("i").Op("<").Len(jen.Id("lk")).Op("&&").Id("i").Op("<").Len(jen.Id("rk")), diff --git a/astool/rdf/parse.go b/astool/rdf/parse.go index a015f50..038f561 100644 --- a/astool/rdf/parse.go +++ b/astool/rdf/parse.go @@ -611,6 +611,7 @@ func parseJSONLDContext(registry *RDFRegistry, input JSONLD) (nodes []RDFNode, e s, ok := i.(string) if !ok { err = fmt.Errorf("single @context value is not a string") + return } return registry.getFor(s) } diff --git a/astool/rdf/xsd/ontology.go b/astool/rdf/xsd/ontology.go index 55b53ec..acef58c 100644 --- a/astool/rdf/xsd/ontology.go +++ b/astool/rdf/xsd/ontology.go @@ -1008,7 +1008,8 @@ func (d *duration) Apply(key string, value interface{}, ctx *rdf.ParsingContext) ), ), jen.Id("re").Op(":=").Qual("regexp", "MustCompile").Call( - jen.Lit("P(\\d*Y)?(\\d*M)?(\\d*D)?(T(\\d*H)?(\\d*M)?(\\d*S)?)?"), + // raw string, recommended by https://github.com/dave/jennifer/issues/50 + jen.Op("`P(\\d*Y)?(\\d*M)?(\\d*D)?(T(\\d*H)?(\\d*M)?(\\d*S)?)?`"), ), jen.Id("res").Op(":=").Id("re").Dot("FindStringSubmatch").Call(jen.Id("s")), jen.Var().Id("dur").Qual("time", "Duration"), diff --git a/pub/base_actor_test.go b/pub/base_actor_test.go index 5e96960..35baa56 100644 --- a/pub/base_actor_test.go +++ b/pub/base_actor_test.go @@ -187,7 +187,7 @@ func TestBaseActorSocialProtocol(t *testing.T) { req := toAPRequest(toPostOutboxRequest(testCreateNoId)) delegate.EXPECT().AuthenticatePostOutbox(ctx, resp, req).Return(ctx, true, nil) delegate.EXPECT().AddNewIds(ctx, toDeserializedForm(testCreateNoId)).DoAndReturn(func(c context.Context, activity Activity) error { - activity = withNewId(activity) + withNewId(activity) return nil }) delegate.EXPECT().PostOutbox( @@ -217,7 +217,7 @@ func TestBaseActorSocialProtocol(t *testing.T) { return wrappedInCreate(t), nil }) delegate.EXPECT().AddNewIds(ctx, wrappedInCreate(toDeserializedForm(testMyNote))).DoAndReturn(func(c context.Context, activity Activity) error { - activity = withNewId(activity) + withNewId(activity) return nil }) delegate.EXPECT().PostOutbox( @@ -242,7 +242,7 @@ func TestBaseActorSocialProtocol(t *testing.T) { req := toAPRequest(toPostOutboxRequest(testCreateNoId)) delegate.EXPECT().AuthenticatePostOutbox(ctx, resp, req).Return(ctx, true, nil) delegate.EXPECT().AddNewIds(ctx, toDeserializedForm(testCreateNoId)).DoAndReturn(func(c context.Context, activity Activity) error { - activity = withNewId(activity) + withNewId(activity) return nil }) delegate.EXPECT().PostOutbox( @@ -267,7 +267,7 @@ func TestBaseActorSocialProtocol(t *testing.T) { req := toAPRequest(toPostOutboxRequest(testCreateNoId)) delegate.EXPECT().AuthenticatePostOutbox(ctx, resp, req).Return(ctx, true, nil) delegate.EXPECT().AddNewIds(ctx, toDeserializedForm(testCreateNoId)).DoAndReturn(func(c context.Context, activity Activity) error { - activity = withNewId(activity) + withNewId(activity) return nil }) delegate.EXPECT().PostOutbox( @@ -696,7 +696,7 @@ func TestBaseActor(t *testing.T) { req := toAPRequest(toPostOutboxRequest(testCreateNoId)) delegate.EXPECT().AuthenticatePostOutbox(ctx, resp, req).Return(ctx, true, nil) delegate.EXPECT().AddNewIds(ctx, toDeserializedForm(testCreateNoId)).DoAndReturn(func(c context.Context, activity Activity) error { - activity = withNewId(activity) + withNewId(activity) return nil }) delegate.EXPECT().PostOutbox( @@ -723,7 +723,7 @@ func TestBaseActor(t *testing.T) { req := toAPRequest(toPostOutboxRequest(testCreateNoId)) delegate.EXPECT().AuthenticatePostOutbox(ctx, resp, req).Return(ctx, true, nil) delegate.EXPECT().AddNewIds(ctx, toDeserializedForm(testCreateNoId)).DoAndReturn(func(c context.Context, activity Activity) error { - activity = withNewId(activity) + withNewId(activity) return nil }) delegate.EXPECT().PostOutbox( diff --git a/pub/social_wrapped_callbacks.go b/pub/social_wrapped_callbacks.go index 3f2d109..77f2c1c 100644 --- a/pub/social_wrapped_callbacks.go +++ b/pub/social_wrapped_callbacks.go @@ -185,7 +185,7 @@ func (w SocialWrappedCallbacks) create(c context.Context, a vocab.ActivityStream } // Obtain all actor IRIs. actors := a.GetActivityStreamsActor() - createActorIds := make(map[string]*url.URL, 0) + createActorIds := make(map[string]*url.URL) if actors != nil { createActorIds = make(map[string]*url.URL, actors.Len()) for iter := actors.Begin(); iter != actors.End(); iter = iter.Next() { diff --git a/pub/transport.go b/pub/transport.go index a7f55bf..8c4d3a1 100644 --- a/pub/transport.go +++ b/pub/transport.go @@ -113,7 +113,7 @@ func (h HttpSigTransport) Dereference(c context.Context, iri *url.URL) ([]byte, if err != nil { return nil, err } - req.WithContext(c) + req = req.WithContext(c) req.Header.Add(acceptHeader, acceptHeaderValue) req.Header.Add("Accept-Charset", "utf-8") req.Header.Add("Date", h.clock.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05")+" GMT") @@ -144,7 +144,7 @@ func (h HttpSigTransport) Deliver(c context.Context, b []byte, to *url.URL) erro if err != nil { return err } - req.WithContext(c) + req = req.WithContext(c) req.Header.Add(contentTypeHeader, contentTypeHeaderValue) req.Header.Add("Accept-Charset", "utf-8") req.Header.Add("Date", h.clock.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05")+" GMT")