├── .gitignore
├── .travis.yml
├── LICENSE
├── Makefile
├── README.md
├── cache
├── cache.go
└── cache_test.go
├── config
├── README.md
├── config.go
└── config_test.go
├── doc
├── log_test.png
└── logo.png
├── example
└── log.go
├── go.mod
├── go.sum
├── http
├── client
│ ├── client.go
│ └── client_test.go
└── server
│ ├── common.go
│ ├── handler.go
│ ├── reflect.go
│ ├── reflect_test.go
│ ├── vars.go
│ └── vars_test.go
├── log
├── README.md
├── context.go
├── level.go
├── log.go
├── log_test.go
└── writer.go
├── main.go
├── meta
└── common.go
├── orm
├── db.go
├── orm.go
└── orm_test.go
├── project.ini
├── skiplist
├── skiplist.go
└── skiplist_test.go
├── util
├── aes
│ ├── ecb.go
│ └── ecb_test.go
└── str
│ └── str.go
├── uuid
├── uuid.go
└── uuid_test.go
└── validation
├── util.go
├── validation.go
├── validation_test.go
└── validators.go
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled Object files, Static and Dynamic libs (Shared Objects)
2 | *.o
3 | *.a
4 | *.so
5 |
6 | # Folders
7 | _obj
8 | _test
9 | bin
10 | .idea
11 |
12 | # Architecture specific extensions/prefixes
13 | *.[568vq]
14 | [568vq].out
15 |
16 | *.cgo1.go
17 | *.cgo2.c
18 | _cgo_defun.c
19 | _cgo_gotypes.go
20 | _cgo_export.*
21 |
22 | _testmain.go
23 |
24 | *.exe
25 | *.test
26 | *.prof
27 |
28 | tags
29 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: go
2 |
3 | go:
4 | - 1.16.x
5 |
6 |
7 | before_install:
8 | - go get -t -v ./...
9 |
10 | script:
11 | - go test -race -coverprofile=coverage.txt -covermode=atomic
12 |
13 | after_success:
14 | - bash <(curl -s https://codecov.io/bash)
15 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | all: crab
2 |
3 | SOURCE_PATH := orm validation cache log util http
4 |
5 | golint:
6 | go install golang.org/x/lint/golint@latest
7 |
8 | staticcheck:
9 | go install honnef.co/go/tools/cmd/staticcheck@latest
10 |
11 | lint: golint staticcheck
12 | @for path in $(SOURCE_PATH); do echo "golint $$path"; golint $$path"/..."; done;
13 | @for path in $(SOURCE_PATH); do echo "gofmt -s -l -w $$path"; gofmt -s -l -w $$path; done;
14 | go vet ./...
15 | staticcheck ./...
16 |
17 | clean:
18 | @rm -rf bin
19 |
20 | fmt:
21 | @for path in $(SOURCE_PATH); do echo "gofmt -s -l -w $$path"; gofmt -s -l -w $$path; done;
22 |
23 | crab: lint
24 | @for path in $(SOURCE_PATH); do echo "go test ./$$path"; go test "./"$$path/...; done;
25 |
26 |
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Crab 开发必备库
2 | 
3 |
4 | [](https://codecov.io/gh/dearcode/crab)
5 | # config
6 | 加载ini格式的配置文件, 支持以;或者#开头的注释
7 | ```go
8 | type testConf struct {
9 | DB struct {
10 | Domain string
11 | Port int `default:"9088"`
12 | Enable bool
13 | }
14 | aaa int
15 | }
16 |
17 | var conf testConf
18 | if err := LoadConfig(path, &conf); err != nil {
19 | t.Fatalf(errors.ErrorStack(err))
20 | }
21 | t.Logf("conf:%+v", conf)
22 |
23 | ```
24 | 配置文件
25 | ```ini
26 | [db]
27 | domain =jd.com
28 | enable=true
29 | # test comments
30 | ;port=3306
31 | ```
32 | 只要传入对应的ini文件全路径及struct指针就可以了,简单高效.
33 | 运行结果:
34 | ```go
35 | conf:{DB:{Domain:jd.com Port:9088 Enable:true} aaa:0}
36 | ```
37 | `注意`:只会解析有访问权限的变量(大写)
38 |
39 |
40 | # handler
41 | 简单高效的HTTP路由,支持指定接口函数,支持自动注册接口
42 | 指定接口注册示例:
43 | ```go
44 | handler.Server.AddHandler(handler.GET, "/test/", false, onTestGet)
45 | handler.Server.AddHandler(handler.POST, "/test/", false, onTestPost)
46 | handler.Server.AddHandler(handler.DELETE, "/test/", false, onTestDelete)
47 | ```
48 | 自动注册接口示例:
49 | ```go
50 | //以包名为路径
51 | handler.Server.AddInterface(&user{}, "")
52 | //指定path
53 | handler.Server.AddInterface(&user{}, "/api/user/")
54 |
55 | type user struct {
56 | }
57 |
58 | //DoGet 默认get方法
59 | func (u *user) DoGet(w http.ResponseWriter, r *http.Request) {
60 | w.Write([]byte("Get user"))
61 | }
62 |
63 | //DoPost 默认post方法
64 | func (u *user) DoPost(w http.ResponseWriter, r *http.Request) {
65 | w.Write([]byte("Post user"))
66 | }
67 | ```
68 |
69 | # orm
70 | 只支持mysql
71 | 查询示例
72 | ```go
73 | result := struct {
74 | ID int64
75 | User string
76 | Password string
77 | }{}
78 |
79 | if err = NewStmt(db, "userinfo").Where("id=2").Query(&result); err != nil {
80 | t.Fatal(err.Error())
81 | }
82 |
83 | ```
84 | 修改示例
85 | ```go
86 | data := struct {
87 | User string
88 | Password string
89 | }{
90 | User: fmt.Sprintf("new_user_%d", time.Now().Unix()),
91 | Password: fmt.Sprintf("new_password_%d", time.Now().Unix()),
92 | }
93 |
94 | id, err := NewStmt(db, "userinfo").Where("id=2").Update(&data)
95 | if err != nil {
96 | t.Fatal(err.Error())
97 | }
98 |
99 | ```
100 | 添加示例
101 | ```go
102 | data := struct {
103 | ID int64 `db_defult:"auto"`
104 | User string
105 | Password string
106 | }{
107 | User: fmt.Sprintf("user_%d", time.Now().Unix()),
108 | Password: fmt.Sprintf("password_%d", time.Now().Unix()),
109 | }
110 |
111 | id, err := NewStmt(db, "userinfo").Insert(&data)
112 | if err != nil {
113 | t.Fatal(err.Error())
114 | }
115 |
116 | ```
117 |
118 |
119 |
120 |
--------------------------------------------------------------------------------
/cache/cache.go:
--------------------------------------------------------------------------------
1 | package cache
2 |
3 | import (
4 | "container/list"
5 | "sync"
6 | "time"
7 | )
8 |
9 | // Cache 根据key索引的cache
10 | type Cache struct {
11 | timeout int64
12 | vars map[string]*cacheEntry
13 | ll *list.List
14 | mu sync.RWMutex
15 | }
16 |
17 | type cacheEntry struct {
18 | key string
19 | last int64
20 | val interface{}
21 | le *list.Element
22 | }
23 |
24 | // NewCache 生成一个lruCache,超时单位是秒
25 | func NewCache(timeout int64) *Cache {
26 | return &Cache{timeout: timeout, vars: make(map[string]*cacheEntry), ll: list.New()}
27 | }
28 |
29 | // Get get from cache and remove expired key.
30 | func (c *Cache) Get(key string) interface{} {
31 | c.evict()
32 |
33 | c.mu.RLock()
34 | if v, ok := c.vars[key]; ok {
35 | c.mu.RUnlock()
36 | return v.val
37 | }
38 | c.mu.RUnlock()
39 | return nil
40 | }
41 |
42 | // Add add to cache and remove expired key.
43 | func (c *Cache) Add(key string, val interface{}) {
44 | c.mu.Lock()
45 | if v, ok := c.vars[key]; ok {
46 | v.val = val
47 | c.mu.Unlock()
48 | return
49 | }
50 |
51 | v := &cacheEntry{key: key, val: val, last: time.Now().Unix()}
52 | v.le = c.ll.PushFront(v)
53 | c.vars[key] = v
54 | c.mu.Unlock()
55 |
56 | }
57 |
58 | func (c *Cache) evict() {
59 | last := time.Now().Unix() - c.timeout
60 | c.mu.Lock()
61 | for b := c.ll.Back(); b != nil; b = c.ll.Back() {
62 | e := b.Value.(*cacheEntry)
63 | if last < e.last {
64 | break
65 | }
66 | c.ll.Remove(b)
67 | delete(c.vars, e.key)
68 | }
69 | c.mu.Unlock()
70 | }
71 |
--------------------------------------------------------------------------------
/cache/cache_test.go:
--------------------------------------------------------------------------------
1 | package cache
2 |
3 | import (
4 | "testing"
5 | "time"
6 | )
7 |
8 | func TestCacheActive(t *testing.T) {
9 | data := struct {
10 | User string
11 | Pass string
12 | }{
13 | "test@mailchina.org",
14 | "password",
15 | }
16 | c := NewCache(2)
17 |
18 | c.Add("1", &data)
19 | c.Get("1")
20 | time.Sleep(time.Second)
21 | val := c.Get("1")
22 | if val == nil {
23 | t.Fatalf("not found, expect %v", data)
24 | }
25 | }
26 |
27 | func TestCacheInactive(t *testing.T) {
28 | data := struct {
29 | User string
30 | Pass string
31 | }{
32 | "test@mailchina.org",
33 | "password",
34 | }
35 | c := NewCache(2)
36 |
37 | c.Add("1", &data)
38 | c.Get("1")
39 |
40 | time.Sleep(time.Second * 2)
41 |
42 | val := c.Get("1")
43 | if val != nil {
44 | t.Fatalf("expect not found")
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/config/README.md:
--------------------------------------------------------------------------------
1 | ## Config
2 | 支持加载ini格式的配置文件
3 | 支持以;或者#开头的注释
4 |
5 | ### example
6 | ```go
7 | type testConf struct {
8 | DB struct {
9 | Domain string
10 | Port int `default:"9088"`
11 | Enable bool
12 | }
13 | aaa int
14 | }
15 |
16 | var conf testConf
17 | if err := LoadConfig(path, &conf); err != nil {
18 | t.Fatalf(errors.ErrorStack(err))
19 | }
20 | t.Logf("conf:%+v", conf)
21 |
22 | ```
23 | 配置文件
24 | ```ini
25 | [db]
26 | domain =jd.com
27 | enable=true
28 | # test comments
29 | ;port=3306
30 | ```
31 | 只要传入对应的ini文件全路径及struct指针就可以了,简单高效.
32 | 运行结果:
33 | ```go
34 | conf:{DB:{Domain:jd.com Port:9088 Enable:true} aaa:0}
35 | ```
36 |
37 |
38 |
--------------------------------------------------------------------------------
/config/config.go:
--------------------------------------------------------------------------------
1 | package config
2 |
3 | import (
4 | "fmt"
5 | "os"
6 | "reflect"
7 | "strconv"
8 | "strings"
9 |
10 | "github.com/juju/errors"
11 | )
12 |
13 | var (
14 | //ErrUnsupported 不支持类型
15 | ErrUnsupported = fmt.Errorf("expect ptr data")
16 | //ErrInvalidType 参数必须为指针类型
17 | ErrInvalidType = fmt.Errorf("result must be ptr")
18 | // ErrNotFound 没找到key
19 | ErrNotFound = fmt.Errorf("key not found")
20 | )
21 |
22 | // Config 读ini配置文件.
23 | type Config struct {
24 | kv map[string]string
25 | }
26 |
27 | func configSplit(raw, sep string) []string {
28 | if i := strings.Index(raw, sep); i > 0 {
29 | return []string{strings.TrimSpace(raw[:i]), strings.TrimSpace(raw[i+1:])}
30 | }
31 | return []string{raw}
32 | }
33 |
34 | // NewConfig 加载配置文件.
35 | func NewConfig(path, body string) (c *Config, err error) {
36 | if path != "" {
37 | dat, err := os.ReadFile(path)
38 | if err != nil {
39 | return nil, err
40 | }
41 | body = string(dat)
42 | }
43 |
44 | c = &Config{kv: make(map[string]string)}
45 | s := ""
46 |
47 | for _, line := range strings.Split(body, "\n") {
48 | line = strings.TrimSpace(line)
49 | if len(line) < 3 || line[0] == ';' || line[0] == '#' {
50 | continue
51 | }
52 |
53 | if line[0] == '[' && line[len(line)-1] == ']' {
54 | s = line[1 : len(line)-1]
55 | continue
56 | }
57 |
58 | kv := configSplit(line, "=")
59 | if len(kv) == 2 {
60 | key := makeKey(s, kv[0])
61 | c.kv[key] = kv[1]
62 | }
63 | }
64 |
65 | return c, nil
66 | }
67 |
68 | func makeKey(s, k string) string {
69 | return strings.ToLower(strings.TrimSpace(s) + "/" + strings.TrimSpace(k))
70 | }
71 |
72 | // GetData 获取指定段的指定key的值, 支持int,string.
73 | func (c *Config) GetData(s, k string, result interface{}, d interface{}) error {
74 | rt := reflect.TypeOf(result)
75 | if rt.Kind() != reflect.Ptr {
76 | return errors.Trace(ErrInvalidType)
77 | }
78 | rt = rt.Elem()
79 | rv := reflect.ValueOf(result).Elem()
80 |
81 | key := makeKey(s, k)
82 |
83 | v, ok := c.kv[key]
84 | if !ok {
85 | //没有对应的key, 这时候要看看有没有default.
86 | if d == nil {
87 | return errors.Annotatef(ErrNotFound, "%v->%v", s, k)
88 | }
89 | rv.Set(reflect.ValueOf(d))
90 | return nil
91 | }
92 |
93 | switch rt.Kind() {
94 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
95 | data, err := strconv.ParseUint(v, 10, 64)
96 | if err != nil {
97 | return errors.Trace(err)
98 | }
99 | rv.SetUint(data)
100 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
101 | data, err := strconv.ParseInt(v, 10, 64)
102 | if err != nil {
103 | return errors.Trace(err)
104 | }
105 | rv.SetInt(data)
106 |
107 | case reflect.String:
108 | rv.SetString(v)
109 |
110 | case reflect.Bool:
111 | data, err := strconv.ParseBool(v)
112 | if err != nil {
113 | return errors.Trace(err)
114 | }
115 | rv.SetBool(data)
116 | case reflect.Float32, reflect.Float64:
117 | data, err := strconv.ParseFloat(v, 32)
118 | if err != nil {
119 | return errors.Trace(err)
120 | }
121 | rv.SetFloat(data)
122 | default:
123 | return errors.Trace(ErrUnsupported)
124 | }
125 |
126 | return nil
127 | }
128 |
129 | // ParseConfig 解析内存中配置文件.
130 | func ParseConfig(body string, result interface{}) error {
131 | c, err := NewConfig("", body)
132 | if err != nil {
133 | return errors.Trace(err)
134 | }
135 | return c.Parse(result)
136 | }
137 |
138 | // LoadConfig 加载文件形式配置文件, 并解析成指定结构.
139 | func LoadConfig(path string, result interface{}) error {
140 | c, err := NewConfig(path, "")
141 | if err != nil {
142 | return errors.Trace(err)
143 | }
144 | return c.Parse(result)
145 | }
146 |
147 | func (c Config) getDefault(k reflect.Kind, v string) (interface{}, error) {
148 | switch k {
149 | case reflect.Uint:
150 | d, err := strconv.ParseUint(v, 10, 64)
151 | if err != nil {
152 | return nil, errors.Trace(err)
153 | }
154 | return uint(d), nil
155 | case reflect.Uint8:
156 | d, err := strconv.ParseUint(v, 10, 64)
157 | if err != nil {
158 | return nil, errors.Trace(err)
159 | }
160 | return uint8(d), nil
161 | case reflect.Uint16:
162 | d, err := strconv.ParseUint(v, 10, 64)
163 | if err != nil {
164 | return nil, errors.Trace(err)
165 | }
166 | return uint16(d), nil
167 | case reflect.Uint32:
168 | d, err := strconv.ParseUint(v, 10, 64)
169 | if err != nil {
170 | return nil, errors.Trace(err)
171 | }
172 | return uint32(d), nil
173 | case reflect.Uint64:
174 | return strconv.ParseUint(v, 10, 64)
175 | case reflect.Int:
176 | d, err := strconv.ParseInt(v, 10, 64)
177 | if err != nil {
178 | return nil, errors.Trace(err)
179 | }
180 | return int(d), nil
181 | case reflect.Int8:
182 | d, err := strconv.ParseInt(v, 10, 64)
183 | if err != nil {
184 | return nil, errors.Trace(err)
185 | }
186 | return uint8(d), nil
187 | case reflect.Int16:
188 | d, err := strconv.ParseInt(v, 10, 64)
189 | if err != nil {
190 | return nil, errors.Trace(err)
191 | }
192 | return uint16(d), nil
193 | case reflect.Int32:
194 | d, err := strconv.ParseInt(v, 10, 64)
195 | if err != nil {
196 | return nil, errors.Trace(err)
197 | }
198 | return uint32(d), nil
199 | case reflect.Int64:
200 | return strconv.ParseInt(v, 10, 64)
201 | case reflect.String:
202 | return v, nil
203 | case reflect.Bool:
204 | return strconv.ParseBool(v)
205 | case reflect.Float32:
206 | d, err := strconv.ParseFloat(v, 32)
207 | if err != nil {
208 | return nil, errors.Trace(err)
209 | }
210 | return float32(d), nil
211 | case reflect.Float64:
212 | return strconv.ParseFloat(v, 32)
213 |
214 | default:
215 | return nil, ErrUnsupported
216 | }
217 | }
218 |
219 | // Parse 根据result结构读配置文件.
220 | func (c *Config) Parse(result interface{}) error {
221 | rt := reflect.TypeOf(result)
222 | rv := reflect.ValueOf(result)
223 |
224 | if rt.Kind() != reflect.Ptr {
225 | return errors.Trace(ErrInvalidType)
226 | }
227 |
228 | //去指针
229 | for rt.Kind() == reflect.Ptr {
230 | rt = rt.Elem()
231 | rv = rv.Elem()
232 | }
233 |
234 | //只有两层
235 | for i := 0; i < rt.NumField(); i++ {
236 | f := rt.Field(i)
237 | if f.PkgPath != "" && !f.Anonymous { // unexported
238 | continue
239 | }
240 | ft := rt.Field(i).Type
241 | fv := rv.Field(i)
242 | if f.Type.Kind() == reflect.Ptr {
243 | fv = fv.Elem()
244 | ft = ft.Elem()
245 | }
246 |
247 | if ft.Kind() != reflect.Struct {
248 | continue
249 | }
250 |
251 | segment := f.Name
252 | if name := f.Tag.Get("cfg_key"); name != "" {
253 | segment = name
254 | }
255 |
256 | for j := 0; j < ft.NumField(); j++ {
257 | sf := ft.Field(j)
258 | if sf.PkgPath != "" && !sf.Anonymous { // unexported
259 | continue
260 | }
261 |
262 | sfv := fv.Field(j)
263 | if sf.Type.Kind() == reflect.Ptr {
264 | sfv = reflect.New(sfv.Elem().Type())
265 | fv.Field(j).Set(sfv)
266 | }
267 |
268 | var cfgDefault interface{}
269 | if v := sf.Tag.Get("cfg_default"); v != "" {
270 | d, err := c.getDefault(sf.Type.Kind(), v)
271 | if err != nil {
272 | return errors.Trace(err)
273 | }
274 | cfgDefault = d
275 | }
276 |
277 | key := sf.Name
278 | if name := sf.Tag.Get("cfg_key"); name != "" {
279 | key = name
280 | }
281 |
282 | if err := c.GetData(segment, key, sfv.Addr().Interface(), cfgDefault); err != nil {
283 | return errors.Trace(err)
284 | }
285 | }
286 | }
287 |
288 | return nil
289 | }
290 |
--------------------------------------------------------------------------------
/config/config_test.go:
--------------------------------------------------------------------------------
1 | package config
2 |
3 | import (
4 | "os"
5 | "testing"
6 |
7 | "github.com/juju/errors"
8 | )
9 |
10 | var (
11 | c *Config
12 | data = `
13 | [db]
14 | domain =mailchina.org
15 | db_enable=true
16 | # test comments
17 | ;port=3306
18 | [api]
19 | url=http://baiud.com/
20 | enable=T
21 | headersize=123
22 | `
23 | )
24 |
25 | func TestMain(main *testing.M) {
26 | nc, err := NewConfig("", data)
27 | if err != nil {
28 | panic(err.Error())
29 | }
30 | c = nc
31 |
32 | os.Exit(main.Run())
33 | }
34 |
35 | func TestConfigString(t *testing.T) {
36 | var domain string
37 | if err := c.GetData("db", "domain", &domain, "test.db.com"); err != nil {
38 | t.Fatalf(err.Error())
39 | }
40 | t.Logf("domain:%v", domain)
41 | }
42 |
43 | func TestConfigInt(t *testing.T) {
44 | var port int
45 | if err := c.GetData("db", "port", &port, 3359); err != nil {
46 | t.Fatalf(err.Error())
47 | }
48 | t.Logf("port:%d", port)
49 | }
50 |
51 | func TestConfigBool(t *testing.T) {
52 | var enable bool
53 | if err := c.GetData("db", "enable", &enable, false); err != nil {
54 | t.Fatalf(err.Error())
55 | }
56 | t.Logf("enable:%v", enable)
57 | }
58 |
59 | type testConf struct {
60 | DB struct {
61 | Domain string
62 | Port int `cfg_default:"9088"`
63 | Enable bool `cfg_key:"db_enable"`
64 | }
65 |
66 | API struct {
67 | URL string
68 | Enable bool
69 | HeaderSize int
70 | }
71 | }
72 |
73 | func TestConfigStruct(t *testing.T) {
74 | var conf testConf
75 | if err := ParseConfig(data, &conf); err != nil {
76 | t.Fatalf(errors.ErrorStack(err))
77 | }
78 | t.Logf("conf:%+v", conf)
79 | }
80 |
--------------------------------------------------------------------------------
/doc/log_test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dearcode/crab/7fcf17a3684cf49537d14477c3f013e4abe59672/doc/log_test.png
--------------------------------------------------------------------------------
/doc/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dearcode/crab/7fcf17a3684cf49537d14477c3f013e4abe59672/doc/logo.png
--------------------------------------------------------------------------------
/example/log.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "time"
5 |
6 | "dearcode.net/crab/log"
7 | )
8 |
9 | func main() {
10 | log.Debug("default log begin")
11 | log.Infof("%v test log", time.Now())
12 |
13 | l := log.NewLogger()
14 | l.Debug("logger 1111111111")
15 | l.Info("logger 2222222222")
16 | l.Warningf("logger 33333 %v", time.Now())
17 | l.Errorf("logger color %v xxxxxx", time.Now().UnixNano())
18 |
19 | //关闭颜色显示
20 | l.SetColor(false)
21 |
22 | l.Errorf("logger no color %v yyyyyy", time.Now().UnixNano())
23 | log.Infof("%v default has color test log", time.Now())
24 |
25 | //指定输出文件
26 | l.SetOutputFile("./vvv.log").SetRolling(true)
27 | l.Info(time.Now())
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module dearcode.net/crab
2 |
3 | go 1.20
4 |
5 | require (
6 | github.com/go-sql-driver/mysql v1.7.1
7 | github.com/google/btree v1.1.2
8 | github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f
9 | github.com/juju/errors v1.0.0
10 | gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0
11 | )
12 |
13 | require (
14 | github.com/fatih/color v1.15.0 // indirect
15 | github.com/mattn/go-colorable v0.1.13 // indirect
16 | github.com/mattn/go-isatty v0.0.17 // indirect
17 | golang.org/x/sys v0.6.0 // indirect
18 | )
19 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
2 | github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
3 | github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
4 | github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
5 | github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
6 | github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
7 | github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
8 | github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
9 | github.com/juju/errors v1.0.0 h1:yiq7kjCLll1BiaRuNY53MGI0+EQ3rF6GB+wvboZDefM=
10 | github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5Qe8=
11 | github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
12 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
13 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
14 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
15 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
16 | github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
17 | github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
18 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19 | golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
20 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21 | gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 h1:FVCohIoYO7IJoDDVpV2pdq7SgrMH6wHnuTyrdrxJNoY=
22 | gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw=
23 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
24 |
--------------------------------------------------------------------------------
/http/client/client.go:
--------------------------------------------------------------------------------
1 | package client
2 |
3 | import (
4 | "bytes"
5 | "compress/gzip"
6 | "encoding/json"
7 | "fmt"
8 | "io"
9 | "net"
10 | "net/http"
11 | "strings"
12 | "time"
13 |
14 | "github.com/juju/errors"
15 |
16 | "dearcode.net/crab/log"
17 | )
18 |
19 | // HTTPClient 带超时重试控制的http客户端.
20 | type HTTPClient struct {
21 | retryTimes int
22 | ignoreStatusCheck bool //忽略http status code(返回非200的不按错误处理)继续解析返回内容
23 | timeout time.Duration
24 | client http.Client
25 | logger *log.Logger
26 | }
27 |
28 | // StatusError http错误.
29 | type StatusError struct {
30 | Code int
31 | Message string
32 | }
33 |
34 | func (se *StatusError) Error() string {
35 | return fmt.Sprintf("HTTP Status %v %s", se.Code, se.Message)
36 | }
37 |
38 | func (c *HTTPClient) dial(network, addr string) (net.Conn, error) {
39 | var conn net.Conn
40 | var err error
41 |
42 | for i := 0; i < c.retryTimes; i++ {
43 | conn, err = net.DialTimeout(network, addr, c.timeout)
44 | if err == nil {
45 | break
46 | }
47 | c.logger.Errorf("DialTimeout %s:%s error:%v retry:%v", network, addr, err, i+1)
48 | }
49 |
50 | if err != nil {
51 | c.logger.Errorf("DialTimeout %s:%s error:%v", network, addr, err)
52 | return nil, errors.Trace(err)
53 | }
54 |
55 | deadline := time.Now().Add(c.timeout)
56 | if err = conn.SetDeadline(deadline); err != nil {
57 | c.logger.Errorf("SetDeadline %s:%s", network, addr)
58 | conn.Close()
59 | return nil, errors.Trace(err)
60 | }
61 |
62 | return conn, nil
63 | }
64 |
65 | const (
66 | defaultRetryTimes = 3
67 | defaultTimeout = 300
68 | )
69 |
70 | // New 创建一个带超时和重试控制的http client, 单位秒.
71 | func New() *HTTPClient {
72 | hc := &HTTPClient{
73 | client: http.Client{},
74 | timeout: time.Duration(defaultTimeout) * time.Second,
75 | retryTimes: defaultRetryTimes,
76 | }
77 |
78 | hc.client.Transport = &http.Transport{Dial: hc.dial}
79 | return hc
80 | }
81 |
82 | // Timeout 设置超时时间,单位:秒, 默认300秒.
83 | func (c *HTTPClient) Timeout(t int) *HTTPClient {
84 | c.timeout = time.Duration(t) * time.Second
85 | return c
86 | }
87 |
88 | // IgnoreStatusCheck 忽略http返回状态(200)检测.
89 | func (c *HTTPClient) IgnoreStatusCheck() *HTTPClient {
90 | c.ignoreStatusCheck = true
91 | return c
92 | }
93 |
94 | // SetLogger 开启日志.
95 | func (c *HTTPClient) SetLogger(l *log.Logger) *HTTPClient {
96 | c.logger = l
97 | return c
98 | }
99 |
100 | // RetryTimes 设置连接重试次数,默认为3次
101 | func (c *HTTPClient) RetryTimes(t int) *HTTPClient {
102 | c.retryTimes = t
103 | return c
104 | }
105 |
106 | func (c HTTPClient) do(method, url string, headers map[string]string, body []byte) ([]byte, error) {
107 | req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
108 | if err != nil {
109 | return nil, errors.Trace(err)
110 | }
111 |
112 | for k, v := range headers {
113 | req.Header.Set(k, v)
114 | }
115 |
116 | resp, err := c.client.Do(req)
117 | if err != nil {
118 | return nil, errors.Trace(err)
119 | }
120 | defer resp.Body.Close()
121 |
122 | data, err := io.ReadAll(resp.Body)
123 | if err != nil {
124 | return nil, errors.Trace(err)
125 | }
126 |
127 | if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") || strings.Contains(resp.Header.Get("Content-Type"), "gzip") {
128 | gr, err := gzip.NewReader(bytes.NewBuffer(data))
129 | if err != nil {
130 | return nil, errors.Trace(err)
131 | }
132 | defer gr.Close()
133 | data, err = io.ReadAll(gr)
134 | if err != nil {
135 | return nil, errors.Trace(err)
136 | }
137 | }
138 |
139 | if c.ignoreStatusCheck {
140 | return data, nil
141 | }
142 |
143 | if resp.StatusCode != http.StatusOK {
144 | return data, errors.Trace(&StatusError{Code: resp.StatusCode, Message: string(data)})
145 | }
146 |
147 | return data, nil
148 | }
149 |
150 | // Get 发送Get请求.
151 | func (c HTTPClient) Get(url string, headers map[string]string, body []byte) ([]byte, error) {
152 | return c.do("GET", url, headers, body)
153 | }
154 |
155 | // GetJSON 发送Get请求, 并解析返回json.
156 | func (c HTTPClient) GetJSON(url string, headers map[string]string, resp interface{}) error {
157 | buf, err := c.do("GET", url, headers, nil)
158 | if err != nil {
159 | return errors.Trace(err)
160 | }
161 | c.logger.Debugf("url:%v, resp:%s", url, buf)
162 | return errors.Trace(json.Unmarshal(buf, resp))
163 | }
164 |
165 | // Post 发Post请求.
166 | func (c HTTPClient) Post(url string, headers map[string]string, body []byte) ([]byte, error) {
167 | return c.do("POST", url, headers, body)
168 | }
169 |
170 | // PostJSON 发送json结构数据请求,并解析返回结果.
171 | func (c HTTPClient) PostJSON(url string, headers map[string]string, data interface{}, resp interface{}) error {
172 | buf, err := json.Marshal(data)
173 | if err != nil {
174 | return errors.Trace(err)
175 | }
176 |
177 | if headers == nil {
178 | headers = make(map[string]string)
179 | }
180 | headers["Content-type"] = "application/json"
181 |
182 | if buf, err = c.do("POST", url, headers, buf); err != nil {
183 | return errors.Trace(err)
184 | }
185 |
186 | c.logger.Debugf("url:%v, req:%+v, resp:%s", url, data, buf)
187 | return json.Unmarshal(buf, resp)
188 | }
189 |
190 | // Put 发送put请求.
191 | func (c HTTPClient) Put(url string, headers map[string]string, body []byte) ([]byte, error) {
192 | return c.do("PUT", url, headers, body)
193 | }
194 |
195 | // PutJSON 发送json请求解析返回结果.
196 | func (c HTTPClient) PutJSON(url string, headers map[string]string, data interface{}, resp interface{}) error {
197 | buf, err := json.Marshal(data)
198 | if err != nil {
199 | return errors.Trace(err)
200 | }
201 |
202 | if headers == nil {
203 | headers = make(map[string]string)
204 | }
205 | headers["Content-type"] = "application/json"
206 |
207 | if buf, err = c.do("PUT", url, headers, buf); err != nil {
208 | return errors.Trace(err)
209 | }
210 | c.logger.Debugf("url:%v, resp:%s", url, buf)
211 | return json.Unmarshal(buf, resp)
212 | }
213 |
214 | // Delete 发送delete请求.
215 | func (c HTTPClient) Delete(url string, headers map[string]string, body []byte) ([]byte, error) {
216 | return c.do("DELETE", url, headers, body)
217 | }
218 |
219 | // DeleteJSON 发送JSON格式delete请求, 并解析返回结果.
220 | func (c HTTPClient) DeleteJSON(url string, headers map[string]string, resp interface{}) error {
221 | buf, err := c.do("DELETE", url, headers, nil)
222 | if err != nil {
223 | return errors.Trace(err)
224 | }
225 | c.logger.Debugf("url:%v, resp:%s", url, buf)
226 | return json.Unmarshal(buf, resp)
227 | }
228 |
--------------------------------------------------------------------------------
/http/client/client_test.go:
--------------------------------------------------------------------------------
1 | package client
2 |
3 | import (
4 | "dearcode.net/crab/log"
5 | "testing"
6 | )
7 |
8 | func TestHTTPClientZIP(t *testing.T) {
9 | hc := New().SetLogger(log.GetLogger()).Timeout(1)
10 | buf, err := hc.Get("http://www.baidu.com/", map[string]string{"Accept-Encoding": "gzip"}, nil)
11 | if err != nil {
12 | t.Fatalf("err:%v", err)
13 | }
14 |
15 | t.Logf("buf:%s", buf)
16 | }
17 |
18 | func TestHTTPClient(t *testing.T) {
19 | hc := New().SetLogger(log.GetLogger()).Timeout(1)
20 | buf, err := hc.Get("http://www.baidu.com/", nil, nil)
21 | if err != nil {
22 | t.Fatalf("err:%v", err)
23 | }
24 |
25 | t.Logf("buf:%s", buf)
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/http/server/common.go:
--------------------------------------------------------------------------------
1 | package server
2 |
3 | import (
4 | "encoding/json"
5 | "fmt"
6 | "net/http"
7 | "strings"
8 | )
9 |
10 | // VariablePostion 变量位置.
11 | type VariablePostion int
12 |
13 | // Method 请求方式.
14 | type Method int
15 |
16 | const (
17 | //URI 参数在uri里.
18 | URI VariablePostion = iota
19 | //HEADER 参数在头里.
20 | HEADER
21 | //JSON 参数在body的json里.
22 | JSON
23 | //FORM 参数在form表单中.
24 | FORM
25 | )
26 |
27 | // String 类型转字符串
28 | func (p VariablePostion) String() string {
29 | switch p {
30 | case URI:
31 | return "URI"
32 | case HEADER:
33 | return "HEADER"
34 | case JSON:
35 | return "JSON"
36 | case FORM:
37 | return "FORM"
38 | }
39 | return "NIL"
40 | }
41 |
42 | const (
43 | //GET http method.
44 | GET Method = iota
45 | //POST http method.
46 | POST
47 | //PUT http method.
48 | PUT
49 | //DELETE http method.
50 | DELETE
51 | //RESTful any method, may be get,post,put or delete.
52 | RESTful
53 | )
54 |
55 | // NewMethod 转换字符串method到Method类型.
56 | func NewMethod(m string) Method {
57 | switch strings.ToUpper(m) {
58 | case http.MethodGet:
59 | return GET
60 | case http.MethodPost:
61 | return POST
62 | case http.MethodPut:
63 | return PUT
64 | case http.MethodDelete:
65 | return DELETE
66 |
67 | }
68 | return RESTful
69 | }
70 |
71 | // String 类型转字符串
72 | func (m Method) String() string {
73 | switch m {
74 | case GET:
75 | return "GET"
76 | case POST:
77 | return "POST"
78 | case PUT:
79 | return "PUT"
80 | case DELETE:
81 | return "DELETE"
82 | case RESTful:
83 | return "RESTful"
84 | }
85 | return "NIL"
86 | }
87 |
88 | // Response 通用返回结果
89 | type Response struct {
90 | Status int
91 | Message string `json:",omitempty"`
92 | Data interface{} `json:",omitempty"`
93 | }
94 |
95 | // SendResponse 返回结果,支持json
96 | func SendResponse(w http.ResponseWriter, status int, f string, args ...interface{}) {
97 | w.Header().Add("Content-Type", "application/json")
98 | r := Response{Status: status, Message: f}
99 | if len(args) > 0 {
100 | r.Message = fmt.Sprintf(f, args...)
101 | }
102 |
103 | buf, _ := json.Marshal(&r)
104 | w.Write(buf)
105 | }
106 |
107 | // Abort 返回结果,支持json
108 | func Abort(w http.ResponseWriter, f string, args ...interface{}) {
109 | w.WriteHeader(http.StatusInternalServerError)
110 | fmt.Fprintf(w, f, args...)
111 | }
112 |
113 | // SendResponseData 返回结果,支持json
114 | func SendResponseData(w http.ResponseWriter, data interface{}) {
115 | w.Header().Add("Content-Type", "application/json")
116 | buf, _ := json.Marshal(&Response{Data: data})
117 | w.Write(buf)
118 | }
119 |
120 | // SendErrorDetail 返回详细的错误信息,支持json
121 | func SendErrorDetail(w http.ResponseWriter, status int, data interface{}, f string, args ...interface{}) {
122 | w.Header().Add("Content-Type", "application/json")
123 | resp := Response{Status: status, Message: f, Data: data}
124 | if len(args) > 0 {
125 | resp.Message = fmt.Sprintf(f, args...)
126 | }
127 | buf, _ := json.Marshal(&resp)
128 | w.Write(buf)
129 | }
130 |
131 | // SendRows 为bootstrap-talbe返回结果,根据条件查找,total是总记录数,rows是数据
132 | func SendRows(w http.ResponseWriter, total interface{}, data interface{}) {
133 | var resp = struct {
134 | Total interface{} `json:"total"`
135 | Rows interface{} `json:"rows"`
136 | }{total, data}
137 |
138 | w.Header().Add("Content-Type", "application/json")
139 | buf, _ := json.Marshal(resp)
140 | w.Write(buf)
141 | }
142 |
143 | // SendData 为bootstrap-talbe客户端分页返回结果.
144 | func SendData(w http.ResponseWriter, data interface{}) {
145 | w.Header().Add("Content-Type", "application/json")
146 | buf, _ := json.Marshal(data)
147 | w.Write(buf)
148 | }
149 |
150 | // SendResponseOK 返回成功结果.
151 | func SendResponseOK(w http.ResponseWriter) {
152 | w.Header().Add("Content-Type", "application/json")
153 | buf, _ := json.Marshal(&Response{})
154 | w.Write(buf)
155 | }
156 |
--------------------------------------------------------------------------------
/http/server/handler.go:
--------------------------------------------------------------------------------
1 | package server
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net"
7 | "net/http"
8 | "reflect"
9 | "regexp"
10 | "runtime/debug"
11 | "strings"
12 | "sync"
13 |
14 | "github.com/google/btree"
15 | "github.com/juju/errors"
16 |
17 | "dearcode.net/crab/log"
18 | )
19 |
20 | // userKey 用户自定义的key
21 | type userKey string
22 |
23 | type handlerRegexp struct {
24 | keys []userKey
25 | exp *regexp.Regexp
26 | handler
27 | }
28 |
29 | // handler 对外服务接口, path格式:Method/URI
30 | type handler struct {
31 | path string
32 | call func(http.ResponseWriter, *http.Request)
33 | }
34 |
35 | type prefix struct {
36 | path string
37 | exps []handlerRegexp
38 | }
39 |
40 | type httpServer struct {
41 | path map[string]handler
42 | prefix *btree.BTree
43 | filter Filter
44 | listener net.Listener
45 | mu sync.RWMutex
46 | }
47 |
48 | var (
49 | server = newHTTPServer()
50 | keysExp *regexp.Regexp
51 | )
52 |
53 | func init() {
54 | exp, err := regexp.Compile(`{(\w+)?}`)
55 | if err != nil {
56 | panic(err.Error())
57 | }
58 | keysExp = exp
59 | }
60 |
61 | func newHTTPServer() *httpServer {
62 | return &httpServer{
63 | path: make(map[string]handler),
64 | prefix: btree.New(3),
65 | filter: defaultFilter,
66 | }
67 | }
68 |
69 | // Filter 请求过滤, 如果返回结果为nil,直接返回,不再进行后续处理.
70 | type Filter func(http.ResponseWriter, *http.Request) *http.Request
71 |
72 | func (p *prefix) Less(bi btree.Item) bool {
73 | return strings.Compare(p.path, bi.(*prefix).path) == 1
74 | }
75 |
76 | // NameToPath 类名转路径
77 | func NameToPath(name string, depth int) string {
78 | buf := []byte(name)
79 | d := 0
80 | index := 0
81 | for i := range buf {
82 | if buf[i] == '.' || buf[i] == '*' {
83 | buf[i] = '/'
84 | d++
85 | if d == depth {
86 | index = i
87 | }
88 | }
89 | }
90 | return string(buf[index:])
91 | }
92 |
93 | // Register 只要struct实现了Get(),Post(),Delete(),Put()接口就可以自动注册
94 | func Register(obj interface{}) error {
95 | return register(obj, "", false)
96 | }
97 |
98 | // RegisterMust 只要struct实现了Get(),Post(),Delete(),Put()接口就可以自动注册, 如果添加失败panic.
99 | func RegisterMust(obj interface{}) {
100 | if err := register(obj, "", false); err != nil {
101 | panic(err.Error())
102 | }
103 |
104 | }
105 |
106 | // RegisterPath 注册url完全匹配.
107 | func RegisterPath(obj interface{}, path string) error {
108 | return register(obj, path, false)
109 | }
110 |
111 | // RegisterPathMust 注册url完全匹配,如果遇到错误panic.
112 | func RegisterPathMust(obj interface{}, path string) {
113 | if err := register(obj, path, false); err != nil {
114 | panic(err.Error())
115 | }
116 | }
117 |
118 | // RegisterHandler 注册自定义url完全匹配.
119 | func RegisterHandler(call func(http.ResponseWriter, *http.Request), method, path string) error {
120 | h := handler{
121 | path: fmt.Sprintf("%v%v", method, path),
122 | call: call,
123 | }
124 |
125 | server.mu.Lock()
126 | defer server.mu.Unlock()
127 |
128 | if _, ok := server.path[h.path]; ok {
129 | return errors.Errorf("exist url:%v %v", method, path)
130 | }
131 |
132 | server.path[h.path] = h
133 |
134 | log.Infof("handler %v %v", method, path)
135 |
136 | return nil
137 | }
138 |
139 | // RegisterPrefix 注册url前缀.
140 | func RegisterPrefix(obj interface{}, path string) error {
141 | return register(obj, path, true)
142 | }
143 |
144 | // RegisterPrefixMust 注册url前缀并保证成功.
145 | func RegisterPrefixMust(obj interface{}, path string) {
146 | if err := RegisterPrefix(obj, path); err != nil {
147 | panic(err.Error())
148 | }
149 | }
150 |
151 | func newHandlerRegexp(h handler) handlerRegexp {
152 | hr := handlerRegexp{handler: h}
153 |
154 | for _, m := range keysExp.FindAllStringSubmatch(hr.path, -1) {
155 | hr.keys = append(hr.keys, userKey(m[1]))
156 | }
157 |
158 | np := keysExp.ReplaceAllString(hr.path, "(.+)")
159 | exp, err := regexp.Compile(np)
160 | if err != nil {
161 | panic(err.Error())
162 | }
163 |
164 | hr.exp = exp
165 |
166 | return hr
167 | }
168 |
169 | func register(obj interface{}, path string, isPrefix bool) error {
170 | rt := reflect.TypeOf(obj)
171 | if rt.Kind() != reflect.Ptr {
172 | return fmt.Errorf("need ptr")
173 | }
174 |
175 | if path == "" {
176 | path = NameToPath(rt.String(), 0) + "/"
177 | }
178 |
179 | if !strings.HasPrefix(path, "/") {
180 | path = "/" + path
181 | }
182 |
183 | server.mu.Lock()
184 | defer server.mu.Unlock()
185 |
186 | rv := reflect.ValueOf(obj)
187 | for i := 0; i < rv.NumMethod(); i++ {
188 | method := rt.Method(i).Name
189 | //log.Debugf("rt:%v, %d, method:%v", rt, i, method)
190 | switch method {
191 | case http.MethodPost:
192 | case http.MethodGet:
193 | case http.MethodPut:
194 | case http.MethodDelete:
195 | default:
196 | log.Warningf("ignore func:%v %v %v", method, path, rt)
197 | continue
198 | }
199 |
200 | mt := rv.MethodByName(method)
201 | if mt.Type().NumIn() != 2 || mt.Type().In(0).String() != "http.ResponseWriter" || mt.Type().In(1).String() != "*http.Request" {
202 | log.Debugf("ignore func:%v %v %v", method, path, mt.Type())
203 | continue
204 | }
205 |
206 | h := handler{
207 | path: fmt.Sprintf("%v%v", method, path),
208 | call: mt.Interface().(func(http.ResponseWriter, *http.Request)),
209 | }
210 |
211 | //前缀匹配
212 | if isPrefix {
213 | p := prefix{
214 | path: fmt.Sprintf("%v%v", method, path),
215 | }
216 |
217 | if idx := strings.Index(path, "{"); idx > 0 {
218 | p.path = fmt.Sprintf("%v%v", method, path[:idx])
219 | }
220 |
221 | // log.Debugf("path:%v", p.path)
222 | if server.prefix.Has(&p) {
223 | p = *(server.prefix.Get(&p).(*prefix))
224 | }
225 |
226 | exp := newHandlerRegexp(h)
227 |
228 | p.exps = append(p.exps, exp)
229 |
230 | server.prefix.ReplaceOrInsert(&p)
231 |
232 | log.Infof("prefix %v %v %v %v", method, path, exp.keys, rt)
233 | continue
234 | }
235 |
236 | //全路径匹配
237 | if _, ok := server.path[h.path]; ok {
238 | return errors.Errorf("exist url:%v %v", method, path)
239 | }
240 |
241 | server.path[h.path] = h
242 | log.Infof("path %v %v %v", method, path, rt)
243 | }
244 |
245 | return nil
246 | }
247 |
248 | // AddFilter 添加过滤函数.
249 | func AddFilter(filter Filter) {
250 | server.mu.Lock()
251 | defer server.mu.Unlock()
252 | server.filter = filter
253 | }
254 |
255 | func parseRequestValues(path string, ur handlerRegexp) context.Context {
256 | ctx := context.Background()
257 | m := ur.exp.FindAllStringSubmatch(path, -1)
258 | if len(m) == 0 {
259 | return ctx
260 | }
261 | for i := 1; i < len(m[0]); i++ {
262 | log.Debugf("i:%v, key:%v, val:%v", i, ur.keys[i-1], m[0][i])
263 | ctx = context.WithValue(ctx, ur.keys[i-1], m[0][i])
264 | }
265 |
266 | return ctx
267 | }
268 |
269 | // RESTValue 取restful方式传递的值
270 | func RESTValue(req *http.Request, key string) (string, bool) {
271 | i := req.Context().Value(userKey(key))
272 | if i == nil {
273 | return "", false
274 | }
275 | s, ok := i.(string)
276 | return s, ok
277 | }
278 |
279 | func getHandler(method, path string) (func(http.ResponseWriter, *http.Request), context.Context) {
280 | server.mu.RLock()
281 | defer server.mu.RUnlock()
282 |
283 | path = method + path
284 |
285 | if i, ok := server.path[path]; ok {
286 | //log.Debugf("find path:%v", path)
287 | return i.call, nil
288 | }
289 |
290 | var p prefix
291 | var ok bool
292 | //如果完全匹配没找到,再找前缀的
293 | server.prefix.AscendGreaterOrEqual(&prefix{path: path}, func(item btree.Item) bool {
294 | p = *(item.(*prefix))
295 | ok = strings.HasPrefix(path, p.path)
296 | //log.Debugf("path:%v, prefix:%v, ok:%v", path, p.path, ok)
297 | return !ok
298 | })
299 | // log.Debugf("ok:%v, prefix:%v", ok, p)
300 |
301 | if !ok {
302 | return nil, nil
303 | }
304 |
305 | for _, ue := range p.exps {
306 | if ue.exp.MatchString(path) {
307 | if len(ue.keys) == 0 {
308 | return ue.call, nil
309 | }
310 | // log.Debugf("uri exp:%v, path:%v", ue, path)
311 | return ue.call, parseRequestValues(path, ue)
312 | }
313 | }
314 |
315 | return nil, nil
316 | }
317 |
318 | // ServeHTTP 真正对外服务接口
319 | func (s *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
320 | defer func() {
321 | if p := recover(); p != nil {
322 | log.Errorf("panic:%v req:%v, stack:%s", p, r, debug.Stack())
323 | Abort(w, "%v\n%s", p, debug.Stack())
324 | return
325 | }
326 | }()
327 |
328 | nr := s.filter(w, r)
329 | if nr == nil {
330 | log.Debugf("%v %v %v ignore", r.RemoteAddr, r.Method, r.URL)
331 | return
332 | }
333 |
334 | h, ctx := getHandler(r.Method, r.URL.Path)
335 | if h == nil {
336 | log.Errorf("%v %v %v not found.", r.RemoteAddr, r.Method, r.URL)
337 | w.WriteHeader(http.StatusNotFound)
338 | return
339 | }
340 |
341 | if ctx != nil {
342 | nr = nr.WithContext(ctx)
343 | }
344 |
345 | log.Debugf("%v %v %v h:%p", r.RemoteAddr, r.Method, r.URL, h)
346 |
347 | h(w, nr)
348 | }
349 |
350 | func defaultFilter(_ http.ResponseWriter, r *http.Request) *http.Request {
351 | return r
352 | }
353 |
354 | // Start 启动httpServer.
355 | func Start(addr string) (net.Listener, error) {
356 | ln, err := net.Listen("tcp", addr)
357 | if err != nil {
358 | return nil, errors.Trace(err)
359 | }
360 |
361 | server.mu.Lock()
362 | server.listener = ln
363 | server.mu.Unlock()
364 |
365 | go func() {
366 | if err = http.Serve(ln, server); err != nil {
367 | log.Errorf("Serve error:%v", err)
368 | }
369 | }()
370 | return ln, nil
371 | }
372 |
--------------------------------------------------------------------------------
/http/server/reflect.go:
--------------------------------------------------------------------------------
1 | package server
2 |
3 | import (
4 | "reflect"
5 | "strconv"
6 | "strings"
7 |
8 | "github.com/juju/errors"
9 | )
10 |
11 | // fieldName 解析field中的json,db等标签,提取别名
12 | func fieldName(f reflect.StructField) string {
13 | if name := f.Tag.Get("json"); name != "" {
14 | return name
15 | }
16 |
17 | if name := f.Tag.Get("db"); name != "" {
18 | return name
19 | }
20 |
21 | if name := f.Tag.Get("cfg"); name != "" {
22 | return name
23 | }
24 |
25 | return f.Name
26 | }
27 |
28 | func setValue(val string, rv reflect.Value) error {
29 | val = strings.TrimSpace(val)
30 | switch rv.Type().Kind() {
31 | case reflect.Int, reflect.Int64:
32 | i, err := strconv.ParseInt(val, 10, 64)
33 | if err != nil {
34 | return errors.Trace(err)
35 | }
36 | rv.SetInt(i)
37 |
38 | case reflect.Uint, reflect.Uint64:
39 | u, err := strconv.ParseUint(val, 10, 64)
40 | if err != nil {
41 | return errors.Trace(err)
42 | }
43 | rv.SetUint(u)
44 |
45 | case reflect.String:
46 | rv.SetString(val)
47 |
48 | case reflect.Bool:
49 | b, err := strconv.ParseBool(val)
50 | if err != nil {
51 | return errors.Trace(err)
52 | }
53 | rv.SetBool(b)
54 | case reflect.Slice:
55 | vals := strings.Split(val, "\x00")
56 | rvs := reflect.MakeSlice(rv.Type(), 0, len(vals))
57 | for i := 0; i < len(vals); i++ {
58 | v := reflect.New(rv.Type().Elem())
59 | setValue(vals[i], v.Elem())
60 | rvs = reflect.Append(rvs, v.Elem())
61 | }
62 | rv.Set(rvs)
63 | }
64 |
65 | return nil
66 | }
67 |
68 | func setValueOrPtr(f reflect.StructField, val string, rv reflect.Value) error {
69 | var pv reflect.Value
70 | v := rv
71 | if f.Type.Kind() == reflect.Ptr {
72 | pv = reflect.New(f.Type.Elem())
73 | v = pv.Elem()
74 | }
75 |
76 | if err := setValue(val, v); err != nil {
77 | return errors.Trace(err)
78 | }
79 |
80 | if f.Type.Kind() == reflect.Ptr {
81 | rv.Set(pv)
82 | }
83 |
84 | return nil
85 | }
86 |
87 | type getValueFunc func(string) (string, bool)
88 |
89 | func reflectKeyValue(getVal getValueFunc, rt reflect.Type, rv reflect.Value, level int) error {
90 | if rv.Type().Kind() == reflect.Ptr && rv.IsNil() {
91 | pv := reflect.New(rt.Elem())
92 | rv.Set(pv)
93 | }
94 |
95 | if rt.Kind() == reflect.Ptr && rt.Elem().Kind() == reflect.Struct {
96 | rt = rt.Elem()
97 | rv = rv.Elem()
98 | }
99 |
100 | for i := 0; i < rt.NumField(); i++ {
101 | f := rt.Field(i)
102 | if f.PkgPath != "" && !f.Anonymous {
103 | continue
104 | }
105 |
106 | if (f.Type.Kind() == reflect.Struct) || (f.Type.Kind() == reflect.Ptr && f.Type.Elem().Kind() == reflect.Struct) {
107 | if level < 1 {
108 | continue
109 | }
110 | if err := reflectKeyValue(getVal, f.Type, rv.Field(i), level-1); err != nil {
111 | return errors.Trace(err)
112 | }
113 | continue
114 | }
115 |
116 | key := fieldName(f)
117 | val, ok := getVal(key)
118 | if !ok {
119 | continue
120 | }
121 |
122 | if err := setValueOrPtr(f, val, rv.Field(i)); err != nil {
123 | return errors.Trace(err)
124 | }
125 | }
126 |
127 | return nil
128 | }
129 |
130 | // reflectStruct 反射结构体中字段,根据字段名或标签取对应结果,并赋值返回
131 | func reflectStruct(getVal getValueFunc, obj interface{}) error {
132 | rt := reflect.TypeOf(obj)
133 | rv := reflect.ValueOf(obj)
134 | return reflectKeyValue(getVal, rt, rv, 2)
135 | }
136 |
--------------------------------------------------------------------------------
/http/server/reflect_test.go:
--------------------------------------------------------------------------------
1 | package server
2 |
3 | import (
4 | "fmt"
5 | "strings"
6 | "testing"
7 |
8 | "github.com/hokaccha/go-prettyjson"
9 | )
10 |
11 | type TestSub struct {
12 | Subint int
13 | SubStr []string
14 | }
15 |
16 | type TestSubSecond struct {
17 | Secondint int
18 | SecondStr string
19 | }
20 |
21 | type TestPtrSub struct {
22 | PSubint *int
23 | PSubStr *string
24 | Second *TestSubSecond
25 | }
26 |
27 | type TestStruct struct {
28 | *TestPtrSub
29 | TestSub
30 | Key string
31 | PVal *string
32 | }
33 |
34 | func TestReflectStruct(t *testing.T) {
35 | ts := TestStruct{
36 | TestPtrSub: &TestPtrSub{
37 | Second: &TestSubSecond{
38 | SecondStr: "second str",
39 | Secondint: 333,
40 | },
41 | },
42 | TestSub: TestSub{
43 | SubStr: []string{"s1", "s2"},
44 | Subint: 111,
45 | },
46 | Key: "main string key",
47 | }
48 |
49 | psv := "ptr sub string val"
50 | ts.PSubStr = &psv
51 | psi := 222
52 | ts.PSubint = &psi
53 |
54 | pv := "ptr string values"
55 | ts.PVal = &pv
56 |
57 | nt := &TestStruct{}
58 |
59 | err := reflectStruct(
60 | func(k string) (string, bool) {
61 | switch k {
62 | case "PSubint":
63 | return fmt.Sprintf(" %v ", *ts.PSubint), true
64 | case "PSubStr":
65 | return *ts.PSubStr, true
66 | case "Subint":
67 | return fmt.Sprintf("%v", ts.Subint), true
68 | case "SubStr":
69 | return strings.Join(ts.SubStr, "\x00"), true
70 | case "Key":
71 | return ts.Key, true
72 | case "PVal":
73 | return *ts.PVal, true
74 | case "Secondint":
75 | return fmt.Sprintf("%v", ts.Second.Secondint), true
76 | case "SecondStr":
77 | return ts.Second.SecondStr, true
78 | default:
79 | return "", false
80 | }
81 | },
82 | nt,
83 | )
84 |
85 | if err != nil {
86 | t.Fatalf("%v", err)
87 | }
88 |
89 | b, _ := prettyjson.Marshal(ts)
90 | nb, _ := prettyjson.Marshal(nt)
91 |
92 | if string(b) != string(nb) {
93 | t.Fatalf("expect:%s, recv:%s", b, nb)
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/http/server/vars.go:
--------------------------------------------------------------------------------
1 | package server
2 |
3 | import (
4 | "encoding/json"
5 | "io"
6 | "net/http"
7 | "net/url"
8 | "strings"
9 |
10 | "github.com/juju/errors"
11 |
12 | "dearcode.net/crab/log"
13 | "dearcode.net/crab/meta"
14 | "dearcode.net/crab/validation"
15 | )
16 |
17 | // UnmarshalForm 解析form中或者url中参数, 只支持int和string.
18 | func UnmarshalForm(req *http.Request, result interface{}) error {
19 | req.ParseForm()
20 | fvs := req.Form
21 | hvs := req.Header
22 |
23 | return reflectStruct(func(key string) (string, bool) {
24 | vals, exist := fvs[key]
25 | if !exist {
26 | if vals, exist = hvs[key]; !exist {
27 | return "", false
28 | }
29 | }
30 | return strings.Join(vals, "\x00"), true
31 | }, result)
32 | }
33 |
34 | // UnmarshalJSON 解析body中的json数据.
35 | func UnmarshalJSON(req *http.Request, result interface{}) error {
36 | body, err := io.ReadAll(req.Body)
37 | if err != nil {
38 | return errors.Trace(err)
39 | }
40 | //空body不解析,不报错
41 | if len(body) == 0 {
42 | return nil
43 | }
44 |
45 | return json.Unmarshal(body, result)
46 | }
47 |
48 | // UnmarshalBody 解析body中的json, form数据.
49 | func UnmarshalBody(req *http.Request, result interface{}) error {
50 | body, err := io.ReadAll(req.Body)
51 | if err != nil {
52 | return errors.Trace(err)
53 | }
54 |
55 | //空body不解析,不报错
56 | if len(body) == 0 {
57 | return nil
58 | }
59 |
60 | if err := json.Unmarshal(body, result); err != nil {
61 | //如果指定类型为json的,解析出错要抛出错误信息, 但大多人使用时不指定content-type
62 | if ct := req.Header.Get("Content-Type"); strings.Contains(strings.ToLower(ct), "json") {
63 | return errors.Trace(err)
64 | }
65 | }
66 |
67 | values, _ := url.ParseQuery(string(body))
68 |
69 | return reflectStruct(func(key string) (string, bool) {
70 | vals, exist := values[key]
71 | if !exist {
72 | return "", false
73 | }
74 | return vals[0], true
75 | }, result)
76 | }
77 |
78 | // UnmarshalValidate 解析并检证参数.
79 | func UnmarshalValidate(req *http.Request, postion VariablePostion, result interface{}) error {
80 | var err error
81 | if result == nil {
82 | return meta.ErrArgIsNil
83 | }
84 |
85 | if postion == JSON {
86 | err = UnmarshalJSON(req, result)
87 | } else {
88 | err = UnmarshalForm(req, result)
89 | }
90 |
91 | if err != nil {
92 | return errors.Trace(err)
93 | }
94 |
95 | log.Debugf("request %s vars:%#v", postion, result)
96 | valid := validation.Validation{}
97 | _, err = valid.Valid(result)
98 | return errors.Trace(err)
99 | }
100 |
101 | // ParseHeaderVars 解析并验证头中参数.
102 | func ParseHeaderVars(req *http.Request, result interface{}) error {
103 | return UnmarshalValidate(req, HEADER, result)
104 | }
105 |
106 | // ParseFormVars 解析并验证Form表单中参数.
107 | func ParseFormVars(req *http.Request, result interface{}) error {
108 | return UnmarshalValidate(req, FORM, result)
109 | }
110 |
111 | // ParseJSONVars 解析并验证Body中的Json参数.
112 | func ParseJSONVars(req *http.Request, result interface{}) error {
113 | return UnmarshalValidate(req, JSON, result)
114 | }
115 |
116 | // ParseVars 通用解析,先解析url,再解析body,最后验证结果
117 | func ParseVars(req *http.Request, result interface{}) error {
118 | if result == nil {
119 | return meta.ErrArgIsNil
120 | }
121 |
122 | if err := ParseURLVars(req, result); err != nil {
123 | return errors.Trace(err)
124 | }
125 |
126 | if err := UnmarshalBody(req, result); err != nil {
127 | return errors.Trace(err)
128 | }
129 |
130 | valid := validation.Validation{}
131 | _, err := valid.Valid(result)
132 | return errors.Trace(err)
133 | }
134 |
135 | // ParseURLVars 解析url中参数.
136 | func ParseURLVars(req *http.Request, result interface{}) error {
137 | values := req.URL.Query()
138 | return reflectStruct(func(key string) (string, bool) {
139 | vals, exist := values[key]
140 | if !exist {
141 | return "", false
142 | }
143 | return vals[0], true
144 | }, result)
145 | }
146 |
--------------------------------------------------------------------------------
/http/server/vars_test.go:
--------------------------------------------------------------------------------
1 | package server
2 |
3 | import (
4 | "bytes"
5 | "io"
6 | "net/http"
7 | "testing"
8 | )
9 |
10 | func TestVarsForm(t *testing.T) {
11 | url := "http://www.baidu.com/test/"
12 | body := io.NopCloser(bytes.NewBufferString("email=test@mailchina.org&passwd=1qaz"))
13 | req, err := http.NewRequest("POST", url, body)
14 | if err != nil {
15 | t.Fatal(err.Error())
16 | }
17 | req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
18 |
19 | result := struct {
20 | Email string `json:"email" valid:"Required"`
21 | Password string `json:"passwd" valid:"Required"`
22 | }{}
23 |
24 | if err = ParseFormVars(req, &result); err != nil {
25 | t.Fatal(err.Error())
26 | }
27 | }
28 |
29 | func TestVarsHeader(t *testing.T) {
30 | url := "http://www.baidu.com/test/"
31 | req, err := http.NewRequest("GET", url, nil)
32 | if err != nil {
33 | t.Fatal(err.Error())
34 | }
35 | req.Header.Add("Email", "test@mailchina.org")
36 | req.Header.Add("Passwd", "1qaz")
37 |
38 | result := struct {
39 | Email string `json:"Email" valid:"Required"`
40 | Password string `json:"Passwd" valid:"Required"`
41 | }{}
42 |
43 | if err = ParseHeaderVars(req, &result); err != nil {
44 | t.Fatal(err.Error())
45 | }
46 | }
47 |
48 | func TestVarsUrl(t *testing.T) {
49 | url := "http://www.baidu.com/test/?email=test@mailchina.org&passwd=1qaz"
50 | req, err := http.NewRequest("GET", url, nil)
51 | if err != nil {
52 | t.Fatal(err.Error())
53 | }
54 |
55 | result := struct {
56 | Email string `json:"email" valid:"Required"`
57 | Password string `json:"passwd" valid:"Required"`
58 | }{}
59 |
60 | if err = ParseURLVars(req, &result); err != nil {
61 | t.Fatal(err.Error())
62 | }
63 | }
64 |
65 | func TestVarsJSON(t *testing.T) {
66 | url := "http://www.baidu.com/test/"
67 | body := io.NopCloser(bytes.NewBufferString(`{"email":"test@mailchina.org","passwd":"1qaz"}`))
68 | req, err := http.NewRequest("POST", url, body)
69 | if err != nil {
70 | t.Fatal(err.Error())
71 | }
72 | result := struct {
73 | Email string `json:"email" valid:"Required"`
74 | Password string `json:"passwd" valid:"Required"`
75 | }{}
76 |
77 | if err = ParseJSONVars(req, &result); err != nil {
78 | t.Fatal(err.Error())
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/log/README.md:
--------------------------------------------------------------------------------
1 | ## crab log
2 | crab log 是一个轻量级log库,支持不同级别不同颜色输出,按天滚动输出日志文件等常用日志功能
3 |
4 | ## Example
5 | [](log_test.go)
6 |
7 | ```go
8 | package main
9 |
10 | import (
11 | "time"
12 |
13 | "dearcode.net/crab/log"
14 | )
15 |
16 | func main() {
17 | log.Debug("default log begin")
18 | log.Infof("%v test log", time.Now())
19 |
20 | l := log.NewLogger()
21 | l.Debug("logger 1111111111")
22 | l.Info("logger 2222222222")
23 | l.Warningf("logger 33333 %v", time.Now())
24 | l.Errorf("logger color %v xxxxxx", time.Now().UnixNano())
25 |
26 | //关闭颜色显示
27 | l.SetColor(false)
28 |
29 | l.Errorf("logger no color %v yyyyyy", time.Now().UnixNano())
30 | log.Infof("%v default has color test log", time.Now())
31 |
32 | //指定输出文件
33 | l.SetOutputFile("./vvv.log").SetRolling(true)
34 | l.Info(time.Now())
35 |
36 | }
37 | ```
38 |
39 | ## Installing
40 |
41 | ### Using *go get*
42 | ```bash
43 | $ go get dearcode.net/crab/log
44 | ```
45 | After this command *log* is ready to use. Its source will be in:
46 | ```bash
47 | $GOPATH/src/dearcode.net/crab/log
48 | ```
49 |
50 | You can use `go get -u` to update the package.
51 |
52 | ## Documentation
53 |
54 | For docs, see http://godoc.org/dearcode.net/crab/log or run:
55 | ```bash
56 | $ godoc dearcode.net/crab/log
57 | ```
58 |
59 |
--------------------------------------------------------------------------------
/log/context.go:
--------------------------------------------------------------------------------
1 | package log
2 |
3 | import "context"
4 |
5 | type logKey struct{}
6 |
7 | // ToContext 保存log对象到ctx中
8 | func ToContext(ctx context.Context, log *Logger) context.Context {
9 | return context.WithValue(ctx, logKey{}, log)
10 | }
11 |
12 | // FromContext 从ctx中查找日志对象,找不到返回nil
13 | func FromContext(ctx context.Context) *Logger {
14 | if v := ctx.Value(logKey{}); v != nil {
15 | return v.(*Logger)
16 | }
17 | return nil
18 | }
19 |
20 | // FromContextOrDefault 从ctx中查找日志对象,找不到返回默认log对象
21 | func FromContextOrDefault(ctx context.Context) *Logger {
22 | if v := ctx.Value(logKey{}); v != nil {
23 | return v.(*Logger)
24 | }
25 | return NewLogger()
26 | }
27 |
--------------------------------------------------------------------------------
/log/level.go:
--------------------------------------------------------------------------------
1 | package log
2 |
3 | // Level 日志级别.
4 | type Level int
5 |
6 | const (
7 | //LogFatal fatal.
8 | LogFatal Level = iota
9 | //LogError error.
10 | LogError
11 | //LogWarning warning.
12 | LogWarning
13 | //LogInfo info.
14 | LogInfo
15 | //LogDebug debug.
16 | LogDebug
17 | )
18 |
19 | // stringToLevel 字符串转Level.
20 | func stringToLevel(level string) Level {
21 | switch level {
22 | case "fatal":
23 | return LogFatal
24 | case "error":
25 | return LogError
26 | case "warn":
27 | return LogWarning
28 | case "warning":
29 | return LogWarning
30 | case "debug":
31 | return LogDebug
32 | case "info":
33 | return LogInfo
34 | }
35 | return LogDebug
36 | }
37 |
38 | // Level Level 转字符串.
39 | func (l Level) String() string {
40 | switch l {
41 | case LogFatal:
42 | return "fatal"
43 | case LogError:
44 | return "error"
45 | case LogWarning:
46 | return "warning"
47 | case LogDebug:
48 | return "debug"
49 | case LogInfo:
50 | return "info"
51 | }
52 | return "unknown"
53 | }
54 |
55 | // color Level转颜色.
56 | func (l Level) color() string {
57 | switch l {
58 | case LogFatal:
59 | return "\033[0;31m"
60 | case LogError:
61 | return "\033[0;31m"
62 | case LogWarning:
63 | return "\033[0;33m"
64 | case LogDebug:
65 | return "\033[0;36m"
66 | case LogInfo:
67 | return "\033[0;32m"
68 | }
69 | return "\033[0;37m"
70 | }
71 |
--------------------------------------------------------------------------------
/log/log.go:
--------------------------------------------------------------------------------
1 | package log
2 |
3 | import (
4 | "fmt"
5 | )
6 |
7 | var mlog = NewLogger()
8 |
9 | // SetLevel 设置日志级别.
10 | func SetLevel(level Level) {
11 | mlog.SetLevel(level)
12 | }
13 |
14 | // GetLogLevel 获取日志级别.
15 | func GetLogLevel() Level {
16 | return mlog.level
17 | }
18 |
19 | // GetLogger 获取当前日志对象.
20 | func GetLogger() *Logger {
21 | return mlog
22 | }
23 |
24 | // Info .
25 | func Info(v ...interface{}) {
26 | mlog.write(LogInfo, fmt.Sprint(v...))
27 | }
28 |
29 | // Infof .
30 | func Infof(format string, v ...interface{}) {
31 | mlog.write(LogInfo, format, v...)
32 | }
33 |
34 | // Debug .
35 | func Debug(v ...interface{}) {
36 | mlog.write(LogDebug, fmt.Sprint(v...))
37 | }
38 |
39 | // Debugf .
40 | func Debugf(format string, v ...interface{}) {
41 | mlog.write(LogDebug, format, v...)
42 | }
43 |
44 | // Warning .
45 | func Warning(v ...interface{}) {
46 | mlog.write(LogWarning, fmt.Sprint(v...))
47 | }
48 |
49 | // Warningf .
50 | func Warningf(format string, v ...interface{}) {
51 | mlog.write(LogWarning, format, v...)
52 | }
53 |
54 | // Error .
55 | func Error(v ...interface{}) {
56 | mlog.write(LogError, fmt.Sprint(v...))
57 | }
58 |
59 | // Errorf .
60 | func Errorf(format string, v ...interface{}) {
61 | mlog.write(LogError, format, v...)
62 | }
63 |
64 | // Fatal .
65 | func Fatal(v ...interface{}) {
66 | mlog.write(LogFatal, fmt.Sprint(v...))
67 | }
68 |
69 | // Fatalf .
70 | func Fatalf(format string, v ...interface{}) {
71 | mlog.write(LogFatal, format, v...)
72 | }
73 |
74 | // SetLevelByString 设置日志级别.
75 | func SetLevelByString(level string) {
76 | mlog.SetLevelByString(level)
77 | }
78 |
79 | // SetColor 设置是否显示颜色.
80 | func SetColor(color bool) {
81 | mlog.SetColor(color)
82 | }
83 |
84 | // SetOutputFile 初始化时设置输出文件.
85 | func SetOutputFile(f string) {
86 | mlog.SetOutputFile(f)
87 | }
88 |
89 | // SetRolling 每天生成一个文件.
90 | func SetRolling(on bool) {
91 | mlog.SetRolling(on)
92 | }
93 |
--------------------------------------------------------------------------------
/log/log_test.go:
--------------------------------------------------------------------------------
1 | package log
2 |
3 | import (
4 | "context"
5 | "os"
6 | "testing"
7 | "time"
8 | )
9 |
10 | const (
11 | testLogFile = "./test.log"
12 | )
13 |
14 | func TestLog(t *testing.T) {
15 | Debug("default log begin")
16 | Infof("%v test log", time.Now())
17 |
18 | l := NewLogger()
19 | l.Debug("logger 1111111111")
20 | l.Info("logger 2222222222")
21 | l.Warningf("logger 33333 %v", time.Now())
22 | l.Errorf("logger color %v xxxxxx", time.Now().UnixNano())
23 | l.SetColor(false)
24 | l.Errorf("logger no color %v yyyyyy", time.Now().UnixNano())
25 | Infof("%v default has color test log", time.Now())
26 |
27 | l.SetOutputFile(testLogFile).SetRolling(true)
28 | l.Info(time.Now())
29 | os.Remove(testLogFile)
30 |
31 | }
32 |
33 | func TestLogContext(t *testing.T) {
34 | l := NewLogger()
35 | ctx := context.Background()
36 |
37 | ctx = ToContext(ctx, l)
38 |
39 | l2 := FromContext(ctx)
40 | if l2 == nil {
41 | t.Fatalf("expect l2 != nil, FromContext recv:%v", l2)
42 | }
43 |
44 | l3 := FromContext(context.Background())
45 | if l3 != nil {
46 | t.Fatalf("expect l3 == nil, FromContext recv:%v", l3)
47 | }
48 |
49 | l2.Infof("ok")
50 | }
51 |
--------------------------------------------------------------------------------
/log/writer.go:
--------------------------------------------------------------------------------
1 | package log
2 |
3 | import (
4 | "bufio"
5 | "bytes"
6 | "fmt"
7 | "os"
8 | "runtime"
9 | "sync"
10 | "syscall"
11 | "time"
12 | )
13 |
14 | type pos struct {
15 | file string
16 | function string
17 | }
18 |
19 | // Logger 日志对象.
20 | type Logger struct {
21 | rolling bool
22 | fileName string
23 | fileTime time.Time
24 | file *os.File
25 | out *bufio.Writer
26 | level Level
27 | color bool
28 | posCache map[uintptr]pos
29 | mu sync.Mutex
30 | }
31 |
32 | // NewLogger 创建日志对象.
33 | func NewLogger() *Logger {
34 | return &Logger{
35 | out: bufio.NewWriter(os.Stdout),
36 | level: LogDebug,
37 | color: true,
38 | posCache: make(map[uintptr]pos),
39 | }
40 | }
41 |
42 | // SetColor 开启/关闭颜色.
43 | func (l *Logger) SetColor(on bool) *Logger {
44 | if l == nil {
45 | return nil
46 | }
47 | l.color = on
48 | return l
49 | }
50 |
51 | // SetRolling 每天生成一个文件.
52 | func (l *Logger) SetRolling(on bool) *Logger {
53 | if l == nil {
54 | return nil
55 | }
56 |
57 | l.rolling = on
58 | return l
59 | }
60 |
61 | // SetOutputFile 初始化时设置输出文件.
62 | func (l *Logger) SetOutputFile(path string) *Logger {
63 | f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
64 | if err != nil {
65 | panic(fmt.Sprintf("open %s error:%v", path, err.Error()))
66 | }
67 |
68 | now, _ := time.ParseInLocation("20060102", time.Now().Format("20060102"), time.Local)
69 | l.fileTime = now.Add(time.Hour * 24)
70 | l.file = f
71 | l.fileName = path
72 | l.out = bufio.NewWriter(f)
73 |
74 | return l
75 | }
76 |
77 | // SetLevel 设置日志级别.
78 | func (l *Logger) SetLevel(level Level) *Logger {
79 | if l == nil {
80 | return nil
81 | }
82 | l.level = level
83 | return l
84 | }
85 |
86 | // SetLevelByString 设置字符串格式的日志级别.
87 | func (l *Logger) SetLevelByString(level string) *Logger {
88 | if l == nil {
89 | return nil
90 | }
91 | l.level = stringToLevel(level)
92 | return l
93 | }
94 |
95 | func (l *Logger) caller() (string, string) {
96 | pc, file, line, _ := runtime.Caller(3)
97 |
98 | p, ok := l.posCache[pc]
99 | if ok {
100 | return p.file, p.function
101 | }
102 |
103 | name := runtime.FuncForPC(pc).Name()
104 | if i := bytes.LastIndexAny([]byte(name), "."); i != -1 {
105 | name = name[i+1:]
106 | }
107 | if i := bytes.LastIndexAny([]byte(file), "/"); i != -1 {
108 | file = file[i+1:]
109 | }
110 |
111 | p = pos{file: fmt.Sprintf("%s:%d", file, line), function: name}
112 | l.posCache[pc] = p
113 |
114 | return p.file, p.function
115 | }
116 |
117 | func (l *Logger) rotate(now time.Time) {
118 | if !l.rolling || l.file == nil || now.Before(l.fileTime) {
119 | return
120 | }
121 |
122 | l.out.Flush()
123 |
124 | oldFile := l.fileName + "." + now.AddDate(0, 0, -1).Format("20060102")
125 |
126 | syscall.Flock(int(l.file.Fd()), syscall.LOCK_EX)
127 | if _, err := os.Stat(oldFile); err != nil {
128 | os.Rename(l.fileName, oldFile)
129 | }
130 | syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN)
131 |
132 | l.file.Close()
133 |
134 | l.SetOutputFile(l.fileName)
135 | }
136 |
137 | func (l *Logger) write(t Level, format string, argv ...interface{}) {
138 | if l == nil {
139 | return
140 | }
141 | if t > l.level {
142 | return
143 | }
144 |
145 | l.mu.Lock()
146 | defer l.mu.Unlock()
147 |
148 | now := time.Now()
149 | l.rotate(now)
150 | date := now.Format("2006/01/02 15:04:05")
151 |
152 | file, function := l.caller()
153 |
154 | //时间,源码文件和行号
155 | l.out.WriteString(date)
156 | l.out.WriteString(" ")
157 | l.out.WriteString(file)
158 | l.out.WriteString(" ")
159 |
160 | if l.color {
161 | //颜色开始
162 | l.out.WriteString(t.color())
163 | }
164 |
165 | //日志级别
166 | l.out.WriteString(t.String())
167 |
168 | l.out.WriteString(" ")
169 |
170 | //函数名
171 | l.out.WriteString(function)
172 |
173 | l.out.WriteString(" ")
174 | //日志正文
175 | fmt.Fprintf(l.out, format, argv...)
176 |
177 | if l.color {
178 | //颜色结束
179 | l.out.WriteString("\033[0m")
180 | }
181 |
182 | l.out.WriteString("\n")
183 |
184 | l.out.Flush()
185 | }
186 |
187 | // Info .
188 | func (l *Logger) Info(v ...interface{}) {
189 | if l == nil {
190 | return
191 | }
192 | l.write(LogInfo, fmt.Sprint(v...))
193 | }
194 |
195 | // Infof .
196 | func (l *Logger) Infof(format string, v ...interface{}) {
197 | if l == nil {
198 | return
199 | }
200 | l.write(LogInfo, format, v...)
201 | }
202 |
203 | // Debug .
204 | func (l *Logger) Debug(v ...interface{}) {
205 | if l != nil {
206 | return
207 | }
208 | l.write(LogDebug, fmt.Sprint(v...))
209 | }
210 |
211 | // Debugf .
212 | func (l *Logger) Debugf(format string, v ...interface{}) {
213 | if l == nil {
214 | return
215 | }
216 | l.write(LogDebug, format, v...)
217 | }
218 |
219 | // Warning .
220 | func (l *Logger) Warning(v ...interface{}) {
221 | if l == nil {
222 | return
223 | }
224 | l.write(LogWarning, fmt.Sprint(v...))
225 | }
226 |
227 | // Warningf .
228 | func (l *Logger) Warningf(format string, v ...interface{}) {
229 | if l == nil {
230 | return
231 | }
232 | l.write(LogWarning, format, v...)
233 | }
234 |
235 | // Error .
236 | func (l *Logger) Error(v ...interface{}) {
237 | if l == nil {
238 | return
239 | }
240 | l.write(LogError, fmt.Sprint(v...))
241 | }
242 |
243 | // Errorf .
244 | func (l *Logger) Errorf(format string, v ...interface{}) {
245 | if l == nil {
246 | return
247 | }
248 | l.write(LogError, format, v...)
249 | }
250 |
251 | // Fatal .
252 | func (l *Logger) Fatal(v ...interface{}) {
253 | if l == nil {
254 | return
255 | }
256 | l.write(LogFatal, fmt.Sprint(v...))
257 | os.Exit(-1)
258 | }
259 |
260 | // Fatalf .
261 | func (l *Logger) Fatalf(format string, v ...interface{}) {
262 | if l == nil {
263 | return
264 | }
265 | l.write(LogFatal, format, v...)
266 | os.Exit(-1)
267 | }
268 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "flag"
5 | "fmt"
6 | "net/http"
7 | "time"
8 |
9 | "dearcode.net/crab/http/client"
10 | "dearcode.net/crab/http/server"
11 | )
12 |
13 | type regexpTest struct {
14 | }
15 |
16 | func (rt *regexpTest) GET(w http.ResponseWriter, req *http.Request) {
17 | user, ok := server.RESTValue(req, "user")
18 | if !ok {
19 | panic("context `user` not found")
20 | }
21 | id, ok := server.RESTValue(req, "id")
22 | if !ok {
23 | panic("context `user` not found")
24 | }
25 |
26 | _, ok = server.RESTValue(req, "idx")
27 | if ok {
28 | panic("find invalid key idx")
29 | }
30 |
31 | fmt.Fprintf(w, "user:%v, id:%v", user, id)
32 | }
33 |
34 | type index struct {
35 | r *http.Request
36 | }
37 |
38 | func (i *index) GET(w http.ResponseWriter, req *http.Request) {
39 | i.r = req
40 | w.Write([]byte(fmt.Sprintf("client:%v addr:%p", i.r.RemoteAddr, i)))
41 | }
42 |
43 | func testRESTClient() {
44 | url := "http://127.0.0.1:9000/regexp/mailchina/test/"
45 | //url := fmt.Sprintf("http://127.0.0.1:9000/regexp/mailchina/test/u1%v", time.Now().UnixNano())
46 | buf, err := client.New().Get(url, nil, nil)
47 | if err != nil {
48 | panic(err.Error())
49 | }
50 | fmt.Printf("response:%s\n", buf)
51 | }
52 |
53 | func testHTTPClient() {
54 | url := fmt.Sprintf("http://127.0.0.1:9000/main/index/?id=111&tm=%v", time.Now().UnixNano())
55 | buf, err := client.New().Get(url, nil, nil)
56 | if err != nil {
57 | panic(err.Error())
58 | }
59 | fmt.Printf("response:%s\n", buf)
60 | }
61 |
62 | func testHandler(w http.ResponseWriter, req *http.Request) {
63 | fmt.Fprintf(w, "testHandler:%v", req.RemoteAddr)
64 | }
65 |
66 | func main() {
67 | addr := flag.String("h", ":9000", "api listen address")
68 | flag.Parse()
69 |
70 | server.Register(&index{})
71 | server.RegisterPrefix(®expTest{}, "/regexp/{user}/test/{id}")
72 |
73 | server.RegisterHandler(testHandler, "GET", "/testHandler/")
74 |
75 | ln, err := server.Start(*addr)
76 | if err != nil {
77 | panic(err.Error())
78 | }
79 |
80 | fmt.Printf("listen:%v\n", ln.Addr())
81 |
82 | for i := 0; i < 5; i++ {
83 | time.Sleep(time.Second)
84 | testHTTPClient()
85 | testRESTClient()
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/meta/common.go:
--------------------------------------------------------------------------------
1 | package meta
2 |
3 | import (
4 | "github.com/juju/errors"
5 | )
6 |
7 | var (
8 | //ErrNotFound db not found.
9 | ErrNotFound = errors.New("not found")
10 | //ErrArgNotPtr argument is not ptr.
11 | ErrArgNotPtr = errors.New("argument not ptr")
12 | //ErrFieldNotFound struct's field not found.
13 | ErrFieldNotFound = errors.New("struct's field not found")
14 | //ErrArgIsNil argument is nil.
15 | ErrArgIsNil = errors.New("argument is nil")
16 | )
17 |
--------------------------------------------------------------------------------
/orm/db.go:
--------------------------------------------------------------------------------
1 | package orm
2 |
3 | import (
4 | "database/sql"
5 | "fmt"
6 | "strings"
7 |
8 | "github.com/juju/errors"
9 | //mysql
10 | _ "github.com/go-sql-driver/mysql"
11 | )
12 |
13 | // DB db instance.
14 | type DB struct {
15 | IP string
16 | Port int
17 | DBName string
18 | UserName string
19 | Passwd string
20 | Charset string
21 | Timeout int
22 | }
23 |
24 | // NewDB create db instance, timeout 单位:秒.
25 | func NewDB(ip string, port int, dbName, user, pass, charset string, timeout int) *DB {
26 | return &DB{
27 | IP: ip,
28 | Port: port,
29 | DBName: dbName,
30 | UserName: user,
31 | Passwd: pass,
32 | Charset: charset,
33 | Timeout: timeout,
34 | }
35 | }
36 |
37 | // GetConnection open new connect to db.
38 | func (db *DB) GetConnection() (*sql.DB, error) {
39 | dsn := db.getDSN()
40 | stmtDB, err := sql.Open("mysql", dsn)
41 | if err != nil {
42 | return nil, errors.Trace(err)
43 | }
44 |
45 | stmtDB.SetMaxOpenConns(0)
46 | if err := stmtDB.Ping(); err != nil {
47 | return nil, errors.Trace(err)
48 | }
49 |
50 | return stmtDB, nil
51 | }
52 |
53 | const (
54 | maxAllowedPacket = 134217728
55 | )
56 |
57 | func (db *DB) getDSN() string {
58 | dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", db.UserName, db.Passwd, db.IP, db.Port, db.DBName)
59 |
60 | if optStr := db.getOpt(); optStr != "" {
61 | dsn = fmt.Sprintf("%s?%s", dsn, optStr)
62 | }
63 |
64 | return dsn
65 | }
66 |
67 | func (db *DB) getOpt() string {
68 | var opts []string
69 |
70 | if len(db.Charset) > 0 {
71 | opts = append(opts, fmt.Sprintf("charset=%s", db.Charset))
72 | }
73 |
74 | if db.Timeout > 0 {
75 | opts = append(opts, fmt.Sprintf("timeout=%ds", db.Timeout))
76 | }
77 |
78 | opts = append(opts, fmt.Sprintf("maxAllowedPacket=%d", maxAllowedPacket))
79 |
80 | return strings.Join(opts, "&")
81 | }
82 |
--------------------------------------------------------------------------------
/orm/orm.go:
--------------------------------------------------------------------------------
1 | package orm
2 |
3 | import (
4 | "bytes"
5 | "database/sql"
6 | "fmt"
7 | "reflect"
8 | "strings"
9 |
10 | "github.com/juju/errors"
11 |
12 | "dearcode.net/crab/log"
13 | "dearcode.net/crab/meta"
14 | "dearcode.net/crab/util/str"
15 | )
16 |
17 | // Stmt db stmt.
18 | type Stmt struct {
19 | table string
20 | where string
21 | sort string
22 | order string
23 | group string
24 | offset int
25 | limit int
26 | raw string
27 | db *sql.DB
28 | logger *log.Logger
29 | }
30 |
31 | // IsNotFound error为not found.
32 | func IsNotFound(err error) bool {
33 | return errors.Cause(err) == meta.ErrNotFound
34 | }
35 |
36 | // NewStmt new db stmt.
37 | func NewStmt(db *sql.DB, table string) *Stmt {
38 | return &Stmt{
39 | table: table,
40 | db: db,
41 | }
42 | }
43 |
44 | // SetLogger 输出log.
45 | func (s *Stmt) SetLogger(l *log.Logger) *Stmt {
46 | s.logger = l
47 | return s
48 | }
49 |
50 | // Where 添加查询条件
51 | func (s *Stmt) Where(f string, args ...interface{}) *Stmt {
52 | if len(args) > 0 {
53 | s.where = fmt.Sprintf(f, args...)
54 | } else {
55 | s.where = f
56 | }
57 | return s
58 | }
59 |
60 | // Sort 添加sort
61 | func (s *Stmt) Sort(sort string) *Stmt {
62 | s.sort = sort
63 | return s
64 | }
65 |
66 | // Group 添加group by.
67 | func (s *Stmt) Group(group string) *Stmt {
68 | s.group = group
69 | return s
70 | }
71 |
72 | // Order 添加order
73 | func (s *Stmt) Order(order string) *Stmt {
74 | s.order = order
75 | return s
76 | }
77 |
78 | // Offset 添加offset
79 | func (s *Stmt) Offset(offset int) *Stmt {
80 | s.offset = offset
81 | return s
82 | }
83 |
84 | // Limit 添加limit
85 | func (s *Stmt) Limit(limit int) *Stmt {
86 | s.limit = limit
87 | return s
88 | }
89 |
90 | // sqlQueryBuilder build sql query.
91 | func (s *Stmt) sqlQueryBuilder(result interface{}) (string, error) {
92 | rt := reflect.TypeOf(result)
93 | if rt.Kind() != reflect.Ptr {
94 | return "", fmt.Errorf("result type must be ptr, recv:%v", rt.Kind())
95 | }
96 |
97 | //ptr
98 | rt = rt.Elem()
99 | if rt.Kind() == reflect.Slice {
100 | rt = rt.Elem()
101 | } else {
102 | //只查一条加上limit 1
103 | s.limit = 1
104 | }
105 |
106 | //empty struct
107 | if rt.NumField() == 0 {
108 | return "", fmt.Errorf("result not found field")
109 | }
110 |
111 | return s.sqlQuery(rt), nil
112 | }
113 |
114 | func (s *Stmt) addWhere(w string) {
115 | if s.where != "" {
116 | s.where += " and "
117 | }
118 | s.where += w
119 | }
120 |
121 | // sqlOption where, order, limit
122 | func (s *Stmt) sqlOption(bs *bytes.Buffer) *bytes.Buffer {
123 | if s.where != "" {
124 | fmt.Fprintf(bs, " where %s", s.where)
125 | }
126 |
127 | if s.sort != "" {
128 | fmt.Fprintf(bs, " order by %s", s.sort)
129 | if s.order != "" {
130 | fmt.Fprintf(bs, " %s", s.order)
131 | }
132 | }
133 |
134 | if s.group != "" {
135 | fmt.Fprintf(bs, " group by %s", s.group)
136 | }
137 |
138 | if s.limit > 0 {
139 | bs.WriteString(" limit ")
140 | if s.offset > 0 {
141 | fmt.Fprintf(bs, "%d,", s.offset)
142 | }
143 | fmt.Fprintf(bs, "%d", s.limit)
144 | }
145 | return bs
146 | }
147 |
148 | // sqlCount 根据条件及结构生成查询sql
149 | func (s *Stmt) sqlCount() string {
150 | bs := bytes.NewBufferString("select count(*) from ")
151 | bs.WriteString(s.table)
152 |
153 | s.sqlOption(bs)
154 |
155 | sql := bs.String()
156 | s.logger.Debugf("sql:%v", sql)
157 | return sql
158 | }
159 |
160 | // sqlColumn 生成查询需要的列,目前只是内部用.
161 | func (s *Stmt) sqlColumn(rt reflect.Type, table string) string {
162 | bs := bytes.NewBufferString("")
163 |
164 | for i := 0; i < rt.NumField(); i++ {
165 | f := rt.Field(i)
166 | if f.PkgPath != "" && !f.Anonymous { // unexported
167 | continue
168 | }
169 | switch f.Type.Kind() {
170 | case reflect.Struct:
171 | if f.Tag.Get("db_table") == "one" {
172 | s.table += ","
173 | field := str.FieldEscape(f.Name)
174 | s.table += field
175 | bs.WriteString(s.sqlColumn(f.Type, str.FieldEscape(f.Name)))
176 |
177 | if rf := f.Tag.Get("db_relation_field"); rf != "" {
178 | s.addWhere(fmt.Sprintf("%s.%s = %s.id", table, rf, field))
179 | } else {
180 | s.addWhere(fmt.Sprintf("%s.%s_id = %s.id", table, field, field))
181 | }
182 |
183 | continue
184 | }
185 | case reflect.Slice:
186 | continue
187 | }
188 |
189 | attr := ""
190 | name := f.Tag.Get("db")
191 |
192 | if kv := strings.Split(name, ","); len(kv) == 2 {
193 | name = kv[0]
194 | attr = kv[1]
195 | }
196 |
197 | if name == "" {
198 | name = str.FieldEscape(f.Name)
199 | }
200 |
201 | switch attr {
202 | case "distinct":
203 | bs.WriteString("distinct ")
204 | }
205 |
206 | if !strings.Contains(name, ".") && !strings.Contains(name, "(") {
207 | fmt.Fprintf(bs, "%s.", table)
208 | }
209 | fmt.Fprintf(bs, "%s, ", name)
210 | }
211 |
212 | return bs.String()
213 | }
214 |
215 | // sqlQuery 根据条件及结构生成查询sql
216 | func (s *Stmt) sqlQuery(rt reflect.Type) string {
217 | if s.raw != "" {
218 | return s.raw
219 | }
220 |
221 | bs := bytes.NewBufferString("select ")
222 | firstTable := strings.Split(s.table, ",")[0]
223 | bs.WriteString(s.sqlColumn(rt, firstTable))
224 | bs.Truncate(bs.Len() - 2)
225 | bs.WriteString(" from ")
226 | bs.WriteString(s.table)
227 |
228 | s.sqlOption(bs)
229 | sql := bs.String()
230 |
231 | s.logger.Debugf("sql:%v", sql)
232 |
233 | return sql
234 | }
235 |
236 | func (s *Stmt) firstTable() string {
237 | if idx := strings.Index(s.table, ","); idx > -1 {
238 | return s.table[:idx]
239 | }
240 | return s.table
241 | }
242 |
243 | // addRelation 添加多表关联条件
244 | func (s *Stmt) addRelation(t1, t2 string, id interface{}) *Stmt {
245 | t1 = str.FieldEscape(t1)
246 | t2 = str.FieldEscape(t2)
247 | s.addWhere(fmt.Sprintf("id in (select %s_id from %s_%s_relation where %s_id=%d)", t1, t2, t1, t2, id))
248 | return s
249 | }
250 |
251 | // addOne2More 添加一对多关联条件
252 | func (s *Stmt) addOne2More(t1, t2 string, id interface{}) *Stmt {
253 | t1 = str.FieldEscape(t1)
254 | t2 = str.FieldEscape(t2)
255 | s.addWhere(fmt.Sprintf("%s.%s_id=%d", t1, t2, id))
256 | return s
257 | }
258 |
259 | // Query 根据传入的result结构,生成查询sql,并返回执行结果, result 必需是一个指向切片的指针.
260 | func (s *Stmt) Query(result interface{}) error {
261 | if result == nil {
262 | return meta.ErrArgIsNil
263 | }
264 |
265 | rt := reflect.TypeOf(result)
266 |
267 | if rt.Kind() != reflect.Ptr {
268 | return errors.Trace(meta.ErrArgNotPtr)
269 | }
270 |
271 | //ptr
272 | rt = rt.Elem()
273 | if rt.Kind() == reflect.Slice {
274 | rt = rt.Elem()
275 | } else {
276 | //只查一条加上limit 1
277 | s.limit = 1
278 | }
279 |
280 | //empty struct
281 | if rt.NumField() == 0 {
282 | return fmt.Errorf("result not found field")
283 | }
284 |
285 | sql := s.sqlQuery(rt)
286 |
287 | rows, err := s.db.Query(sql)
288 | if err != nil {
289 | return errors.Annotatef(err, sql)
290 | }
291 | defer rows.Close()
292 |
293 | rv := reflect.ValueOf(result).Elem()
294 |
295 | for rows.Next() {
296 | var refs []interface{}
297 | obj := reflect.New(rt).Elem()
298 | var idx int
299 |
300 | for i := 0; i < obj.NumField(); i++ {
301 | f := rt.Field(i)
302 | if f.PkgPath != "" && !f.Anonymous { // unexported
303 | continue
304 | }
305 |
306 | if f.Name == "ID" {
307 | idx = len(refs)
308 | }
309 |
310 | switch f.Type.Kind() {
311 | case reflect.Struct:
312 | if f.Tag.Get("db_table") == "one" {
313 | //一对一,这里代码重复是为了减少交互.
314 | for j := 0; j < obj.Field(i).NumField(); j++ {
315 | sf := rt.Field(i).Type.Field(j)
316 | if sf.PkgPath != "" && !sf.Anonymous { // unexported
317 | continue
318 | }
319 | if sf.Type.Kind() == reflect.Slice {
320 | continue
321 | }
322 |
323 | refs = append(refs, obj.Field(i).Field(j).Addr().Interface())
324 | }
325 | continue
326 | }
327 | case reflect.Slice:
328 | continue
329 | }
330 |
331 | refs = append(refs, obj.Field(i).Addr().Interface())
332 | }
333 |
334 | if err = rows.Scan(refs...); err != nil {
335 | return errors.Trace(err)
336 | }
337 |
338 | //一对多
339 | for i := 0; i < obj.NumField(); i++ {
340 | f := rt.Field(i)
341 | if f.PkgPath != "" && !f.Anonymous { // unexported
342 | continue
343 | }
344 | if f.Type.Kind() != reflect.Slice {
345 | continue
346 | }
347 |
348 | lr := obj.Field(i).Addr().Interface()
349 | id := reflect.ValueOf(refs[idx]).Elem().Interface()
350 |
351 | switch f.Tag.Get("db_table") {
352 | case "more":
353 | //填充一对多结果,每次去查询
354 | if err = NewStmt(s.db, str.FieldEscape(f.Name)).addRelation(f.Name, s.firstTable(), id).Query(lr); err != nil {
355 | if errors.Cause(err) != meta.ErrNotFound {
356 | return errors.Trace(err)
357 | }
358 | }
359 | case "one2more":
360 | //填充一对多结果,每次去查询
361 | if err = NewStmt(s.db, str.FieldEscape(f.Name)).addOne2More(f.Name, s.firstTable(), id).Query(lr); err != nil {
362 | if errors.Cause(err) != meta.ErrNotFound {
363 | return errors.Trace(err)
364 | }
365 | }
366 | }
367 | }
368 |
369 | if rv.Kind() == reflect.Struct {
370 | reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj.Interface()))
371 | s.logger.Debugf("result %#v", result)
372 | return nil
373 | }
374 |
375 | rv = reflect.Append(rv, obj)
376 | }
377 |
378 | if rv.Kind() == reflect.Struct || rv.Len() == 0 {
379 | return errors.Trace(meta.ErrNotFound)
380 | }
381 |
382 | reflect.ValueOf(result).Elem().Set(reflect.ValueOf(rv.Interface()))
383 | s.logger.Debugf("result %v", result)
384 |
385 | return nil
386 | }
387 |
388 | // Count 查询总数.
389 | func (s *Stmt) Count() (int64, error) {
390 | rows, err := s.db.Query(s.sqlCount())
391 | if err != nil {
392 | return 0, errors.Trace(err)
393 | }
394 | defer rows.Close()
395 |
396 | rows.Next()
397 |
398 | var n int64
399 | if err = rows.Scan(&n); err != nil {
400 | return 0, errors.Trace(err)
401 | }
402 |
403 | return n, nil
404 | }
405 |
406 | // sqlInsert 添加数据
407 | func (s *Stmt) sqlInsert(rt reflect.Type, rv reflect.Value) (sql string, refs []interface{}) {
408 | bs := bytes.NewBufferString("insert into ")
409 | bs.WriteString(s.table)
410 | bs.WriteString(" (")
411 |
412 | dbs := bytes.NewBufferString(") values (")
413 |
414 | for i := 0; i < rt.NumField(); i++ {
415 | key, val, ok := s.sqlStructField(rt.Field(i), false)
416 | if !ok {
417 | continue
418 | }
419 |
420 | bs.WriteString(key)
421 | bs.WriteString(", ")
422 |
423 | if val != "" {
424 | dbs.WriteString(val)
425 | dbs.WriteString(", ")
426 | continue
427 | }
428 |
429 | dbs.WriteString("?, ")
430 | refs = append(refs, rv.Field(i).Interface())
431 | }
432 |
433 | bs.Truncate(bs.Len() - 2)
434 | dbs.Truncate(dbs.Len() - 2)
435 |
436 | bs.WriteString(dbs.String())
437 |
438 | bs.WriteString(") ")
439 | sql = bs.String()
440 | s.logger.Debugf("sql:%v", sql)
441 | return
442 | }
443 |
444 | // sqlStructField 解析结构体中的字段,根据default及db值内容生成对应key ,value.
445 | func (s *Stmt) sqlStructField(f reflect.StructField, isUpdate bool) (key string, val string, ok bool) {
446 | if f.PkgPath != "" && !f.Anonymous { // unexported
447 | return
448 | }
449 |
450 | switch f.Type.Kind() {
451 | case reflect.Struct, reflect.Slice, reflect.Ptr:
452 | return
453 | }
454 |
455 | if _, ignore := f.Tag.Lookup("db_auto"); ignore {
456 | return
457 | }
458 |
459 | if _, ignore := f.Tag.Lookup("db_ignore"); ignore {
460 | return
461 | }
462 |
463 | if key, ok = f.Tag.Lookup("db"); !ok {
464 | key = str.FieldEscape(f.Name)
465 | }
466 |
467 | //insert 解析default
468 | if !isUpdate {
469 | if val, ok = f.Tag.Lookup("db_default"); ok {
470 | return
471 | }
472 | }
473 |
474 | if val, ok = f.Tag.Lookup("db_const"); ok {
475 | return
476 | }
477 |
478 | return key, "", true
479 | }
480 |
481 | // sqlUpdate 根据条件及结构生成update sql
482 | func (s *Stmt) sqlUpdate(rt reflect.Type, rv reflect.Value) (sql string, refs []interface{}) {
483 | bs := bytes.NewBufferString(fmt.Sprintf("update `%s` set ", s.table))
484 |
485 | for i := 0; i < rt.NumField(); i++ {
486 | key, val, ok := s.sqlStructField(rt.Field(i), true)
487 | if !ok {
488 | continue
489 | }
490 |
491 | if val != "" {
492 | fmt.Fprintf(bs, "`%s`=%s, ", key, val)
493 | continue
494 | }
495 |
496 | fmt.Fprintf(bs, "`%s`=?, ", key)
497 |
498 | refs = append(refs, rv.Field(i).Interface())
499 | }
500 |
501 | bs.Truncate(bs.Len() - 2)
502 |
503 | return s.sqlOption(bs).String(), refs
504 | }
505 |
506 | // Update sql update db.
507 | func (s *Stmt) Update(data interface{}) (int64, error) {
508 | if data == nil {
509 | return 0, errors.Trace(meta.ErrArgIsNil)
510 | }
511 | rt := reflect.TypeOf(data)
512 | rv := reflect.ValueOf(data)
513 |
514 | if rt.Kind() == reflect.Ptr {
515 | rt = rt.Elem()
516 | rv = rv.Elem()
517 | }
518 |
519 | if rt.NumField() == 0 {
520 | return 0, errors.Trace(meta.ErrFieldNotFound)
521 | }
522 |
523 | sql, refs := s.sqlUpdate(rt, rv)
524 | s.logger.Debugf("sql:%v, vals:%#v", sql, refs)
525 | r, err := s.db.Exec(sql, refs...)
526 | if err != nil {
527 | return 0, errors.Trace(err)
528 | }
529 | return r.RowsAffected()
530 | }
531 |
532 | // Insert sql update db.
533 | func (s *Stmt) Insert(data interface{}) (int64, error) {
534 | if data == nil {
535 | return 0, errors.Trace(meta.ErrArgIsNil)
536 | }
537 | rt := reflect.TypeOf(data)
538 | rv := reflect.ValueOf(data)
539 |
540 | if rt.Kind() == reflect.Ptr {
541 | rt = rt.Elem()
542 | rv = rv.Elem()
543 | }
544 |
545 | if rt.NumField() == 0 {
546 | return 0, errors.Trace(meta.ErrFieldNotFound)
547 | }
548 |
549 | sql, refs := s.sqlInsert(rt, rv)
550 | r, err := s.db.Exec(sql, refs...)
551 | if err != nil {
552 | return 0, errors.Trace(err)
553 | }
554 |
555 | return r.LastInsertId()
556 | }
557 |
558 | // BatchInsert 数组插入
559 | func (s *Stmt) BatchInsert(data []interface{}) ([]int64, error) {
560 | fmt.Printf("data:%#v\n", data)
561 | if len(data) == 0 {
562 | return nil, errors.Trace(meta.ErrArgIsNil)
563 | }
564 |
565 | txn, err := s.db.Begin()
566 | if err != nil {
567 | return nil, errors.Trace(err)
568 | }
569 |
570 | defer func() {
571 | if err != nil {
572 | txn.Rollback()
573 | return
574 | }
575 | txn.Commit()
576 | }()
577 |
578 | var ir sql.Result
579 | var ids []int64
580 |
581 | fmt.Printf("data:%#v\n", data)
582 | for _, d := range data {
583 | rt := reflect.TypeOf(d)
584 | rv := reflect.ValueOf(d)
585 |
586 | if rt.Kind() == reflect.Ptr {
587 | rt = rt.Elem()
588 | rv = rv.Elem()
589 | }
590 |
591 | if rt.Kind() != reflect.Struct || rt.NumField() == 0 {
592 | s.logger.Errorf("kind:%v, type:%v", rt.Kind(), rt)
593 | return nil, errors.Trace(meta.ErrFieldNotFound)
594 | }
595 |
596 | stmt, refs := s.sqlInsert(rt, rv)
597 |
598 | if ir, err = txn.Exec(stmt, refs...); err != nil {
599 | return nil, errors.Trace(err)
600 | }
601 |
602 | id, _ := ir.LastInsertId()
603 |
604 | ids = append(ids, id)
605 | }
606 |
607 | return ids, nil
608 | }
609 |
610 | // Exec 保留的原始执行接口.
611 | func (s *Stmt) Exec(query string, args ...interface{}) (int64, error) {
612 | rs, err := s.db.Exec(query, args...)
613 | if err != nil {
614 | return -1, errors.Trace(err)
615 | }
616 | return rs.RowsAffected()
617 | }
618 |
619 | // Raw 原始sql.
620 | func (s *Stmt) Raw(query string, args ...interface{}) *Stmt {
621 | s.raw = fmt.Sprintf(query, args...)
622 | return s
623 | }
624 |
--------------------------------------------------------------------------------
/orm/orm_test.go:
--------------------------------------------------------------------------------
1 | package orm
2 |
3 | import (
4 | "database/sql"
5 | "fmt"
6 | "testing"
7 | "time"
8 |
9 | _ "github.com/go-sql-driver/mysql"
10 | "gopkg.in/DATA-DOG/go-sqlmock.v1"
11 |
12 | "dearcode.net/crab/log"
13 | )
14 |
15 | func TestORMStructDistinct(t *testing.T) {
16 | expect := "select userinfo.id, distinct userinfo.user, avg(age), sum(size), userinfo.password from userinfo limit 1"
17 | result := struct {
18 | ID int64
19 | User string `db:"user,distinct"`
20 | Age int `db:"avg(age)"`
21 | Size int `db:"sum(size)"`
22 | Password string
23 | }{}
24 | sql, err := NewStmt(nil, "userinfo").sqlQueryBuilder(&result)
25 | if err != nil {
26 | t.Fatal(err.Error())
27 | }
28 | if sql != expect {
29 | t.Fatalf("expect:%s,\n recv:%s.", expect, sql)
30 | }
31 | }
32 |
33 | func TestORMStruct(t *testing.T) {
34 | expect := "select userinfo.id, userinfo.user, userinfo.password from userinfo limit 1"
35 | result := struct {
36 | ID int64
37 | User string
38 | Password string
39 | }{}
40 | sql, err := NewStmt(nil, "userinfo").sqlQueryBuilder(&result)
41 | if err != nil {
42 | t.Fatal(err.Error())
43 | }
44 | if sql != expect {
45 | t.Fatalf("expect:%s,\n recv:%s.", expect, sql)
46 | }
47 | }
48 |
49 | func TestORMArray(t *testing.T) {
50 | expect := "select userinfo.id, userinfo.user, userinfo.password from userinfo"
51 | result := []struct {
52 | ID int64
53 | User string
54 | Password string
55 | }{}
56 | s := NewStmt(nil, "userinfo")
57 | sql, err := s.sqlQueryBuilder(&result)
58 | if err != nil {
59 | t.Fatal(err.Error())
60 | }
61 | if sql != expect {
62 | t.Fatalf("expect:%s, \nrecv:%s.", expect, sql)
63 | }
64 | }
65 |
66 | func TestORMSort(t *testing.T) {
67 | expect := "select userinfo.id, userinfo.user, userinfo.password from userinfo order by user"
68 | result := []struct {
69 | ID int64
70 | User string
71 | Password string
72 | }{}
73 | sql, err := NewStmt(nil, "userinfo").Sort("user").sqlQueryBuilder(&result)
74 | if err != nil {
75 | t.Fatal(err.Error())
76 | }
77 | if sql != expect {
78 | t.Fatalf("expect:%s, \nrecv:%s.", expect, sql)
79 | }
80 | }
81 |
82 | func TestORMSortOrder(t *testing.T) {
83 | expect := "select userinfo.id, userinfo.user, userinfo.password from userinfo order by user desc"
84 | result := []struct {
85 | ID int64
86 | User string
87 | Password string
88 | }{}
89 | sql, err := NewStmt(nil, "userinfo").Sort("user").Order("desc").sqlQueryBuilder(&result)
90 | if err != nil {
91 | t.Fatal(err.Error())
92 | }
93 | if sql != expect {
94 | t.Fatalf("expect:%s, \nrecv:%s.", expect, sql)
95 | }
96 | }
97 |
98 | func TestORMLimit(t *testing.T) {
99 | expect := "select userinfo.id, userinfo.user, userinfo.password from userinfo limit 10"
100 | result := []struct {
101 | ID int64
102 | User string
103 | Password string
104 | }{}
105 | sql, err := NewStmt(nil, "userinfo").Limit(10).sqlQueryBuilder(&result)
106 | if err != nil {
107 | t.Fatal(err.Error())
108 | }
109 | if sql != expect {
110 | t.Fatalf("expect:%s, \nrecv:%s.", expect, sql)
111 | }
112 | }
113 |
114 | func TestORMLimitOffset(t *testing.T) {
115 | expect := "select userinfo.id, userinfo.user, userinfo.password from userinfo limit 5,10"
116 | result := []struct {
117 | ID int64
118 | User string
119 | Password string
120 | }{}
121 | sql, err := NewStmt(nil, "userinfo").Limit(10).Offset(5).sqlQueryBuilder(&result)
122 | if err != nil {
123 | t.Fatal(err.Error())
124 | }
125 | if sql != expect {
126 | t.Fatalf("expect:%s, \nrecv:%s.", expect, sql)
127 | }
128 | }
129 |
130 | func TestORMWhere(t *testing.T) {
131 | expect := "select userinfo.id, userinfo.user, userinfo.password from userinfo where id=1010"
132 | result := []struct {
133 | ID int64
134 | User string
135 | Password string
136 | }{}
137 | sql, err := NewStmt(nil, "userinfo").Where("id=1010").sqlQueryBuilder(&result)
138 | if err != nil {
139 | t.Fatal(err.Error())
140 | }
141 | if sql != expect {
142 | t.Fatalf("expect:%s, \nrecv:%s.", expect, sql)
143 | }
144 | }
145 |
146 | func TestORMMutilTalbe(t *testing.T) {
147 | expect := "select userinfo.id, userinfo.user, userinfo.password, ext.qq from userinfo, ext where ext.user_id=userinfo.id and id=1010"
148 | result := []struct {
149 | ID int64
150 | User string
151 | Password string
152 | QQ string `db:"ext.qq"`
153 | }{}
154 | sql, err := NewStmt(nil, "userinfo, ext").Where("ext.user_id=userinfo.id and id=1010").sqlQueryBuilder(&result)
155 | if err != nil {
156 | t.Fatal(err.Error())
157 | }
158 | if sql != expect {
159 | t.Fatalf("expect:%s, \nrecv:%s.", expect, sql)
160 | }
161 | }
162 |
163 | func TestORMQuerySlice(t *testing.T) {
164 | result := []struct {
165 | ID int64
166 | User string
167 | Password string
168 | }{}
169 |
170 | db, mock, err := sqlmock.New()
171 | if err != nil {
172 | t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
173 | }
174 | defer db.Close()
175 |
176 | mock.ExpectQuery("select userinfo.id, userinfo.user, userinfo.password from userinfo order by id desc").WillReturnRows(sqlmock.NewRows([]string{"id", "user", "password"}).AddRow(3, "333", "3333").AddRow(1, "111", "1111"))
177 |
178 | if err = NewStmt(db, "userinfo").Sort("id").Order("desc").Query(&result); err != nil {
179 | t.Fatal(err.Error())
180 | }
181 |
182 | t.Logf("result:%+v", result)
183 | }
184 |
185 | func TestORMQueryOne(t *testing.T) {
186 | result := struct {
187 | ID int64
188 | User string
189 | Password string
190 | }{}
191 |
192 | db, mock, err := sqlmock.New()
193 | if err != nil {
194 | t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
195 | }
196 | defer db.Close()
197 |
198 | mock.ExpectQuery("select userinfo.id, userinfo.user, userinfo.password from userinfo where id=(.+) limit 1").WillReturnRows(sqlmock.NewRows([]string{"id", "user", "password"}).AddRow(2, "tgy", "123456"))
199 |
200 | if err = NewStmt(db, "userinfo").Where("id=2").Query(&result); err != nil {
201 | t.Fatal(err.Error())
202 | }
203 |
204 | t.Logf("result:%+v", result)
205 | }
206 |
207 | func TestORMUpdate(t *testing.T) {
208 | data := struct {
209 | ClusterIDs []int64 `db_ignore:""`
210 | User string
211 | Password string
212 | UpdateTime string `db:"utime" db_const:"now()"`
213 | }{
214 | User: fmt.Sprintf("new_user_%d", time.Now().Unix()),
215 | Password: fmt.Sprintf("new_password_%d", time.Now().Unix()),
216 | }
217 |
218 | db, mock, err := sqlmock.New()
219 | if err != nil {
220 | t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
221 | }
222 | defer db.Close()
223 |
224 | mock.ExpectExec("update `userinfo` set `user`=(.+), `password`=(.+) where id=(.+)").WithArgs(data.User, data.Password).WillReturnResult(sqlmock.NewResult(0, 1))
225 |
226 | id, err := NewStmt(db, "userinfo").SetLogger(log.GetLogger()).Where("id=2").Update(&data)
227 | if err != nil {
228 | t.Fatal(err.Error())
229 | }
230 |
231 | t.Logf("affected row:%+v", id)
232 | }
233 |
234 | func TestORMInsert(t *testing.T) {
235 | data := struct {
236 | ID int64 `db_defult:"auto"`
237 | ClusterIDs []int64 `db_ignore:""`
238 | User string
239 | Password string
240 | }{
241 | User: fmt.Sprintf("user_%d", time.Now().Unix()),
242 | Password: fmt.Sprintf("password_%d", time.Now().Unix()),
243 | }
244 |
245 | db, mock, err := sqlmock.New()
246 | if err != nil {
247 | t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
248 | }
249 | defer db.Close()
250 |
251 | mock.ExpectExec("").WillReturnResult(sqlmock.NewResult(1, 0))
252 |
253 | id, err := NewStmt(db, "userinfo").Insert(&data)
254 | if err != nil {
255 | t.Fatal(err.Error())
256 | }
257 |
258 | t.Logf("new id:%+v", id)
259 | }
260 |
261 | func TestORMSubStruct(t *testing.T) {
262 | site := []struct {
263 | ID int64 `db_defult:"auto"`
264 | Name string
265 | UserID sql.NullInt64
266 | List struct {
267 | ID int64
268 | Name string
269 | } `db_table:"one"`
270 |
271 | App struct {
272 | ID int64
273 | Name string
274 | } `db_table:"one" db_relation_field:"application_id"`
275 |
276 | Filter []struct {
277 | ID int64
278 | Key1 string
279 | Key2 string
280 | } `db_table:"more"`
281 | }{}
282 |
283 | sql := "select site.id, site.name, site.user_id, list.id, list.name, app.id, app.name from site,list,app where site.list_id = list.id and site.application_id = app.id"
284 |
285 | str, err := NewStmt(nil, "site").sqlQueryBuilder(&site)
286 | if err != nil {
287 | t.Fatal(err.Error())
288 | }
289 | if str != sql {
290 | t.Fatalf("\n\texpect:%s\n\t recv:%s", sql, str)
291 | }
292 |
293 | t.Logf("sql:%+v", str)
294 | }
295 |
--------------------------------------------------------------------------------
/project.ini:
--------------------------------------------------------------------------------
1 | #按F9对应Make
2 | Make=make
3 | #按F8对应Test
4 | Test=make test
5 | #按,f对应格式化
6 | Format=go fmt
7 | #生成tags时跳过以下目录
8 | ExcludePath=docs,bin,Godeps,_vendor,vendor,logs
9 | #生成tags时跳过以下类型文件
10 | CtagsExcludeFile=*.cc,*.c,*.h,*.go
11 | #查找文件时跳过以下类型文件
12 | FindExcludeFile=*.cc,*.c,*.h,*_test.go
13 | #扩展tags目录
14 | #CtagsExtendFile=/usr/local/go/src
15 |
--------------------------------------------------------------------------------
/skiplist/skiplist.go:
--------------------------------------------------------------------------------
1 | package skiplist
2 |
3 | import (
4 | "bytes"
5 | "fmt"
6 | "math/rand"
7 | "time"
8 | )
9 |
10 | type node struct {
11 | key string
12 | value interface{}
13 | level int
14 | next []*node
15 | }
16 |
17 | type nodes *node
18 |
19 | type Skiplist struct {
20 | maxLevel int
21 | lists []nodes
22 | rand *rand.Rand
23 | }
24 |
25 | func New(maxLevel int) *Skiplist {
26 |
27 | lists := make([]nodes, maxLevel)
28 | first := &node{
29 | next: make([]*node, maxLevel),
30 | }
31 | for i := range lists {
32 | lists[i] = first
33 | }
34 |
35 | return &Skiplist{
36 | maxLevel: maxLevel,
37 | lists: lists,
38 | rand: rand.New(rand.NewSource(time.Now().UnixNano())),
39 | }
40 | }
41 |
42 | func (s *Skiplist) Insert(key string, value interface{}) {
43 | pns := s.priorNodes(key)
44 | level := s.randomLevel()
45 |
46 | n := &node{
47 | key: key,
48 | value: value,
49 | level: level,
50 | next: make([]*node, s.maxLevel),
51 | }
52 |
53 | for i := level; i < s.maxLevel; i++ {
54 | n.next[i] = pns[i].next[i]
55 | pns[i].next[i] = n
56 | }
57 | }
58 |
59 | func (s *Skiplist) Delete(key string) {
60 |
61 | }
62 |
63 | func (s *Skiplist) Get(key string) interface{} {
64 | ptr := s.lists[0]
65 | for i := range s.lists {
66 | for ; ptr != nil; ptr = ptr.next[i] {
67 | if ptr.key == key {
68 | return ptr.value
69 | }
70 | if ptr.next[i] == nil || key < ptr.next[i].key {
71 | break
72 | }
73 | }
74 | }
75 |
76 | return nil
77 | }
78 |
79 | func (s *Skiplist) randomLevel() int {
80 | return s.rand.Intn(s.maxLevel)
81 | }
82 |
83 | func (s *Skiplist) priorNodes(key string) []*node {
84 | pns := make([]*node, s.maxLevel)
85 | ptr := s.lists[0]
86 | for i := range s.lists {
87 | pns[i] = ptr
88 | for ; ptr != nil; ptr = ptr.next[i] {
89 | if key < ptr.key || ptr.next[i] == nil {
90 | pns[i] = ptr
91 | break
92 | }
93 | }
94 | }
95 | return pns
96 | }
97 |
98 | func (s *Skiplist) String() string {
99 | buf := bytes.NewBuffer(nil)
100 |
101 | for i := range s.lists {
102 | fmt.Fprintf(buf, "level[%v]:", i)
103 | nodes := s.lists[i]
104 | for ptr := nodes; ptr != nil; ptr = ptr.next[i] {
105 | fmt.Fprintf(buf, "key[%d]:%s\t", ptr.level, ptr.key)
106 | }
107 | fmt.Fprintf(buf, "\n")
108 | }
109 | return buf.String()
110 | }
111 |
--------------------------------------------------------------------------------
/skiplist/skiplist_test.go:
--------------------------------------------------------------------------------
1 | package skiplist
2 |
3 | import (
4 | "fmt"
5 | "testing"
6 | )
7 |
8 | func TestInsert(t *testing.T) {
9 | table := New(3)
10 | table.Insert("aaaaaaaaaaa", 111111)
11 | fmt.Printf("---------%v\n", table)
12 | }
13 |
--------------------------------------------------------------------------------
/util/aes/ecb.go:
--------------------------------------------------------------------------------
1 | package aes
2 |
3 | import (
4 | "bytes"
5 | "crypto/aes"
6 | "crypto/cipher"
7 | "encoding/base64"
8 | "strings"
9 |
10 | "github.com/juju/errors"
11 | )
12 |
13 | func byteKey(key string) []byte {
14 | for i := 16; i < 33; i += 8 {
15 | key += strings.Repeat("\x00", i-len(key))
16 | if len(key) == i {
17 | break
18 | }
19 | }
20 | return []byte(key)
21 | }
22 |
23 | // Encrypt aes 加密.
24 | func Encrypt(encodeStr string, key string) (string, error) {
25 | encodeBytes := []byte(encodeStr)
26 | encodeKey := byteKey(key)
27 | block, err := aes.NewCipher(encodeKey)
28 | if err != nil {
29 | return "", errors.Trace(err)
30 | }
31 | blockSize := block.BlockSize()
32 | encodeBytes = pkcs5Padding(encodeBytes, blockSize)
33 | blockMode := cipher.NewCBCEncrypter(block, encodeKey[:blockSize])
34 | crypted := make([]byte, len(encodeBytes))
35 | blockMode.CryptBlocks(crypted, encodeBytes)
36 |
37 | return base64.RawURLEncoding.EncodeToString(crypted), nil
38 | }
39 |
40 | // Decrypt aes 解密.
41 | func Decrypt(decodeStr string, key string) (string, error) {
42 | decodeKey := byteKey(key)
43 | decodeBytes, err := base64.RawURLEncoding.DecodeString(decodeStr)
44 | if err != nil {
45 | return "", errors.Trace(err)
46 | }
47 | block, err := aes.NewCipher(decodeKey)
48 | if err != nil {
49 | return "", errors.Trace(err)
50 | }
51 | blockSize := block.BlockSize()
52 | blockMode := cipher.NewCBCDecrypter(block, decodeKey[:blockSize])
53 | origData := make([]byte, len(decodeBytes))
54 | blockMode.CryptBlocks(origData, decodeBytes)
55 | origData, err = pkcs5UnPadding(origData)
56 | if err != nil {
57 | return "", errors.Trace(err)
58 | }
59 |
60 | return string(origData), nil
61 | }
62 |
63 | // pkcs5Padding pkcs5 添加数据.
64 | func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
65 | padding := blockSize - len(ciphertext)%blockSize
66 | padtext := bytes.Repeat([]byte{byte(padding)}, padding)
67 | return append(ciphertext, padtext...)
68 | }
69 |
70 | // pkcs5UnPadding pkcs5 删除数据.
71 | func pkcs5UnPadding(origData []byte) ([]byte, error) {
72 | length := len(origData)
73 | if length < 1 {
74 | return nil, errors.New("origData is empty")
75 | }
76 |
77 | unpadding := int(origData[length-1])
78 | if unpadding > length {
79 | return nil, errors.New("invalid origData")
80 | }
81 | return origData[:(length - unpadding)], nil
82 | }
83 |
--------------------------------------------------------------------------------
/util/aes/ecb_test.go:
--------------------------------------------------------------------------------
1 | package aes
2 |
3 | import (
4 | "github.com/juju/errors"
5 | "testing"
6 | )
7 |
8 | func TestAes(t *testing.T) {
9 | src := "test"
10 | key := "1qaz@WSX"
11 |
12 | crypted, err := Encrypt(src, key)
13 | if err != nil {
14 | t.Fatalf("AesEncrypt error:%s", errors.ErrorStack(err))
15 | }
16 | t.Logf("dest:%v", crypted)
17 | deSrc, err := Decrypt(crypted, key)
18 | if err != nil {
19 | t.Fatalf("AesDecrypt error:%s", err.Error())
20 | }
21 | t.Logf("desc:%s", deSrc)
22 | }
23 |
24 | func TestDesPanic(t *testing.T) {
25 | key := "1qaz@WSX"
26 | crypted := "hX0Y5u-EkggQstomvCXgQw=="
27 | deSrc, err := Decrypt(crypted, key)
28 | if err != nil {
29 | t.Logf("err:%v", err)
30 | }
31 |
32 | t.Logf("%s", deSrc)
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/util/str/str.go:
--------------------------------------------------------------------------------
1 | package str
2 |
3 | import (
4 | "strings"
5 | "unicode"
6 | )
7 |
8 | // TrimSplit 按sep拆分,并去掉空字符.
9 | func TrimSplit(raw, sep string) []string {
10 | var ss []string
11 | for _, val := range strings.Split(raw, sep) {
12 | if s := strings.TrimSpace(val); s != "" {
13 | ss = append(ss, s)
14 | }
15 | }
16 | return ss
17 | }
18 |
19 | // FieldEscape 转换为小写下划线分隔
20 | func FieldEscape(k string) string {
21 | buf := []byte{}
22 | up := true
23 | for _, c := range k {
24 | if unicode.IsUpper(c) {
25 | if !up {
26 | buf = append(buf, '_')
27 | }
28 | c += 32
29 | up = true
30 | } else {
31 | up = false
32 | }
33 |
34 | buf = append(buf, byte(c))
35 | }
36 | return string(buf)
37 | }
38 |
--------------------------------------------------------------------------------
/uuid/uuid.go:
--------------------------------------------------------------------------------
1 | package uuid
2 |
3 | import (
4 | "encoding/base32"
5 | "encoding/binary"
6 | "fmt"
7 | "net"
8 | "os"
9 | "sync/atomic"
10 | "time"
11 | )
12 |
13 | const encodeStr = "123456789abcdefghijklmnpqrstuvxy"
14 |
15 | var (
16 | seq uint32
17 | initBuf = make([]byte, 14)
18 | encoding = base32.NewEncoding(encodeStr).WithPadding(base32.NoPadding)
19 | )
20 |
21 | func init() {
22 | addrs, _ := net.InterfaceAddrs()
23 | for _, addr := range addrs {
24 | if i, ok := addr.(*net.IPNet); ok {
25 | if !i.IP.IsLoopback() {
26 | i4 := i.IP.To4()
27 | if len(i4) == net.IPv4len {
28 | initBuf[4] = i.IP.To4()[0]
29 | initBuf[5] = i.IP.To4()[1]
30 | initBuf[6] = i.IP.To4()[2]
31 | initBuf[7] = i.IP.To4()[3]
32 | break
33 | }
34 | }
35 | }
36 | }
37 | binary.LittleEndian.PutUint16(initBuf[8:], uint16(os.Getpid()))
38 | }
39 |
40 | //String 生成字符串的uuid
41 | func String() string {
42 | buf := initBuf[:]
43 | binary.BigEndian.PutUint32(buf[0:], uint32(time.Now().UTC().Unix()))
44 | binary.BigEndian.PutUint32(buf[10:], atomic.AddUint32(&seq, 1))
45 | return encoding.EncodeToString(buf)
46 | }
47 |
48 | //Decode 解析uuid字符串,返回具体细节.
49 | func Decode(s string) (ip string, pid int, tm time.Time, seq uint32, err error) {
50 | var buf []byte
51 | buf, err = encoding.DecodeString(s)
52 | if err != nil {
53 | return
54 | }
55 |
56 | tm = time.Unix(int64(binary.BigEndian.Uint32(buf)), 0)
57 | ip = net.IPv4(buf[4], buf[5], buf[6], buf[7]).String()
58 | pid = int(binary.LittleEndian.Uint16(buf[8:]))
59 | seq = binary.BigEndian.Uint32(buf[10:])
60 |
61 | return
62 | }
63 |
64 | //Info 解析uuid中信息.
65 | func Info(s string) (string, error) {
66 | ip, pid, tm, seq, err := Decode(s)
67 | if err != nil {
68 | return "", err
69 | }
70 | return fmt.Sprintf("ip:%s pid:%d time:%s sequence:%d", ip, pid, tm.Format(time.RFC3339), seq), nil
71 | }
72 |
--------------------------------------------------------------------------------
/uuid/uuid_test.go:
--------------------------------------------------------------------------------
1 | package uuid
2 |
3 | import (
4 | "testing"
5 | )
6 |
7 | func TestUUID(t *testing.T) {
8 | for i := 0; i < 1000; i++ {
9 | id := String()
10 | info, _ := Info(id)
11 | t.Logf("%s, %s", id, info)
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/validation/util.go:
--------------------------------------------------------------------------------
1 | package validation
2 |
3 | import (
4 | "fmt"
5 | "reflect"
6 | "regexp"
7 | "strconv"
8 | "strings"
9 | )
10 |
11 | const (
12 | // ValidTag struct tag
13 | ValidTag = "valid"
14 | //AliaseTag 别名,报错时用的.
15 | AliaseTag = "aliase"
16 | )
17 |
18 | var (
19 | // key: function name
20 | // value: the number of parameters
21 | funcs = make(Funcs)
22 |
23 | // doesn't belong to validation functions
24 | unFuncs = map[string]bool{
25 | "Clear": true,
26 | "HasErrors": true,
27 | "ErrorMap": true,
28 | "Error": true,
29 | "apply": true,
30 | "Check": true,
31 | "Valid": true,
32 | "NoMatch": true,
33 | }
34 | )
35 |
36 | func init() {
37 | v := &Validation{}
38 | t := reflect.TypeOf(v)
39 | for i := 0; i < t.NumMethod(); i++ {
40 | m := t.Method(i)
41 | if !unFuncs[m.Name] {
42 | funcs[m.Name] = m.Func
43 | }
44 | }
45 | }
46 |
47 | // CustomFunc is for custom validate function
48 | type CustomFunc func(v *Validation, obj interface{}, key string)
49 |
50 | // AddCustomFunc Add a custom function to validation
51 | // The name can not be:
52 | //
53 | // Clear
54 | // HasErrors
55 | // ErrorMap
56 | // Error
57 | // Check
58 | // Valid
59 | // NoMatch
60 | //
61 | // If the name is same with exists function, it will replace the origin valid function
62 | func AddCustomFunc(name string, f CustomFunc) error {
63 | if unFuncs[name] {
64 | return fmt.Errorf("invalid function name: %s", name)
65 | }
66 |
67 | funcs[name] = reflect.ValueOf(f)
68 | return nil
69 | }
70 |
71 | // ValidFunc Valid function type
72 | type ValidFunc struct {
73 | Name string
74 | Aliase string
75 | Params []interface{}
76 | }
77 |
78 | // Funcs Validate function map
79 | type Funcs map[string]reflect.Value
80 |
81 | // Call validate values with named type string
82 | func (f Funcs) Call(name string, params ...interface{}) (result []reflect.Value, err error) {
83 | defer func() {
84 | if r := recover(); r != nil {
85 | err = fmt.Errorf("%v", r)
86 | }
87 | }()
88 | if _, ok := f[name]; !ok {
89 | err = fmt.Errorf("%s does not exist", name)
90 | return
91 | }
92 | if len(params) != f[name].Type().NumIn() {
93 | err = fmt.Errorf("the number of params is not adapted")
94 | return
95 | }
96 | in := make([]reflect.Value, len(params))
97 | for k, param := range params {
98 | in[k] = reflect.ValueOf(param)
99 | }
100 | result = f[name].Call(in)
101 | return
102 | }
103 |
104 | func isStruct(t reflect.Type) bool {
105 | return t.Kind() == reflect.Struct
106 | }
107 |
108 | func isStructPtr(t reflect.Type) bool {
109 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
110 | }
111 |
112 | func getValidFuncs(f reflect.StructField) (vfs []ValidFunc, err error) {
113 | tag := f.Tag.Get(ValidTag)
114 | if len(tag) == 0 {
115 | return
116 | }
117 |
118 | aliase := f.Tag.Get(AliaseTag)
119 | if aliase == "" {
120 | aliase = f.Name
121 | }
122 |
123 | if vfs, tag, err = getRegFuncs(tag, aliase, f.Name); err != nil {
124 | return
125 | }
126 | fs := strings.Split(tag, ";")
127 | for _, vfunc := range fs {
128 | var vf ValidFunc
129 | if len(vfunc) == 0 {
130 | continue
131 | }
132 | vf, err = parseFunc(vfunc, aliase, f.Name)
133 | if err != nil {
134 | return
135 | }
136 | vfs = append(vfs, vf)
137 | }
138 | return
139 | }
140 |
141 | // Get Match function
142 | // May be get NoMatch function in the future
143 | func getRegFuncs(tag, aliase, key string) (vfs []ValidFunc, str string, err error) {
144 | tag = strings.TrimSpace(tag)
145 | index := strings.Index(tag, "Match(/")
146 | if index == -1 {
147 | str = tag
148 | return
149 | }
150 | end := strings.LastIndex(tag, "/)")
151 | if end < index {
152 | err = fmt.Errorf("invalid Match function")
153 | return
154 | }
155 | reg, err := regexp.Compile(tag[index+len("Match(/") : end])
156 | if err != nil {
157 | return
158 | }
159 | vfs = []ValidFunc{{"Match", aliase, []interface{}{reg, key + ".Match"}}}
160 | str = strings.TrimSpace(tag[:index]) + strings.TrimSpace(tag[end+len("/)"):])
161 | return
162 | }
163 |
164 | func parseFunc(vfunc, aliase, key string) (v ValidFunc, err error) {
165 | defer func() {
166 | if r := recover(); r != nil {
167 | err = fmt.Errorf("%v", r)
168 | }
169 | }()
170 |
171 | vfunc = strings.TrimSpace(vfunc)
172 | start := strings.Index(vfunc, "(")
173 | var num int
174 |
175 | // doesn't need parameter valid function
176 | if start == -1 {
177 | if num, err = numIn(vfunc); err != nil {
178 | return
179 | }
180 | if num != 0 {
181 | err = fmt.Errorf("%s require %d parameters", vfunc, num)
182 | return
183 | }
184 | v = ValidFunc{vfunc, aliase, []interface{}{key + "." + vfunc}}
185 | return
186 | }
187 |
188 | end := strings.Index(vfunc, ")")
189 | if end == -1 {
190 | err = fmt.Errorf("invalid valid function")
191 | return
192 | }
193 |
194 | name := strings.TrimSpace(vfunc[:start])
195 | if num, err = numIn(name); err != nil {
196 | return
197 | }
198 |
199 | params := strings.Split(vfunc[start+1:end], ",")
200 | // the num of param must be equal
201 | if num != len(params) {
202 | err = fmt.Errorf("%s require %d parameters", name, num)
203 | return
204 | }
205 |
206 | tParams, err := trim(name, key+"."+name, params)
207 | if err != nil {
208 | return
209 | }
210 | v = ValidFunc{name, key, tParams}
211 | return
212 | }
213 |
214 | func numIn(name string) (num int, err error) {
215 | fn, ok := funcs[name]
216 | if !ok {
217 | err = fmt.Errorf("doesn't exsits %s valid function", name)
218 | return
219 | }
220 | // sub *Validation obj and key
221 | num = fn.Type().NumIn() - 3
222 | return
223 | }
224 |
225 | func trim(name, key string, s []string) (ts []interface{}, err error) {
226 | ts = make([]interface{}, len(s), len(s)+1)
227 | fn, ok := funcs[name]
228 | if !ok {
229 | err = fmt.Errorf("doesn't exsits %s valid function", name)
230 | return
231 | }
232 | for i := 0; i < len(s); i++ {
233 | var param interface{}
234 | // skip *Validation and obj params
235 | if param, err = parseParam(fn.Type().In(i+2), strings.TrimSpace(s[i])); err != nil {
236 | return
237 | }
238 | ts[i] = param
239 | }
240 | ts = append(ts, key)
241 | return
242 | }
243 |
244 | // modify the parameters's type to adapt the function input parameters' type
245 | func parseParam(t reflect.Type, s string) (i interface{}, err error) {
246 | switch t.Kind() {
247 | case reflect.Int:
248 | i, err = strconv.Atoi(s)
249 | case reflect.String:
250 | i = s
251 | case reflect.Ptr:
252 | if t.Elem().String() != "regexp.Regexp" {
253 | err = fmt.Errorf("does not support %s", t.Elem().String())
254 | return
255 | }
256 | i, err = regexp.Compile(s)
257 | default:
258 | err = fmt.Errorf("does not support %s", t.Kind().String())
259 | }
260 | return
261 | }
262 |
263 | func mergeParam(v *Validation, obj interface{}, params []interface{}) []interface{} {
264 | return append([]interface{}{v, obj}, params...)
265 | }
266 |
--------------------------------------------------------------------------------
/validation/validation.go:
--------------------------------------------------------------------------------
1 | package validation
2 |
3 | import (
4 | "fmt"
5 | "reflect"
6 | "regexp"
7 | "strings"
8 | //"github.com/juju/errors"
9 | )
10 |
11 | // ValidFormer valid interface
12 | type ValidFormer interface {
13 | Valid(*Validation)
14 | }
15 |
16 | // Error show the error
17 | type Error struct {
18 | Message, Key, Name, Field, Tmpl string
19 | Value interface{}
20 | LimitValue interface{}
21 | }
22 |
23 | // String Returns the Message.
24 | func (e *Error) String() string {
25 | if e == nil {
26 | return ""
27 | }
28 | return e.Message
29 | }
30 |
31 | // Implement Error interface.
32 | // Return e.String()
33 | func (e *Error) Error() string { return e.String() }
34 |
35 | // Result is returned from every validation method.
36 | // It provides an indication of success, and a pointer to the Error (if any).
37 | type Result struct {
38 | Error *Error
39 | Ok bool
40 | }
41 |
42 | // Key Get Result by given key string.
43 | func (r *Result) Key(key string) *Result {
44 | if r.Error != nil {
45 | r.Error.Key = key
46 | }
47 | return r
48 | }
49 |
50 | // Message Set Result message by string or format string with args
51 | func (r *Result) Message(message string, args ...interface{}) *Result {
52 | if r.Error != nil {
53 | if len(args) == 0 {
54 | r.Error.Message = message
55 | } else {
56 | r.Error.Message = fmt.Sprintf(message, args...)
57 | }
58 | }
59 | return r
60 | }
61 |
62 | // A Validation context manages data validation and error messages.
63 | type Validation struct {
64 | Errors []*Error
65 | ErrorsMap map[string]*Error
66 | }
67 |
68 | // Clear Clean all ValidationError.
69 | func (v *Validation) Clear() {
70 | v.Errors = []*Error{}
71 | v.ErrorsMap = nil
72 | }
73 |
74 | // HasErrors Has ValidationError nor not.
75 | func (v *Validation) HasErrors() bool {
76 | return len(v.Errors) > 0
77 | }
78 |
79 | // ErrorMap Return the errors mapped by key.
80 | // If there are multiple validation errors associated with a single key, the
81 | // first one "wins". (Typically the first validation will be the more basic).
82 | func (v *Validation) ErrorMap() map[string]*Error {
83 | return v.ErrorsMap
84 | }
85 |
86 | // Error Add an error to the validation context.
87 | func (v *Validation) Error(message string, args ...interface{}) *Result {
88 | result := (&Result{
89 | Ok: false,
90 | Error: &Error{},
91 | }).Message(message, args...)
92 | v.Errors = append(v.Errors, result.Error)
93 | return result
94 | }
95 |
96 | // Required Test that the argument is non-nil and non-empty (if string or list)
97 | func (v *Validation) Required(obj interface{}, key string) *Result {
98 | return v.apply(Required{key}, obj)
99 | }
100 |
101 | // Min Test that the obj is greater than min if obj's type is int
102 | func (v *Validation) Min(obj interface{}, min int, key string) *Result {
103 | return v.apply(Min{min, key}, obj)
104 | }
105 |
106 | // Max Test that the obj is less than max if obj's type is int
107 | func (v *Validation) Max(obj interface{}, max int, key string) *Result {
108 | return v.apply(Max{max, key}, obj)
109 | }
110 |
111 | // Range Test that the obj is between mni and max if obj's type is int
112 | func (v *Validation) Range(obj interface{}, min, max int, key string) *Result {
113 | return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, obj)
114 | }
115 |
116 | // MinSize Test that the obj is longer than min size if type is string or slice
117 | func (v *Validation) MinSize(obj interface{}, min int, key string) *Result {
118 | return v.apply(MinSize{min, key}, obj)
119 | }
120 |
121 | // MaxSize Test that the obj is shorter than max size if type is string or slice
122 | func (v *Validation) MaxSize(obj interface{}, max int, key string) *Result {
123 | return v.apply(MaxSize{max, key}, obj)
124 | }
125 |
126 | // Length Test that the obj is same length to n if type is string or slice
127 | func (v *Validation) Length(obj interface{}, n int, key string) *Result {
128 | return v.apply(Length{n, key}, obj)
129 | }
130 |
131 | // Alpha Test that the obj is [a-zA-Z] if type is string
132 | func (v *Validation) Alpha(obj interface{}, key string) *Result {
133 | return v.apply(Alpha{key}, obj)
134 | }
135 |
136 | // Numeric Test that the obj is [0-9] if type is string
137 | func (v *Validation) Numeric(obj interface{}, key string) *Result {
138 | return v.apply(Numeric{key}, obj)
139 | }
140 |
141 | // AlphaNumeric Test that the obj is [0-9a-zA-Z] if type is string
142 | func (v *Validation) AlphaNumeric(obj interface{}, key string) *Result {
143 | return v.apply(AlphaNumeric{key}, obj)
144 | }
145 |
146 | // Match Test that the obj matches regexp if type is string
147 | func (v *Validation) Match(obj interface{}, regex *regexp.Regexp, key string) *Result {
148 | return v.apply(Match{regex, key}, obj)
149 | }
150 |
151 | // NoMatch Test that the obj doesn't match regexp if type is string
152 | func (v *Validation) NoMatch(obj interface{}, regex *regexp.Regexp, key string) *Result {
153 | return v.apply(NoMatch{Match{Regexp: regex}, key}, obj)
154 | }
155 |
156 | // AlphaDash Test that the obj is [0-9a-zA-Z_-] if type is string
157 | func (v *Validation) AlphaDash(obj interface{}, key string) *Result {
158 | return v.apply(AlphaDash{NoMatch{Match: Match{Regexp: alphaDashPattern}}, key}, obj)
159 | }
160 |
161 | // Email Test that the obj is email address if type is string
162 | func (v *Validation) Email(obj interface{}, key string) *Result {
163 | return v.apply(Email{Match{Regexp: emailPattern}, key}, obj)
164 | }
165 |
166 | // IP Test that the obj is IP address if type is string
167 | func (v *Validation) IP(obj interface{}, key string) *Result {
168 | return v.apply(IP{Match{Regexp: ipPattern}, key}, obj)
169 | }
170 |
171 | // Base64 Test that the obj is base64 encoded if type is string
172 | func (v *Validation) Base64(obj interface{}, key string) *Result {
173 | return v.apply(Base64{Match{Regexp: base64Pattern}, key}, obj)
174 | }
175 |
176 | // Mobile Test that the obj is chinese mobile number if type is string
177 | func (v *Validation) Mobile(obj interface{}, key string) *Result {
178 | return v.apply(Mobile{Match{Regexp: mobilePattern}, key}, obj)
179 | }
180 |
181 | // Tel Test that the obj is chinese telephone number if type is string
182 | func (v *Validation) Tel(obj interface{}, key string) *Result {
183 | return v.apply(Tel{Match{Regexp: telPattern}, key}, obj)
184 | }
185 |
186 | // Phone Test that the obj is chinese mobile or telephone number if type is string
187 | func (v *Validation) Phone(obj interface{}, key string) *Result {
188 | return v.apply(Phone{Mobile{Match: Match{Regexp: mobilePattern}},
189 | Tel{Match: Match{Regexp: telPattern}}, key}, obj)
190 | }
191 |
192 | // ZipCode Test that the obj is chinese zip code if type is string
193 | func (v *Validation) ZipCode(obj interface{}, key string) *Result {
194 | return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, obj)
195 | }
196 |
197 | func (v *Validation) apply(chk Validator, obj interface{}) *Result {
198 | if chk.IsSatisfied(obj) {
199 | return &Result{Ok: true}
200 | }
201 |
202 | // Add the error to the validation context.
203 | key := chk.GetKey()
204 | Name := key
205 | Field := ""
206 |
207 | parts := strings.Split(key, ".")
208 | if len(parts) == 2 {
209 | Field = parts[0]
210 | Name = parts[1]
211 | }
212 |
213 | err := &Error{
214 | Message: chk.DefaultMessage(),
215 | Key: key,
216 | Name: Name,
217 | Field: Field,
218 | Value: obj,
219 | Tmpl: MessageTmpls[Name],
220 | LimitValue: chk.GetLimitValue(),
221 | }
222 | v.setError(err)
223 |
224 | // Also return it in the result.
225 | return &Result{
226 | Ok: false,
227 | Error: err,
228 | }
229 | }
230 |
231 | func (v *Validation) setError(err *Error) {
232 | v.Errors = append(v.Errors, err)
233 | if v.ErrorsMap == nil {
234 | v.ErrorsMap = make(map[string]*Error)
235 | }
236 | if _, ok := v.ErrorsMap[err.Field]; !ok {
237 | v.ErrorsMap[err.Field] = err
238 | }
239 | }
240 |
241 | // SetError Set error message for one field in ValidationError
242 | func (v *Validation) SetError(fieldName string, errMsg string) *Error {
243 | err := &Error{Key: fieldName, Field: fieldName, Tmpl: errMsg, Message: errMsg}
244 | v.setError(err)
245 | return err
246 | }
247 |
248 | // Check Apply a group of validators to a field, in order, and return the
249 | // ValidationResult from the first one that fails, or the last one that
250 | // succeeds.
251 | func (v *Validation) Check(obj interface{}, checks ...Validator) *Result {
252 | var result *Result
253 | for _, check := range checks {
254 | result = v.apply(check, obj)
255 | if !result.Ok {
256 | return result
257 | }
258 | }
259 | return result
260 | }
261 |
262 | // Valid Validate a struct.
263 | // the obj parameter must be a struct or a struct pointer
264 | func (v *Validation) Valid(obj interface{}) (b bool, err error) {
265 | objT := reflect.TypeOf(obj)
266 | objV := reflect.ValueOf(obj)
267 | switch {
268 | case isStruct(objT):
269 | case isStructPtr(objT):
270 | objT = objT.Elem()
271 | objV = objV.Elem()
272 | default:
273 | err = fmt.Errorf("%v must be a struct or a struct pointer", obj)
274 | return
275 | }
276 |
277 | for i := 0; i < objT.NumField(); i++ {
278 | tag := objT.Field(i).Tag.Get("valid")
279 | if tag == "" {
280 | continue
281 | }
282 | switch objT.Field(i).Type.Kind() {
283 | case reflect.Array, reflect.Slice:
284 | for si := 0; si < objV.Field(i).Len(); si++ {
285 | sv := objV.Field(i).Index(si)
286 | if sv.Kind() != reflect.Struct {
287 | continue
288 | }
289 | b, err = v.Valid(sv.Interface())
290 | if err != nil {
291 | return b, fmt.Errorf("[]%v.%v", sv.Type().Name(), err.Error())
292 | }
293 | }
294 | return
295 | case reflect.Struct:
296 | sv := objV.Field(i)
297 | b, err = v.Valid(sv.Interface())
298 | if err != nil {
299 | return b, fmt.Errorf("%v.%v", sv.Type().Name(), err.Error())
300 | }
301 | return
302 | }
303 | var vfs []ValidFunc
304 | if vfs, err = getValidFuncs(objT.Field(i)); err != nil {
305 | return
306 | }
307 |
308 | var rs []reflect.Value
309 | for _, vf := range vfs {
310 | rs, err = funcs.Call(vf.Name, mergeParam(v, objV.Field(i).Interface(), vf.Params)...)
311 | if err != nil {
312 | return
313 | }
314 | result, _ := rs[0].Interface().(*Result)
315 | if result.Error != nil {
316 | return false, fmt.Errorf("%v %v", vf.Aliase, result.Error.Error())
317 | }
318 | }
319 | }
320 |
321 | if !v.HasErrors() {
322 | if form, ok := obj.(ValidFormer); ok {
323 | form.Valid(v)
324 | }
325 | }
326 |
327 | return !v.HasErrors(), nil
328 | }
329 |
--------------------------------------------------------------------------------
/validation/validation_test.go:
--------------------------------------------------------------------------------
1 | package validation
2 |
3 | import (
4 | "regexp"
5 | "testing"
6 | "time"
7 | )
8 |
9 | func TestRequired(t *testing.T) {
10 | valid := Validation{}
11 |
12 | if valid.Required(nil, "nil").Ok {
13 | t.Error("nil object should be false")
14 | }
15 | if !valid.Required(true, "bool").Ok {
16 | t.Error("Bool value should always return true")
17 | }
18 | if !valid.Required(false, "bool").Ok {
19 | t.Error("Bool value should always return true")
20 | }
21 | if valid.Required("", "string").Ok {
22 | t.Error("\"'\" string should be false")
23 | }
24 | if !valid.Required("astaxie", "string").Ok {
25 | t.Error("string should be true")
26 | }
27 | if valid.Required(0, "zero").Ok {
28 | t.Error("Integer should not be equal 0")
29 | }
30 | if !valid.Required(1, "int").Ok {
31 | t.Error("Integer except 0 should be true")
32 | }
33 | if !valid.Required(time.Now(), "time").Ok {
34 | t.Error("time should be true")
35 | }
36 | if valid.Required([]string{}, "emptySlice").Ok {
37 | t.Error("empty slice should be false")
38 | }
39 | if !valid.Required([]interface{}{"ok"}, "slice").Ok {
40 | t.Error("slice should be true")
41 | }
42 | }
43 |
44 | func TestMin(t *testing.T) {
45 | valid := Validation{}
46 |
47 | if valid.Min(-1, 0, "min0").Ok {
48 | t.Error("-1 is less than the minimum value of 0 should be false")
49 | }
50 | if !valid.Min(1, 0, "min0").Ok {
51 | t.Error("1 is greater or equal than the minimum value of 0 should be true")
52 | }
53 | }
54 |
55 | func TestMax(t *testing.T) {
56 | valid := Validation{}
57 |
58 | if valid.Max(1, 0, "max0").Ok {
59 | t.Error("1 is greater than the minimum value of 0 should be false")
60 | }
61 | if !valid.Max(-1, 0, "max0").Ok {
62 | t.Error("-1 is less or equal than the maximum value of 0 should be true")
63 | }
64 | }
65 |
66 | func TestRange(t *testing.T) {
67 | valid := Validation{}
68 |
69 | if valid.Range(-1, 0, 1, "range0_1").Ok {
70 | t.Error("-1 is between 0 and 1 should be false")
71 | }
72 | if !valid.Range(1, 0, 1, "range0_1").Ok {
73 | t.Error("1 is between 0 and 1 should be true")
74 | }
75 | }
76 |
77 | func TestMinSize(t *testing.T) {
78 | valid := Validation{}
79 |
80 | if valid.MinSize("", 1, "minSize1").Ok {
81 | t.Error("the length of \"\" is less than the minimum value of 1 should be false")
82 | }
83 | if !valid.MinSize("ok", 1, "minSize1").Ok {
84 | t.Error("the length of \"ok\" is greater or equal than the minimum value of 1 should be true")
85 | }
86 | if valid.MinSize([]string{}, 1, "minSize1").Ok {
87 | t.Error("the length of empty slice is less than the minimum value of 1 should be false")
88 | }
89 | if !valid.MinSize([]interface{}{"ok"}, 1, "minSize1").Ok {
90 | t.Error("the length of [\"ok\"] is greater or equal than the minimum value of 1 should be true")
91 | }
92 | }
93 |
94 | func TestMaxSize(t *testing.T) {
95 | valid := Validation{}
96 |
97 | if valid.MaxSize("ok", 1, "maxSize1").Ok {
98 | t.Error("the length of \"ok\" is greater than the maximum value of 1 should be false")
99 | }
100 | if !valid.MaxSize("", 1, "maxSize1").Ok {
101 | t.Error("the length of \"\" is less or equal than the maximum value of 1 should be true")
102 | }
103 | if valid.MaxSize([]interface{}{"ok", false}, 1, "maxSize1").Ok {
104 | t.Error("the length of [\"ok\", false] is greater than the maximum value of 1 should be false")
105 | }
106 | if !valid.MaxSize([]string{}, 1, "maxSize1").Ok {
107 | t.Error("the length of empty slice is less or equal than the maximum value of 1 should be true")
108 | }
109 | }
110 |
111 | func TestLength(t *testing.T) {
112 | valid := Validation{}
113 |
114 | if valid.Length("", 1, "length1").Ok {
115 | t.Error("the length of \"\" must equal 1 should be false")
116 | }
117 | if !valid.Length("1", 1, "length1").Ok {
118 | t.Error("the length of \"1\" must equal 1 should be true")
119 | }
120 | if valid.Length([]string{}, 1, "length1").Ok {
121 | t.Error("the length of empty slice must equal 1 should be false")
122 | }
123 | if !valid.Length([]interface{}{"ok"}, 1, "length1").Ok {
124 | t.Error("the length of [\"ok\"] must equal 1 should be true")
125 | }
126 | }
127 |
128 | func TestAlpha(t *testing.T) {
129 | valid := Validation{}
130 |
131 | if valid.Alpha("a,1-@ $", "alpha").Ok {
132 | t.Error("\"a,1-@ $\" are valid alpha characters should be false")
133 | }
134 | if !valid.Alpha("abCD", "alpha").Ok {
135 | t.Error("\"abCD\" are valid alpha characters should be true")
136 | }
137 | }
138 |
139 | func TestNumeric(t *testing.T) {
140 | valid := Validation{}
141 |
142 | if valid.Numeric("a,1-@ $", "numeric").Ok {
143 | t.Error("\"a,1-@ $\" are valid numeric characters should be false")
144 | }
145 | if !valid.Numeric("1234", "numeric").Ok {
146 | t.Error("\"1234\" are valid numeric characters should be true")
147 | }
148 | }
149 |
150 | func TestAlphaNumeric(t *testing.T) {
151 | valid := Validation{}
152 |
153 | if valid.AlphaNumeric("a,1-@ $", "alphaNumeric").Ok {
154 | t.Error("\"a,1-@ $\" are valid alpha or numeric characters should be false")
155 | }
156 | if !valid.AlphaNumeric("1234aB", "alphaNumeric").Ok {
157 | t.Error("\"1234aB\" are valid alpha or numeric characters should be true")
158 | }
159 | }
160 |
161 | func TestMatch(t *testing.T) {
162 | valid := Validation{}
163 |
164 | if valid.Match("suchuangji@gmail", regexp.MustCompile(`^\w+@\w+\.\w+$`), "match").Ok {
165 | t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be false")
166 | }
167 | if !valid.Match("suchuangji@gmail.com", regexp.MustCompile(`^\w+@\w+\.\w+$`), "match").Ok {
168 | t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be true")
169 | }
170 | }
171 |
172 | func TestNoMatch(t *testing.T) {
173 | valid := Validation{}
174 |
175 | if valid.NoMatch("123@gmail", regexp.MustCompile(`[^\w\d]`), "nomatch").Ok {
176 | t.Error("\"123@gmail\" not match \"[^\\w\\d]\" should be false")
177 | }
178 | if !valid.NoMatch("123gmail", regexp.MustCompile(`[^\w\d]`), "match").Ok {
179 | t.Error("\"123@gmail\" not match \"[^\\w\\d@]\" should be true")
180 | }
181 | }
182 |
183 | func TestAlphaDash(t *testing.T) {
184 | valid := Validation{}
185 |
186 | if valid.AlphaDash("a,1-@ $", "alphaDash").Ok {
187 | t.Error("\"a,1-@ $\" are valid alpha or numeric or dash(-_) characters should be false")
188 | }
189 | if !valid.AlphaDash("1234aB-_", "alphaDash").Ok {
190 | t.Error("\"1234aB\" are valid alpha or numeric or dash(-_) characters should be true")
191 | }
192 | }
193 |
194 | func TestEmail(t *testing.T) {
195 | valid := Validation{}
196 |
197 | if valid.Email("not@a email", "email").Ok {
198 | t.Error("\"not@a email\" is a valid email address should be false")
199 | }
200 | if !valid.Email("suchuangji@gmail.com", "email").Ok {
201 | t.Error("\"suchuangji@gmail.com\" is a valid email address should be true")
202 | }
203 | }
204 |
205 | func TestIP(t *testing.T) {
206 | valid := Validation{}
207 |
208 | if valid.IP("11.255.255.256", "IP").Ok {
209 | t.Error("\"11.255.255.256\" is a valid ip address should be false")
210 | }
211 | if !valid.IP("01.11.11.11", "IP").Ok {
212 | t.Error("\"suchuangji@gmail.com\" is a valid ip address should be true")
213 | }
214 | }
215 |
216 | func TestBase64(t *testing.T) {
217 | valid := Validation{}
218 |
219 | if valid.Base64("suchuangji@gmail.com", "base64").Ok {
220 | t.Error("\"suchuangji@gmail.com\" are a valid base64 characters should be false")
221 | }
222 | if !valid.Base64("c3VjaHVhbmdqaUBnbWFpbC5jb20=", "base64").Ok {
223 | t.Error("\"c3VjaHVhbmdqaUBnbWFpbC5jb20=\" are a valid base64 characters should be true")
224 | }
225 | }
226 |
227 | func TestMobile(t *testing.T) {
228 | valid := Validation{}
229 |
230 | if valid.Mobile("19800008888", "mobile").Ok {
231 | t.Error("\"19800008888\" is a valid mobile phone number should be false")
232 | }
233 | if !valid.Mobile("18800008888", "mobile").Ok {
234 | t.Error("\"18800008888\" is a valid mobile phone number should be true")
235 | }
236 | if !valid.Mobile("18000008888", "mobile").Ok {
237 | t.Error("\"18000008888\" is a valid mobile phone number should be true")
238 | }
239 | if !valid.Mobile("8618300008888", "mobile").Ok {
240 | t.Error("\"8618300008888\" is a valid mobile phone number should be true")
241 | }
242 | if !valid.Mobile("+8614700008888", "mobile").Ok {
243 | t.Error("\"+8614700008888\" is a valid mobile phone number should be true")
244 | }
245 | }
246 |
247 | func TestTel(t *testing.T) {
248 | valid := Validation{}
249 |
250 | if valid.Tel("222-00008888", "telephone").Ok {
251 | t.Error("\"222-00008888\" is a valid telephone number should be false")
252 | }
253 | if !valid.Tel("022-70008888", "telephone").Ok {
254 | t.Error("\"022-70008888\" is a valid telephone number should be true")
255 | }
256 | if !valid.Tel("02270008888", "telephone").Ok {
257 | t.Error("\"02270008888\" is a valid telephone number should be true")
258 | }
259 | if !valid.Tel("70008888", "telephone").Ok {
260 | t.Error("\"70008888\" is a valid telephone number should be true")
261 | }
262 | }
263 |
264 | func TestPhone(t *testing.T) {
265 | valid := Validation{}
266 |
267 | if valid.Phone("222-00008888", "phone").Ok {
268 | t.Error("\"222-00008888\" is a valid phone number should be false")
269 | }
270 | if !valid.Mobile("+8614700008888", "phone").Ok {
271 | t.Error("\"+8614700008888\" is a valid phone number should be true")
272 | }
273 | if !valid.Tel("02270008888", "phone").Ok {
274 | t.Error("\"02270008888\" is a valid phone number should be true")
275 | }
276 | }
277 |
278 | func TestZipCode(t *testing.T) {
279 | valid := Validation{}
280 |
281 | if valid.ZipCode("", "zipcode").Ok {
282 | t.Error("\"00008888\" is a valid zipcode should be false")
283 | }
284 | if !valid.ZipCode("536000", "zipcode").Ok {
285 | t.Error("\"536000\" is a valid zipcode should be true")
286 | }
287 | }
288 |
289 | func TestValid(t *testing.T) {
290 | type user struct {
291 | ID int
292 | Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
293 | Age int `valid:"Required;Range(1, 140)"`
294 | }
295 | valid := Validation{}
296 |
297 | u := user{Name: "test@/test/;com", Age: 40}
298 | b, err := valid.Valid(u)
299 | if err != nil {
300 | t.Fatal(err)
301 | }
302 | if !b {
303 | t.Error("validation should be passed")
304 | }
305 |
306 | uptr := &user{Name: "test", Age: 40}
307 | valid.Clear()
308 |
309 | b, _ = valid.Valid(uptr)
310 | if b {
311 | t.Error("validation should not be passed")
312 | }
313 | if len(valid.Errors) != 1 {
314 | t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
315 | }
316 | if valid.Errors[0].Key != "Name.Match" {
317 | t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
318 | }
319 |
320 | u = user{Name: "test@/test/;com", Age: 180}
321 | valid.Clear()
322 | b, _ = valid.Valid(u)
323 | if b {
324 | t.Error("validation should not be passed")
325 | }
326 | if len(valid.Errors) != 1 {
327 | t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
328 | }
329 | if valid.Errors[0].Key != "Age.Range" {
330 | t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
331 | }
332 | }
333 |
334 | func TestRecursiveValid(t *testing.T) {
335 | type User struct {
336 | ID int
337 | Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
338 | Age int `valid:"Required;Range(1, 140)"`
339 | }
340 |
341 | type AnonymouseUser struct {
342 | ID2 int
343 | Name2 string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
344 | Age2 int `valid:"Required;Range(1, 140)"`
345 | }
346 |
347 | type Account struct {
348 | Password string `valid:"Required"`
349 | U User
350 | AnonymouseUser
351 | }
352 | valid := Validation{}
353 |
354 | u := Account{Password: "", U: User{}}
355 | b, _ := valid.Valid(u)
356 | if b {
357 | t.Error("validation should not be passed")
358 | }
359 | }
360 |
361 | func TestAliase(t *testing.T) {
362 | type user struct {
363 | kk string
364 | Name string
365 | a int
366 | }
367 |
368 | valid := Validation{}
369 | u := user{Name: "aaaaa@mailchina.org", kk: "kkk", a: 1}
370 | _, err := valid.Valid(u)
371 | t.Logf("%+v", err)
372 | }
373 |
374 | func TestArray(t *testing.T) {
375 | type User struct {
376 | Name string `valid:"Required"`
377 | }
378 | type Conn struct {
379 | Users []User `valid:"Required"`
380 | }
381 |
382 | type Context struct {
383 | Size int `valid:"Required"`
384 | Conns Conn `valid:"Required"`
385 | }
386 |
387 | valid := Validation{}
388 | c := &Context{Conns: Conn{Users: []User{{Name: ""}, {Name: "222"}}}, Size: 111}
389 | _, err := valid.Valid(c)
390 | if err == nil {
391 | t.Fatalf("expect Email error")
392 | }
393 | }
394 |
--------------------------------------------------------------------------------
/validation/validators.go:
--------------------------------------------------------------------------------
1 | package validation
2 |
3 | import (
4 | "fmt"
5 | "reflect"
6 | "regexp"
7 | "time"
8 | "unicode/utf8"
9 | )
10 |
11 | // MessageTmpls store commond validate template
12 | var MessageTmpls = map[string]string{
13 | "Required": "Can not be empty",
14 | "Min": "Minimum is %d",
15 | "Max": "Maximum is %d",
16 | "Range": "Range is %d to %d",
17 | "MinSize": "Minimum size is %d",
18 | "MaxSize": "Maximum size is %d",
19 | "Length": "Required length is %d",
20 | "Alpha": "Must be valid alpha characters",
21 | "Numeric": "Must be valid numeric characters",
22 | "AlphaNumeric": "Must be valid alpha or numeric characters",
23 | "Match": "Must match %s",
24 | "NoMatch": "Must not match %s",
25 | "AlphaDash": "Must be valid alpha or numeric or dash(-_) characters",
26 | "Email": "Must be a valid email address",
27 | "IP": "Must be a valid ip address",
28 | "Base64": "Must be valid base64 characters",
29 | "Mobile": "Must be valid mobile number",
30 | "Tel": "Must be valid telephone number",
31 | "Phone": "Must be valid telephone or mobile phone number",
32 | "ZipCode": "Must be valid zipcode",
33 | }
34 |
35 | // SetDefaultMessage set default messages
36 | // if not set, the default messages are
37 | //
38 | // "Required": "Can not be empty",
39 | // "Min": "Minimum is %d",
40 | // "Max": "Maximum is %d",
41 | // "Range": "Range is %d to %d",
42 | // "MinSize": "Minimum size is %d",
43 | // "MaxSize": "Maximum size is %d",
44 | // "Length": "Required length is %d",
45 | // "Alpha": "Must be valid alpha characters",
46 | // "Numeric": "Must be valid numeric characters",
47 | // "AlphaNumeric": "Must be valid alpha or numeric characters",
48 | // "Match": "Must match %s",
49 | // "NoMatch": "Must not match %s",
50 | // "AlphaDash": "Must be valid alpha or numeric or dash(-_) characters",
51 | // "Email": "Must be a valid email address",
52 | // "IP": "Must be a valid ip address",
53 | // "Base64": "Must be valid base64 characters",
54 | // "Mobile": "Must be valid mobile number",
55 | // "Tel": "Must be valid telephone number",
56 | // "Phone": "Must be valid telephone or mobile phone number",
57 | // "ZipCode": "Must be valid zipcode",
58 | func SetDefaultMessage(msg map[string]string) {
59 | if len(msg) == 0 {
60 | return
61 | }
62 |
63 | for name := range msg {
64 | MessageTmpls[name] = msg[name]
65 | }
66 | }
67 |
68 | // Validator interface
69 | type Validator interface {
70 | IsSatisfied(interface{}) bool
71 | DefaultMessage() string
72 | GetKey() string
73 | GetLimitValue() interface{}
74 | }
75 |
76 | // Required struct
77 | type Required struct {
78 | Key string
79 | }
80 |
81 | // IsSatisfied judge whether obj has value
82 | func (r Required) IsSatisfied(obj interface{}) bool {
83 | if obj == nil {
84 | return false
85 | }
86 |
87 | if str, ok := obj.(string); ok {
88 | return len(str) > 0
89 | }
90 | if _, ok := obj.(bool); ok {
91 | return true
92 | }
93 | if i, ok := obj.(int); ok {
94 | return i != 0
95 | }
96 | if i, ok := obj.(uint); ok {
97 | return i != 0
98 | }
99 | if i, ok := obj.(int8); ok {
100 | return i != 0
101 | }
102 | if i, ok := obj.(uint8); ok {
103 | return i != 0
104 | }
105 | if i, ok := obj.(int16); ok {
106 | return i != 0
107 | }
108 | if i, ok := obj.(uint16); ok {
109 | return i != 0
110 | }
111 | if i, ok := obj.(uint32); ok {
112 | return i != 0
113 | }
114 | if i, ok := obj.(int32); ok {
115 | return i != 0
116 | }
117 | if i, ok := obj.(int64); ok {
118 | return i != 0
119 | }
120 | if i, ok := obj.(uint64); ok {
121 | return i != 0
122 | }
123 | if t, ok := obj.(time.Time); ok {
124 | return !t.IsZero()
125 | }
126 | v := reflect.ValueOf(obj)
127 | if v.Kind() == reflect.Slice {
128 | return v.Len() > 0
129 | }
130 | return true
131 | }
132 |
133 | // DefaultMessage return the default error message
134 | func (r Required) DefaultMessage() string {
135 | return MessageTmpls["Required"]
136 | }
137 |
138 | // GetKey return the r.Key
139 | func (r Required) GetKey() string {
140 | return r.Key
141 | }
142 |
143 | // GetLimitValue return nil now
144 | func (r Required) GetLimitValue() interface{} {
145 | return nil
146 | }
147 |
148 | // Min check struct
149 | type Min struct {
150 | Min int
151 | Key string
152 | }
153 |
154 | // IsSatisfied judge whether obj is valid
155 | func (m Min) IsSatisfied(obj interface{}) bool {
156 | num, ok := obj.(int)
157 | if ok {
158 | return num >= m.Min
159 | }
160 | return false
161 | }
162 |
163 | // DefaultMessage return the default min error message
164 | func (m Min) DefaultMessage() string {
165 | return fmt.Sprintf(MessageTmpls["Min"], m.Min)
166 | }
167 |
168 | // GetKey return the m.Key
169 | func (m Min) GetKey() string {
170 | return m.Key
171 | }
172 |
173 | // GetLimitValue return the limit value, Min
174 | func (m Min) GetLimitValue() interface{} {
175 | return m.Min
176 | }
177 |
178 | // Max validate struct
179 | type Max struct {
180 | Max int
181 | Key string
182 | }
183 |
184 | // IsSatisfied judge whether obj is valid
185 | func (m Max) IsSatisfied(obj interface{}) bool {
186 | num, ok := obj.(int)
187 | if ok {
188 | return num <= m.Max
189 | }
190 | return false
191 | }
192 |
193 | // DefaultMessage return the default max error message
194 | func (m Max) DefaultMessage() string {
195 | return fmt.Sprintf(MessageTmpls["Max"], m.Max)
196 | }
197 |
198 | // GetKey return the m.Key
199 | func (m Max) GetKey() string {
200 | return m.Key
201 | }
202 |
203 | // GetLimitValue return the limit value, Max
204 | func (m Max) GetLimitValue() interface{} {
205 | return m.Max
206 | }
207 |
208 | // Range Requires an integer to be within Min, Max inclusive.
209 | type Range struct {
210 | Min
211 | Max
212 | Key string
213 | }
214 |
215 | // IsSatisfied judge whether obj is valid
216 | func (r Range) IsSatisfied(obj interface{}) bool {
217 | return r.Min.IsSatisfied(obj) && r.Max.IsSatisfied(obj)
218 | }
219 |
220 | // DefaultMessage return the default Range error message
221 | func (r Range) DefaultMessage() string {
222 | return fmt.Sprintf(MessageTmpls["Range"], r.Min.Min, r.Max.Max)
223 | }
224 |
225 | // GetKey return the m.Key
226 | func (r Range) GetKey() string {
227 | return r.Key
228 | }
229 |
230 | // GetLimitValue return the limit value, Max
231 | func (r Range) GetLimitValue() interface{} {
232 | return []int{r.Min.Min, r.Max.Max}
233 | }
234 |
235 | // MinSize Requires an array or string to be at least a given length.
236 | type MinSize struct {
237 | Min int
238 | Key string
239 | }
240 |
241 | // IsSatisfied judge whether obj is valid
242 | func (m MinSize) IsSatisfied(obj interface{}) bool {
243 | if str, ok := obj.(string); ok {
244 | return utf8.RuneCountInString(str) >= m.Min
245 | }
246 | v := reflect.ValueOf(obj)
247 | if v.Kind() == reflect.Slice {
248 | return v.Len() >= m.Min
249 | }
250 | return false
251 | }
252 |
253 | // DefaultMessage return the default MinSize error message
254 | func (m MinSize) DefaultMessage() string {
255 | return fmt.Sprintf(MessageTmpls["MinSize"], m.Min)
256 | }
257 |
258 | // GetKey return the m.Key
259 | func (m MinSize) GetKey() string {
260 | return m.Key
261 | }
262 |
263 | // GetLimitValue return the limit value
264 | func (m MinSize) GetLimitValue() interface{} {
265 | return m.Min
266 | }
267 |
268 | // MaxSize Requires an array or string to be at most a given length.
269 | type MaxSize struct {
270 | Max int
271 | Key string
272 | }
273 |
274 | // IsSatisfied judge whether obj is valid
275 | func (m MaxSize) IsSatisfied(obj interface{}) bool {
276 | if str, ok := obj.(string); ok {
277 | return utf8.RuneCountInString(str) <= m.Max
278 | }
279 | v := reflect.ValueOf(obj)
280 | if v.Kind() == reflect.Slice {
281 | return v.Len() <= m.Max
282 | }
283 | return false
284 | }
285 |
286 | // DefaultMessage return the default MaxSize error message
287 | func (m MaxSize) DefaultMessage() string {
288 | return fmt.Sprintf(MessageTmpls["MaxSize"], m.Max)
289 | }
290 |
291 | // GetKey return the m.Key
292 | func (m MaxSize) GetKey() string {
293 | return m.Key
294 | }
295 |
296 | // GetLimitValue return the limit value
297 | func (m MaxSize) GetLimitValue() interface{} {
298 | return m.Max
299 | }
300 |
301 | // Length Requires an array or string to be exactly a given length.
302 | type Length struct {
303 | N int
304 | Key string
305 | }
306 |
307 | // IsSatisfied judge whether obj is valid
308 | func (l Length) IsSatisfied(obj interface{}) bool {
309 | if str, ok := obj.(string); ok {
310 | return utf8.RuneCountInString(str) == l.N
311 | }
312 | v := reflect.ValueOf(obj)
313 | if v.Kind() == reflect.Slice {
314 | return v.Len() == l.N
315 | }
316 | return false
317 | }
318 |
319 | // DefaultMessage return the default Length error message
320 | func (l Length) DefaultMessage() string {
321 | return fmt.Sprintf(MessageTmpls["Length"], l.N)
322 | }
323 |
324 | // GetKey return the m.Key
325 | func (l Length) GetKey() string {
326 | return l.Key
327 | }
328 |
329 | // GetLimitValue return the limit value
330 | func (l Length) GetLimitValue() interface{} {
331 | return l.N
332 | }
333 |
334 | // Alpha check the alpha
335 | type Alpha struct {
336 | Key string
337 | }
338 |
339 | // IsSatisfied judge whether obj is valid
340 | func (a Alpha) IsSatisfied(obj interface{}) bool {
341 | if str, ok := obj.(string); ok {
342 | for _, v := range str {
343 | if ('Z' < v || v < 'A') && ('z' < v || v < 'a') {
344 | return false
345 | }
346 | }
347 | return true
348 | }
349 | return false
350 | }
351 |
352 | // DefaultMessage return the default Length error message
353 | func (a Alpha) DefaultMessage() string {
354 | return MessageTmpls["Alpha"]
355 | }
356 |
357 | // GetKey return the m.Key
358 | func (a Alpha) GetKey() string {
359 | return a.Key
360 | }
361 |
362 | // GetLimitValue return the limit value
363 | func (a Alpha) GetLimitValue() interface{} {
364 | return nil
365 | }
366 |
367 | // Numeric check number
368 | type Numeric struct {
369 | Key string
370 | }
371 |
372 | // IsSatisfied judge whether obj is valid
373 | func (n Numeric) IsSatisfied(obj interface{}) bool {
374 | if str, ok := obj.(string); ok {
375 | for _, v := range str {
376 | if '9' < v || v < '0' {
377 | return false
378 | }
379 | }
380 | return true
381 | }
382 | return false
383 | }
384 |
385 | // DefaultMessage return the default Length error message
386 | func (n Numeric) DefaultMessage() string {
387 | return MessageTmpls["Numeric"]
388 | }
389 |
390 | // GetKey return the n.Key
391 | func (n Numeric) GetKey() string {
392 | return n.Key
393 | }
394 |
395 | // GetLimitValue return the limit value
396 | func (n Numeric) GetLimitValue() interface{} {
397 | return nil
398 | }
399 |
400 | // AlphaNumeric check alpha and number
401 | type AlphaNumeric struct {
402 | Key string
403 | }
404 |
405 | // IsSatisfied judge whether obj is valid
406 | func (a AlphaNumeric) IsSatisfied(obj interface{}) bool {
407 | if str, ok := obj.(string); ok {
408 | for _, v := range str {
409 | if ('Z' < v || v < 'A') && ('z' < v || v < 'a') && ('9' < v || v < '0') {
410 | return false
411 | }
412 | }
413 | return true
414 | }
415 | return false
416 | }
417 |
418 | // DefaultMessage return the default Length error message
419 | func (a AlphaNumeric) DefaultMessage() string {
420 | return MessageTmpls["AlphaNumeric"]
421 | }
422 |
423 | // GetKey return the a.Key
424 | func (a AlphaNumeric) GetKey() string {
425 | return a.Key
426 | }
427 |
428 | // GetLimitValue return the limit value
429 | func (a AlphaNumeric) GetLimitValue() interface{} {
430 | return nil
431 | }
432 |
433 | // Match Requires a string to match a given regex.
434 | type Match struct {
435 | Regexp *regexp.Regexp
436 | Key string
437 | }
438 |
439 | // IsSatisfied judge whether obj is valid
440 | func (m Match) IsSatisfied(obj interface{}) bool {
441 | return m.Regexp.MatchString(fmt.Sprintf("%v", obj))
442 | }
443 |
444 | // DefaultMessage return the default Match error message
445 | func (m Match) DefaultMessage() string {
446 | return fmt.Sprintf(MessageTmpls["Match"], m.Regexp.String())
447 | }
448 |
449 | // GetKey return the m.Key
450 | func (m Match) GetKey() string {
451 | return m.Key
452 | }
453 |
454 | // GetLimitValue return the limit value
455 | func (m Match) GetLimitValue() interface{} {
456 | return m.Regexp.String()
457 | }
458 |
459 | // NoMatch Requires a string to not match a given regex.
460 | type NoMatch struct {
461 | Match
462 | Key string
463 | }
464 |
465 | // IsSatisfied judge whether obj is valid
466 | func (n NoMatch) IsSatisfied(obj interface{}) bool {
467 | return !n.Match.IsSatisfied(obj)
468 | }
469 |
470 | // DefaultMessage return the default NoMatch error message
471 | func (n NoMatch) DefaultMessage() string {
472 | return fmt.Sprintf(MessageTmpls["NoMatch"], n.Regexp.String())
473 | }
474 |
475 | // GetKey return the n.Key
476 | func (n NoMatch) GetKey() string {
477 | return n.Key
478 | }
479 |
480 | // GetLimitValue return the limit value
481 | func (n NoMatch) GetLimitValue() interface{} {
482 | return n.Regexp.String()
483 | }
484 |
485 | var alphaDashPattern = regexp.MustCompile(`[^\d\w-_]`)
486 |
487 | // AlphaDash check not Alpha
488 | type AlphaDash struct {
489 | NoMatch
490 | Key string
491 | }
492 |
493 | // DefaultMessage return the default AlphaDash error message
494 | func (a AlphaDash) DefaultMessage() string {
495 | return MessageTmpls["AlphaDash"]
496 | }
497 |
498 | // GetKey return the n.Key
499 | func (a AlphaDash) GetKey() string {
500 | return a.Key
501 | }
502 |
503 | // GetLimitValue return the limit value
504 | func (a AlphaDash) GetLimitValue() interface{} {
505 | return nil
506 | }
507 |
508 | var emailPattern = regexp.MustCompile(`[\w!#$%&'*+/=?^_{|}~-]+(?:\.[\w!#$%&'*+/=?^_{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[a-zA-Z0-9](?:[\w-]*[\w])?`)
509 |
510 | // Email check struct
511 | type Email struct {
512 | Match
513 | Key string
514 | }
515 |
516 | // DefaultMessage return the default Email error message
517 | func (e Email) DefaultMessage() string {
518 | return MessageTmpls["Email"]
519 | }
520 |
521 | // GetKey return the n.Key
522 | func (e Email) GetKey() string {
523 | return e.Key
524 | }
525 |
526 | // GetLimitValue return the limit value
527 | func (e Email) GetLimitValue() interface{} {
528 | return nil
529 | }
530 |
531 | var ipPattern = regexp.MustCompile(`^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$`)
532 |
533 | // IP check struct
534 | type IP struct {
535 | Match
536 | Key string
537 | }
538 |
539 | // DefaultMessage return the default IP error message
540 | func (i IP) DefaultMessage() string {
541 | return MessageTmpls["IP"]
542 | }
543 |
544 | // GetKey return the i.Key
545 | func (i IP) GetKey() string {
546 | return i.Key
547 | }
548 |
549 | // GetLimitValue return the limit value
550 | func (i IP) GetLimitValue() interface{} {
551 | return nil
552 | }
553 |
554 | var base64Pattern = regexp.MustCompile(`^(?:[A-Za-z0-99+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`)
555 |
556 | // Base64 check struct
557 | type Base64 struct {
558 | Match
559 | Key string
560 | }
561 |
562 | // DefaultMessage return the default Base64 error message
563 | func (b Base64) DefaultMessage() string {
564 | return MessageTmpls["Base64"]
565 | }
566 |
567 | // GetKey return the b.Key
568 | func (b Base64) GetKey() string {
569 | return b.Key
570 | }
571 |
572 | // GetLimitValue return the limit value
573 | func (b Base64) GetLimitValue() interface{} {
574 | return nil
575 | }
576 |
577 | // just for chinese mobile phone number
578 | var mobilePattern = regexp.MustCompile(`^((\+86)|(86))?(1(([35][0-9])|[8][0-9]|[7][06789]|[4][579]))\d{8}$`)
579 |
580 | // Mobile check struct
581 | type Mobile struct {
582 | Match
583 | Key string
584 | }
585 |
586 | // DefaultMessage return the default Mobile error message
587 | func (m Mobile) DefaultMessage() string {
588 | return MessageTmpls["Mobile"]
589 | }
590 |
591 | // GetKey return the m.Key
592 | func (m Mobile) GetKey() string {
593 | return m.Key
594 | }
595 |
596 | // GetLimitValue return the limit value
597 | func (m Mobile) GetLimitValue() interface{} {
598 | return nil
599 | }
600 |
601 | // just for chinese telephone number
602 | var telPattern = regexp.MustCompile(`^(0\d{2,3}(\-)?)?\d{7,8}$`)
603 |
604 | // Tel check telephone struct
605 | type Tel struct {
606 | Match
607 | Key string
608 | }
609 |
610 | // DefaultMessage return the default Tel error message
611 | func (t Tel) DefaultMessage() string {
612 | return MessageTmpls["Tel"]
613 | }
614 |
615 | // GetKey return the t.Key
616 | func (t Tel) GetKey() string {
617 | return t.Key
618 | }
619 |
620 | // GetLimitValue return the limit value
621 | func (t Tel) GetLimitValue() interface{} {
622 | return nil
623 | }
624 |
625 | // Phone just for chinese telephone or mobile phone number
626 | type Phone struct {
627 | Mobile
628 | Tel
629 | Key string
630 | }
631 |
632 | // IsSatisfied judge whether obj is valid
633 | func (p Phone) IsSatisfied(obj interface{}) bool {
634 | return p.Mobile.IsSatisfied(obj) || p.Tel.IsSatisfied(obj)
635 | }
636 |
637 | // DefaultMessage return the default Phone error message
638 | func (p Phone) DefaultMessage() string {
639 | return MessageTmpls["Phone"]
640 | }
641 |
642 | // GetKey return the p.Key
643 | func (p Phone) GetKey() string {
644 | return p.Key
645 | }
646 |
647 | // GetLimitValue return the limit value
648 | func (p Phone) GetLimitValue() interface{} {
649 | return nil
650 | }
651 |
652 | // just for chinese zipcode
653 | var zipCodePattern = regexp.MustCompile(`^[1-9]\d{5}$`)
654 |
655 | // ZipCode check the zip struct
656 | type ZipCode struct {
657 | Match
658 | Key string
659 | }
660 |
661 | // DefaultMessage return the default Zip error message
662 | func (z ZipCode) DefaultMessage() string {
663 | return MessageTmpls["ZipCode"]
664 | }
665 |
666 | // GetKey return the z.Key
667 | func (z ZipCode) GetKey() string {
668 | return z.Key
669 | }
670 |
671 | // GetLimitValue return the limit value
672 | func (z ZipCode) GetLimitValue() interface{} {
673 | return nil
674 | }
675 |
--------------------------------------------------------------------------------