ä»æ¥æ¸ãã®ã¯ãå æ¥Goè¨èªã®å人ããã¸ã§ã¯ãã§ãã Pixela ã«æãå ããéã«å®æãããã¨ã«ã¤ãã¦ã
å æ¥æãå ãããã®ã®ä¸é¨ã«ã以ä¸ã®ãããªãã®ããã£ãã
> 以ä¸ã®ãããªã³ãã³ããå®è¡ãã¦ã¿ã¾ãããã > > curl -X GET https://pixe.la/v1/users/a-know/graphs/stopwatch-test/20200504 -H 'X-USER-TOKEN:thisissecret' > > Pixela ã§ã¯ãåæ¥ä»ãã¨ã®ãã¼ã¿ã pixel ã¨å¼ãã§ããã®ã§ããããã®è©³ç´°ãåå¾ã§ããã³ãã³ãã§ãã`20200504` ã®ã¨ããã¯è©³ç´°ã確èªãããæ¥ä»ãæå®ãã¾ãããã®ã³ãã³ãã®å®è¡ããã¾ãããã°ã以ä¸ã®ãããªçµæã表示ãããã¨æãã¾ãã > > {"quantity":"0.50","optionalData":"{\"stopwatchUsage\":{\"stopwatchUseCount\":2,\"stopwatchPeriods\":[\"20:04:48 - 20:05:00\",\"20:05:13 - 20:05:30\"]}}"} > > quantity ã¨ããã®ã¯ãã®æ¥ã®è¨æ¸¬æéã®åè¨å¤ã®ãã¨ã§ããä¸æ¹ãoptionalData ã«æå®ããã¦ããã®ã¯ JSON å½¢å¼ã§è¡¨ããããªãã¸ã§ã¯ãã§ãstopwatchUseCount ã«ã¯ãè¨æ¸¬éå§ï¼çµäºãä½ã»ããå®æ½ããããã¨ããã«ã¦ã³ãããstopwatchPeriods ã«ã¯ãè¨æ¸¬å¯¾è±¡ã¨ãªã£ãæé帯ãããããããè¨é²ããã¦ãã¾ããä¸è¨ã®ä¾ã ã¨ããè¨æ¸¬éå§ï¼çµäºã2ã»ããããè¨æ¸¬ããã®ã¯ 20æ04å48ç§ã20æ05å00ç§ ã¨ 20æ05å13ã20æ05å30ç§ãã¨ããæ å ±ãã»ããããã¦ããå½¢ã«ãªãã¾ãã
Pixel
ã¨ãã以ä¸ã®ãããªï¼ã¨ã¯ããã説æã«ä¸è¦ãªãã®ã¯çç¥ãã¦ãããï¼ã¨ã³ãã£ãã£ããã£ã¦ã
type Pixel struct { Date string `json:"date"` Quantity string `json:"quantity"` OptionalData string `json:"optionalData"` }
ãã®ãã¡ OptionalData
ã¯ãã¦ã¼ã¶ã¼ãèªç±ã«JSONæååãæ ¼ç´ãããã¨ãã§ãããã¨ããä»æ§ããããªä»æ§ã ã£ãã¨ããã«ãä»åã®ã¢ãããã¼ãã§ãã·ã¹ãã å´ã OptionalData
ãç¨ããããã«ããããªã£ããã¨ãããã¨ã«ãªãã
ãã£ããè¦ä»¶ãã¾ã¨ããã¨ããã¨ã以ä¸ã®ãããªæãã ãããã
OptionalData
ã Unmarshal ãããã®ã«ã"stopwatchUsage":{"stopwatchUseCount":1,"stopwatchPeriods":["20:00:00 - 20:05:00"]}
ã¨ãã£ããªãã¸ã§ã¯ããçªã£è¾¼ã¿ãããOptionalData
ã«ãã¨ãã¨æ ¼ç´ããã¦ããæ å ±ã¯ããã¡ããç¶æããªããã°ãªããªãã- ã©ããªæ å ±ãæ ¼ç´ããã¦ãããã¯ããããªããä¿è¨¼ããã¦ãããã¨ã¯ããããJSONå½¢å¼ã®æååã§ãããã¨ãããã¨ã ãã
- ãã¼ã
stopwatchUsage
ã¨éè¤ããã±ã¼ã¹ã¯èæ ®ããªãï¼ã·ã¹ãã å´ãä¸æ¸ããããã¨ã許容ããï¼
æåã«è¡ãçããæ å ±ã¯ãã¡ã golang は ゆるふわに JSON を扱えまぁす! — KaoriYa ããã ãããã«ããæ¹æ³ã ã¨ãå ¨ãã®æªç¥ã®æ§é ã®ãªãã¸ã§ã¯ãã«å¯¾ãã¦é©ç¨ãããã¨ãã§ããªãããã«æãããï¼ããã¯ããã§ä¾¿å©ã«ä½¿ããå ´é¢ã¯ãããã®ã ã¨æããããã©ï¼ã
ãã ãä¸è¨ã®ãã¼ã¸å ã§ç´¹ä»ããã¦ãã mattn/go-jsonpointer ããã¾ãã«ä»åã®ã±ã¼ã¹ã§ã´ã£ããããããããªãã®ã§ãå®éãããã使ããã¨ã§é常ã«ãããªãå®è£ ãçµãããã¨ãã§ããã
JSON Pointer ã® GOå®è£ ã以ä¸ã«ãä»åã®ã±ã¼ã¹ã«ããã¦ãããç¨ããå®ä¾ã示ãã
以ä¸ã®ããã«ãOptionalData
ã«çªã£è¾¼ã¿ãã struct ãäºãç¨æãã¦ããã
count := 1 periods := []string{"20:00:00 - 20:05:00"} usage := &stopwatchUsage{ StopwatchUseCount: count, StopwatchPeriods: periods, }
*interface{}
ã渡ã㦠OptionalData
ã Unmarshalã
var obj interface{} json.Unmarshal([]byte(pixel.OptionalData), &obj)
ãã¨ã¯ãUnmarshal ããããã®ã«å¯¾ã㦠go-jsonpointer ã使ã£ã¦ãstopwatchUsage
ã¨ãããã¼ã§ set ãã¦ããã ãã§ããã£ãã
jsonpointer.Set(obj, "/stopwatchUsage", usage) optinalDataString, err := json.Marshal(obj)
/stopwatchUsage
ã¨ããã®ã JSON Pointerï¼RFC6901 ã¨ãã¦æ¨æºåããããJSONãªãã¸ã§ã¯ãã«å¯¾ããã¯ã¨ãªæååï¼ã
ä»ã«ããGet
Remove
ãã§ããããªãã¸ããªã® README ã«é常ã«ç°¡æ½ã«æ¸ãã¦ããã ãã¦ããã®ã§ãè¿·ããã¨ãä½ããªãã£ãããOptionalData
ããªãã§ãããªèªç±ãªé
ç®ã«ãããã......ã¯ã¤ã®ãã«ï¼ããªãã¦å¾æãããã¨ããªãã¦ãæ¬å½ã«è¯ãã£ãï¼åçã¯ããã»ããããããï¼ãæè¬ã§ãã