From 3fb8338c363e2ad46fc037abc54777f81eb2fc96 Mon Sep 17 00:00:00 2001 From: dosco <832235+dosco@users.noreply.github.com> Date: Fri, 12 Jul 2024 19:03:06 -0700 Subject: [PATCH] fix: GraphJinEngine should be graphJinService its not a public api --- core/api.go | 28 ++++++++++++++-------------- core/args.go | 2 +- core/cache.go | 2 +- core/core.go | 21 ++++++++++----------- core/gstate.go | 4 ++-- core/init.go | 7 +++---- core/intro.go | 14 +------------- core/resolve.go | 6 +++--- core/rolestmt.go | 2 +- core/subs.go | 21 ++++++++++----------- core/watcher.go | 4 ++-- 11 files changed, 48 insertions(+), 63 deletions(-) diff --git a/core/api.go b/core/api.go index b615b028..932c5d14 100644 --- a/core/api.go +++ b/core/api.go @@ -46,7 +46,7 @@ const ( // GraphJin struct is an instance of the GraphJin engine it holds all the required information like // datase schemas, relationships, etc that the GraphQL to SQL compiler would need to do it's job. -type GraphjinEngine struct { +type graphjinEngine struct { conf *Config db *sql.DB log *_log.Logger @@ -83,7 +83,7 @@ type GraphJin struct { done chan bool } -type Option func(*GraphjinEngine) error +type Option func(*graphjinEngine) error // NewGraphJin creates the GraphJin struct, this involves querying the database to learn its // schemas and relationships @@ -131,7 +131,7 @@ func (g *GraphJin) newGraphJin(conf *Config, t := time.Now() - gj := &GraphjinEngine{ + gj := &graphjinEngine{ conf: conf, db: db, dbinfo: dbinfo, @@ -204,7 +204,7 @@ func (g *GraphJin) newGraphJin(conf *Config, } func OptionSetNamespace(namespace string) Option { - return func(s *GraphjinEngine) error { + return func(s *graphjinEngine) error { s.namespace = namespace return nil } @@ -212,7 +212,7 @@ func OptionSetNamespace(namespace string) Option { // OptionSetFS sets the file system to be used by GraphJin func OptionSetFS(fs FS) Option { - return func(s *GraphjinEngine) error { + return func(s *graphjinEngine) error { s.fs = fs return nil } @@ -220,7 +220,7 @@ func OptionSetFS(fs FS) Option { // OptionSetTrace sets the tracer to be used by GraphJin func OptionSetTrace(trace Tracer) Option { - return func(s *GraphjinEngine) error { + return func(s *graphjinEngine) error { s.trace = trace return nil } @@ -228,7 +228,7 @@ func OptionSetTrace(trace Tracer) Option { // OptionSetResolver sets the resolver function to be used by GraphJin func OptionSetResolver(name string, fn ResolverFn) Option { - return func(s *GraphjinEngine) error { + return func(s *graphjinEngine) error { if s.rtmap == nil { s.rtmap = s.newRTMap() } @@ -301,7 +301,7 @@ func (g *GraphJin) GraphQL(c context.Context, vars json.RawMessage, rc *RequestConfig, ) (res *Result, err error) { - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) c1, span := gj.spanStart(c, "GraphJin Query") defer span.End() @@ -382,7 +382,7 @@ func (g *GraphJin) GraphQLByName(c context.Context, vars json.RawMessage, rc *RequestConfig, ) (res *Result, err error) { - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) c1, span := gj.spanStart(c, "GraphJin Query") defer span.End() @@ -432,7 +432,7 @@ type GraphqlResponse struct { } // newGraphqlReq creates a new GraphQL request -func (gj *GraphjinEngine) newGraphqlReq(rc *RequestConfig, +func (gj *graphjinEngine) newGraphqlReq(rc *RequestConfig, op string, name string, query []byte, @@ -466,13 +466,13 @@ func (r *GraphqlReq) Set(item allow.Item) { } // GraphQL function is our main function it takes a GraphQL query compiles it -func (gj *GraphjinEngine) queryWithResult(c context.Context, r GraphqlReq) (res *Result, err error) { +func (gj *graphjinEngine) queryWithResult(c context.Context, r GraphqlReq) (res *Result, err error) { resp, err := gj.query(c, r) return &resp.res, err } // GraphQL function is our main function it takes a GraphQL query compiles it -func (gj *GraphjinEngine) query(c context.Context, r GraphqlReq) ( +func (gj *graphjinEngine) query(c context.Context, r GraphqlReq) ( resp GraphqlResponse, err error, ) { resp.res = Result{ @@ -527,14 +527,14 @@ func (g *GraphJin) Reload() error { // reload redoes database discover and reinitializes GraphJin. func (g *GraphJin) reload(di *sdata.DBInfo) (err error) { - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) err = g.newGraphJin(gj.conf, gj.db, di, gj.fs, gj.opts...) return } // IsProd return true for production mode or false for development mode func (g *GraphJin) IsProd() bool { - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) return gj.prod } diff --git a/core/args.go b/core/args.go index 755fa7d2..0d6245e0 100644 --- a/core/args.go +++ b/core/args.go @@ -18,7 +18,7 @@ type args struct { cindx int // index of cursor arg } -func (gj *GraphjinEngine) argList(c context.Context, +func (gj *graphjinEngine) argList(c context.Context, md psql.Metadata, fields map[string]json.RawMessage, rc *RequestConfig, diff --git a/core/cache.go b/core/cache.go index 52a03d64..524d4c30 100644 --- a/core/cache.go +++ b/core/cache.go @@ -9,7 +9,7 @@ type Cache struct { } // initCache initializes the cache -func (gj *GraphjinEngine) initCache() (err error) { +func (gj *graphjinEngine) initCache() (err error) { gj.cache.cache, err = lru.New2Q(500) return } diff --git a/core/core.go b/core/core.go index b00ef3f4..37511743 100644 --- a/core/core.go +++ b/core/core.go @@ -56,7 +56,7 @@ const ( // Duration time.Duration `json:"duration"` // } -func (gj *GraphjinEngine) getIntroResult() (data json.RawMessage, err error) { +func (gj *graphjinEngine) getIntroResult() (data json.RawMessage, err error) { var ok bool if data, ok = gj.cache.Get("_intro"); ok { return @@ -69,7 +69,7 @@ func (gj *GraphjinEngine) getIntroResult() (data json.RawMessage, err error) { } // Initializes the database discovery process on graphjin -func (gj *GraphjinEngine) initDiscover() (err error) { +func (gj *graphjinEngine) initDiscover() (err error) { switch gj.conf.DBType { case "": gj.dbtype = "postgres" @@ -86,7 +86,7 @@ func (gj *GraphjinEngine) initDiscover() (err error) { } // Private method that does the actual database discovery for initDiscover -func (gj *GraphjinEngine) _initDiscover() (err error) { +func (gj *graphjinEngine) _initDiscover() (err error) { if gj.prod && gj.conf.EnableSchema { b, err := gj.fs.Get("db.graphql") if err != nil { @@ -132,14 +132,14 @@ func (gj *GraphjinEngine) _initDiscover() (err error) { } // Initializes the database schema on graphjin -func (gj *GraphjinEngine) initSchema() error { +func (gj *graphjinEngine) initSchema() error { if err := gj._initSchema(); err != nil { return fmt.Errorf("%s: %w", gj.dbtype, err) } return nil } -func (gj *GraphjinEngine) _initSchema() (err error) { +func (gj *graphjinEngine) _initSchema() (err error) { if len(gj.dbinfo.Tables) == 0 { return fmt.Errorf("no tables found in database") } @@ -170,7 +170,6 @@ func (gj *GraphjinEngine) _initSchema() (err error) { gj.schema, err = sdata.NewDBSchema( gj.dbinfo, getDBTableAliases(gj.conf)) - if err != nil { return } @@ -178,7 +177,7 @@ func (gj *GraphjinEngine) _initSchema() (err error) { return } -func (gj *GraphjinEngine) initIntro() (err error) { +func (gj *graphjinEngine) initIntro() (err error) { if !gj.prod && gj.conf.EnableIntrospection { var introJSON json.RawMessage introJSON, err = gj.getIntroResult() @@ -194,7 +193,7 @@ func (gj *GraphjinEngine) initIntro() (err error) { } // Initializes the qcode compilers -func (gj *GraphjinEngine) initCompilers() (err error) { +func (gj *graphjinEngine) initCompilers() (err error) { qcc := qcode.Config{ TConfig: gj.tmap, DefaultBlock: gj.conf.DefaultBlock, @@ -225,7 +224,7 @@ func (gj *GraphjinEngine) initCompilers() (err error) { return } -func (gj *GraphjinEngine) executeRoleQuery(c context.Context, +func (gj *graphjinEngine) executeRoleQuery(c context.Context, conn *sql.Conn, vmap map[string]json.RawMessage, rc *RequestConfig, @@ -387,7 +386,7 @@ func (s *gstate) debugLogStmt() { } // Saved the query qcode to the allow list -func (gj *GraphjinEngine) saveToAllowList(qc *qcode.QCode, ns string) (err error) { +func (gj *graphjinEngine) saveToAllowList(qc *qcode.QCode, ns string) (err error) { if gj.conf.DisableAllowList { return nil } @@ -417,7 +416,7 @@ func (gj *GraphjinEngine) saveToAllowList(qc *qcode.QCode, ns string) (err error } // Starts tracing with the given name -func (gj *GraphjinEngine) spanStart(c context.Context, name string) (context.Context, Spaner) { +func (gj *graphjinEngine) spanStart(c context.Context, name string) (context.Context, Spaner) { return gj.trace.Start(c, name) } diff --git a/core/gstate.go b/core/gstate.go index 8d305bfd..19a46d24 100644 --- a/core/gstate.go +++ b/core/gstate.go @@ -16,7 +16,7 @@ import ( ) type gstate struct { - gj *GraphjinEngine + gj *graphjinEngine r GraphqlReq cs *cstate vmap map[string]json.RawMessage @@ -40,7 +40,7 @@ type stmt struct { sql string } -func newGState(c context.Context, gj *GraphjinEngine, r GraphqlReq) (s gstate, err error) { +func newGState(c context.Context, gj *graphjinEngine, r GraphqlReq) (s gstate, err error) { s.gj = gj s.r = r diff --git a/core/init.go b/core/init.go index 9fcbb4a2..f70dc7a7 100644 --- a/core/init.go +++ b/core/init.go @@ -11,7 +11,7 @@ import ( ) // Initializes the graphjin instance with the config -func (gj *GraphjinEngine) initConfig() error { +func (gj *graphjinEngine) initConfig() error { c := gj.conf tableMap := make(map[string]struct{}) @@ -85,7 +85,7 @@ func (gj *GraphjinEngine) initConfig() error { } // addTableInfo adds table info to the compiler -func (gj *GraphjinEngine) addTableInfo(t Table) error { +func (gj *graphjinEngine) addTableInfo(t Table) error { obm := map[string][][2]string{} for k, ob := range t.OrderBy { @@ -440,12 +440,11 @@ func isASCII(s string) (int, bool) { } // initAllowList initializes the allow list -func (gj *GraphjinEngine) initAllowList() (err error) { +func (gj *graphjinEngine) initAllowList() (err error) { gj.allowList, err = allow.New( gj.log, gj.fs, gj.conf.DisableAllowList) // if true then read only - if err != nil { return fmt.Errorf("failed to initialize allow list: %w", err) } diff --git a/core/intro.go b/core/intro.go index b409441c..c1afa277 100644 --- a/core/intro.go +++ b/core/intro.go @@ -199,8 +199,7 @@ type Introspection struct { } // introQuery returns the introspection query result -func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) { - +func (gj *graphjinEngine) introQuery() (result json.RawMessage, err error) { // Initialize the introscpection object in := Introspection{ schema: gj.schema, @@ -217,7 +216,6 @@ func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) { MutationType: &ShortFullType{Name: "Mutation"}, } - // Add the standard types // Add the standard types for _, v := range stdTypes { in.addType(v) @@ -242,21 +240,14 @@ func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) { in.addExpTypes(v, "IntList", newTypeRef("", "Int", nil)) in.addExpTypes(v, "BooleanList", newTypeRef("", "Boolean", nil)) in.addExpTypes(v, "FloatList", newTypeRef("", "Float", nil)) - in.addExpTypes(v, "StringList", newTypeRef("", "String", nil)) - in.addExpTypes(v, "IntList", newTypeRef("", "Int", nil)) - in.addExpTypes(v, "BooleanList", newTypeRef("", "Boolean", nil)) - in.addExpTypes(v, "FloatList", newTypeRef("", "Float", nil)) v = append(expAll, expJSON...) in.addExpTypes(v, "JSON", newTypeRef("", "String", nil)) - in.addExpTypes(v, "JSON", newTypeRef("", "String", nil)) - // Add the roles // Add the roles in.addRolesEnumType(gj.roles) in.addTablesEnumType() - // Get all the alias and add to the schema // Get all the alias and add to the schema for alias, t := range in.schema.GetAliases() { if err = in.addTable(t, alias); err != nil { @@ -264,7 +255,6 @@ func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) { } } - // Get all the tables and add to the schema // Get all the tables and add to the schema for _, t := range in.schema.GetTables() { if err = in.addTable(t, ""); err != nil { @@ -272,14 +262,12 @@ func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) { } } - // Add the directives // Add the directives for _, dt := range dirTypes { in.addDirType(dt) } in.addDirValidateType() - // Add the types to the schema // Add the types to the schema for _, v := range in.types { in.result.Schema.Types = append(in.result.Schema.Types, v) diff --git a/core/resolve.go b/core/resolve.go index 678cc402..fc5526f1 100644 --- a/core/resolve.go +++ b/core/resolve.go @@ -16,7 +16,7 @@ type resItem struct { } // newRTMap returns a map of resolver functions -func (gj *GraphjinEngine) newRTMap() map[string]ResolverFn { +func (gj *graphjinEngine) newRTMap() map[string]ResolverFn { return map[string]ResolverFn{ "remote_api": func(v ResolverProps) (Resolver, error) { return newRemoteAPI(v, gj.trace.NewHTTPClient()) @@ -25,7 +25,7 @@ func (gj *GraphjinEngine) newRTMap() map[string]ResolverFn { } // initResolvers initializes the resolvers -func (gj *GraphjinEngine) initResolvers() error { +func (gj *graphjinEngine) initResolvers() error { gj.rmap = make(map[string]resItem) if gj.rtmap == nil { @@ -45,7 +45,7 @@ func (gj *GraphjinEngine) initResolvers() error { } // initRemote initializes the remote resolver -func (gj *GraphjinEngine) initRemote( +func (gj *graphjinEngine) initRemote( rc ResolverConfig, rtmap map[string]ResolverFn, ) error { // Defines the table column to be used as an id in the diff --git a/core/rolestmt.go b/core/rolestmt.go index 61a59aa2..de87f58f 100644 --- a/core/rolestmt.go +++ b/core/rolestmt.go @@ -8,7 +8,7 @@ import ( ) // nolint:errcheck -func (gj *GraphjinEngine) prepareRoleStmt() error { +func (gj *graphjinEngine) prepareRoleStmt() error { if !gj.abacEnabled { return nil } diff --git a/core/subs.go b/core/subs.go index ed149a46..57d43e5b 100644 --- a/core/subs.go +++ b/core/subs.go @@ -91,7 +91,7 @@ func (g *GraphJin) Subscribe( return } - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) // create the request object r := gj.newGraphqlReq(rc, "subscription", h.Name, nil, vars) @@ -121,7 +121,7 @@ func (g *GraphJin) SubscribeByName( vars json.RawMessage, rc *RequestConfig, ) (m *Member, err error) { - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) item, err := gj.allowList.GetByName(name, gj.prod) if err != nil { @@ -135,7 +135,7 @@ func (g *GraphJin) SubscribeByName( } // subscribe function is called on the graphjin struct to subscribe to a query. -func (gj *GraphjinEngine) subscribe(c context.Context, r GraphqlReq) ( +func (gj *graphjinEngine) subscribe(c context.Context, r GraphqlReq) ( m *Member, err error, ) { if r.operation != qcode.QTSubscription { @@ -208,7 +208,7 @@ func (gj *GraphjinEngine) subscribe(c context.Context, r GraphqlReq) ( } // initSub function is called on the graphjin struct to initialize a subscription. -func (gj *GraphjinEngine) initSub(c context.Context, sub *sub) (err error) { +func (gj *graphjinEngine) initSub(c context.Context, sub *sub) (err error) { if err = sub.s.compile(); err != nil { return } @@ -229,7 +229,7 @@ func (gj *GraphjinEngine) initSub(c context.Context, sub *sub) (err error) { } // subController function is called on the graphjin struct to control the subscription. -func (gj *GraphjinEngine) subController(sub *sub) { +func (gj *graphjinEngine) subController(sub *sub) { // remove subscription if controller exists defer gj.subs.Delete(sub.k) @@ -345,7 +345,7 @@ func (s *sub) updateMember(msg mmsg) error { } // fanOutJobs function is called on the sub struct to fan out jobs. -func (s *sub) fanOutJobs(gj *GraphjinEngine) { +func (s *sub) fanOutJobs(gj *graphjinEngine) { switch { case len(s.ids) == 0: return @@ -363,7 +363,7 @@ func (s *sub) fanOutJobs(gj *GraphjinEngine) { } // subCheckUpdates function is called on the graphjin struct to check updates. -func (gj *GraphjinEngine) subCheckUpdates(sub *sub, mv mval, start int) { +func (gj *graphjinEngine) subCheckUpdates(sub *sub, mv mval, start int) { // Do not use the `mval` embedded inside sub since // its not thread safe use the copy `mv mval`. @@ -410,7 +410,6 @@ func (gj *GraphjinEngine) subCheckUpdates(sub *sub, mv mval, start int) { } return }) - if err != nil { gj.log.Printf(errSubs, "query", err) return @@ -442,7 +441,7 @@ func (gj *GraphjinEngine) subCheckUpdates(sub *sub, mv mval, start int) { } // subFirstQuery function is called on the graphjin struct to get the first query. -func (gj *GraphjinEngine) subFirstQuery(sub *sub, m *Member) (mmsg, error) { +func (gj *graphjinEngine) subFirstQuery(sub *sub, m *Member) (mmsg, error) { c := context.Background() // when params are not available we use a more optimized @@ -483,7 +482,7 @@ func (gj *GraphjinEngine) subFirstQuery(sub *sub, m *Member) (mmsg, error) { } // subNotifyMember function is called on the graphjin struct to notify a member. -func (gj *GraphjinEngine) subNotifyMember(s *sub, mv mval, j int, js json.RawMessage) { +func (gj *graphjinEngine) subNotifyMember(s *sub, mv mval, j int, js json.RawMessage) { _, err := gj.subNotifyMemberEx(s, mv.mi[j].dh, mv.mi[j].cindx, @@ -495,7 +494,7 @@ func (gj *GraphjinEngine) subNotifyMember(s *sub, mv mval, j int, js json.RawMes } // subNotifyMemberEx function is called on the graphjin struct to notify a member. -func (gj *GraphjinEngine) subNotifyMemberEx(sub *sub, +func (gj *graphjinEngine) subNotifyMemberEx(sub *sub, dh [32]byte, cindx int, id uint64, rc chan *Result, js json.RawMessage, update bool, ) (mmsg, error) { mm := mmsg{id: id} diff --git a/core/watcher.go b/core/watcher.go index f1bb1478..3f3b58ae 100644 --- a/core/watcher.go +++ b/core/watcher.go @@ -8,7 +8,7 @@ import ( // initDBWatcher initializes the database schema watcher func (g *GraphJin) initDBWatcher() error { - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) // no schema polling in production if gj.prod { @@ -37,7 +37,7 @@ func (g *GraphJin) startDBWatcher(ps time.Duration) { defer ticker.Stop() for range ticker.C { - gj := g.Load().(*GraphjinEngine) + gj := g.Load().(*graphjinEngine) latestDi, err := sdata.GetDBInfo( gj.db,