Posts in category “Programming”

PL/SQL: Local function definition in a public procedure / function

Occasionally, we will need to define local used functions in a public procedure or a function. Fortunately, PL/SQL supports embedded functions. I spent more than an hour trying to find the correct way to use this feature. Yes. forgive me, I am a little dumb. According to my retries, the best position to put your local functions is sharp before the BEGIN symbol. And here's a sample.

set serveroutput on;
CREATE OR REPLACE PROCEDURE tmp12345(v_full_name IN VARCHAR2) AS
	localv varchar2(100);
	-- local function definitaions start ---
	FUNCTION FETCH_REAL_NAME(v_statement_name IN VARCHAR2) RETURN VARCHAR2
	AS
		pattern VARCHAR2(100) := '^(Dr|Ms|Mx|Mr|Mrs)\s+';
		full_name varchar2(100);
	BEGIN
		-- replace possible multiple spaces to one space
		full_name := TRIM(REGEXP_REPLACE(v_statement_name, '\s+',  ' '));
		IF (REGEXP_LIKE(full_name, pattern, 'i')) THEN
			-- REGEXP_REPLACE(string, pattern [, replacement_string [, start_position [, nth_appearance [, match_parameter ] ] ] ]) 
			RETURN REGEXP_REPLACE(full_name, pattern, '', 1, 0, 'i');
		ELSE
			RETURN full_name;
		END IF;
	END;
	FUNCTION FETCH_MIDDLE_NAME(full_name IN VARCHAR) RETURN VARCHAR AS
		middle_name VARCHAR2(100);
	BEGIN
		middle_name := SUBSTR(full_name, INSTR(full_name, ' ') + 1, INSTR(full_name, ' ', 1, 2) - INSTR(full_name, ' ') - 1);
		IF (LENGTH(middle_name) > 10) THEN
			-- return initial instead
			RETURN SUBSTR(middle_name, 1, 1);
		ELSE
			RETURN middle_name;
		END IF;
	END;
	-- local functions definitaions end ---
BEGIN
	-- main logic here
	localv := v_full_name;
	DBMS_OUTPUT.PUT_LINE(localv);
	DBMS_OUTPUT.PUT_LINE(FETCH_MIDDLE_NAME(FETCH_REAL_NAME(v_full_name)));
END;
/

Approval Testing is tasty

Just record a few tips here:

[UseApprovalSubdirectory("Approvals")] can be used to set up the directory that saves the approval results.

It can be used for a test class or a test method.

Links

ApprovalTests.Net Project

An awesome entry to better code: The gilded rose kata

For some reason, I was told an interesting github repo, The Gilded Rose Kata. It really grasps my attention. The code's behavior is correct, but both code quality and code style are terrible. The author of the repo made it so bad on purpose. So it is the best material to teach newbies how to refactor.

From the Readme file of the repo, I learnt some new tools, one is the text based approval test. I actually know that concept before many years ago, but never practiced it. I just found it is easy to setup and easy to run. It tests your code from a new angle. I believe it could belong to a kind of greybox test. You need to know the code to test, but you don't know too much like other unit test methods.

I have done the refactoring, and my result has been put on Github.com

  1. first approach is on the main branch https://github.com/shukebeta/GildedRose-Refactoring-Kata/tree/main/csharpcore

  2. a better approach is on the other branch https://github.com/shukebeta/GildedRose-Refactoring-Kata/tree/simple-factory-pattern-approch

Here are some good links, and I would update it timely.

Writing good tests for the gilded rose kata
Principles for agile test automation
The gilded rose refactoring kata
How to refactor gildedrose refactoring kata with simple factory pattern and strategy pattern

Fix swagger stuck on extend an API: How to configure Swashbuckle to ignore property on model

Referenced

I found the root cause is that the JSON generated by swagger is too large: Swagger outputs over 18000 lines for that API! Further research told me that two property fields contribute about 17000 lines. So how to configure swagger to ignore these two properties is the key. After googling a while, I found VeganHunter's answer in this thread Reference is the simplest.

Solution for .NET Core 3.1 and .NET Standard 2.1:

Use JsonIgnore from System.Text.Json.Serialization namespace.

( JsonIgnore from Newtonsoft.Json will NOT work )

public class Test
{
    [System.Text.Json.Serialization.JsonIgnore]
    public int HiddenProperty { get; set; }
    public int VisibleProperty { get; set; }
}

Hope it could also help someone else. 😀

A tricky issue about [getImageInfo:fail image not found] painter component for WeChat Miniprogram

I heard my colleague Joe met a tricky bug this morning and he has no idea about it. After a brief google, I found it's a rather famous one. So many people met this issue and most of them haven't got an answer.

So I dug it deeper and found a clue that locates at https://developers.weixin.qq.com/community/develop/doc/0000c6d4b6c2d0cb128a1c0475b000?_at=1566630830developers.weixin.qq.com

and eventually, I found my solution. I shared it at https://developers.weixin.qq.com/community/develop/doc/000ac29db444e0b922c6caee759c00

PS: there's a common phenomenon among developers in China. Someone asks a question, and many people replyed they have met the same issue. But, no answers. I don't think all of them haven't found out their answer. People are interested in asking questions but have less interest to answer, even they finally got a valuable answer. It's a shame. I won't be that kind of person.