-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build tags for smaller binaries that don't need net/http or encoding/…
…json (#63) * no_http/no_net and no_json buildtags * adding info about no_json and github.com/Zxilly/go-size-analyzer/cmd/gsa * update comments and add test for nil *string * adding make coverage now that fortio/workflows#51 is done * added tests for struct etc * lint the no_json path too * json->JSON Co-authored-by: ccoVeille <[email protected]> * verify through test that byte = uint8 works and added log.Rune(k,v) to print 1 character as one would most likely expect * put variable in makefile for go so I can run 'make GO_BIN=go1.23rc1' --------- Co-authored-by: ccoVeille <[email protected]>
- Loading branch information
Showing
13 changed files
with
365 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
.DS_Store | ||
.golangci.yml | ||
fullsize | ||
smallsize | ||
coverage.out | ||
coverage?.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch test function with tags", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "test", | ||
"program": "${workspaceFolder}", | ||
"buildFlags": "-tags=no_json" | ||
} | ||
|
||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
coverage: | ||
ignore: | ||
- "levelsDemo" | ||
# not used (is nothing at all used?) | ||
# files: | ||
# - "coverage*.out" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2017-2024 Fortio Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Moved json logging out so it can be skipped for smallest binaries based on build tags. | ||
// only difference is with nested struct/array logging or logging of types with json Marchaller interface. | ||
|
||
//go:build !no_json | ||
|
||
package log // import "fortio.org/log" | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
var fullJSON = true | ||
|
||
func toJSON(v any) string { | ||
bytes, err := json.Marshal(v) | ||
if err != nil { | ||
return strconv.Quote(fmt.Sprintf("ERR marshaling %v: %v", v, err)) | ||
} | ||
str := string(bytes) | ||
// We now handle errors before calling toJSON: if there is a marshaller we use it | ||
// otherwise we use the string from .Error() | ||
return str | ||
} | ||
|
||
func (v ValueType[T]) String() string { | ||
// if the type is numeric, use Sprint(v.val) otherwise use Sprintf("%q", v.Val) to quote it. | ||
switch s := any(v.Val).(type) { | ||
case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, | ||
float32, float64: | ||
return fmt.Sprint(s) | ||
case string: | ||
return fmt.Sprintf("%q", s) | ||
case error: | ||
// Sadly structured errors like nettwork error don't have the reason in | ||
// the exposed struct/JSON - ie on gets | ||
// {"Op":"read","Net":"tcp","Source":{"IP":"127.0.0.1","Port":60067,"Zone":""}, | ||
// "Addr":{"IP":"127.0.0.1","Port":3000,"Zone":""},"Err":{}} | ||
// instead of | ||
// read tcp 127.0.0.1:60067->127.0.0.1:3000: i/o timeout | ||
// Noticed in https://github.com/fortio/fortio/issues/913 | ||
_, hasMarshaller := s.(json.Marshaler) | ||
if hasMarshaller { | ||
return toJSON(v.Val) | ||
} else { | ||
return fmt.Sprintf("%q", s.Error()) | ||
} | ||
/* It's all handled by json fallback now even though slightly more expensive at runtime, it's a lot simpler */ | ||
default: | ||
return toJSON(v.Val) // was fmt.Sprintf("%q", fmt.Sprint(v.Val)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//go:build !no_json | ||
// +build !no_json | ||
|
||
package log // import "fortio.org/fortio/log" | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestToJSON_MarshalError(t *testing.T) { | ||
badValue := make(chan int) | ||
|
||
expected := fmt.Sprintf("\"ERR marshaling %v: %v\"", badValue, "json: unsupported type: chan int") | ||
actual := toJSON(badValue) | ||
|
||
if actual != expected { | ||
t.Errorf("Expected %q, got %q", expected, actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.