fcee603081
Previously if the only names defined by _test packages were examples, the gotest script would emit an incorrect _testmain.go file. I worked around that by marking the example_test.go files +build ignored. This CL changes the gotest script to handle this case correctly, and removes the now-unnecessary build tags. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/214039 From-SVN: r280085
68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package json_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
type Size int
|
|
|
|
const (
|
|
Unrecognized Size = iota
|
|
Small
|
|
Large
|
|
)
|
|
|
|
func (s *Size) UnmarshalText(text []byte) error {
|
|
switch strings.ToLower(string(text)) {
|
|
default:
|
|
*s = Unrecognized
|
|
case "small":
|
|
*s = Small
|
|
case "large":
|
|
*s = Large
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s Size) MarshalText() ([]byte, error) {
|
|
var name string
|
|
switch s {
|
|
default:
|
|
name = "unrecognized"
|
|
case Small:
|
|
name = "small"
|
|
case Large:
|
|
name = "large"
|
|
}
|
|
return []byte(name), nil
|
|
}
|
|
|
|
func Example_textMarshalJSON() {
|
|
blob := `["small","regular","large","unrecognized","small","normal","small","large"]`
|
|
var inventory []Size
|
|
if err := json.Unmarshal([]byte(blob), &inventory); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
counts := make(map[Size]int)
|
|
for _, size := range inventory {
|
|
counts[size] += 1
|
|
}
|
|
|
|
fmt.Printf("Inventory Counts:\n* Small: %d\n* Large: %d\n* Unrecognized: %d\n",
|
|
counts[Small], counts[Large], counts[Unrecognized])
|
|
|
|
// Output:
|
|
// Inventory Counts:
|
|
// * Small: 3
|
|
// * Large: 2
|
|
// * Unrecognized: 3
|
|
}
|