-
Notifications
You must be signed in to change notification settings - Fork 15
/
type_common.go
653 lines (519 loc) · 16.5 KB
/
type_common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/* Copyright (C) 2015-2023 김운하 (unha.kim@ghts.org)
이 파일은 GHTS의 일부입니다.
이 프로그램은 자유 소프트웨어입니다.
소프트웨어의 피양도자는 자유 소프트웨어 재단이 공표한 GNU LGPL 2.1판
규정에 따라 프로그램을 개작하거나 재배포할 수 있습니다.
이 프로그램은 유용하게 사용될 수 있으리라는 희망에서 배포되고 있지만,
특정한 목적에 적합하다거나, 이익을 안겨줄 수 있다는 묵시적인 보증을 포함한
어떠한 형태의 보증도 제공하지 않습니다.
보다 자세한 사항에 대해서는 GNU LGPL 2.1판을 참고하시기 바랍니다.
GNU LGPL 2.1판은 이 프로그램과 함께 제공됩니다.
만약, 이 문서가 누락되어 있다면 자유 소프트웨어 재단으로 문의하시기 바랍니다.
(자유 소프트웨어 재단 : Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA)
Copyright (C) 2015-2023년 UnHa Kim (unha.kim@ghts.org)
This file is part of GHTS.
GHTS is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, version 2.1 of the License.
GHTS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GHTS. If not, see <http://www.gnu.org/licenses/>. */
package lib
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"log"
"math/big"
"strings"
"time"
)
var 비어있는_일자값 = time.Time{}
func F비어있는_일자값() time.Time {
return 비어있는_일자값
}
type S비어있음 struct{}
type T정수 interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}
type T실수 interface {
float32 | float64
}
type T숫자 interface {
T정수 | T실수
}
func New에러(포맷_문자열or에러 interface{}, 추가_매개변수 ...interface{}) error {
switch 변환값 := 포맷_문자열or에러.(type) {
case nil:
return nil
case *S에러:
변환값.호출_경로_모음 = F호출경로_모음()
return 변환값
case S에러:
(&변환값).호출_경로_모음 = F호출경로_모음()
return 변환값
case error:
if len(추가_매개변수) > 0 {
panic(New에러("New에러() 예상하지 못한 추가 매개변수 : '%v'", len(추가_매개변수)))
}
에러 := new(S에러)
에러.원래_에러 = 변환값
에러.시점 = time.Now()
에러.에러_메시지 = strings.TrimSpace(변환값.Error())
에러.출력_완료 = false
에러.호출_경로_모음 = F호출경로_모음()
return 에러
case string:
에러 := new(S에러)
에러.원래_에러 = nil
에러.시점 = time.Now()
에러.에러_메시지 = fmt.Sprintf(strings.TrimSpace(변환값), 추가_매개변수...)
에러.출력_완료 = false
에러.호출_경로_모음 = F호출경로_모음()
return 에러
default:
panic(New에러("new에러() 예상하지 못한 자료형. '%T'", 포맷_문자열or에러))
}
}
func New에러with출력(포맷_문자열or에러 interface{}, 추가_매개변수 ...interface{}) error {
에러 := New에러(포맷_문자열or에러, 추가_매개변수...)
if !에러.(*S에러).G출력_완료() {
log.Println(에러.Error())
에러.(*S에러).출력_완료 = true
}
return 에러
}
type S에러 struct {
원래_에러 error
시점 time.Time
에러_메시지 string
호출_경로_모음 []string
출력_완료 bool
}
func (s S에러) Error() string {
버퍼 := new(bytes.Buffer)
if !strings.HasPrefix(s.에러_메시지, "\n") {
버퍼.WriteString("\n")
}
버퍼.WriteString(s.에러_메시지)
if !strings.HasSuffix(s.에러_메시지, "\n") {
버퍼.WriteString("\n")
}
버퍼.WriteString(" ")
for _, 호출경로 := range s.호출_경로_모음 {
버퍼.WriteString(호출경로)
버퍼.WriteString("\n")
}
return 버퍼.String()
}
func (s S에러) Is(에러값 error) bool {
if s.원래_에러 != nil {
return errors.Is(s.원래_에러, 에러값)
} else if s.에러_메시지 == 에러값.Error() {
return true
}
return false
}
func (s S에러) Unwrap() error { return s.원래_에러 }
func (s *S에러) G출력_완료() bool { return s.출력_완료 }
func (s *S에러) S출력_완료() { s.출력_완료 = true }
type S종목 struct {
코드 string
이름 string
시장_구분 T시장구분
전일_종가 int64
상한가 int64
하한가 int64
기준가 int64
}
func (s S종목) G코드() string { return s.코드 }
func (s S종목) G이름() string { return s.이름 }
func (s S종목) G식별_문자열() string { return F2문자열("%v[%v]", s.G이름(), s.G코드()) }
func (s S종목) G시장구분() T시장구분 { return s.시장_구분 }
func (s S종목) G전일_종가() int64 { return s.전일_종가 }
func (s S종목) G상한가() int64 { return s.상한가 }
func (s S종목) G하한가() int64 { return s.하한가 }
func (s S종목) G기준가() int64 { return s.기준가 }
func (s S종목) String() string {
버퍼 := new(bytes.Buffer)
버퍼.WriteString(s.이름)
버퍼.WriteString("(")
버퍼.WriteString(s.코드)
버퍼.WriteString(" : ")
버퍼.WriteString(s.시장_구분.String())
버퍼.WriteString(")")
return 버퍼.String()
}
func (s S종목) G복제본() *S종목 {
복제본 := new(S종목)
복제본.코드 = s.코드
복제본.이름 = s.이름
복제본.시장_구분 = s.시장_구분
복제본.전일_종가 = s.전일_종가
복제본.상한가 = s.상한가
복제본.하한가 = s.하한가
복제본.기준가 = s.기준가
return 복제본
}
func (s S종목) MarshalBinary() ([]byte, error) {
// TODO : 추가된 항목에 맞게 업데이트 필요.
속성 := make([]byte, 1)
속성[0] = uint8(s.시장_구분)
코드_길이 := make([]byte, 2)
binary.LittleEndian.PutUint16(코드_길이, uint16(len(s.코드))) // 인텔 및 AMD 계열 CPU는 리틀 엔디언
이름_길이 := make([]byte, 2)
binary.LittleEndian.PutUint16(이름_길이, uint16(len(s.이름)))
값_모음 := [][]byte{속성, 코드_길이, 이름_길이, []byte(s.코드), []byte(s.이름)}
버퍼 := new(bytes.Buffer)
for _, 값 := range 값_모음 {
if _, 에러 := 버퍼.Write(값); 에러 != nil {
return nil, 에러
}
}
return 버퍼.Bytes(), nil
}
func (s *S종목) UnmarshalBinary(값 []byte) (에러 error) {
defer func() {
if 에러 != nil {
s.코드 = ""
s.이름 = ""
}
}()
const 헤더_길이 = 5
switch {
case len(값) == 0:
return New에러with출력("비어있는 M값")
case len(값) < 헤더_길이:
return New에러with출력("너무 짧은 M값. %v", len(값))
}
속성 := 값[:1]
s.시장_구분 = T시장구분(속성[0])
코드_길이 := int(binary.LittleEndian.Uint16(값[1:3])) // 인텔 및 AMD 계열 CPU는 리틀 엔디언
이름_길이 := int(binary.LittleEndian.Uint16(값[3:5]))
총_길이 := 헤더_길이 + 코드_길이 + 이름_길이
if len(값) != 총_길이 {
return New에러with출력("무효한 M값. %v %v %v %v", len(값),
헤더_길이, 코드_길이, 이름_길이)
}
시작점 := 헤더_길이
s.코드 = string(값[시작점:(시작점 + 코드_길이)])
시작점 = 시작점 + 코드_길이
s.이름 = string(값[시작점:(시작점 + 이름_길이)])
return nil
}
func (s S종목) MarshalText() ([]byte, error) {
버퍼 := new(bytes.Buffer)
버퍼.WriteString(`{"종목_코드": "`)
버퍼.WriteString(s.코드)
버퍼.WriteString(`", "종목_이름": "`)
버퍼.WriteString(s.이름)
버퍼.WriteString(`", "시장_구분": "`)
버퍼.WriteString(s.시장_구분.String())
버퍼.WriteString(`"}`)
return 버퍼.Bytes(), nil
}
func (s *S종목) UnmarshalText(값 []byte) error {
문자열 := string(값)
s.코드 = F정규식_검색(문자열, []string{`{"종목_코드": ".+", "종목_이름": "`})
s.코드 = s.코드[len(`{"종목_코드": "`):]
s.코드 = s.코드[:len(s.코드)-len(`", "종목_이름": "`)]
s.이름 = F정규식_검색(문자열, []string{`"종목_이름": ".+", "시장_구분": "`})
s.이름 = s.이름[len(`"종목_이름": "`):]
s.이름 = s.이름[:len(s.이름)-len(`", "시장_구분": "`)]
시장_구분 := F정규식_검색(문자열, []string{`"시장_구분": ".+"}`})
시장_구분 = 시장_구분[len(`"시장_구분": "`):]
시장_구분 = 시장_구분[:len(시장_구분)-len(`"}`)]
if 에러 := s.시장_구분.Parse(시장_구분); 에러 != nil {
return 에러
}
return nil
}
// 종목
func New종목(코드 string, 이름 string, 시장_구분 T시장구분) *S종목 {
switch 시장_구분 {
case P시장구분_코스피, P시장구분_코스닥, P시장구분_ETF, P시장구분_코넥스:
if len(코드) != 6 {
코드 = F정규식_검색(코드, []string{"[0-9]+K?"})
}
if len(코드) != 6 {
panic(New에러with출력("잘못된 코드 '%v' '%v' '%v'", 코드, 이름, 시장_구분))
}
}
s := new(S종목)
s.코드 = 코드
s.이름 = 이름
s.시장_구분 = 시장_구분
return s
}
func New종목with가격정보(코드 string, 이름 string, 시장_구분 T시장구분, 전일_종가, 상한가, 하한가, 기준가 int64) *S종목 {
switch 시장_구분 {
case P시장구분_코스피, P시장구분_코스닥, P시장구분_ETF, P시장구분_코넥스:
if len(코드) != 6 {
코드 = F정규식_검색(코드, []string{"[0-9]+K?"})
}
if len(코드) != 6 {
panic(New에러with출력("잘못된 코드 '%v' '%v' '%v'", 코드, 이름, 시장_구분))
}
}
s := new(S종목)
s.코드 = 코드
s.이름 = 이름
s.시장_구분 = 시장_구분
s.전일_종가 = 전일_종가
s.상한가 = 상한가
s.하한가 = 하한가
s.기준가 = 기준가
return s
}
// 통화
type S통화 struct {
단위 T통화
금액 *big.Float
변경불가 bool
}
func (s *S통화) G단위() T통화 { return s.단위 }
func (s *S통화) G정수64() int64 {
값, _ := s.금액.Int64()
return 값
}
func (s *S통화) G실수64() float64 {
실수64, _ := s.금액.Float64()
return 실수64
}
func (s *S통화) G정밀값() *big.Float {
return new(big.Float).Copy(s.금액)
}
func (s *S통화) G문자열() string { return s.금액.String() }
func (s *S통화) G문자열_고정소숫점(소숫점_이하_자릿수 int) string {
return s.금액.Text('f', 소숫점_이하_자릿수)
}
func (s *S통화) G비교(다른_통화 *S통화) T비교 {
switch {
case s.단위 != 다른_통화.G단위():
return P비교_불가
default:
return T비교(s.금액.Cmp(다른_통화.G정밀값()))
}
}
func (s *S통화) G복사본() *S통화 {
복사본 := new(S통화)
복사본.단위 = s.G단위()
복사본.금액 = s.G정밀값()
복사본.변경불가 = false
return 복사본
}
func (s *S통화) G변경불가() bool {
return s.변경불가
}
func (s *S통화) S동결() { s.변경불가 = true }
func (s *S통화) S더하기(값 float64) *S통화 {
if s.변경불가 {
panic("변경불가능한 값입니다.")
//return s
}
s.금액 = new(big.Float).Add(s.금액, big.NewFloat(값))
return s
}
func (s *S통화) S빼기(값 float64) *S통화 {
if s.변경불가 {
panic(New에러with출력("변경불가능한 값입니다."))
//return s
}
s.금액 = new(big.Float).Sub(s.금액, big.NewFloat(값))
return s
}
func (s *S통화) S곱하기(값 float64) *S통화 {
if s.변경불가 {
panic(New에러("변경불가능한 값입니다."))
//return s
}
s.금액 = new(big.Float).Mul(s.금액, big.NewFloat(값))
return s
}
func (s *S통화) S나누기(값 float64) (*S통화, error) {
switch {
case s.변경불가:
panic("변경불가능한 값입니다.")
//return s, 에러
case 값 == 0.0:
return nil, New에러with출력("분모가 0인 나눗셈 불가.")
default:
s.금액 = new(big.Float).Quo(s.금액, big.NewFloat(값))
return s, nil
}
}
func (s *S통화) S금액(금액 float64) *S통화 {
if s.변경불가 {
panic("변경불가능한 값입니다.")
//return s
}
s.금액 = big.NewFloat(금액)
return s
}
func (s *S통화) String() string {
return s.단위.String() + " " + s.금액.String()
}
func (s S통화) MarshalBinary() ([]byte, error) {
var 변경_불가 byte
if !s.변경불가 {
변경_불가 = byte(0)
} else {
변경_불가 = byte(1)
}
금액, 에러 := s.금액.MarshalText()
if 에러 != nil {
return nil, 에러
}
버퍼 := new(bytes.Buffer)
버퍼.WriteByte(변경_불가)
버퍼.WriteByte(byte(s.단위))
버퍼.Write(금액)
return 버퍼.Bytes(), nil
}
func (s *S통화) UnmarshalBinary(값 []byte) (에러 error) {
defer func() {
if 에러 != nil {
s.단위 = T통화(byte(' '))
s.금액 = big.NewFloat(0.0)
s.변경불가 = true
}
}()
const 헤더_길이 = 2
switch {
case len(값) == 0:
return New에러with출력("비어있는 M값")
case len(값) <= 헤더_길이:
return New에러with출력("너무 짧은 M값. %v", len(값))
}
if 변경_불가 := int(값[0]); 변경_불가 == 0 {
s.변경불가 = false
} else {
s.변경불가 = true
}
s.단위 = T통화(값[1])
F확인1(s.금액.UnmarshalText(값[2:]))
return
}
func (s S통화) MarshalText() ([]byte, error) {
버퍼 := new(bytes.Buffer)
버퍼.WriteString("{")
버퍼.WriteString(s.단위.String())
버퍼.WriteString(",")
금액 := F확인2(s.금액.MarshalText())
버퍼.Write(금액)
버퍼.WriteString(",")
if s.변경불가 {
버퍼.WriteString("T")
} else {
버퍼.WriteString("F")
}
버퍼.WriteString("}")
return 버퍼.Bytes(), nil
}
func (s *S통화) UnmarshalText(값 []byte) (에러 error) {
defer func() {
if 에러 != nil {
s.단위 = T통화(byte(' '))
s.금액 = big.NewFloat(0.0)
s.변경불가 = true
}
}()
문자열 := string(값)
문자열_모음 := strings.Split(문자열, ",")
if len(문자열_모음) != 3 {
return New에러with출력("예상하지 못한 경우. %v", len(문자열_모음))
}
s.단위.Parse(strings.TrimSpace(문자열_모음[0][1:]))
if s.금액 == nil {
s.금액 = new(big.Float)
}
금액_문자열 := strings.TrimSpace(문자열_모음[1])
F확인1(s.금액.UnmarshalText([]byte(금액_문자열)))
switch strings.TrimSpace(문자열_모음[2])[:1] {
case "T", "t":
s.변경불가 = true
case "F", "f":
s.변경불가 = false
default:
에러 = New에러with출력("예상하지 못한 문자열. '%v'", strings.TrimSpace(문자열_모음[2]))
}
return nil
}
func New원화(금액 float64) *S통화 { return New통화(KRW, 금액) }
func New달러(금액 float64) *S통화 { return New통화(USD, 금액) }
func New유로(금액 float64) *S통화 { return New통화(EUR, 금액) }
func New위안(금액 float64) *S통화 { return New통화(CNY, 금액) }
func New통화(단위 T통화, 금액 float64) *S통화 {
F확인1(F통화단위_검사(단위))
s := new(S통화)
s.단위 = 단위
s.금액 = big.NewFloat(금액)
s.변경불가 = false
return s
}
func F통화단위_검사(통화단위 T통화) error {
switch 통화단위 {
case KRW, USD, EUR, CNY:
return nil
default:
return New에러with출력("잘못된 통화단위. '%v'", 통화단위)
}
}
// 중복 없고 무작위 순서의 문자열 모음.
type I문자열_집합 interface {
G슬라이스() []string
G포함(값 string) bool
G길이() int
S추가(값 string)
S삭제(값 string)
String() string
}
func New문자열_집합() I문자열_집합 {
s := new(s문자열_집합)
s.맵 = make(map[string]S비어있음)
return s
}
// 중복 없고 무작위 순서의 문자열 모음.
type s문자열_집합 struct {
맵 map[string]S비어있음
}
func (s *s문자열_집합) G슬라이스() []string {
길이 := len(s.맵)
값 := make([]string, 길이, 길이)
i := 0
for 문자열 := range s.맵 {
값[i] = 문자열
i++
}
return 값
}
func (s *s문자열_집합) G길이() int { return len(s.맵) }
func (s *s문자열_집합) G포함(값 string) bool {
for 문자열 := range s.맵 {
if 값 == 문자열 {
return true
}
}
return false
}
func (s *s문자열_집합) S추가(값 string) { s.맵[값] = S비어있음{} }
func (s *s문자열_집합) S삭제(값 string) { delete(s.맵, 값) }
func (s *s문자열_집합) String() string {
버퍼 := new(bytes.Buffer)
버퍼.WriteString("[")
마지막_인덱스 := len(s.맵) - 1
i := 0
for 문자열 := range s.맵 {
버퍼.WriteString(문자열)
if i != 마지막_인덱스 {
버퍼.WriteString(", ")
}
}
버퍼.WriteString("]")
return 버퍼.String()
}