Merge lp:~xavi-garcia-mena/go-unityscopes/cannedquery-new-methods-v2 into lp:go-unityscopes/v2
- cannedquery-new-methods-v2
- Merge into v2
Proposed by Xavi Garcia
| Status: | Needs review |
|---|---|
| Proposed branch: | lp:~xavi-garcia-mena/go-unityscopes/cannedquery-new-methods-v2 |
| Merge into: | lp:go-unityscopes/v2 |
| Diff against target: | 214 lines (+149/-3) 5 files modified query.cpp (+33/-0) query.go (+41/-0) query_test.go (+69/-1) shim.h (+4/-0) version.h (+2/-2) |
| To merge this branch: | bzr merge lp:~xavi-garcia-mena/go-unityscopes/cannedquery-new-methods-v2 |
| Related bugs: |
| Reviewer | Review Type | Date Requested | Status |
|---|---|---|---|
| dobey (community) | Needs Fixing | ||
| James Henstridge | Approve | ||
| Review via email: | |||
Commit message
Description of the change
New Canned query methods for v2.
This is a port from branch lp:~xavi-garcia-mena/go-unityscopes/cannedquery-new-methods, that fixes conflicts with v2.
To post a comment you must log in.
Revision history for this message
| dobey (dobey) wrote : | # |
Apparently this has been sitting around for about 18 months, and now has a conflict in shim.h. Please fix.
review: Needs Fixing
Unmerged revisions
- 66. By Xavi Garcia
-
Conflicts resolved
- 65. By Xavi Garcia
-
Minimum version required updated
- 64. By Xavi Garcia
-
New canned query methods for v2 added
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
| 1 | === modified file 'query.cpp' |
| 2 | --- query.cpp 2015-02-28 00:44:27 +0000 |
| 3 | +++ query.cpp 2015-05-01 09:01:25 +0000 |
| 4 | @@ -41,6 +41,16 @@ |
| 5 | return as_bytes(json_data, length); |
| 6 | } |
| 7 | |
| 8 | +void canned_query_set_filter_state(_CannedQuery *query, char *json_data, int json_data_length, char **error) { |
| 9 | + try { |
| 10 | + Variant val = Variant::deserialize_json(std::string(json_data, json_data_length)); |
| 11 | + FilterState filter = FilterState::deserialize(val.get_dict()); |
| 12 | + reinterpret_cast<CannedQuery*>(query)->set_filter_state(filter); |
| 13 | + } catch (const std::exception &e) { |
| 14 | + *error = strdup(e.what()); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | char *canned_query_get_query_string(_CannedQuery *query) { |
| 19 | return strdup(reinterpret_cast<CannedQuery*>(query)->query_string().c_str()); |
| 20 | } |
| 21 | @@ -56,3 +66,26 @@ |
| 22 | char *canned_query_to_uri(_CannedQuery *query) { |
| 23 | return strdup(reinterpret_cast<CannedQuery*>(query)->to_uri().c_str()); |
| 24 | } |
| 25 | + |
| 26 | +void *canned_query_get_user_data(_CannedQuery *query, int *data_length, char **error) { |
| 27 | + std::string data; |
| 28 | + try { |
| 29 | + data = reinterpret_cast<CannedQuery*>(query)->user_data().serialize_json(); |
| 30 | + } catch (const std::exception &e) { |
| 31 | + *error = strdup(e.what()); |
| 32 | + } |
| 33 | + return as_bytes(data, data_length); |
| 34 | +} |
| 35 | + |
| 36 | +void canned_query_set_user_data(_CannedQuery *query, char *json_data, int json_data_length, char **error) { |
| 37 | + try { |
| 38 | + Variant value = Variant::deserialize_json(std::string(json_data, json_data_length)); |
| 39 | + reinterpret_cast<CannedQuery*>(query)->set_user_data(value); |
| 40 | + } catch (const std::exception & e) { |
| 41 | + *error = strdup(e.what()); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +int canned_query_has_user_data(_CannedQuery *query) { |
| 46 | + return static_cast<int>(reinterpret_cast<CannedQuery*>(query)->has_user_data()); |
| 47 | +} |
| 48 | |
| 49 | === modified file 'query.go' |
| 50 | --- query.go 2015-02-28 00:44:27 +0000 |
| 51 | +++ query.go 2015-05-01 09:01:25 +0000 |
| 52 | @@ -70,6 +70,17 @@ |
| 53 | return state |
| 54 | } |
| 55 | |
| 56 | +// SetFilterState returns the state of the filters for this canned query. |
| 57 | +func (query *CannedQuery) SetFilterState(filter FilterState) error { |
| 58 | + data, err := json.Marshal(filter) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + var errorString *C.char |
| 63 | + C.canned_query_set_filter_state(query.q, (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), &errorString) |
| 64 | + return checkError(errorString) |
| 65 | +} |
| 66 | + |
| 67 | // SetDepartmentID changes the department ID for this canned query. |
| 68 | func (query *CannedQuery) SetDepartmentID(departmentID string) { |
| 69 | C.canned_query_set_department_id(query.q, unsafe.Pointer(&departmentID)) |
| 70 | @@ -86,3 +97,33 @@ |
| 71 | defer C.free(unsafe.Pointer(s)) |
| 72 | return C.GoString(s) |
| 73 | } |
| 74 | + |
| 75 | +// SetUserData attaches arbitrary data to the query. |
| 76 | +func (query *CannedQuery) SetUserData(v interface{}) error { |
| 77 | + data, err := json.Marshal(v) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + var errorString *C.char |
| 82 | + C.canned_query_set_user_data(query.q, (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), &errorString) |
| 83 | + return checkError(errorString) |
| 84 | +} |
| 85 | + |
| 86 | +// HasUserData checks if user data has been attached to this query. |
| 87 | +func (query *CannedQuery) HasUserData() bool { |
| 88 | + api_ret := C.canned_query_has_user_data(query.q) |
| 89 | + if api_ret == 0 { |
| 90 | + return false |
| 91 | + } else { |
| 92 | + return true |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// UserData gets user data attached to this query. |
| 97 | +func (query *CannedQuery) UserData(v interface{}) error { |
| 98 | + var dataLength C.int |
| 99 | + var errorString *C.char |
| 100 | + scopeData := C.canned_query_get_user_data(query.q, &dataLength, &errorString) |
| 101 | + defer C.free(scopeData) |
| 102 | + return json.Unmarshal(C.GoBytes(scopeData, dataLength), v) |
| 103 | +} |
| 104 | |
| 105 | === modified file 'query_test.go' |
| 106 | --- query_test.go 2015-03-30 10:11:02 +0000 |
| 107 | +++ query_test.go 2015-05-01 09:01:25 +0000 |
| 108 | @@ -23,5 +23,73 @@ |
| 109 | query.SetQueryString("new_query_value") |
| 110 | c.Check(query.QueryString(), Equals, "new_query_value") |
| 111 | |
| 112 | - // TODO FilterState setter |
| 113 | + filter1 := scopes.NewOptionSelectorFilter("f1", "Options", false) |
| 114 | + filter1.AddOption("1", "Option 1") |
| 115 | + filter1.AddOption("2", "Option 2") |
| 116 | + |
| 117 | + fstate := make(scopes.FilterState) |
| 118 | + |
| 119 | + // enable option1 |
| 120 | + filter1.UpdateState(fstate, "1", true) |
| 121 | + _, ok := fstate["f1"] |
| 122 | + c.Check(ok, Equals, true) |
| 123 | + c.Check(filter1.HasActiveOption(fstate), Equals, true) |
| 124 | + |
| 125 | + err := query.SetFilterState(fstate) |
| 126 | + c.Check(err, IsNil) |
| 127 | + |
| 128 | + filter := query.FilterState() |
| 129 | + c.Check(filter["f1"], DeepEquals, []interface{}{"1"}) |
| 130 | + // TODO uncomment this test when filters are merged |
| 131 | + // c.Check(filter1.HasActiveOption(filter), Equals, true) |
| 132 | + |
| 133 | + err = query.SetFilterState(nil) |
| 134 | + c.Check(err, Not(Equals), nil) |
| 135 | +} |
| 136 | + |
| 137 | +func (s *S) TestQueryUserData(c *C) { |
| 138 | + query := scopes.NewCannedQuery("scope", "query_string", "department_string") |
| 139 | + c.Check(query.HasUserData(), Equals, false) |
| 140 | + |
| 141 | + var value interface{} |
| 142 | + err := query.UserData(&value) |
| 143 | + c.Check(err, Not(Equals), nil) |
| 144 | + |
| 145 | + err = query.SetUserData("test_string") |
| 146 | + c.Check(err, IsNil) |
| 147 | + |
| 148 | + err = query.UserData(&value) |
| 149 | + c.Check(err, IsNil) |
| 150 | + c.Check(value, Equals, "test_string") |
| 151 | + |
| 152 | + // check setting a slice |
| 153 | + err = query.SetUserData([]string{"test_string", "test_string2", "test_string3"}) |
| 154 | + c.Check(err, IsNil) |
| 155 | + c.Check(query.HasUserData(), Equals, true) |
| 156 | + |
| 157 | + err = query.UserData(&value) |
| 158 | + c.Check(err, IsNil) |
| 159 | + c.Check(value, DeepEquals, []interface{}{"test_string", "test_string2", "test_string3"}) |
| 160 | + c.Check(query.HasUserData(), Equals, true) |
| 161 | + |
| 162 | + // try passing an unserializable object, we should get an error |
| 163 | + var errorUnserializable unserializable |
| 164 | + err = query.SetUserData(&errorUnserializable) |
| 165 | + c.Check(err, Not(Equals), nil) |
| 166 | + c.Check(err.Error(), Equals, "json: error calling MarshalJSON for type *scopes_test.unserializable: Can not marshal to JSON") |
| 167 | + |
| 168 | + // try passing an unserializable object, we should get an error |
| 169 | + err = query.UserData(&errorUnserializable) |
| 170 | + c.Check(err, Not(Equals), nil) |
| 171 | + c.Check(err.Error(), Equals, "Can not unmarshal from JSON") |
| 172 | + |
| 173 | + err = query.SetUserData(1999) |
| 174 | + c.Check(err, IsNil) |
| 175 | + c.Check(query.HasUserData(), Equals, true) |
| 176 | + |
| 177 | + var int_value int |
| 178 | + err = query.UserData(&int_value) |
| 179 | + c.Check(err, IsNil) |
| 180 | + c.Check(query.HasUserData(), Equals, true) |
| 181 | + c.Check(int_value, Equals, 1999) |
| 182 | } |
| 183 | |
| 184 | === modified file 'shim.h' |
| 185 | --- shim.h 2015-04-17 09:54:06 +0000 |
| 186 | +++ shim.h 2015-05-01 09:01:25 +0000 |
| 187 | @@ -61,9 +61,13 @@ |
| 188 | char *canned_query_get_department_id(_CannedQuery *query); |
| 189 | char *canned_query_get_query_string(_CannedQuery *query); |
| 190 | void *canned_query_get_filter_state(_CannedQuery *query, int *length); |
| 191 | +void canned_query_set_filter_state(_CannedQuery *query, char *json_data, int json_data_length, char **error); |
| 192 | void canned_query_set_department_id(_CannedQuery *query, void *department_id); |
| 193 | void canned_query_set_query_string(_CannedQuery *query, void *query_str); |
| 194 | char *canned_query_to_uri(_CannedQuery *query); |
| 195 | +void *canned_query_get_user_data(_CannedQuery *query, int *data_length, char **error); |
| 196 | +void canned_query_set_user_data(_CannedQuery *query, char *json_data, int json_data_length, char **error); |
| 197 | +int canned_query_has_user_data(_CannedQuery *query); |
| 198 | |
| 199 | /* Category objects */ |
| 200 | void destroy_category_ptr(SharedPtrData data); |
| 201 | |
| 202 | === modified file 'version.h' |
| 203 | --- version.h 2015-04-17 14:32:37 +0000 |
| 204 | +++ version.h 2015-05-01 09:01:25 +0000 |
| 205 | @@ -5,7 +5,7 @@ |
| 206 | |
| 207 | // check that we have a compatible version of lib-unityscopes installed |
| 208 | static_assert(UNITY_SCOPES_VERSION_MAJOR > 0 || |
| 209 | - (UNITY_SCOPES_VERSION_MAJOR == 0 && (UNITY_SCOPES_VERSION_MINOR > 6 || (UNITY_SCOPES_VERSION_MINOR == 6 && UNITY_SCOPES_VERSION_MICRO >= 15))), |
| 210 | - "Version of Unity scopes API mismatch. Minimum required version is 0.6.15."); |
| 211 | + (UNITY_SCOPES_VERSION_MAJOR == 0 && (UNITY_SCOPES_VERSION_MINOR > 6 || (UNITY_SCOPES_VERSION_MINOR == 6 && UNITY_SCOPES_VERSION_MICRO >= 16))), |
| 212 | + "Version of Unity scopes API mismatch. Minimum required version is 0.6.16."); |
| 213 | |
| 214 | #endif |
It looks like this branch needs to bump the minimum libunity-scopes version, due to the use of the CannedQuery user_data feature. I think 0.6.16 is the new minimum:
unity-scopes-api (0.6.16+ 15.04.20150410. 3-0ubuntu1) vivid; urgency=medium
[ Pawel Stolowski ]
* Added support for attaching arbitrary data to CannedQuery.
Other than that, this looks great.