How to use Is method of types Package

Best Ginkgo code snippet using types.Is

core.go

Source:core.go Github

copy

Full Screen

...72 // initiators with no matching PDI, matchingPDI will be set to the length of73 // the input string.74 matchingPDI []int75 // Index of matching isolate initiator for PDI characters. For other76 // characters, and for PDIs with no matching isolate initiator, the value of77 // matchingIsolateInitiator will be set to -1.78 matchingIsolateInitiator []int79}80// newParagraph initializes a paragraph. The user needs to supply a few arrays81// corresponding to the preprocessed text input. The types correspond to the82// Unicode BiDi classes for each rune. pairTypes indicates the bracket type for83// each rune. pairValues provides a unique bracket class identifier for each84// rune (suggested is the rune of the open bracket for opening and matching85// close brackets, after normalization). The embedding levels are optional, but86// may be supplied to encode embedding levels of styled text.87//88// TODO: return an error.89func newParagraph(types []Class, pairTypes []bracketType, pairValues []rune, levels level) *paragraph {90 validateTypes(types)91 validatePbTypes(pairTypes)92 validatePbValues(pairValues, pairTypes)93 validateParagraphEmbeddingLevel(levels)94 p := &paragraph{95 initialTypes: append([]Class(nil), types...),96 embeddingLevel: levels,97 pairTypes: pairTypes,98 pairValues: pairValues,99 resultTypes: append([]Class(nil), types...),100 }101 p.run()102 return p103}104func (p *paragraph) Len() int { return len(p.initialTypes) }105// The algorithm. Does not include line-based processing (Rules L1, L2).106// These are applied later in the line-based phase of the algorithm.107func (p *paragraph) run() {108 p.determineMatchingIsolates()109 // 1) determining the paragraph level110 // Rule P1 is the requirement for entering this algorithm.111 // Rules P2, P3.112 // If no externally supplied paragraph embedding level, use default.113 if p.embeddingLevel == implicitLevel {114 p.embeddingLevel = p.determineParagraphEmbeddingLevel(0, p.Len())115 }116 // Initialize result levels to paragraph embedding level.117 p.resultLevels = make([]level, p.Len())118 setLevels(p.resultLevels, p.embeddingLevel)119 // 2) Explicit levels and directions120 // Rules X1-X8.121 p.determineExplicitEmbeddingLevels()122 // Rule X9.123 // We do not remove the embeddings, the overrides, the PDFs, and the BNs124 // from the string explicitly. But they are not copied into isolating run125 // sequences when they are created, so they are removed for all126 // practical purposes.127 // Rule X10.128 // Run remainder of algorithm one isolating run sequence at a time129 for _, seq := range p.determineIsolatingRunSequences() {130 // 3) resolving weak types131 // Rules W1-W7.132 seq.resolveWeakTypes()133 // 4a) resolving paired brackets134 // Rule N0135 resolvePairedBrackets(seq)136 // 4b) resolving neutral types137 // Rules N1-N3.138 seq.resolveNeutralTypes()139 // 5) resolving implicit embedding levels140 // Rules I1, I2.141 seq.resolveImplicitLevels()142 // Apply the computed levels and types143 seq.applyLevelsAndTypes()144 }145 // Assign appropriate levels to 'hide' LREs, RLEs, LROs, RLOs, PDFs, and146 // BNs. This is for convenience, so the resulting level array will have147 // a value for every character.148 p.assignLevelsToCharactersRemovedByX9()149}150// determineMatchingIsolates determines the matching PDI for each isolate151// initiator and vice versa.152//153// Definition BD9.154//155// At the end of this function:156//157// - The member variable matchingPDI is set to point to the index of the158// matching PDI character for each isolate initiator character. If there is159// no matching PDI, it is set to the length of the input text. For other160// characters, it is set to -1.161// - The member variable matchingIsolateInitiator is set to point to the162// index of the matching isolate initiator character for each PDI character.163// If there is no matching isolate initiator, or the character is not a PDI,164// it is set to -1.165func (p *paragraph) determineMatchingIsolates() {166 p.matchingPDI = make([]int, p.Len())167 p.matchingIsolateInitiator = make([]int, p.Len())168 for i := range p.matchingIsolateInitiator {169 p.matchingIsolateInitiator[i] = -1170 }171 for i := range p.matchingPDI {172 p.matchingPDI[i] = -1173 if t := p.resultTypes[i]; t.in(LRI, RLI, FSI) {174 depthCounter := 1175 for j := i + 1; j < p.Len(); j++ {176 if u := p.resultTypes[j]; u.in(LRI, RLI, FSI) {177 depthCounter++178 } else if u == PDI {179 if depthCounter--; depthCounter == 0 {180 p.matchingPDI[i] = j181 p.matchingIsolateInitiator[j] = i182 break183 }184 }185 }186 if p.matchingPDI[i] == -1 {187 p.matchingPDI[i] = p.Len()188 }189 }190 }191}192// determineParagraphEmbeddingLevel reports the resolved paragraph direction of193// the substring limited by the given range [start, end).194//195// Determines the paragraph level based on rules P2, P3. This is also used196// in rule X5c to find if an FSI should resolve to LRI or RLI.197func (p *paragraph) determineParagraphEmbeddingLevel(start, end int) level {198 var strongType Class = unknownClass199 // Rule P2.200 for i := start; i < end; i++ {201 if t := p.resultTypes[i]; t.in(L, AL, R) {202 strongType = t203 break204 } else if t.in(FSI, LRI, RLI) {205 i = p.matchingPDI[i] // skip over to the matching PDI206 if i > end {207 log.Panic("assert (i <= end)")208 }209 }210 }211 // Rule P3.212 switch strongType {213 case unknownClass: // none found214 // default embedding level when no strong types found is 0.215 return 0216 case L:217 return 0218 default: // AL, R219 return 1220 }221}222const maxDepth = 125223// This stack will store the embedding levels and override and isolated224// statuses225type directionalStatusStack struct {226 stackCounter int227 embeddingLevelStack [maxDepth + 1]level228 overrideStatusStack [maxDepth + 1]Class229 isolateStatusStack [maxDepth + 1]bool230}231func (s *directionalStatusStack) empty() { s.stackCounter = 0 }232func (s *directionalStatusStack) pop() { s.stackCounter-- }233func (s *directionalStatusStack) depth() int { return s.stackCounter }234func (s *directionalStatusStack) push(level level, overrideStatus Class, isolateStatus bool) {235 s.embeddingLevelStack[s.stackCounter] = level236 s.overrideStatusStack[s.stackCounter] = overrideStatus237 s.isolateStatusStack[s.stackCounter] = isolateStatus238 s.stackCounter++239}240func (s *directionalStatusStack) lastEmbeddingLevel() level {241 return s.embeddingLevelStack[s.stackCounter-1]242}243func (s *directionalStatusStack) lastDirectionalOverrideStatus() Class {244 return s.overrideStatusStack[s.stackCounter-1]245}246func (s *directionalStatusStack) lastDirectionalIsolateStatus() bool {247 return s.isolateStatusStack[s.stackCounter-1]248}249// Determine explicit levels using rules X1 - X8250func (p *paragraph) determineExplicitEmbeddingLevels() {251 var stack directionalStatusStack252 var overflowIsolateCount, overflowEmbeddingCount, validIsolateCount int253 // Rule X1.254 stack.push(p.embeddingLevel, ON, false)255 for i, t := range p.resultTypes {256 // Rules X2, X3, X4, X5, X5a, X5b, X5c257 switch t {258 case RLE, LRE, RLO, LRO, RLI, LRI, FSI:259 isIsolate := t.in(RLI, LRI, FSI)260 isRTL := t.in(RLE, RLO, RLI)261 // override if this is an FSI that resolves to RLI262 if t == FSI {263 isRTL = (p.determineParagraphEmbeddingLevel(i+1, p.matchingPDI[i]) == 1)264 }265 if isIsolate {266 p.resultLevels[i] = stack.lastEmbeddingLevel()267 if stack.lastDirectionalOverrideStatus() != ON {268 p.resultTypes[i] = stack.lastDirectionalOverrideStatus()269 }270 }271 var newLevel level272 if isRTL {273 // least greater odd274 newLevel = (stack.lastEmbeddingLevel() + 1) | 1275 } else {276 // least greater even277 newLevel = (stack.lastEmbeddingLevel() + 2) &^ 1278 }279 if newLevel <= maxDepth && overflowIsolateCount == 0 && overflowEmbeddingCount == 0 {280 if isIsolate {281 validIsolateCount++282 }283 // Push new embedding level, override status, and isolated284 // status.285 // No check for valid stack counter, since the level check286 // suffices.287 switch t {288 case LRO:289 stack.push(newLevel, L, isIsolate)290 case RLO:291 stack.push(newLevel, R, isIsolate)292 default:293 stack.push(newLevel, ON, isIsolate)294 }295 // Not really part of the spec296 if !isIsolate {297 p.resultLevels[i] = newLevel298 }299 } else {300 // This is an invalid explicit formatting character,301 // so apply the "Otherwise" part of rules X2-X5b.302 if isIsolate {303 overflowIsolateCount++304 } else { // !isIsolate305 if overflowIsolateCount == 0 {306 overflowEmbeddingCount++307 }308 }309 }310 // Rule X6a311 case PDI:312 if overflowIsolateCount > 0 {313 overflowIsolateCount--314 } else if validIsolateCount == 0 {315 // do nothing316 } else {317 overflowEmbeddingCount = 0318 for !stack.lastDirectionalIsolateStatus() {319 stack.pop()320 }321 stack.pop()322 validIsolateCount--323 }324 p.resultLevels[i] = stack.lastEmbeddingLevel()325 // Rule X7326 case PDF:327 // Not really part of the spec328 p.resultLevels[i] = stack.lastEmbeddingLevel()329 if overflowIsolateCount > 0 {330 // do nothing331 } else if overflowEmbeddingCount > 0 {332 overflowEmbeddingCount--333 } else if !stack.lastDirectionalIsolateStatus() && stack.depth() >= 2 {334 stack.pop()335 }336 case B: // paragraph separator.337 // Rule X8.338 // These values are reset for clarity, in this implementation B339 // can only occur as the last code in the array.340 stack.empty()341 overflowIsolateCount = 0342 overflowEmbeddingCount = 0343 validIsolateCount = 0344 p.resultLevels[i] = p.embeddingLevel345 default:346 p.resultLevels[i] = stack.lastEmbeddingLevel()347 if stack.lastDirectionalOverrideStatus() != ON {348 p.resultTypes[i] = stack.lastDirectionalOverrideStatus()349 }350 }351 }352}353type isolatingRunSequence struct {354 p *paragraph355 indexes []int // indexes to the original string356 types []Class // type of each character using the index357 resolvedLevels []level // resolved levels after application of rules358 level level359 sos, eos Class360}361func (i *isolatingRunSequence) Len() int { return len(i.indexes) }362func maxLevel(a, b level) level {363 if a > b {364 return a365 }366 return b367}368// Rule X10, second bullet: Determine the start-of-sequence (sos) and end-of-sequence (eos) types,369// either L or R, for each isolating run sequence.370func (p *paragraph) isolatingRunSequence(indexes []int) *isolatingRunSequence {371 length := len(indexes)372 types := make([]Class, length)373 for i, x := range indexes {374 types[i] = p.resultTypes[x]375 }376 // assign level, sos and eos377 prevChar := indexes[0] - 1378 for prevChar >= 0 && isRemovedByX9(p.initialTypes[prevChar]) {379 prevChar--380 }381 prevLevel := p.embeddingLevel382 if prevChar >= 0 {383 prevLevel = p.resultLevels[prevChar]384 }385 var succLevel level386 lastType := types[length-1]387 if lastType.in(LRI, RLI, FSI) {388 succLevel = p.embeddingLevel389 } else {390 // the first character after the end of run sequence391 limit := indexes[length-1] + 1392 for ; limit < p.Len() && isRemovedByX9(p.initialTypes[limit]); limit++ {393 }394 succLevel = p.embeddingLevel395 if limit < p.Len() {396 succLevel = p.resultLevels[limit]397 }398 }399 level := p.resultLevels[indexes[0]]400 return &isolatingRunSequence{401 p: p,402 indexes: indexes,403 types: types,404 level: level,405 sos: typeForLevel(maxLevel(prevLevel, level)),406 eos: typeForLevel(maxLevel(succLevel, level)),407 }408}409// Resolving weak types Rules W1-W7.410//411// Note that some weak types (EN, AN) remain after this processing is412// complete.413func (s *isolatingRunSequence) resolveWeakTypes() {414 // on entry, only these types remain415 s.assertOnly(L, R, AL, EN, ES, ET, AN, CS, B, S, WS, ON, NSM, LRI, RLI, FSI, PDI)416 // Rule W1.417 // Changes all NSMs.418 preceedingCharacterType := s.sos419 for i, t := range s.types {420 if t == NSM {421 s.types[i] = preceedingCharacterType422 } else {423 if t.in(LRI, RLI, FSI, PDI) {424 preceedingCharacterType = ON425 }426 preceedingCharacterType = t427 }428 }429 // Rule W2.430 // EN does not change at the start of the run, because sos != AL.431 for i, t := range s.types {432 if t == EN {433 for j := i - 1; j >= 0; j-- {434 if t := s.types[j]; t.in(L, R, AL) {435 if t == AL {436 s.types[i] = AN437 }438 break439 }440 }441 }442 }443 // Rule W3.444 for i, t := range s.types {445 if t == AL {446 s.types[i] = R447 }448 }449 // Rule W4.450 // Since there must be values on both sides for this rule to have an451 // effect, the scan skips the first and last value.452 //453 // Although the scan proceeds left to right, and changes the type454 // values in a way that would appear to affect the computations455 // later in the scan, there is actually no problem. A change in the456 // current value can only affect the value to its immediate right,457 // and only affect it if it is ES or CS. But the current value can458 // only change if the value to its right is not ES or CS. Thus459 // either the current value will not change, or its change will have460 // no effect on the remainder of the analysis.461 for i := 1; i < s.Len()-1; i++ {462 t := s.types[i]463 if t == ES || t == CS {464 prevSepType := s.types[i-1]465 succSepType := s.types[i+1]466 if prevSepType == EN && succSepType == EN {467 s.types[i] = EN468 } else if s.types[i] == CS && prevSepType == AN && succSepType == AN {469 s.types[i] = AN470 }471 }472 }473 // Rule W5.474 for i, t := range s.types {475 if t == ET {476 // locate end of sequence477 runStart := i478 runEnd := s.findRunLimit(runStart, ET)479 // check values at ends of sequence480 t := s.sos481 if runStart > 0 {482 t = s.types[runStart-1]483 }484 if t != EN {485 t = s.eos486 if runEnd < len(s.types) {487 t = s.types[runEnd]488 }489 }490 if t == EN {491 setTypes(s.types[runStart:runEnd], EN)492 }493 // continue at end of sequence494 i = runEnd495 }496 }497 // Rule W6.498 for i, t := range s.types {499 if t.in(ES, ET, CS) {500 s.types[i] = ON501 }502 }503 // Rule W7.504 for i, t := range s.types {505 if t == EN {506 // set default if we reach start of run507 prevStrongType := s.sos508 for j := i - 1; j >= 0; j-- {509 t = s.types[j]510 if t == L || t == R { // AL's have been changed to R511 prevStrongType = t512 break513 }514 }515 if prevStrongType == L {516 s.types[i] = L517 }518 }519 }520}521// 6) resolving neutral types Rules N1-N2.522func (s *isolatingRunSequence) resolveNeutralTypes() {523 // on entry, only these types can be in resultTypes524 s.assertOnly(L, R, EN, AN, B, S, WS, ON, RLI, LRI, FSI, PDI)525 for i, t := range s.types {526 switch t {527 case WS, ON, B, S, RLI, LRI, FSI, PDI:528 // find bounds of run of neutrals529 runStart := i530 runEnd := s.findRunLimit(runStart, B, S, WS, ON, RLI, LRI, FSI, PDI)531 // determine effective types at ends of run532 var leadType, trailType Class533 // Note that the character found can only be L, R, AN, or534 // EN.535 if runStart == 0 {536 leadType = s.sos537 } else {538 leadType = s.types[runStart-1]539 if leadType.in(AN, EN) {540 leadType = R541 }542 }543 if runEnd == len(s.types) {544 trailType = s.eos545 } else {546 trailType = s.types[runEnd]547 if trailType.in(AN, EN) {548 trailType = R549 }550 }551 var resolvedType Class552 if leadType == trailType {553 // Rule N1.554 resolvedType = leadType555 } else {556 // Rule N2.557 // Notice the embedding level of the run is used, not558 // the paragraph embedding level.559 resolvedType = typeForLevel(s.level)560 }561 setTypes(s.types[runStart:runEnd], resolvedType)562 // skip over run of (former) neutrals563 i = runEnd564 }565 }566}567func setLevels(levels []level, newLevel level) {568 for i := range levels {569 levels[i] = newLevel570 }571}572func setTypes(types []Class, newType Class) {573 for i := range types {574 types[i] = newType575 }576}577// 7) resolving implicit embedding levels Rules I1, I2.578func (s *isolatingRunSequence) resolveImplicitLevels() {579 // on entry, only these types can be in resultTypes580 s.assertOnly(L, R, EN, AN)581 s.resolvedLevels = make([]level, len(s.types))582 setLevels(s.resolvedLevels, s.level)583 if (s.level & 1) == 0 { // even level584 for i, t := range s.types {585 // Rule I1.586 if t == L {587 // no change588 } else if t == R {589 s.resolvedLevels[i] += 1590 } else { // t == AN || t == EN591 s.resolvedLevels[i] += 2592 }593 }594 } else { // odd level595 for i, t := range s.types {596 // Rule I2.597 if t == R {598 // no change599 } else { // t == L || t == AN || t == EN600 s.resolvedLevels[i] += 1601 }602 }603 }604}605// Applies the levels and types resolved in rules W1-I2 to the606// resultLevels array.607func (s *isolatingRunSequence) applyLevelsAndTypes() {608 for i, x := range s.indexes {609 s.p.resultTypes[x] = s.types[i]610 s.p.resultLevels[x] = s.resolvedLevels[i]611 }612}613// Return the limit of the run consisting only of the types in validSet614// starting at index. This checks the value at index, and will return615// index if that value is not in validSet.616func (s *isolatingRunSequence) findRunLimit(index int, validSet ...Class) int {617loop:618 for ; index < len(s.types); index++ {619 t := s.types[index]620 for _, valid := range validSet {621 if t == valid {622 continue loop623 }624 }625 return index // didn't find a match in validSet626 }627 return len(s.types)628}629// Algorithm validation. Assert that all values in types are in the630// provided set.631func (s *isolatingRunSequence) assertOnly(codes ...Class) {632loop:633 for i, t := range s.types {634 for _, c := range codes {635 if t == c {636 continue loop637 }638 }639 log.Panicf("invalid bidi code %v present in assertOnly at position %d", t, s.indexes[i])640 }641}642// determineLevelRuns returns an array of level runs. Each level run is643// described as an array of indexes into the input string.644//645// Determines the level runs. Rule X9 will be applied in determining the646// runs, in the way that makes sure the characters that are supposed to be647// removed are not included in the runs.648func (p *paragraph) determineLevelRuns() [][]int {649 run := []int{}650 allRuns := [][]int{}651 currentLevel := implicitLevel652 for i := range p.initialTypes {653 if !isRemovedByX9(p.initialTypes[i]) {654 if p.resultLevels[i] != currentLevel {655 // we just encountered a new run; wrap up last run656 if currentLevel >= 0 { // only wrap it up if there was a run657 allRuns = append(allRuns, run)658 run = nil659 }660 // Start new run661 currentLevel = p.resultLevels[i]662 }663 run = append(run, i)664 }665 }666 // Wrap up the final run, if any667 if len(run) > 0 {668 allRuns = append(allRuns, run)669 }670 return allRuns671}672// Definition BD13. Determine isolating run sequences.673func (p *paragraph) determineIsolatingRunSequences() []*isolatingRunSequence {674 levelRuns := p.determineLevelRuns()675 // Compute the run that each character belongs to676 runForCharacter := make([]int, p.Len())677 for i, run := range levelRuns {678 for _, index := range run {679 runForCharacter[index] = i680 }681 }682 sequences := []*isolatingRunSequence{}683 var currentRunSequence []int684 for _, run := range levelRuns {685 first := run[0]686 if p.initialTypes[first] != PDI || p.matchingIsolateInitiator[first] == -1 {687 currentRunSequence = nil688 // int run = i;689 for {690 // Copy this level run into currentRunSequence691 currentRunSequence = append(currentRunSequence, run...)692 last := currentRunSequence[len(currentRunSequence)-1]693 lastT := p.initialTypes[last]694 if lastT.in(LRI, RLI, FSI) && p.matchingPDI[last] != p.Len() {695 run = levelRuns[runForCharacter[p.matchingPDI[last]]]696 } else {697 break698 }699 }700 sequences = append(sequences, p.isolatingRunSequence(currentRunSequence))...

Full Screen

Full Screen

types.go

Source:types.go Github

copy

Full Screen

1// Copyright 2010 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4// This file contains the pieces of the tool that use typechecking from the go/types package.5package main6import (7 "go/ast"8 "go/importer"9 "go/token"10 "go/types"11)12// stdImporter is the importer we use to import packages.13// It is created during initialization so that all packages14// are imported by the same importer.15var stdImporter = importer.Default()16var (17 errorType *types.Interface18 stringerType *types.Interface // possibly nil19 formatterType *types.Interface // possibly nil20)21func init() {22 errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)23 if typ := importType("fmt", "Stringer"); typ != nil {24 stringerType = typ.Underlying().(*types.Interface)25 }26 if typ := importType("fmt", "Formatter"); typ != nil {27 formatterType = typ.Underlying().(*types.Interface)28 }29}30// importType returns the type denoted by the qualified identifier31// path.name, and adds the respective package to the imports map32// as a side effect. In case of an error, importType returns nil.33func importType(path, name string) types.Type {34 pkg, err := stdImporter.Import(path)35 if err != nil {36 // This can happen if the package at path hasn't been compiled yet.37 warnf("import failed: %v", err)38 return nil39 }40 if obj, ok := pkg.Scope().Lookup(name).(*types.TypeName); ok {41 return obj.Type()42 }43 warnf("invalid type name %q", name)44 return nil45}46func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error {47 pkg.defs = make(map[*ast.Ident]types.Object)48 pkg.uses = make(map[*ast.Ident]types.Object)49 pkg.selectors = make(map[*ast.SelectorExpr]*types.Selection)50 pkg.spans = make(map[types.Object]Span)51 pkg.types = make(map[ast.Expr]types.TypeAndValue)52 config := types.Config{53 // We use the same importer for all imports to ensure that54 // everybody sees identical packages for the given paths.55 Importer: stdImporter,56 // By providing a Config with our own error function, it will continue57 // past the first error. There is no need for that function to do anything.58 Error: func(error) {},59 }60 info := &types.Info{61 Selections: pkg.selectors,62 Types: pkg.types,63 Defs: pkg.defs,64 Uses: pkg.uses,65 }66 typesPkg, err := config.Check(pkg.path, fs, astFiles, info)67 pkg.typesPkg = typesPkg68 // update spans69 for id, obj := range pkg.defs {70 pkg.growSpan(id, obj)71 }72 for id, obj := range pkg.uses {73 pkg.growSpan(id, obj)74 }75 return err76}77// matchArgType reports an error if printf verb t is not appropriate78// for operand arg.79//80// typ is used only for recursive calls; external callers must supply nil.81//82// (Recursion arises from the compound types {map,chan,slice} which83// may be printed with %d etc. if that is appropriate for their element84// types.)85func (f *File) matchArgType(t printfArgType, typ types.Type, arg ast.Expr) bool {86 return f.matchArgTypeInternal(t, typ, arg, make(map[types.Type]bool))87}88// matchArgTypeInternal is the internal version of matchArgType. It carries a map89// remembering what types are in progress so we don't recur when faced with recursive90// types or mutually recursive types.91func (f *File) matchArgTypeInternal(t printfArgType, typ types.Type, arg ast.Expr, inProgress map[types.Type]bool) bool {92 // %v, %T accept any argument type.93 if t == anyType {94 return true95 }96 if typ == nil {97 // external call98 typ = f.pkg.types[arg].Type99 if typ == nil {100 return true // probably a type check problem101 }102 }103 // If the type implements fmt.Formatter, we have nothing to check.104 if f.isFormatter(typ) {105 return true106 }107 // If we can use a string, might arg (dynamically) implement the Stringer or Error interface?108 if t&argString != 0 {109 if types.AssertableTo(errorType, typ) || stringerType != nil && types.AssertableTo(stringerType, typ) {110 return true111 }112 }113 typ = typ.Underlying()114 if inProgress[typ] {115 // We're already looking at this type. The call that started it will take care of it.116 return true117 }118 inProgress[typ] = true119 switch typ := typ.(type) {120 case *types.Signature:121 return t&argPointer != 0122 case *types.Map:123 // Recur: map[int]int matches %d.124 return t&argPointer != 0 ||125 (f.matchArgTypeInternal(t, typ.Key(), arg, inProgress) && f.matchArgTypeInternal(t, typ.Elem(), arg, inProgress))126 case *types.Chan:127 return t&argPointer != 0128 case *types.Array:129 // Same as slice.130 if types.Identical(typ.Elem().Underlying(), types.Typ[types.Byte]) && t&argString != 0 {131 return true // %s matches []byte132 }133 // Recur: []int matches %d.134 return t&argPointer != 0 || f.matchArgTypeInternal(t, typ.Elem().Underlying(), arg, inProgress)135 case *types.Slice:136 // Same as array.137 if types.Identical(typ.Elem().Underlying(), types.Typ[types.Byte]) && t&argString != 0 {138 return true // %s matches []byte139 }140 // Recur: []int matches %d. But watch out for141 // type T []T142 // If the element is a pointer type (type T[]*T), it's handled fine by the Pointer case below.143 return t&argPointer != 0 || f.matchArgTypeInternal(t, typ.Elem(), arg, inProgress)144 case *types.Pointer:145 // Ugly, but dealing with an edge case: a known pointer to an invalid type,146 // probably something from a failed import.147 if typ.Elem().String() == "invalid type" {148 if *verbose {149 f.Warnf(arg.Pos(), "printf argument %v is pointer to invalid or unknown type", f.gofmt(arg))150 }151 return true // special case152 }153 // If it's actually a pointer with %p, it prints as one.154 if t == argPointer {155 return true156 }157 // If it's pointer to struct, that's equivalent in our analysis to whether we can print the struct.158 if str, ok := typ.Elem().Underlying().(*types.Struct); ok {159 return f.matchStructArgType(t, str, arg, inProgress)160 }161 // The rest can print with %p as pointers, or as integers with %x etc.162 return t&(argInt|argPointer) != 0163 case *types.Struct:164 return f.matchStructArgType(t, typ, arg, inProgress)165 case *types.Interface:166 // There's little we can do.167 // Whether any particular verb is valid depends on the argument.168 // The user may have reasonable prior knowledge of the contents of the interface.169 return true170 case *types.Basic:171 switch typ.Kind() {172 case types.UntypedBool,173 types.Bool:174 return t&argBool != 0175 case types.UntypedInt,176 types.Int,177 types.Int8,178 types.Int16,179 types.Int32,180 types.Int64,181 types.Uint,182 types.Uint8,183 types.Uint16,184 types.Uint32,185 types.Uint64,186 types.Uintptr:187 return t&argInt != 0188 case types.UntypedFloat,189 types.Float32,190 types.Float64:191 return t&argFloat != 0192 case types.UntypedComplex,193 types.Complex64,194 types.Complex128:195 return t&argComplex != 0196 case types.UntypedString,197 types.String:198 return t&argString != 0199 case types.UnsafePointer:200 return t&(argPointer|argInt) != 0201 case types.UntypedRune:202 return t&(argInt|argRune) != 0203 case types.UntypedNil:204 return t&argPointer != 0 // TODO?205 case types.Invalid:206 if *verbose {207 f.Warnf(arg.Pos(), "printf argument %v has invalid or unknown type", f.gofmt(arg))208 }209 return true // Probably a type check problem.210 }211 panic("unreachable")212 }213 return false214}215// hasBasicType reports whether x's type is a types.Basic with the given kind.216func (f *File) hasBasicType(x ast.Expr, kind types.BasicKind) bool {217 t := f.pkg.types[x].Type218 if t != nil {219 t = t.Underlying()220 }221 b, ok := t.(*types.Basic)222 return ok && b.Kind() == kind223}224// matchStructArgType reports whether all the elements of the struct match the expected225// type. For instance, with "%d" all the elements must be printable with the "%d" format.226func (f *File) matchStructArgType(t printfArgType, typ *types.Struct, arg ast.Expr, inProgress map[types.Type]bool) bool {227 for i := 0; i < typ.NumFields(); i++ {228 if !f.matchArgTypeInternal(t, typ.Field(i).Type(), arg, inProgress) {229 return false230 }231 }232 return true233}234// hasMethod reports whether the type contains a method with the given name.235// It is part of the workaround for Formatters and should be deleted when236// that workaround is no longer necessary.237// TODO: This could be better once issue 6259 is fixed.238func (f *File) hasMethod(typ types.Type, name string) bool {239 // assume we have an addressable variable of type typ240 obj, _, _ := types.LookupFieldOrMethod(typ, true, f.pkg.typesPkg, name)241 _, ok := obj.(*types.Func)242 return ok243}...

Full Screen

Full Screen

util.go

Source:util.go Github

copy

Full Screen

...26func isPointer(typ types.Type) bool {27 _, ok := typ.Underlying().(*types.Pointer)28 return ok29}30func isInterface(T types.Type) bool { return types.IsInterface(T) }31// deref returns a pointer's element type; otherwise it returns typ.32func deref(typ types.Type) types.Type {33 if p, ok := typ.Underlying().(*types.Pointer); ok {34 return p.Elem()35 }36 return typ37}38// recvType returns the receiver type of method obj.39func recvType(obj *types.Func) types.Type {40 return obj.Type().(*types.Signature).Recv().Type()41}42// DefaultType returns the default "typed" type for an "untyped" type;43// it returns the incoming type for all other types. The default type44// for untyped nil is untyped nil....

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("type:", reflect.TypeOf(x))4 v := reflect.ValueOf(x)5 fmt.Println("value:", v)6 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)7 fmt.Println("value:", v.Float())8 fmt.Printf("value is %5.2e9", v.Float())10}

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("type:", reflect.TypeOf(x))4 v := reflect.ValueOf(x)5 fmt.Println("value:", v)6 fmt.Println("type:", v.Type())7 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)8 fmt.Println("value:", v.Float())9}10GoLang - reflect - IsNil() Method

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("type:", reflect.TypeOf(x))4 v := reflect.ValueOf(x)5 fmt.Println("value:", v)6 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)7 fmt.Println("type:", v.Type())8 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)9 fmt.Println("value:", v.Float())10}11import (12func main() {13 fmt.Println("type:", reflect.TypeOf(x))14 v := reflect.ValueOf(x)15 fmt.Println("value:", v)16 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)17 fmt.Println("type:", v.Type())18 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)19 fmt.Println("value:", v.Float())20}21import (22func main() {23 fmt.Println("type:", reflect.TypeOf(x))24 v := reflect.ValueOf(x)25 fmt.Println("value:", v)26 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)27 fmt.Println("type:", v.Type())28 fmt.Println("kind is float64:", v.Kind() == reflect.Float64)29 fmt.Println("value:", v.Float())30}

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("type:", reflect.TypeOf(x))4 fmt.Println("value:", reflect.ValueOf(x))5 fmt.Println("kind is float64:", reflect.ValueOf(x).Kind() == reflect.Float64)6 fmt.Println("kind is float32:", reflect.ValueOf(x).Kind() == reflect.Float32)7}8import (9func main() {10 fmt.Println("type:", reflect.TypeOf(x))11 fmt.Println("value:", reflect.ValueOf(x))12 fmt.Println("kind is float64:", reflect.ValueOf(x).Kind().Is(reflect.Float64))13 fmt.Println("kind is float32:", reflect.ValueOf(x).Kind().Is(reflect.Float32))14}15import (16func main() {17 fmt.Println("type:", reflect.TypeOf(x))18 fmt.Println("value:", reflect.ValueOf(x))19 fmt.Println("kind is float64:", reflect.ValueOf(x).Kind().Is(reflect.Float64))20 fmt.Println("kind is float32:", reflect.ValueOf(x).Kind().Is(reflect.Float32))21}

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(reflect.TypeOf(a))4 fmt.Println(reflect.TypeOf(b))5 fmt.Println(reflect.TypeOf(c))6 fmt.Println(reflect.TypeOf(d))7 fmt.Println(reflect.TypeOf(e))8 fmt.Println(reflect.TypeOf(f))9 fmt.Println(reflect.TypeOf(a).Kind())10 fmt.Println(reflect.TypeOf(b).Kind())11 fmt.Println(reflect.TypeOf(c).Kind())12 fmt.Println(reflect.TypeOf(d).Kind())13 fmt.Println(reflect.TypeOf(e).Kind())14 fmt.Println(reflect.TypeOf(f).Kind())15 fmt.Println(reflect.TypeOf(a).Kind() == reflect.Int)16 fmt.Println(reflect.TypeOf(b).Kind() == reflect.Int64)17 fmt.Println(reflect.TypeOf(c).Kind() == reflect.Float64)18 fmt.Println(reflect.TypeOf(d).Kind() == reflect.Float64)19 fmt.Println(reflect.TypeOf(e).Kind() == reflect.String)20 fmt.Println(reflect.TypeOf(f).Kind() == reflect.String)21}22import (23func main() {

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1import (2type types interface {3 Is(types) bool4}5func (a Int) Is(b types) bool {6 _, ok := b.(Int)7}8func main() {9 fmt.Println(a.Is(b))10}11import (12type types interface {13 Is(types) bool14}15func (a Int) Is(b types) bool {16 _, ok := b.(Int)17}18func (a Float) Is(b types) bool {19 _, ok := b.(Float)20}21func main() {22 fmt.Println(a.Is(b))23}24import (25type types interface {26 Is(types) bool27}28func (a Int) Is(b types) bool {29 _, ok := b.(Int)30}31func (a Float) Is(b types) bool {

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1func main() {2 var a types.Type1 = types.Type1{}3 var b types.Type1 = types.Type1{}4 var c types.Type2 = types.Type2{}5 var d types.Type3 = types.Type3{}6 var e types.Type3 = types.Type3{}7 var f types.Type4 = types.Type4{}8 var g types.Type4 = types.Type4{}9 var h types.Type5 = types.Type5{}10 var i types.Type6 = types.Type6{}11 var j types.Type7 = types.Type7{}12 var k types.Type8 = types.Type8{}13 var l types.Type9 = types.Type9{}14 var m types.Type10 = types.Type10{}15 var n types.Type11 = types.Type11{}16 var o types.Type12 = types.Type12{}17 var p types.Type13 = types.Type13{}18 var q types.Type14 = types.Type14{}19 var r types.Type15 = types.Type15{}20 var s types.Type16 = types.Type16{}21 var t types.Type17 = types.Type17{}22 var u types.Type18 = types.Type18{}23 var v types.Type19 = types.Type19{}24 var w types.Type20 = types.Type20{}25 var x types.Type21 = types.Type21{}26 var y types.Type22 = types.Type22{}27 var z types.Type23 = types.Type23{}28 var aa types.Type24 = types.Type24{}29 var ab types.Type25 = types.Type25{}30 var ac types.Type26 = types.Type26{}31 var ad types.Type27 = types.Type27{}32 var ae types.Type28 = types.Type28{}33 var af types.Type29 = types.Type29{}34 var ag types.Type30 = types.Type30{}35 var ah types.Type31 = types.Type31{}36 var ai types.Type32 = types.Type32{}37 var aj types.Type33 = types.Type33{}38 var ak types.Type34 = types.Type34{}39 var al types.Type35 = types.Type35{}40 var am types.Type36 = types.Type36{}41 var an types.Type37 = types.Type37{}42 var ao types.Type38 = types.Type38{}43 var ap types.Type39 = types.Type39{}44 var aq types.Type40 = types.Type40{}45 var ar types.Type41 = types.Type41{}

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

12: import "fmt"24: type Person struct {37: }49: func (p Person) IsOlder() bool {510: if p.age > 30 {612: }714: }816: func main() {917: p := Person{"John", 32}1018: fmt.Println(p.IsOlder())1119: }

Full Screen

Full Screen

Is

Using AI Code Generation

copy

Full Screen

1import (2type (3 T1 struct {4 }5 T2 struct {6 }7func main() {8 t1 := T1{1}9 t2 := T2{1}10}11func Is(a, b interface{}) bool {12 return fmt.Sprintf("%T", a) == fmt.Sprintf("%T", b)13}14import (15type (16 T1 struct {17 }18 T2 struct {19 }20func main() {21 t1 := T1{1}22 t2 := T2{1}23 fmt.Println(Is(t1

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Ginkgo automation tests on LambdaTest cloud grid

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

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful