8000 Allow selecting struct columns without breaking up the struct by Learath2 · Pull Request #41 · forcedotcom/go-soql · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow selecting struct columns without breaking up the struct #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const (
TableName = "tableName"
// SelectColumn is the tag to be used for selecting a column in select clause
SelectColumn = "selectColumn"
// SelectSingle is the tag to be used for selecting a column in select clause without breaking a struct
SelectOpaque = "selectOpaque"
// SelectChild is the tag to be used when selecting from child tables
SelectChild = "selectChild"
// FieldName is the parameter to be used to specify the name of the field in underlying SOQL object
Expand Down Expand Up @@ -511,7 +513,8 @@ func mapSelectColumns(mappings map[string]string, parent string, gusParent strin
continue
}
// skip all fields that are not tagged as selectColumn
if getClauseKey(tag) != SelectColumn {
clauseKey := getClauseKey(tag)
if clauseKey != SelectColumn && clauseKey != SelectOpaque {
continue
}

Expand Down Expand Up @@ -947,9 +950,13 @@ func MarshalSelectClause(v interface{}, relationShipName string) (string, error)
}
clauseKey := getClauseKey(clauseTag)
isChildRelation := false
breakStructs := true
switch clauseKey {
case SelectColumn:
isChildRelation = false
case SelectOpaque:
isChildRelation = false
breakStructs = false
case SelectChild:
isChildRelation = true
default:
Expand All @@ -966,7 +973,7 @@ func MarshalSelectClause(v interface{}, relationShipName string) (string, error)
}
buff.WriteString(subStr)
} else {
if field.Type.Kind() == reflect.Struct {
if field.Type.Kind() == reflect.Struct && breakStructs {
v := reflect.New(field.Type)
subStr, err := MarshalSelectClause(v.Elem().Interface(), prefix+fieldName)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions marshaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,17 @@ var _ = Describe("Marshaller", func() {
Expect(clause).To(Equal("Major_OS_Version__c DESC,Num_of_CPU_Cores__c ASC,Physical_CPU_Count__c DESC,Last_Restart__c ASC"))
})
})

Context("when an Order slice referring to a SelectOpaque field is passed", func() {
It("returns a valid order clause", func() {
col1 := Order{Field: "OpaqueField", IsDesc: true}
clause, err := MarshalOrderByClause([]Order{col1}, struct {
OpaqueField OpaqueStructSoql `soql:"selectOpaque,fieldName=Role__c"`
}{})
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal("Role__c DESC"))
})
})
})

Context("when invalid order by is passed as argument", func() {
Expand Down Expand Up @@ -1096,6 +1107,22 @@ var _ = Describe("Marshaller", func() {
Expect(str).To(Equal("Id,Name__c,NonNestedStruct__r.Name,NonNestedStruct__r.SomeValue__c"))
})
})

Context("when nested struct with soql fields is selected opaquely", func() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that verifies the entire SOQL generation? Basically add a Context to Describe("Marshal")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into it 👍

It("returns properly resolved list of field names without breaking up dontbreak", func() {
str, err := MarshalSelectClause(NestedStructWithOpaqueSoql{}, "")
Expect(err).ToNot(HaveOccurred())
Expect(str).To(Equal("Id,Name__c,DontBreak__c"))
})
})

Context("when nested struct with no soql fields is selected opaquely", func() {
It("returns properly resolved list of field names without breaking up nobreak", func() {
str, err := MarshalSelectClause(NestedStructWithOpaqueNonSoql{}, "")
Expect(err).ToNot(HaveOccurred())
Expect(str).To(Equal("Id,Name__c,DontBreak__c"))
})
})
})

Context("when relationship name is passed", func() {
Expand Down
20 changes: 20 additions & 0 deletions soql_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ type NestedStruct struct {
NonNestedStruct NonNestedStruct `soql:"selectColumn,fieldName=NonNestedStruct__r"`
}

type OpaqueStructSoql struct {
Role string `soql:"selectColumn,fieldName=Role"`
}

type OpaqueStructNonSoql struct {
Role string
}

type NestedStructWithOpaqueSoql struct {
ID string `soql:"selectColumn,fieldName=Id"`
Name string `soql:"selectColumn,fieldName=Name__c"`
DontBreak OpaqueStructSoql `soql:"selectOpaque,fieldName=DontBreak__c"`
}

type NestedStructWithOpaqueNonSoql struct {
ID string `soql:"selectColumn,fieldName=Id"`
Name string `soql:"selectColumn,fieldName=Name__c"`
DontBreak OpaqueStructNonSoql `soql:"selectOpaque,fieldName=DontBreak__c"`
}

type TestChildStruct struct {
SelectClause ChildStruct `soql:"selectClause,tableName=SM_Application_Versions__c"`
WhereClause ChildQueryCriteria `soql:"whereClause"`
Expand Down
0