How to use tokenString method of types Package

Best Ginkgo code snippet using types.tokenString

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

lexer.go

Source: lexer.go Github

copy
1package lexer
2
3import (
4	"fmt"
5	"github.com/omnilium/mimic/internal/types"
6	"github.com/omnilium/mimic/internal/utility"
7	"regexp"
8	"strings"
9)
10
11const FILTER_SEPARATOR = "|"
12const FILTER_ARGUMENT_SEPARATOR = ":"
13const VARIABLE_ATTRIBUTE_SEPARATOR = "."
14const BLOCK_TAG_START = "{%"
15const BLOCK_TAG_END = "%}"
16const VARIABLE_TAG_START = "{{"
17const VARIABLE_TAG_END = "}}"
18const COMMENT_TAG_START = "{#"
19const COMMENT_TAG_END = "#}"
20const SINGLE_BRACE_START = "{"
21const SINGLE_BRACE_END = "}"
22
23type Lexer struct {
24	template      string
25	verbatim      bool
26	r             *regexp.Regexp
27	verbatimBlock string
28}
29
30// NewLexer initializes a new Lexer with the given template string and a flag for verbatim usage.
31func NewLexer(template string) (*Lexer, error) {
32	r, err := regexp.Compile(`({%.*?%}|{{.*?}}|{#.*?#})`)
33	if err != nil {
34		return nil, err
35	}
36
37	return &Lexer{
38		template:      template,
39		verbatim:      false,
40		r:             r,
41		verbatimBlock: "",
42	}, nil
43}
44
45func (l *Lexer) Tokenize() []types.Token {
46	inTag := false
47	lineNo := 1
48	var result []types.Token
49
50	for _, tokenString := range utility.SmartSplitWithRegex(l.r, l.template, -1) {
51		if tokenString != "" {
52			result = append(result, l.createToken(tokenString, lineNo, inTag))
53			lineNo += strings.Count(tokenString, "\n")
54		}
55
56		inTag = !inTag
57	}
58
59	return result
60}
61
62func (l *Lexer) createToken(tokenString string, lineNo int, inTag bool) types.Token {
63	if inTag {
64		tokenStart := tokenString[0:2]
65		if tokenStart == BLOCK_TAG_START {
66			content := strings.TrimSpace(tokenString[2 : len(tokenString)-2])
67
68			if l.verbatim {
69				if content != l.verbatimBlock {
70					return types.Token{
71						TokenType: types.TOKEN_TEXT,
72						Content:   tokenString,
73						LineNo:    lineNo,
74					}
75				}
76
77				l.verbatim = false
78			} else if content[:8] == "verbatim" {
79				l.verbatim = true
80				l.verbatimBlock = fmt.Sprintf("end%s", content)
81			}
82
83			return types.Token{
84				TokenType: types.TOKEN_BLOCK,
85				Content:   content,
86				LineNo:    lineNo,
87			}
88		}
89
90		if l.verbatim == false {
91			content := strings.TrimSpace(tokenString[2 : len(tokenString)-2])
92
93			if tokenStart == VARIABLE_TAG_START {
94				return types.Token{
95					TokenType: types.TOKEN_VAR,
96					Content:   content,
97					LineNo:    lineNo,
98				}
99			}
100
101			if tokenStart == COMMENT_TAG_START {
102				return types.Token{
103					TokenType: types.TOKEN_COMMENT,
104					Content:   content,
105					LineNo:    lineNo,
106				}
107			}
108		}
109	}
110
111	return types.Token{
112		TokenType: types.TOKEN_TEXT,
113		Content:   tokenString,
114		LineNo:    lineNo,
115	}
116}
117
Full Screen

main.go

Source: main.go Github

copy
1package main
2
3import (
4	"errors"
5	"go/ast"
6	"go/parser"
7	"go/token"
8	"io/ioutil"
9	"log"
10	"os"
11	"path"
12	"strings"
13
14	"golang.org/x/tools/imports"
15)
16
17type Func struct {
18	Name   string
19	Args   string
20	Return string
21}
22
23func (f Func) Interface() string {
24	return f.Name + f.Args + " " + f.Return
25}
26
27func (f Func) Method() string {
28	return f.Name + " func" + f.Args + " " + f.Return
29}
30
31func main() {
32	pkgDir := "app/submodule"
33	typeMap := map[string]string{
34		"Partition":      "chainApiTypes.Partition",
35		"Deadline":       "chainApiTypes.Deadline",
36		"MarketDeal":     "chainApiTypes.MarketDeal",
37		"BlockTemplate":  "mineApiTypes.BlockTemplate",
38		"InvocResult":    "syncApiTypes.InvocResult",
39		"ProtocolParams": "chainApiTypes.ProtocolParams",
40		"BlockMessage":   "chainApiTypes.BlockMessage",
41		"MsgLookup":      "messageApiTypes.MsgLookup",
42		"BlockMessages":  "chainApiTypes.BlockMessages",
43	}
44
45	_, pkgs, err := collectAPIFile(pkgDir)
46	if err != nil {
47		log.Fatal(err)
48		return
49	}
50
51	codes := make(map[string][]Func)
52	for _, pkg := range pkgs {
53		for _, files := range pkg.Files {
54			for _, decl := range files.Decls {
55				if funcDecl, ok := decl.(*ast.FuncDecl); ok {
56					// just func Decl
57
58					function := Func{}
59					receiveName, err := joinFieldList(funcDecl.Recv, typeMap)
60					if err != nil {
61						log.Fatal(err)
62						return
63					}
64					receiveName = strings.Trim(receiveName, "*")
65					if receiveName == "" {
66						continue
67					}
68
69					function.Name = funcDecl.Name.Name
70					if !('A' <= function.Name[0] && function.Name[0] <= 'Z') {
71						continue
72					}
73					// parser parameter
74					fieldList, err := joinFieldList(funcDecl.Type.Params, typeMap)
75					if err != nil {
76						log.Fatal(err)
77						return
78					}
79					function.Args = "(" + fieldList + ")"
80
81					//parser return values
82					fieldList, err = joinFieldList(funcDecl.Type.Results, typeMap)
83					if err != nil {
84						log.Fatal(err)
85						return
86					}
87					if funcDecl.Type.Results != nil && len(funcDecl.Type.Results.List) > 0 {
88						function.Return = "(" + fieldList + ")"
89					} else {
90						function.Return = fieldList
91					}
92
93					if _, has := codes[receiveName]; has {
94						codes[receiveName] = append(codes[receiveName], function)
95					} else {
96						codes[receiveName] = []Func{function}
97					}
98				}
99			}
100		}
101	}
102
103	_ = generateCode(codes, "./app/client/client.go")
104}
105
106func generateCode(codelines map[string][]Func, fname string) error {
107	fs, err := os.Create(fname)
108	if err != nil {
109		return err
110	}
111	packages := `package client
112import (
113		"context"
114		"io"
115		"time"
116		"github.com/filecoin-project/go-address"
117		"github.com/filecoin-project/go-bitfield"
118		"github.com/filecoin-project/go-state-types/abi"
119		"github.com/filecoin-project/go-state-types/big"
120		acrypto "github.com/filecoin-project/go-state-types/crypto"
121		"github.com/filecoin-project/go-state-types/dline"
122		"github.com/filecoin-project/go-state-types/network"
123		"github.com/ipfs/go-cid"
124		ipld "github.com/ipfs/go-ipld-format"
125		"github.com/libp2p/go-libp2p-core/metrics"
126		"github.com/libp2p/go-libp2p-core/peer"
127		ma "github.com/multiformats/go-multiaddr"
128		chainApiTypes "github.com/filecoin-project/venus/app/submodule/chain"
129		mineApiTypes "github.com/filecoin-project/venus/app/submodule/mining"
130		"github.com/filecoin-project/venus/app/submodule/mpool"
131		syncApiTypes "github.com/filecoin-project/venus/app/submodule/syncer"
132		"github.com/filecoin-project/venus/pkg/beacon"
133		"github.com/filecoin-project/venus/pkg/chain"
134		"github.com/filecoin-project/venus/pkg/chainsync/status"
135		"github.com/filecoin-project/venus/pkg/crypto"
136		"github.com/filecoin-project/venus/pkg/net"
137		"github.com/filecoin-project/venus/pkg/specactors/builtin/miner"
138		"github.com/filecoin-project/venus/pkg/types"
139		"github.com/filecoin-project/venus/pkg/vm"
140		"github.com/filecoin-project/venus/pkg/wallet"
141)
142`
143	builder := strings.Builder{}
144	builder.WriteString(packages)
145
146	builder.WriteString("type FullNode struct {\n")
147	for _, functions := range codelines {
148		for _, function := range functions {
149			builder.WriteString("\t" + function.Method() + "\n")
150		}
151		builder.WriteString("\n")
152	}
153	builder.WriteString("}\n\n")
154
155	for name, functions := range codelines {
156		builder.WriteString("type " + name + " struct {\n")
157		for _, function := range functions {
158			builder.WriteString("\t" + function.Method() + "\n")
159		}
160		builder.WriteString("}\n\n")
161	}
162
163	_, _ = fs.WriteString(builder.String())
164	_ = fs.Close()
165
166	options := &imports.Options{
167		TabWidth:  8,
168		TabIndent: true,
169		Comments:  true,
170		Fragment:  true,
171	}
172
173	res, err := imports.Process(fname, nil, options)
174	if err != nil {
175		return err
176	}
177
178	return ioutil.WriteFile(fname, res, 0777)
179}
180
181func joinFieldList(fields *ast.FieldList, typeMap map[string]string) (string, error) {
182	if fields == nil || len(fields.List) == 0 {
183		return "", nil
184	}
185	returnString := ""
186	returns := fields.List
187	for _, r := range returns {
188		tokeString, err := typeString(r.Type, typeMap)
189		if err != nil {
190			log.Fatal(err)
191			return "", err
192		}
193		returnString += tokeString + ","
194	}
195
196	returnString = strings.Trim(returnString, ",")
197	return returnString, nil
198}
199
200func typeString(token ast.Expr, typeMap map[string]string) (string, error) {
201	tokenString := ""
202	switch t := token.(type) {
203	case *ast.SelectorExpr:
204		name, err := typeString(t.X, typeMap)
205		if err != nil {
206			return tokenString, err
207		}
208		tokenString += name + "." + t.Sel.String()
209	case *ast.Ident:
210		if token, has := typeMap[t.String()]; has {
211			tokenString += token
212		} else {
213			tokenString += t.String()
214		}
215
216	case *ast.StarExpr:
217		name, err := typeString(t.X, typeMap)
218		if err != nil {
219			return tokenString, err
220		}
221		tokenString += "*" + name
222	case *ast.ArrayType:
223		name, err := typeString(t.Elt, typeMap)
224		if err != nil {
225			return tokenString, err
226		}
227		tokenString += "[]" + name
228	case *ast.InterfaceType:
229		tokenString += "interface{}"
230	case *ast.ChanType:
231		name, err := typeString(t.Value, typeMap)
232		if err != nil {
233			return tokenString, err
234		}
235		tokenString += "chan " + name
236	case *ast.MapType:
237		keyString, err := typeString(t.Key, typeMap)
238		if err != nil {
239			return tokenString, err
240		}
241		valueString, err := typeString(t.Value, typeMap)
242		if err != nil {
243			return tokenString, err
244		}
245		tokenString += "map[" + keyString + "]" + valueString
246	default:
247		return "", errors.New("unexpect types")
248	}
249	return tokenString, nil
250}
251
252func collectAPIFile(dir string) (*token.FileSet, map[string]*ast.Package, error) {
253	files, err := ioutil.ReadDir(dir)
254	if err != nil {
255		return nil, nil, err
256	}
257	fset := token.NewFileSet()
258	pkgs := make(map[string]*ast.Package)
259	for _, f := range files {
260		subDirPath := path.Join(dir, f.Name())
261
262		subModuleFiles, err := ioutil.ReadDir(subDirPath)
263		if err != nil {
264			return nil, nil, err
265		}
266		for _, goFile := range subModuleFiles {
267			if !goFile.IsDir() && strings.HasSuffix(goFile.Name(), "_api.go") {
268				gofileName := path.Join(subDirPath, goFile.Name())
269				if src, err := parser.ParseFile(fset, gofileName, nil, 0); err == nil {
270					name := src.Name.Name
271					pkg, found := pkgs[name]
272					if !found {
273						pkg = &ast.Package{
274							Name:  name,
275							Files: make(map[string]*ast.File),
276						}
277						pkgs[name] = pkg
278					}
279					pkg.Files[gofileName] = src
280				} else {
281					return nil, nil, err
282				}
283			}
284		}
285	}
286	return fset, pkgs, nil
287}
288
Full Screen

jwt.go

Source: jwt.go Github

copy
1package ethapi
2
3import (
4	"fmt"
5	"github.com/dgrijalva/jwt-go"
6	"github.com/golang/protobuf/proto"
7	"math/big"
8	"pdx-chain/common"
9	"pdx-chain/core/publicBC"
10	"pdx-chain/core/types"
11	"pdx-chain/core/vm"
12	"pdx-chain/crypto"
13	"pdx-chain/ethdb"
14	"pdx-chain/log"
15	"pdx-chain/pdxcc"
16	pb "pdx-chain/pdxcc/protos"
17	"pdx-chain/pdxcc/util"
18	"pdx-chain/utopia"
19	"pdx-chain/utopia/utils"
20)
21
22func ParseToken(tx *types.Transaction, stateDB vm.StateDB) (token string, err error) {
23	//if cc tx
24	var ok bool
25	var jwtBuf []byte
26	to := tx.To()
27	if to != nil {
28		if pdxcc.CanExec(*to) {
29			//is cc
30			token, err := parseCCToken(tx.Data())
31			if err != nil {
32				return "", fmt.Errorf("parse cc token:%v", err)
33			}
34			return token, nil
35		}
36
37		//keyHash := utils.CCKeyHash
38		//ccBuf := stateDB.GetPDXState(*to, keyHash)
39		ccBuf := stateDB.GetCode(*to)
40		if len(ccBuf) != 0 {
41			var deploy pb.Deployment
42			err = proto.Unmarshal(ccBuf, &deploy)
43			if err == nil {
44				//is cc
45				token, err := parseCCToken(tx.Data())
46				if err != nil {
47					return "", fmt.Errorf("parse cc token:%v", err)
48				}
49				return token, nil
50			}
51		}
52	}
53
54	_, meta, err := utopia.ParseData(tx.Data())
55	if err != nil {
56		return "", fmt.Errorf("parse data:%v", err)
57	}
58	if meta == nil {
59		return "", fmt.Errorf("meta == nil")
60	}
61
62	jwtBuf, ok = meta["jwt"]
63	if !ok {
64		return "", fmt.Errorf("jwt not in meta, meta:%v", meta)
65	}
66
67	return string(jwtBuf), nil
68}
69
70func parseCCToken(payload []byte) (token string, err error) {
71	//payload, _, err := utopia.ParseData(tx.Data()) //maybe parse
72	txPb := &pb.Transaction{}
73	err = proto.Unmarshal(payload, txPb)
74	if err != nil {
75		return "", fmt.Errorf("proto unmarshal tx:%v", err)
76	}
77
78	var jwtBuf []byte
79	var ok bool
80	txType := txPb.Type
81	switch txType {
82	case types.Transaction_deploy:
83		deploy := pb.Deployment{}
84		err = proto.Unmarshal(txPb.Payload, &deploy)
85		if err != nil {
86			return "", fmt.Errorf("proto unmarshal deploy:%v", err)
87		}
88		jwtBuf, ok = deploy.Payload.Meta["jwt"]
89		if !ok {
90			return "", fmt.Errorf("jwt not in deploy meta")
91		}
92
93	case types.Transaction_invoke: //start stop withdraw
94		invocation := pb.Invocation{}
95		err = proto.Unmarshal(txPb.Payload, &invocation)
96		if err != nil {
97			return "", fmt.Errorf("proto unmarshal invocation:%v", err)
98		}
99
100		jwtBuf, ok = invocation.Meta["jwt"]
101		if !ok {
102			return "", fmt.Errorf("jwt not in invocation meta")
103		}
104
105	}
106
107	return string(jwtBuf), nil
108}
109
110func CheckFreeze(tx *types.Transaction, stateDB vm.StateDB, from common.Address) bool {
111	//var signer types.Signer = types.HomesteadSigner{}
112	//if tx.Protected() {
113	//	signer = types.NewEIP155Signer(tx.ChainId())
114	//}
115	//from, err := types.Sender(signer, tx)
116	//if err != nil {
117	//	log.Error("check freeze get sender", "err", err)
118	//	return false
119	//}
120	fromHash := util.EthHash(from.Bytes())
121	r := stateDB.GetPDXState(utils.AccessCtlContract, fromHash)
122	if len(r) > 0 && string(r) == "1" {
123		log.Trace("string(r) == 1")
124		return true
125	}
126
127	return false
128}
129
130func CheckToken(tx *types.Transaction, stateDB vm.StateDB) bool {
131	if tx.To() != nil && *tx.To() == utils.AccessCtlContract {
132		log.Trace("access ctl token verify")
133		tokenString, err := ParseToken(tx, stateDB)
134		if err != nil {
135			log.Error("parse token", "err", err)
136			return false
137		}
138
139		if !checkJWT(tokenString, "a") {
140			return false
141		}
142		return true
143	}
144
145	//DappAuth:T UserAuth:T Deploy:d regular Tx:u/d
146	if utopia.ConsortiumConf.DappAuth && utopia.ConsortiumConf.UserAuth {
147		tokenString, err := ParseToken(tx, stateDB)
148		if err != nil {
149			log.Error("parse token", "err", err)
150			return false
151		}
152		//if deploy contract tx must role d
153		if tx.To() == nil || *tx.To() == utils.CCBaapDeploy {
154			if !checkJWT(tokenString, "d") {
155				return false
156			}
157		} else {
158			//if not , regular tx must role u at least
159			if !checkJWT(tokenString, "u/d") {
160				return false
161			}
162		}
163	}
164
165	//DappAuth:F UserAuth:T Deploy:u/d regular Tx:u/d
166	if !utopia.ConsortiumConf.DappAuth && utopia.ConsortiumConf.UserAuth {
167		tokenString, err := ParseToken(tx, stateDB)
168		if err != nil {
169			log.Error("parse token", "err", err)
170			return false
171		}
172
173		//if deploy contract tx must role d
174		if tx.To() == nil || *tx.To() == utils.CCBaapDeploy {
175			if !checkJWT(tokenString, "u/d") {
176				return false
177			}
178		} else {
179			//if not , regular tx must role u at least
180			if !checkJWT(tokenString, "u/d") {
181				return false
182			}
183		}
184	}
185
186	//DappAuth:T UserAuth:F Deploy:d regular Tx:-
187	if utopia.ConsortiumConf.DappAuth && !utopia.ConsortiumConf.UserAuth {
188		//if deploy contract tx must role d
189		if tx.To() == nil || *tx.To() == utils.CCBaapDeploy {
190			tokenString, err := ParseToken(tx, stateDB)
191			if err != nil {
192				log.Error("parse token", "err", err)
193				return false
194			}
195
196			if !checkJWT(tokenString, "d") {
197				return false
198			}
199		}
200	}
201
202	//DappAuth:F UserAuth:F Deploy:- regular Tx:-
203
204	return true
205}
206
207func checkJWT(tokenString string, roleLimit string) (success bool) {
208	if tokenString == "" {
209		log.Error("tokenString empty")
210		return false
211	}
212
213	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
214		// Don't forget to validate the alg is what you expect:
215		if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
216			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
217		}
218
219		if token.Header["alg"] != "ES256" {
220			return nil, fmt.Errorf("invalid signing alg:%v, only ES256 is prefered", token.Header["alg"])
221		}
222
223		claims, ok := token.Claims.(jwt.MapClaims)
224		if !ok {
225			return nil, fmt.Errorf("token claims type error")
226		}
227
228		ak, ok := claims["ak"]
229		if !ok {
230			return nil, fmt.Errorf("ak no exist in claims")
231		}
232		hexPubKey, ok := ak.(string)
233		if !ok || len(hexPubKey) != vm.PUBK_HEX_LEN {
234			return nil, fmt.Errorf("ak format error")
235		}
236
237		//check public key
238		_, ok = utopia.UserCertPublicKeyMap[hexPubKey]
239		if !ok {
240			return nil, fmt.Errorf("ak no exist in user cert public key")
241		}
242
243		return crypto.DecompressPubkey(common.Hex2Bytes(hexPubKey))
244	})
245
246	if err != nil {
247		log.Error("jwt parse", "err", err)
248		return false
249	}
250
251	if claims, success := token.Claims.(jwt.MapClaims); success && token.Valid {
252		limit, success := claims["l"].(float64)
253		if !success {
254			log.Error("l not correct")
255			return false
256		}
257		if !checkLimit(tokenString, int64(limit)) {
258			log.Error("check limit fail")
259			return false
260		}
261
262		role, success := claims["r"]
263		if !success {
264			log.Error("role no match", "role", role, "ok", success)
265
266			return false
267		}
268		if roleLimit == "d" || roleLimit == "a" {
269			if role != roleLimit {
270				log.Error("role no auth", "role", role, "roleLimit", roleLimit)
271				return false
272			}
273		} else {
274			if role == "u" || role == "d" {
275			} else {
276				log.Error("role no exist", "role", role)
277				return false
278			}
279		}
280
281	} else {
282		log.Error("token invalid")
283		return false
284	}
285
286	return true
287}
288
289func checkLimit(tokenString string, limit int64) bool {
290	db := *ethdb.ChainDb
291	tokenHash := util.EthHash([]byte(tokenString))
292	has, err := db.Has(tokenHash.Bytes())
293	if err != nil {
294		log.Error("db has", "err", err)
295		return false
296	}
297
298	currentBlockNum := public.BC.CurrentBlock().Number()
299	if !has {
300		expiredBlockNum := big.NewInt(0).Add(big.NewInt(limit), currentBlockNum)
301		err = db.Put(tokenHash.Bytes(), expiredBlockNum.Bytes())
302		if err != nil {
303			log.Error("db put tokenHash", "err", err)
304			return false
305		}
306	} else {
307		numByts, err := db.Get(tokenHash.Bytes())
308		if err != nil {
309			log.Error("db get tokenHash", "err", err)
310			return false
311		}
312
313		expiredBlockNum := big.NewInt(0).SetBytes(numByts)
314		if currentBlockNum.Cmp(expiredBlockNum) > 0 {
315			log.Error("out of limit", "currentBlockNum", currentBlockNum.String(), "expiredBlockNum", expiredBlockNum)
316			return false
317		}
318	}
319
320	return true
321}
322
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in

Trigger tokenString code on LambdaTest Cloud Grid

Execute automation tests with tokenString on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)