github.com/grahambrereton-form3/tilt@v0.10.18/internal/store/private_fields.go (about) 1 package store 2 3 /*! 4 MIT License 5 6 Copyright (c) 2016 json-iterator 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in all 16 copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 SOFTWARE. 25 */ 26 27 // Copied from 28 // https://github.com/json-iterator/go/blob/master/extra/privat_fields.go 29 // and made possible to re-use in a particular api config 30 31 import ( 32 "strings" 33 "unicode" 34 35 jsoniter "github.com/json-iterator/go" 36 ) 37 38 type privateFieldsExtension struct { 39 jsoniter.DummyExtension 40 } 41 42 func (extension *privateFieldsExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) { 43 for _, binding := range structDescriptor.Fields { 44 isPrivate := unicode.IsLower(rune(binding.Field.Name()[0])) 45 if isPrivate { 46 tag, hastag := binding.Field.Tag().Lookup("json") 47 if !hastag { 48 binding.FromNames = []string{binding.Field.Name()} 49 binding.ToNames = []string{binding.Field.Name()} 50 continue 51 } 52 tagParts := strings.Split(tag, ",") 53 names := calcFieldNames(binding.Field.Name(), tagParts[0], tag) 54 binding.FromNames = names 55 binding.ToNames = names 56 } 57 } 58 } 59 60 func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) []string { 61 // ignore? 62 if wholeTag == "-" { 63 return []string{} 64 } 65 // rename? 66 var fieldNames []string 67 if tagProvidedFieldName == "" { 68 fieldNames = []string{originalFieldName} 69 } else { 70 fieldNames = []string{tagProvidedFieldName} 71 } 72 // private? 73 isNotExported := unicode.IsLower(rune(originalFieldName[0])) 74 if isNotExported { 75 fieldNames = []string{} 76 } 77 return fieldNames 78 }