From 432d44cbf59fd95f84863bbfda1af24af61aefe7 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 8 Apr 2019 16:25:31 -0500 Subject: [PATCH] More work on the library json tags for all structs --- _example/main.go | 44 ++++------- bill_node.go | 154 ++++++++++++++++++------------------ contact_detail_node.go | 8 +- division_node.go | 14 ++-- identifier_node.go | 4 +- jurisdiction_node.go | 10 +-- legislative_session_node.go | 12 +-- link_node.go | 4 +- membership_node.go | 24 +++--- name_node.go | 8 +- openstates.go | 49 +++++++++--- organization_node.go | 32 ++++---- person_node.go | 36 ++++----- post_node.go | 20 ++--- 14 files changed, 220 insertions(+), 199 deletions(-) diff --git a/_example/main.go b/_example/main.go index 7f4aec5..143b6bf 100644 --- a/_example/main.go +++ b/_example/main.go @@ -3,40 +3,30 @@ package main import ( "encoding/json" "fmt" + "log" "git.bullercodeworks.com/brian/openstates" - "github.com/machinebox/graphql" ) func main() { - req := graphql.NewRequest(` - query Reps($lat: Float, $long: Float) { - people(latitude: $lat, longitude: $long, first: 100) { - edges { - node { - id - name - sortName - familyName - givenName - image - birthDate - deathDate - createdAt - updatedAt - extras - } - } - } - } - `) - req.Var("lat", 37.700824) - req.Var("long", -97.352906) + //req := graphql.NewRequest(`query Reps($lat:Float,$long:Float){people(latitude:$lat,longitude:$long,first:100){edges{node{id name sortName familyName givenName image birthDate deathDate createdAt updatedAt extras links}}}}`) + //req.Var("lat", 37.700824) + //req.Var("long") var res response o := openstates.NewOpenStatesApi(ApiKey) - o.TurnOnLogging() - o.MakeRequest(req, &res) + o.SetLogger(func(s string) { log.Println(s) }) + props := make(map[string]interface{}) + props["lat"] = 37.700824 + props["long"] = -97.352906 + o.MakeRequest(&res, props) + //o.MakeRequest(req, &res) + //fmt.Println("= Fields =") + //for _, v := range res.People.Edges { + // v.Node.GetFields() + // break + //} + fmt.Println("= Result =") j, _ := json.Marshal(res) fmt.Println(string(j)) } @@ -48,7 +38,7 @@ type people struct { Edges []edge } type edge struct { - Node node + Node openstates.PersonNode } type node struct { Name string diff --git a/bill_node.go b/bill_node.go index 98782ea..db824bd 100644 --- a/bill_node.go +++ b/bill_node.go @@ -1,117 +1,117 @@ package openstates type BillNode struct { - Id string - LegislativeSession *LegislativeSessionNode - Identifier string - Title string - FromOrganization string - Classification []string - Subject []string - Abstracts []BillAbstractNode - OtherTitles []BillTitleNode - OtherIdentifiers []BillIdentifierNode - Actions []BillActionNode - Sponsorships []BillSponsorshipNode - RelatedBills []RelatedBillNode - Versions []BillDocumentNode - Documents []BillDocumentNode - Votes []VoteEventNode - Sources []LinkNode - OpenStatesUrl string - CreatedAt string - UpdatedAt string - Extras string + Id string `json:"id"` + LegislativeSession *LegislativeSessionNode `json:"legislativeSession"` + Identifier string `json:"identifier"` + Title string `json:"title"` + FromOrganization string `json:"fromOrganization"` + Classification []string `json:"classification"` + Subject []string `json:"subject"` + Abstracts []BillAbstractNode `json:"abstracts"` + OtherTitles []BillTitleNode `json:"otherTitles"` + OtherIdentifiers []BillIdentifierNode `json:"otherIdentifiers"` + Actions []BillActionNode `json:"actions"` + Sponsorships []BillSponsorshipNode `json:"sponsorships"` + RelatedBills []RelatedBillNode `json:"relatedBills"` + Versions []BillDocumentNode `json:"versions"` + Documents []BillDocumentNode `json:"documents"` + Votes []VoteEventNode `json:"votes"` + Sources []LinkNode `json:"sources"` + OpenStatesUrl string `json:"openStatesUrl"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + Extras string `json:"extras"` } type BillAbstractNode struct { - Abstract string - Note string - Date string + Abstract string `json:"abstract"` + Note string `json:"note"` + Date string `json:"date"` } type BillTitleNode struct { - Title string - Note string + Title string `json:"title"` + Note string `json:"note"` } type BillIdentifierNode struct { - Identifier string - Scheme string - Note string + Identifier string `json:"identifier"` + Scheme string `json:"scheme"` + Note string `json:"note"` } type BillActionNode struct { - Organization *OrganizationNode - Description string - Date string - Classification []ActionType - Order int - Extras string - Vote *VoteEventNode - RelatedEntities []RelatedEntityNode + Organization *OrganizationNode `json:"organization"` + Description string `json:"description"` + Date string `json:"date"` + Classification []ActionType `json:"classification"` + Order int `json:"order"` + Extras string `json:"extras"` + Vote *VoteEventNode `json:"vote"` + RelatedEntities []RelatedEntityNode `json:"relatedEntities"` } type RelatedEntityNode struct { - Name string - EntityType EntityType - Organization *OrganizationNode - Person *PersonNode + Name string `json:"name"` + EntityType EntityType `json:"entityType"` + Organization *OrganizationNode `json:"organization"` + Person *PersonNode `json:"person"` } type BillSponsorshipNode struct { - Name string - EntityType EntityType - Organization *OrganizationNode - Person *PersonNode - Primary bool - Classification string + Name string `json:"name"` + EntityType EntityType `json:"entityType"` + Organization *OrganizationNode `json:"organization"` + Person *PersonNode `json:"person"` + Primary bool `json:"primary"` + Classification string `json:"classification"` } type RelatedBillNode struct { - Identifier string - LegislativeSession string - RelationType string - RelatedBill *BillNode + Identifier string `json:"identifier"` + LegislativeSession string `json:"legislativeSession"` + RelationType string `json:"relationType"` + RelatedBill *BillNode `json:"relatedBill"` } type BillDocumentNode struct { - Note string - Date string - Links []MimetypeLinkNode + Note string `json:"note"` + Date string `json:"date"` + Links []MimetypeLinkNode `json:"links"` } type MimetypeLinkNode struct { - MediaType string - Url string - Text string + MediaType string `json:"mediaType"` + Url string `json:"url"` + Text string `json:"text"` } type VoteEventNode struct { - Id string - Identifier string - MotionText string - MotionClassification []string - StartDate string - Result string - Organization *OrganizationNode - BillAction *BillActionNode - Votes []PersonVoteNode - Counts []VoteCountNode - Sources []string - CreatedAt string - UpdatedAt string - Extras string + Id string `json:"id"` + Identifier string `json:"identifier"` + MotionText string `json:"motionText"` + MotionClassification []string `json:"motionClassification"` + StartDate string `json:"startDate"` + Result string `json:"result"` + Organization *OrganizationNode `json:"organization"` + BillAction *BillActionNode `json:"billAction"` + Votes []PersonVoteNode `json:"votes"` + Counts []VoteCountNode `json:"counts"` + Sources []string `json:"sources"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + Extras string `json:"extras"` } type PersonVoteNode struct { - Option string - VoterName string - Voter *PersonNode - Note string + Option string `json:"option"` + VoterName string `json:"voterName"` + Voter *PersonNode `json:"voter"` + Note string `json:"note"` } type VoteCountNode struct { - Option string - Value int + Option string `json:"option"` + Value int `json:"value"` } diff --git a/contact_detail_node.go b/contact_detail_node.go index 2c13d04..1dc94c1 100644 --- a/contact_detail_node.go +++ b/contact_detail_node.go @@ -1,8 +1,8 @@ package openstates type ContactDetailNode struct { - Type string - Value string - Note string - Label string + Type string `json:"type"` + Value string `json:"value"` + Note string `json:"note"` + Label string `json:"label"` } diff --git a/division_node.go b/division_node.go index 36b0aff..ad0b656 100644 --- a/division_node.go +++ b/division_node.go @@ -1,13 +1,13 @@ package openstates type DivisionNode struct { - Id string - Name string - Redirect string - Country string - CreatedAt string - UpdatedAt string - Extras string + Id string `json:"id"` + Name string `json:"name"` + Redirect string `json:"redirect"` + Country string `json:"country"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + Extras string `json:"extras"` } const ( diff --git a/identifier_node.go b/identifier_node.go index 1c2b37a..ba104c9 100644 --- a/identifier_node.go +++ b/identifier_node.go @@ -1,6 +1,6 @@ package openstates type IdentifierNode struct { - Identifier string - Scheme string + Identifier string `json:"identifier"` + Scheme string `json:"scheme"` } diff --git a/jurisdiction_node.go b/jurisdiction_node.go index dc11cff..f7ccf77 100644 --- a/jurisdiction_node.go +++ b/jurisdiction_node.go @@ -1,10 +1,10 @@ package openstates type JurisdictionNode struct { - Id string - Name string - Url string + Id string `json:"id"` + Name string `json:"name"` + Url string `json:"url"` // FeatureFlags -- Reserved for future user - LegislativeSessions []LegislativeSessionNode - Organizations []OrganizationNode + LegislativeSessions []LegislativeSessionNode `json:"legislativeSessions"` + Organizations []OrganizationNode `json:"organizations"` } diff --git a/legislative_session_node.go b/legislative_session_node.go index 2c835cc..fb36438 100644 --- a/legislative_session_node.go +++ b/legislative_session_node.go @@ -1,12 +1,12 @@ package openstates type LegislativeSessionNode struct { - Jurisdiction *JurisdictionNode - Identifier string - Name string - Classification string - StartDate string - EndDate string + Jurisdiction *JurisdictionNode `json:"jurisdiction"` + Identifier string `json:"identifier"` + Name string `json:"name"` + Classification string `json:"classification"` + StartDate string `json:"startDate"` + EndDate string `json:"endDate"` } const ( diff --git a/link_node.go b/link_node.go index 5c699b8..3592a23 100644 --- a/link_node.go +++ b/link_node.go @@ -1,6 +1,6 @@ package openstates type LinkNode struct { - Url string - Text string + Url string `json:"url"` + Text string `json:"text"` } diff --git a/membership_node.go b/membership_node.go index 3434cdf..94e2901 100644 --- a/membership_node.go +++ b/membership_node.go @@ -1,16 +1,16 @@ package openstates type MembershipNode struct { - Id string - PersonName string - Person *PersonNode - Organization *OrganizationNode - Post *PostNode - Label string - Role string - StartDate string - EndDate string - CreatedAt string - UpdatedAt string - Extras string + Id string `json:"id"` + PersonName string `json:"personName"` + Person *PersonNode `json:"person"` + Organization *OrganizationNode `json:"organization"` + Post *PostNode `json:"post"` + Label string `json:"label"` + Role string `json:"role"` + StartDate string `json:"startDate"` + EndDate string `json:"endDate"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + Extras string `json:"extras"` } diff --git a/name_node.go b/name_node.go index 50ba4d0..ffaff82 100644 --- a/name_node.go +++ b/name_node.go @@ -1,8 +1,8 @@ package openstates type NameNode struct { - Name string - Note string - StartDate string - EndDate string + Name string `json:"name"` + Note string `json:"note"` + StartDate string `json:"startDate"` + EndDate string `json:"endDate"` } diff --git a/openstates.go b/openstates.go index bf1a119..e5521be 100644 --- a/openstates.go +++ b/openstates.go @@ -2,7 +2,7 @@ package openstates import ( "context" - "log" + "reflect" "github.com/machinebox/graphql" ) @@ -20,17 +20,48 @@ func NewOpenStatesApi(key string) *OpenStatesApi { return &o } -func (o *OpenStatesApi) TurnOnLogging() { - o.Client.Log = func(s string) { log.Println(s) } +func (o *OpenStatesApi) SetLogger(l func(s string)) { + o.Client.Log = l } -func (o *OpenStatesApi) MakeRequest(req *graphql.Request, res interface{}) { +func (o *OpenStatesApi) MakeQueryRequest(query string, vars map[string]interface{}, res interface{}) error { + req := graphql.NewRequest(query) + for k, v := range vars { + req.Var(k, v) + } + return o.MakeRawRequest(req, &res) +} + +func (o *OpenStatesApi) MakeRequest(res interface{}, vars map[string]interface{}) error { + req := o.buildRequest(res, vars) + return o.MakeRawRequest(req, res) +} + +func (o *OpenStatesApi) MakeRawRequest(req *graphql.Request, res interface{}) error { ctx := context.Background() req.Header.Add("X-API-KEY", o.ApiKey) - if err := o.Client.Run(ctx, req, &res); err != nil { - log.Fatal(err) - } + return o.Client.Run(ctx, req, &res) } -//func (o *OpenStatesApi) GetLegislatorsForArea(lat float, long float) []PersonNode { -//} +func (o *OpenStatesApi) buildRequest(res interface{}, vars map[string]interface{}) *graphql.Request { + // TODO: Build query from res + req := graphql.NewRequest(`query Reps($lat:Float,$long:Float){people(latitude:$lat,longitude:$long,first:100){edges{node{id name sortName familyName givenName image birthDate deathDate createdAt updatedAt extras }}}}`) + for k, v := range vars { + req.Var(k, v) + } + return req +} + +func GetFields(in interface{}) []string { + var ret []string + val := reflect.ValueOf(in) + for i := 0; i < val.Type().NumField(); i++ { + t := val.Type().Field(i) + fieldName := t.Name + if jsonTag := t.Tag.Get("json"); jsonTag != "" && jsonTag != "-" { + fieldName = jsonTag + } + ret = append(ret, fieldName) + } + return ret +} diff --git a/organization_node.go b/organization_node.go index 6c8833d..eceb6da 100644 --- a/organization_node.go +++ b/organization_node.go @@ -1,20 +1,20 @@ package openstates type OrganizationNode struct { - Id string - Name string - Image string - Classification string - FoundingDate string - DissolutionDate string - Parent *OrganizationNode - Children []OrganizationNode - CurrentMemberships []MembershipNode - Identifiers []IdentifierNode - OtherNames []NameNode - Links []LinkNode - Sources []string - CreatedAt string - UpdatedAt string - Extras string + Id string `json:"id"` + Name string `json:"name"` + Image string `json:"image"` + Classification string `json:"classification"` + FoundingDate string `json:"foundingDate"` + DissolutionDate string `json:"dissolutionDate"` + Parent *OrganizationNode `json:"parent"` + Children []OrganizationNode `json:"children"` + CurrentMemberships []MembershipNode `json:"currentMemberships"` + Identifiers []IdentifierNode `json:"identifiers"` + OtherNames []NameNode `json:"otherNames"` + Links []LinkNode `json:"links"` + Sources []string `json:"sources"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + Extras string `json:"extras"` } diff --git a/person_node.go b/person_node.go index 8b1ddc1..32f7fe9 100644 --- a/person_node.go +++ b/person_node.go @@ -1,22 +1,22 @@ package openstates type PersonNode struct { - Id string - Name string - SortName string - FamilyName string - GivenName string - Image string - BirthDate string - DeathDate string - Identifiers []IdentifierNode - OtherNames []NameNode - Links []LinkNode - ContactDetails []ContactDetailNode - CurrentMemberships []MembershipNode - OldMemberships []MembershipNode - Sources []LinkNode - CreatedAt string - UpdatedAt string - Extras string + Id string `json:"id"` + Name string `json:"name"` + SortName string `json:"sortName"` + FamilyName string `json:"familyName"` + GivenName string `json:"givenName"` + Image string `json:"image"` + BirthDate string `json:"birthDate"` + DeathDate string `json:"deathDate"` + Identifiers []IdentifierNode `json:"identifiers"` + OtherNames []NameNode `json:"otherNames"` + Links []LinkNode `json:"links"` + ContactDetails []ContactDetailNode `json:"contactDetails"` + CurrentMemberships []MembershipNode `json:"currentMemberships"` + OldMemberships []MembershipNode `json:"oldMemberships"` + Sources []LinkNode `json:"sources"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + Extras string `json:"extras"` } diff --git a/post_node.go b/post_node.go index cf5a8a2..a8d7f14 100644 --- a/post_node.go +++ b/post_node.go @@ -1,14 +1,14 @@ package openstates type PostNode struct { - Id string - Label string - Role string - Division *DivisionNode - StartDate string - EndDate string - MaximumMberships int - CreatedAt string - UpdatedAt string - Extras string + Id string `json:"id"` + Label string `json:"label"` + Role string `json:"role"` + Division *DivisionNode `json:"division"` + StartDate string `json:"startDate"` + EndDate string `json:"endDate"` + MaximumMemberships int `json:"maximumMemberships"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + Extras string `json:"extras"` }